Changeset 10 for trunk/Database.php
- Timestamp:
- Aug 28, 2019, 1:30:12 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Database.php
r2 r10 2 2 3 3 // Extended database class 4 // Date: 2011-11-25 4 // Date: 2016-01-11 5 6 function microtime_float() 7 { 8 list($usec, $sec) = explode(" ", microtime()); 9 return ((float)$usec + (float)$sec); 10 } 5 11 6 12 class DatabaseResult … … 27 33 class Database 28 34 { 29 var $Prefix = '';35 var $Prefix; 30 36 var $Functions; 31 37 var $Type; 32 38 var $PDO; 33 var $Error = '';39 var $Error; 34 40 var $insert_id; 35 var $LastQuery = '';41 var $LastQuery; 36 42 var $ShowSQLError; 37 43 var $ShowSQLQuery; 44 var $LogSQLQuery; 45 var $LogFile; 38 46 39 47 function __construct() 40 48 { 49 $this->Prefix = ''; 50 $this->Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()'); 41 51 $this->Type = 'mysql'; // mysql, pgsql 52 $this->Error = ''; 53 $this->LastQuery = ''; 42 54 $this->ShowSQLError = false; 43 55 $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 46 60 47 61 function Connect($Host, $User, $Password, $Database) … … 77 91 function query($Query) 78 92 { 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(); 80 95 $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); 81 101 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"); 83 104 $Result = new DatabaseResult(); 84 105 $Result->PDOStatement = $this->PDO->query($Query); … … 88 109 $this->insert_id = $this->PDO->lastInsertId(); 89 110 } else 90 { 111 { 91 112 $this->Error = $this->PDO->errorInfo(); 92 113 $this->Error = $this->Error[2]; … … 110 131 function insert($Table, $Data) 111 132 { 133 $this->query($this->GetInsert($Table, $Data)); 134 $this->insert_id = $this->PDO->lastInsertId(); 135 } 136 137 function GetInsert($Table, $Data) 138 { 112 139 $Name = ''; 113 140 $Values = ''; … … 124 151 $Name = substr($Name, 1); 125 152 $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.')'); 128 154 } 129 155 130 156 function update($Table, $Condition, $Data) 157 { 158 $this->query($this->GetUpdate($Table, $Condition, $Data)); 159 } 160 161 function GetUpdate($Table, $Condition, $Data) 131 162 { 132 163 $Values = ''; … … 141 172 } 142 173 $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.')'); 144 175 } 145 176 … … 175 206 } 176 207 208 function quote($Text) 209 { 210 return($this->PDO->quote($Text)); 211 } 212 177 213 public function __sleep() 178 214 { … … 182 218 public function __wakeup() 183 219 { 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(); 184 231 } 185 232 }
Note:
See TracChangeset
for help on using the changeset viewer.