Changeset 10 for Common/Database.php


Ignore:
Timestamp:
Apr 7, 2020, 8:52:35 PM (5 years ago)
Author:
chronos
Message:
  • Modified: Improved code formatting.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/Database.php

    r9 r10  
    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))
    145       {
    146         if(is_null($Value)) $Value = 'NULL';
     144      if (!in_array($Value, $this->Functions))
     145      {
     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))
    167       {
    168         if(is_null($Value)) $Value = 'NULL';
     164    foreach ($Data as $Key => $Value)
     165    {
     166      if (!in_array($Value, $this->Functions))
     167      {
     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))
    184       {
    185         if(is_null($Value)) $Value = 'NULL';
     181    foreach ($Data as $Key => $Value)
     182    {
     183      if (!in_array($Value, $this->Functions))
     184      {
     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
     
    234234function TimeToMysqlDateTime($Time)
    235235{
    236   if($Time == NULL) return(NULL);
    237     else return(date('Y-m-d H:i:s', $Time));
     236  if ($Time == NULL) return NULL;
     237    else return date('Y-m-d H:i:s', $Time);
    238238}
    239239
    240240function TimeToMysqlDate($Time)
    241241{
    242   if($Time == NULL) return(NULL);
    243     else return(date('Y-m-d', $Time));
     242  if ($Time == NULL) return NULL;
     243    else return date('Y-m-d', $Time);
    244244}
    245245
    246246function TimeToMysqlTime($Time)
    247247{
    248   if($Time == NULL) return(NULL);
    249     else return(date('H:i:s', $Time));
     248  if ($Time == NULL) return NULL;
     249    else return date('H:i:s', $Time);
    250250}
    251251
    252252function MysqlDateTimeToTime($DateTime)
    253253{
    254   if($DateTime == '') return(NULL);
     254  if ($DateTime == '') return NULL;
    255255  $Parts = explode(' ', $DateTime);
    256256  $DateParts = explode('-', $Parts[0]);
    257257  $TimeParts = explode(':', $Parts[1]);
    258258  $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
    259   return($Result);
     259  return $Result;
    260260}
    261261
    262262function MysqlDateToTime($Date)
    263263{
    264   if($Date == '') return(NULL);
    265   return(MysqlDateTimeToTime($Date.' 0:0:0'));
     264  if ($Date == '') return NULL;
     265  return MysqlDateTimeToTime($Date.' 0:0:0');
    266266}
    267267
    268268function MysqlTimeToTime($Time)
    269269{
    270   if($Time == '') return(NULL);
    271   return(MysqlDateTimeToTime('0000-00-00 '.$Time));
    272 }
     270  if ($Time == '') return NULL;
     271  return MysqlDateTimeToTime('0000-00-00 '.$Time);
     272}
Note: See TracChangeset for help on using the changeset viewer.