Changeset 32 for trunk/Database.php
- Timestamp:
- Apr 7, 2020, 1:06:13 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Database.php
r19 r32 7 7 { 8 8 list($usec, $sec) = explode(" ", microtime()); 9 return ( (float)$usec + (float)$sec);9 return (float)$usec + (float)$sec; 10 10 } 11 11 … … 17 17 function fetch_assoc() 18 18 { 19 return ($this->PDOStatement->fetch(PDO::FETCH_ASSOC));19 return $this->PDOStatement->fetch(PDO::FETCH_ASSOC); 20 20 } 21 21 22 22 function fetch_array() 23 23 { 24 return ($this->PDOStatement->fetch(PDO::FETCH_BOTH));24 return $this->PDOStatement->fetch(PDO::FETCH_BOTH); 25 25 } 26 26 27 27 function fetch_row() 28 28 { 29 return ($this->PDOStatement->fetch(PDO::FETCH_NUM));29 return $this->PDOStatement->fetch(PDO::FETCH_NUM); 30 30 } 31 31 } … … 61 61 function Connect($Host, $User, $Password, $Database) 62 62 { 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; 65 65 else $ConnectionString = ''; 66 66 try { … … 81 81 function Connected() 82 82 { 83 return (isset($this->PDO));83 return isset($this->PDO); 84 84 } 85 85 … … 91 91 function query($Query) 92 92 { 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(); 95 95 $this->LastQuery = $Query; 96 96 //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)) 98 98 $Duration = ' ; '.round(microtime_float() - $QueryStartTime, 4). ' s'; 99 if ($this->LogSQLQuery == true)99 if ($this->LogSQLQuery == true) 100 100 file_put_contents($this->LogFile, $Query.$Duration."\n", FILE_APPEND); 101 if ($this->ShowSQLQuery == true)101 if ($this->ShowSQLQuery == true) 102 102 echo('<div style="border-bottom-width: 1px; border-bottom-style: solid; '. 103 103 'padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.$Duration.'</div>'."\n"); 104 104 $Result = new DatabaseResult(); 105 105 $Result->PDOStatement = $this->PDO->query($Query); 106 if ($Result->PDOStatement)106 if ($Result->PDOStatement) 107 107 { 108 108 $Result->num_rows = $Result->PDOStatement->rowCount(); … … 112 112 $this->Error = $this->PDO->errorInfo(); 113 113 $this->Error = $this->Error[2]; 114 if (($this->Error != '') and ($this->ShowSQLError == true))114 if (($this->Error != '') and ($this->ShowSQLError == true)) 115 115 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>'); 116 116 throw new Exception('SQL Error: '.$this->Error.', Query: '.$Query); 117 117 } 118 return ($Result);118 return $Result; 119 119 } 120 120 121 121 function select($Table, $What = '*', $Condition = 1) 122 122 { 123 return ($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition));123 return $this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition); 124 124 } 125 125 … … 139 139 $Name = ''; 140 140 $Values = ''; 141 foreach ($Data as $Key => $Value)141 foreach ($Data as $Key => $Value) 142 142 { 143 143 $Name .= ',`'.$Key.'`'; 144 if (!in_array($Value, $this->Functions))144 if (!in_array($Value, $this->Functions)) 145 145 { 146 if (is_null($Value)) $Value = 'NULL';146 if (is_null($Value)) $Value = 'NULL'; 147 147 else $Value = $this->PDO->quote($Value); 148 148 } … … 151 151 $Name = substr($Name, 1); 152 152 $Values = substr($Values, 1); 153 return ('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');153 return 'INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'; 154 154 } 155 155 … … 162 162 { 163 163 $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)) 167 167 { 168 if (is_null($Value)) $Value = 'NULL';168 if (is_null($Value)) $Value = 'NULL'; 169 169 else $Value = $this->PDO->quote($Value); 170 170 } … … 172 172 } 173 173 $Values = substr($Values, 2); 174 return ('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');174 return 'UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')'; 175 175 } 176 176 … … 179 179 $Name = ''; 180 180 $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)) 184 184 { 185 if (is_null($Value)) $Value = 'NULL';185 if (is_null($Value)) $Value = 'NULL'; 186 186 else $Value = $this->PDO->quote($Value); 187 187 } … … 203 203 function real_escape_string($Text) 204 204 { 205 return (addslashes($Text));205 return addslashes($Text); 206 206 } 207 207 208 208 function quote($Text) 209 209 { 210 return ($this->PDO->quote($Text));210 return $this->PDO->quote($Text); 211 211 } 212 212 … … 232 232 $this->Error = $this->PDO->errorInfo(); 233 233 $this->Error = $this->Error[2]; 234 if (($this->Error != '') and ($this->ShowSQLError == true))234 if (($this->Error != '') and ($this->ShowSQLError == true)) 235 235 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>'); 236 236 throw new Exception('SQL Error: '.$this->Error.', Query: '.$Query); … … 241 241 function TimeToMysqlDateTime($Time) 242 242 { 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); 245 245 } 246 246 247 247 function TimeToMysqlDate($Time) 248 248 { 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); 251 251 } 252 252 253 253 function TimeToMysqlTime($Time) 254 254 { 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); 257 257 } 258 258 259 259 function MysqlDateTimeToTime($DateTime) 260 260 { 261 if ($DateTime == '') return(NULL);261 if ($DateTime == '') return NULL; 262 262 $Parts = explode(' ', $DateTime); 263 263 $DateParts = explode('-', $Parts[0]); 264 264 $TimeParts = explode(':', $Parts[1]); 265 265 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 266 return ($Result);266 return $Result; 267 267 } 268 268 269 269 function MysqlDateToTime($Date) 270 270 { 271 if ($Date == '') return(NULL);272 return (MysqlDateTimeToTime($Date.' 0:0:0'));271 if ($Date == '') return NULL; 272 return MysqlDateTimeToTime($Date.' 0:0:0'); 273 273 } 274 274 275 275 function MysqlTimeToTime($Time) 276 276 { 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.