Changeset 10 for Common/Database.php
- Timestamp:
- Apr 7, 2020, 8:52:35 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/Database.php
r9 r10 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))145 { 146 if (is_null($Value)) $Value = 'NULL';144 if (!in_array($Value, $this->Functions)) 145 { 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))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'; 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))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'; 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 … … 234 234 function TimeToMysqlDateTime($Time) 235 235 { 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); 238 238 } 239 239 240 240 function TimeToMysqlDate($Time) 241 241 { 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); 244 244 } 245 245 246 246 function TimeToMysqlTime($Time) 247 247 { 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); 250 250 } 251 251 252 252 function MysqlDateTimeToTime($DateTime) 253 253 { 254 if ($DateTime == '') return(NULL);254 if ($DateTime == '') return NULL; 255 255 $Parts = explode(' ', $DateTime); 256 256 $DateParts = explode('-', $Parts[0]); 257 257 $TimeParts = explode(':', $Parts[1]); 258 258 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 259 return ($Result);259 return $Result; 260 260 } 261 261 262 262 function MysqlDateToTime($Date) 263 263 { 264 if ($Date == '') return(NULL);265 return (MysqlDateTimeToTime($Date.' 0:0:0'));264 if ($Date == '') return NULL; 265 return MysqlDateTimeToTime($Date.' 0:0:0'); 266 266 } 267 267 268 268 function MysqlTimeToTime($Time) 269 269 { 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.