Changeset 59 for branches/mvc/Base/Database.php
- Timestamp:
- Mar 1, 2015, 12:43:56 PM (10 years ago)
- Location:
- branches/mvc
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/mvc/Base/Database.php
r47 r59 2 2 3 3 // Extended database class 4 // Date: 2009-02-16 5 6 class Database extends mysqli 4 // Date: 2011-11-25 5 6 class DatabaseResult 7 { 8 var $PDOStatement; 9 var $num_rows = 0; 10 11 function fetch_assoc() 12 { 13 return($this->PDOStatement->fetch(PDO::FETCH_ASSOC)); 14 } 15 16 function fetch_array() 17 { 18 return($this->PDOStatement->fetch(PDO::FETCH_BOTH)); 19 } 20 21 function fetch_row() 22 { 23 return($this->PDOStatement->fetch(PDO::FETCH_NUM)); 24 } 25 } 26 27 class Database 7 28 { 8 29 var $Prefix = ''; 9 var $Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()'); 10 var $ShowSQLQuery = false; 11 var $ShowSQLError = false; 30 var $Functions; 31 var $Type; 32 var $PDO; 33 var $Error = ''; 34 var $insert_id; 35 var $LastQuery = ''; 36 var $ShowSQLError; 37 var $ShowSQLQuery; 38 39 function __construct() 40 { 41 $this->Type = 'mysql'; // mysql, pgsql 42 $this->ShowSQLError = false; 43 $this->ShowSQLQuery = false; 44 $this->Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()'); 45 } 46 47 function Connect($Host, $User, $Password, $Database) 48 { 49 if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database; 50 else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host; 51 else $ConnectionString = ''; 52 try { 53 $this->PDO = new PDO($ConnectionString, $User, $Password); 54 55 } catch (Exception $E) 56 { 57 unset($this->PDO); 58 throw new Exception($E->getMessage()); 59 } 60 } 61 62 function Disconnect() 63 { 64 unset($this->PDO); 65 } 66 67 function Connected() 68 { 69 return(isset($this->PDO)); 70 } 71 72 function select_db($Database) 73 { 74 $this->query('USE `'.$Database.'`'); 75 } 12 76 13 77 function query($Query) 14 78 { 15 if($this->ShowSQLQuery) 16 { 17 if(isset($_SERVER['REMOTE_ADDR'])) 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>'); 18 else echo($Query."\n"); 19 } 20 $Result = parent::query($Query); 21 if(($this->error != '') and ($this->ShowSQLError)) 22 { 23 if(isset($_SERVER['REMOTE_ADDR'])) echo('<div><strong>SQL Error: </strong>'.$this->error.'<br />'.$Query.'</div>'); 24 echo('SQL Error: '.$this->error.' '.$Query."\n"); 25 } 26 79 if(!$this->Connected()) throw new Exception('Not connected to database'); 80 $this->LastQuery = $Query; 81 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"); 83 $Result = new DatabaseResult(); 84 $Result->PDOStatement = $this->PDO->query($Query); 85 if($Result->PDOStatement) 86 { 87 $Result->num_rows = $Result->PDOStatement->rowCount(); 88 $this->insert_id = $this->PDO->lastInsertId(); 89 } else 90 { 91 $this->Error = $this->PDO->errorInfo(); 92 $this->Error = $this->Error[2]; 93 if(($this->Error != '') and ($this->ShowSQLError == true)) 94 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>'); 95 throw new Exception('SQL Error: '.$this->Error.', Query: '.$Query); 96 } 27 97 return($Result); 28 98 } … … 45 115 { 46 116 $Name .= ',`'.$Key.'`'; 47 if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"'; 117 if(!in_array($Value, $this->Functions)) 118 { 119 if(is_null($Value)) $Value = 'NULL'; 120 else $Value = $this->PDO->quote($Value); 121 } 48 122 $Values .= ','.$Value; 49 123 } … … 51 125 $Values = substr($Values, 1); 52 126 $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'); 127 $this->insert_id = $this->PDO->lastInsertId(); 53 128 } 54 129 … … 58 133 foreach($Data as $Key => $Value) 59 134 { 60 if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"'; 135 if(!in_array($Value, $this->Functions)) 136 { 137 if(is_null($Value)) $Value = 'NULL'; 138 else $Value = $this->PDO->quote($Value); 139 } 61 140 $Values .= ', `'.$Key.'`='.$Value; 62 141 } … … 71 150 foreach($Data as $Key => $Value) 72 151 { 73 if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"'; 152 if(!in_array($Value, $this->Functions)) 153 { 154 if(is_null($Value)) $Value = 'NULL'; 155 else $Value = $this->PDO->quote($Value); 156 } 74 157 $Name .= ',`'.$Key.'`'; 75 158 $Values .= ','.$Value; … … 79 162 //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br />'); 80 163 $this->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'); 81 //echo($this->error().'<br />');164 //echo($this->error().'<br>'); 82 165 } 83 166 … … 87 170 } 88 171 89 function TimeToMysqlDateTime($Time) 90 { 91 return(date('Y-m-d H:i:s', $Time)); 92 } 93 94 function MysqlDateTimeToTime($Time) 95 { 96 $Parts = explode(' ', $Time); 97 $DateParts = explode('-', $Parts[0]); 98 $TimeParts = explode(':', $Parts[1]); 99 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 100 return($Result); 101 } 102 103 function MysqlDateToTime($Time) 104 { 105 return(MysqlDateTimeToTime($Time.' 0:0:0')); 106 } 107 } 172 function real_escape_string($Text) 173 { 174 return(addslashes($Text)); 175 } 176 177 public function __sleep() 178 { 179 return array('LastQuery'); 180 } 181 182 public function __wakeup() 183 { 184 } 185 186 function MysqlDateTimeToTime($DateTime) 187 { 188 return(MysqlDateTimeToTime($DateTime)); 189 } 190 191 function TimeToMysqlDateTime($DateTime) 192 { 193 return(TimeToMysqlDateTime($DateTime)); 194 } 195 } 196 197 function TimeToMysqlDateTime($Time) 198 { 199 if($Time == NULL) return(NULL); 200 else return(date('Y-m-d H:i:s', $Time)); 201 } 202 203 function TimeToMysqlDate($Time) 204 { 205 if($Time == NULL) return(NULL); 206 else return(date('Y-m-d', $Time)); 207 } 208 209 function TimeToMysqlTime($Time) 210 { 211 if($Time == NULL) return(NULL); 212 else return(date('H:i:s', $Time)); 213 } 214 215 function MysqlDateTimeToTime($DateTime) 216 { 217 if($DateTime == '') return(NULL); 218 $Parts = explode(' ', $DateTime); 219 $DateParts = explode('-', $Parts[0]); 220 $TimeParts = explode(':', $Parts[1]); 221 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 222 return($Result); 223 } 224 225 function MysqlDateToTime($Date) 226 { 227 if($Date == '') return(NULL); 228 return(MysqlDateTimeToTime($Date.' 0:0:0')); 229 } 230 231 function MysqlTimeToTime($Time) 232 { 233 if($Time == '') return(NULL); 234 return(MysqlDateTimeToTime('0000-00-00 '.$Time)); 235 }
Note:
See TracChangeset
for help on using the changeset viewer.