Changeset 32 for trunk/Database.php


Ignore:
Timestamp:
Apr 7, 2020, 1:06:13 AM (5 years ago)
Author:
chronos
Message:
  • Modified: Improved code formatting.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Database.php

    r19 r32  
    77{
    88  list($usec, $sec) = explode(" ", microtime());
    9   return ((float)$usec + (float)$sec);
     9  return (float)$usec + (float)$sec;
    1010}
    1111
     
    1717  function fetch_assoc()
    1818  {
    19     return($this->PDOStatement->fetch(PDO::FETCH_ASSOC));
     19    return $this->PDOStatement->fetch(PDO::FETCH_ASSOC);
    2020  }
    2121
    2222  function fetch_array()
    2323  {
    24     return($this->PDOStatement->fetch(PDO::FETCH_BOTH));
     24    return $this->PDOStatement->fetch(PDO::FETCH_BOTH);
    2525  }
    2626
    2727  function fetch_row()
    2828  {
    29     return($this->PDOStatement->fetch(PDO::FETCH_NUM));
     29    return $this->PDOStatement->fetch(PDO::FETCH_NUM);
    3030  }
    3131}
     
    6161  function Connect($Host, $User, $Password, $Database)
    6262  {
    63     if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database;
    64       else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host;
     63    if ($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database;
     64      else if ($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host;
    6565      else $ConnectionString = '';
    6666    try {
     
    8181  function Connected()
    8282  {
    83     return(isset($this->PDO));
     83    return isset($this->PDO);
    8484  }
    8585
     
    9191  function query($Query)
    9292  {
    93     if(!$this->Connected()) throw new Exception(T('Not connected to database'));
    94     if(($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) $QueryStartTime = microtime_float();
     93    if (!$this->Connected()) throw new Exception(T('Not connected to database'));
     94    if (($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) $QueryStartTime = microtime_float();
    9595    $this->LastQuery = $Query;
    9696    //echo('a'.$this->ShowSQLQuery.'<'.$QueryStartTime.', '.microtime_float());
    97     if(($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true))
     97    if (($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true))
    9898      $Duration = ' ; '.round(microtime_float() - $QueryStartTime, 4). ' s';
    99     if($this->LogSQLQuery == true)
     99    if ($this->LogSQLQuery == true)
    100100      file_put_contents($this->LogFile, $Query.$Duration."\n", FILE_APPEND);
    101     if($this->ShowSQLQuery == true)
     101    if ($this->ShowSQLQuery == true)
    102102      echo('<div style="border-bottom-width: 1px; border-bottom-style: solid; '.
    103103      'padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.$Duration.'</div>'."\n");
    104104    $Result = new DatabaseResult();
    105105    $Result->PDOStatement = $this->PDO->query($Query);
    106     if($Result->PDOStatement)
     106    if ($Result->PDOStatement)
    107107    {
    108108      $Result->num_rows = $Result->PDOStatement->rowCount();
     
    112112      $this->Error = $this->PDO->errorInfo();
    113113      $this->Error = $this->Error[2];
    114       if(($this->Error != '') and ($this->ShowSQLError == true))
     114      if (($this->Error != '') and ($this->ShowSQLError == true))
    115115        echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>');
    116116        throw new Exception('SQL Error: '.$this->Error.', Query: '.$Query);
    117117    }
    118     return($Result);
     118    return $Result;
    119119  }
    120120
    121121  function select($Table, $What = '*', $Condition = 1)
    122122  {
    123     return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition));
     123    return $this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
    124124  }
    125125
     
    139139    $Name = '';
    140140    $Values = '';
    141     foreach($Data as $Key => $Value)
     141    foreach ($Data as $Key => $Value)
    142142    {
    143143      $Name .= ',`'.$Key.'`';
    144       if(!in_array($Value, $this->Functions))
     144      if (!in_array($Value, $this->Functions))
    145145      {
    146         if(is_null($Value)) $Value = 'NULL';
     146        if (is_null($Value)) $Value = 'NULL';
    147147        else $Value = $this->PDO->quote($Value);
    148148      }
     
    151151    $Name = substr($Name, 1);
    152152    $Values = substr($Values, 1);
    153     return('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
     153    return 'INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')';
    154154  }
    155155
     
    162162  {
    163163    $Values = '';
    164     foreach($Data as $Key => $Value)
    165     {
    166       if(!in_array($Value, $this->Functions))
     164    foreach ($Data as $Key => $Value)
     165    {
     166      if (!in_array($Value, $this->Functions))
    167167      {
    168         if(is_null($Value)) $Value = 'NULL';
     168        if (is_null($Value)) $Value = 'NULL';
    169169        else $Value = $this->PDO->quote($Value);
    170170      }
     
    172172    }
    173173    $Values = substr($Values, 2);
    174     return('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
     174    return 'UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')';
    175175  }
    176176
     
    179179    $Name = '';
    180180    $Values = '';
    181     foreach($Data as $Key => $Value)
    182     {
    183       if(!in_array($Value, $this->Functions))
     181    foreach ($Data as $Key => $Value)
     182    {
     183      if (!in_array($Value, $this->Functions))
    184184      {
    185         if(is_null($Value)) $Value = 'NULL';
     185        if (is_null($Value)) $Value = 'NULL';
    186186        else $Value = $this->PDO->quote($Value);
    187187      }
     
    203203  function real_escape_string($Text)
    204204  {
    205     return(addslashes($Text));
     205    return addslashes($Text);
    206206  }
    207207
    208208  function quote($Text)
    209209  {
    210     return($this->PDO->quote($Text));
     210    return $this->PDO->quote($Text);
    211211  }
    212212
     
    232232      $this->Error = $this->PDO->errorInfo();
    233233      $this->Error = $this->Error[2];
    234       if(($this->Error != '') and ($this->ShowSQLError == true))
     234      if (($this->Error != '') and ($this->ShowSQLError == true))
    235235        echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>');
    236236      throw new Exception('SQL Error: '.$this->Error.', Query: '.$Query);
     
    241241function TimeToMysqlDateTime($Time)
    242242{
    243   if($Time == NULL) return(NULL);
    244     else return(date('Y-m-d H:i:s', $Time));
     243  if ($Time == NULL) return NULL;
     244    else return date('Y-m-d H:i:s', $Time);
    245245}
    246246
    247247function TimeToMysqlDate($Time)
    248248{
    249   if($Time == NULL) return(NULL);
    250     else return(date('Y-m-d', $Time));
     249  if ($Time == NULL) return NULL;
     250    else return date('Y-m-d', $Time);
    251251}
    252252
    253253function TimeToMysqlTime($Time)
    254254{
    255   if($Time == NULL) return(NULL);
    256     else return(date('H:i:s', $Time));
     255  if ($Time == NULL) return NULL;
     256    else return date('H:i:s', $Time);
    257257}
    258258
    259259function MysqlDateTimeToTime($DateTime)
    260260{
    261   if($DateTime == '') return(NULL);
     261  if ($DateTime == '') return NULL;
    262262  $Parts = explode(' ', $DateTime);
    263263  $DateParts = explode('-', $Parts[0]);
    264264  $TimeParts = explode(':', $Parts[1]);
    265265  $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
    266   return($Result);
     266  return $Result;
    267267}
    268268
    269269function MysqlDateToTime($Date)
    270270{
    271   if($Date == '') return(NULL);
    272   return(MysqlDateTimeToTime($Date.' 0:0:0'));
     271  if ($Date == '') return NULL;
     272  return MysqlDateTimeToTime($Date.' 0:0:0');
    273273}
    274274
    275275function MysqlTimeToTime($Time)
    276276{
    277   if($Time == '') return(NULL);
    278   return(MysqlDateTimeToTime('0000-00-00 '.$Time));
    279 }
     277  if ($Time == '') return NULL;
     278  return MysqlDateTimeToTime('0000-00-00 '.$Time);
     279}
Note: See TracChangeset for help on using the changeset viewer.