Ignore:
Timestamp:
Apr 7, 2020, 9:13:33 PM (5 years ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/Database.php

    r55 r56  
    22
    33// Extended database class
    4 // Date: 2016-01-11
     4// Date: 2020-04-07
     5
     6function microtime_float()
     7{
     8  list($usec, $sec) = explode(" ", microtime());
     9  return (float)$usec + (float)$sec;
     10}
    511
    612class DatabaseResult
     
    5157    $this->LogFile = dirname(__FILE__).'/../../Query.log';
    5258  }
     59 
    5360
    5461  function Connect($Host, $User, $Password, $Database)
     
    8592  {
    8693    if (!$this->Connected()) throw new Exception(T('Not connected to database'));
    87     if (($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) $QueryStartTime = microtime();
    88     $this->LastQuery = $Query;
     94    if (($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) $QueryStartTime = microtime_float();
     95    $this->LastQuery = $Query;   
    8996    if (($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true))
    90       $Duration = ' ; '.round(microtime() - $QueryStartTime, 4). ' s';
     97      $Duration = ' ; '.round(microtime_float() - $QueryStartTime, 4). ' s';
    9198    if ($this->LogSQLQuery == true)
    9299      file_put_contents($this->LogFile, $Query.$Duration."\n", FILE_APPEND);
     
    101108      $this->insert_id = $this->PDO->lastInsertId();
    102109    } else
    103     {
     110    {     
    104111      $this->Error = $this->PDO->errorInfo();
    105112      $this->Error = $this->Error[2];
     
    123130  function insert($Table, $Data)
    124131  {
     132    $this->query($this->GetInsert($Table, $Data));
     133    $this->insert_id = $this->PDO->lastInsertId();
     134  }
     135 
     136  function GetInsert($Table, $Data)
     137  {
    125138    $Name = '';
    126139    $Values = '';
     
    137150    $Name = substr($Name, 1);
    138151    $Values = substr($Values, 1);
    139     $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
    140     $this->insert_id = $this->PDO->lastInsertId();
     152    return 'INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')';
    141153  }
    142154
    143155  function update($Table, $Condition, $Data)
     156  {
     157    $this->query($this->GetUpdate($Table, $Condition, $Data));
     158  }
     159 
     160  function GetUpdate($Table, $Condition, $Data)
    144161  {
    145162    $Values = '';
     
    154171    }
    155172    $Values = substr($Values, 2);
    156     $this->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
     173    return 'UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')';
    157174  }
    158175
     
    200217  public function __wakeup()
    201218  {
     219  }
     220 
     221  public function Transaction($Queries)
     222  {
     223      $this->PDO->beginTransaction();
     224      foreach ($Queries as $Query)
     225      {
     226        $Statement = $this->PDO->prepare($Query);
     227        $Statement->execute();
     228      }         
     229      $this->PDO->commit();
    202230  }
    203231}
Note: See TracChangeset for help on using the changeset viewer.