Changeset 55 for trunk/Packages/Common/Database.php
- Timestamp:
- Apr 7, 2020, 12:55:39 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/Database.php
r37 r55 11 11 function fetch_assoc() 12 12 { 13 return ($this->PDOStatement->fetch(PDO::FETCH_ASSOC));13 return $this->PDOStatement->fetch(PDO::FETCH_ASSOC); 14 14 } 15 15 16 16 function fetch_array() 17 17 { 18 return ($this->PDOStatement->fetch(PDO::FETCH_BOTH));18 return $this->PDOStatement->fetch(PDO::FETCH_BOTH); 19 19 } 20 20 21 21 function fetch_row() 22 22 { 23 return ($this->PDOStatement->fetch(PDO::FETCH_NUM));23 return $this->PDOStatement->fetch(PDO::FETCH_NUM); 24 24 } 25 25 } … … 54 54 function Connect($Host, $User, $Password, $Database) 55 55 { 56 if ($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database;57 else if ($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host;56 if ($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database; 57 else if ($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host; 58 58 else $ConnectionString = ''; 59 59 try { … … 74 74 function Connected() 75 75 { 76 return (isset($this->PDO));76 return isset($this->PDO); 77 77 } 78 78 … … 84 84 function query($Query) 85 85 { 86 if (!$this->Connected()) throw new Exception(T('Not connected to database'));87 if (($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) $QueryStartTime = microtime();86 if (!$this->Connected()) throw new Exception(T('Not connected to database')); 87 if (($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) $QueryStartTime = microtime(); 88 88 $this->LastQuery = $Query; 89 if (($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true))89 if (($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) 90 90 $Duration = ' ; '.round(microtime() - $QueryStartTime, 4). ' s'; 91 if ($this->LogSQLQuery == true)91 if ($this->LogSQLQuery == true) 92 92 file_put_contents($this->LogFile, $Query.$Duration."\n", FILE_APPEND); 93 if ($this->ShowSQLQuery == true)93 if ($this->ShowSQLQuery == true) 94 94 echo('<div style="border-bottom-width: 1px; border-bottom-style: solid; '. 95 95 'padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.$Duration.'</div>'."\n"); 96 96 $Result = new DatabaseResult(); 97 97 $Result->PDOStatement = $this->PDO->query($Query); 98 if ($Result->PDOStatement)98 if ($Result->PDOStatement) 99 99 { 100 100 $Result->num_rows = $Result->PDOStatement->rowCount(); … … 104 104 $this->Error = $this->PDO->errorInfo(); 105 105 $this->Error = $this->Error[2]; 106 if (($this->Error != '') and ($this->ShowSQLError == true))106 if (($this->Error != '') and ($this->ShowSQLError == true)) 107 107 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>'); 108 108 throw new Exception('SQL Error: '.$this->Error.', Query: '.$Query); 109 109 } 110 return ($Result);110 return $Result; 111 111 } 112 112 113 113 function select($Table, $What = '*', $Condition = 1) 114 114 { 115 return ($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition));115 return $this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition); 116 116 } 117 117 … … 125 125 $Name = ''; 126 126 $Values = ''; 127 foreach ($Data as $Key => $Value)127 foreach ($Data as $Key => $Value) 128 128 { 129 129 $Name .= ',`'.$Key.'`'; 130 if (!in_array($Value, $this->Functions))130 if (!in_array($Value, $this->Functions)) 131 131 { 132 if (is_null($Value)) $Value = 'NULL';132 if (is_null($Value)) $Value = 'NULL'; 133 133 else $Value = $this->PDO->quote($Value); 134 134 } … … 144 144 { 145 145 $Values = ''; 146 foreach ($Data as $Key => $Value)147 { 148 if (!in_array($Value, $this->Functions))146 foreach ($Data as $Key => $Value) 147 { 148 if (!in_array($Value, $this->Functions)) 149 149 { 150 if (is_null($Value)) $Value = 'NULL';150 if (is_null($Value)) $Value = 'NULL'; 151 151 else $Value = $this->PDO->quote($Value); 152 152 } … … 161 161 $Name = ''; 162 162 $Values = ''; 163 foreach ($Data as $Key => $Value)164 { 165 if (!in_array($Value, $this->Functions))163 foreach ($Data as $Key => $Value) 164 { 165 if (!in_array($Value, $this->Functions)) 166 166 { 167 if (is_null($Value)) $Value = 'NULL';167 if (is_null($Value)) $Value = 'NULL'; 168 168 else $Value = $this->PDO->quote($Value); 169 169 } … … 185 185 function real_escape_string($Text) 186 186 { 187 return (addslashes($Text));187 return addslashes($Text); 188 188 } 189 189 190 190 function quote($Text) 191 191 { 192 return ($this->PDO->quote($Text));192 return $this->PDO->quote($Text); 193 193 } 194 194 … … 205 205 function TimeToMysqlDateTime($Time) 206 206 { 207 if ($Time == NULL) return(NULL);208 else return (date('Y-m-d H:i:s', $Time));207 if ($Time == NULL) return NULL; 208 else return date('Y-m-d H:i:s', $Time); 209 209 } 210 210 211 211 function TimeToMysqlDate($Time) 212 212 { 213 if ($Time == NULL) return(NULL);214 else return (date('Y-m-d', $Time));213 if ($Time == NULL) return NULL; 214 else return date('Y-m-d', $Time); 215 215 } 216 216 217 217 function TimeToMysqlTime($Time) 218 218 { 219 if ($Time == NULL) return(NULL);220 else return (date('H:i:s', $Time));219 if ($Time == NULL) return NULL; 220 else return date('H:i:s', $Time); 221 221 } 222 222 223 223 function MysqlDateTimeToTime($DateTime) 224 224 { 225 if ($DateTime == '') return(NULL);225 if ($DateTime == '') return NULL; 226 226 $Parts = explode(' ', $DateTime); 227 227 $DateParts = explode('-', $Parts[0]); 228 228 $TimeParts = explode(':', $Parts[1]); 229 229 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 230 return ($Result);230 return $Result; 231 231 } 232 232 233 233 function MysqlDateToTime($Date) 234 234 { 235 if ($Date == '') return(NULL);236 return (MysqlDateTimeToTime($Date.' 0:0:0'));235 if ($Date == '') return NULL; 236 return MysqlDateTimeToTime($Date.' 0:0:0'); 237 237 } 238 238 239 239 function MysqlTimeToTime($Time) 240 240 { 241 if ($Time == '') return(NULL);242 return (MysqlDateTimeToTime('0000-00-00 '.$Time));243 } 241 if ($Time == '') return NULL; 242 return MysqlDateTimeToTime('0000-00-00 '.$Time); 243 }
Note:
See TracChangeset
for help on using the changeset viewer.