Changeset 10 for trunk/Database.php


Ignore:
Timestamp:
Aug 28, 2019, 1:30:12 AM (5 years ago)
Author:
chronos
Message:
  • Modified: Sync data for all runners and teams from official server.
  • Modified: Optimize sync inserts with transaction.
  • Added: Show table of all teams.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Database.php

    r2 r10  
    22
    33// Extended database class
    4 // Date: 2011-11-25
     4// Date: 2016-01-11
     5
     6function microtime_float()
     7{
     8  list($usec, $sec) = explode(" ", microtime());
     9  return ((float)$usec + (float)$sec);
     10}
    511
    612class DatabaseResult
     
    2733class Database
    2834{
    29   var $Prefix = '';
     35  var $Prefix;
    3036  var $Functions;
    3137  var $Type;
    3238  var $PDO;
    33   var $Error = '';
     39  var $Error;
    3440  var $insert_id;
    35   var $LastQuery = '';
     41  var $LastQuery;
    3642  var $ShowSQLError;
    3743  var $ShowSQLQuery;
     44  var $LogSQLQuery;
     45  var $LogFile;
    3846
    3947  function __construct()
    4048  {
     49    $this->Prefix = '';
     50    $this->Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()');
    4151    $this->Type = 'mysql';  // mysql, pgsql
     52    $this->Error = '';
     53    $this->LastQuery = '';
    4254    $this->ShowSQLError = false;
    4355    $this->ShowSQLQuery = false;
    44     $this->Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()');
    45   }
     56    $this->LogSQLQuery = false;
     57    $this->LogFile = dirname(__FILE__).'/../../Query.log';
     58  }
     59 
    4660
    4761  function Connect($Host, $User, $Password, $Database)
     
    7791  function query($Query)
    7892  {
    79     if(!$this->Connected()) throw new Exception('Not connected to database');
     93    if(!$this->Connected()) throw new Exception(T('Not connected to database'));
     94    if(($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) $QueryStartTime = microtime_float();
    8095    $this->LastQuery = $Query;
     96    //echo('a'.$this->ShowSQLQuery.'<'.$QueryStartTime.', '.microtime_float());
     97    if(($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true))
     98      $Duration = ' ; '.round(microtime_float() - $QueryStartTime, 4). ' s';
     99    if($this->LogSQLQuery == true)
     100      file_put_contents($this->LogFile, $Query.$Duration."\n", FILE_APPEND);
    81101    if($this->ShowSQLQuery == true)
    82       echo('<div style="border-bottom-width: 1px; border-bottom-style: solid; padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.'</div>'."\n");
     102      echo('<div style="border-bottom-width: 1px; border-bottom-style: solid; '.
     103      'padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.$Duration.'</div>'."\n");
    83104    $Result = new DatabaseResult();
    84105    $Result->PDOStatement = $this->PDO->query($Query);
     
    88109      $this->insert_id = $this->PDO->lastInsertId();
    89110    } else
    90     {
     111    {     
    91112      $this->Error = $this->PDO->errorInfo();
    92113      $this->Error = $this->Error[2];
     
    110131  function insert($Table, $Data)
    111132  {
     133    $this->query($this->GetInsert($Table, $Data));
     134    $this->insert_id = $this->PDO->lastInsertId();
     135  }
     136 
     137  function GetInsert($Table, $Data)
     138  {
    112139    $Name = '';
    113140    $Values = '';
     
    124151    $Name = substr($Name, 1);
    125152    $Values = substr($Values, 1);
    126     $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
    127     $this->insert_id = $this->PDO->lastInsertId();
     153    return('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
    128154  }
    129155
    130156  function update($Table, $Condition, $Data)
     157  {
     158    $this->query($this->GetUpdate($Table, $Condition, $Data));
     159  }
     160 
     161  function GetUpdate($Table, $Condition, $Data)
    131162  {
    132163    $Values = '';
     
    141172    }
    142173    $Values = substr($Values, 2);
    143     $this->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
     174    return('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
    144175  }
    145176
     
    175206  }
    176207
     208  function quote($Text)
     209  {
     210    return($this->PDO->quote($Text));
     211  }
     212
    177213  public function __sleep()
    178214  {
     
    182218  public function __wakeup()
    183219  {
     220  }
     221 
     222  public function Transaction($Queries)
     223  {
     224      $this->PDO->beginTransaction();
     225      foreach ($Queries as $Query)
     226      {
     227        $Statement = $this->PDO->prepare($Query);
     228        $Statement->execute();
     229      }         
     230      $this->PDO->commit();
    184231  }
    185232}
Note: See TracChangeset for help on using the changeset viewer.