Changeset 63 for trunk/Packages/Common/Database.php
- Timestamp:
- Aug 3, 2021, 11:20:41 AM (3 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:ignore
-
old new 1 nbproject2 Config.php3 1 .settings 4 2 .project 5 3 .buildpath 4 .htaccess
-
- Property svn:ignore
-
trunk/Packages/Common/Database.php
r56 r63 2 2 3 3 // Extended database class 4 // Date: 2020- 04-074 // Date: 2020-11-10 5 5 6 6 function microtime_float() … … 12 12 class DatabaseResult 13 13 { 14 var$PDOStatement;15 var$num_rows = 0;14 public PDOStatement $PDOStatement; 15 public int $num_rows = 0; 16 16 17 17 function fetch_assoc() … … 33 33 class Database 34 34 { 35 var $Prefix; 36 var $Functions; 37 var $Type; 38 var $PDO; 39 var $Error; 40 var $insert_id; 41 var $LastQuery; 42 var $ShowSQLError; 43 var $ShowSQLQuery; 44 var $LogSQLQuery; 45 var $LogFile; 35 public string $Prefix; 36 public array $Functions; 37 public string $Type; 38 public PDO $PDO; 39 public string $Error; 40 public string $insert_id; 41 public string $LastQuery; 42 public bool $ShowSQLError; 43 public bool $ShowSQLQuery; 44 public bool $LogSQLQuery; 45 public string $LogFile; 46 public string $Database; 46 47 47 48 function __construct() 48 49 { 49 50 $this->Prefix = ''; 50 $this->Functions = array('NOW( )', 'CURDATE()', 'CURTIME()', 'UUID()');51 $this->Functions = array('NOW(', 'CURDATE(', 'CURTIME(', 'UUID(', 'SHA1('); 51 52 $this->Type = 'mysql'; // mysql, pgsql 52 53 $this->Error = ''; … … 56 57 $this->LogSQLQuery = false; 57 58 $this->LogFile = dirname(__FILE__).'/../../Query.log'; 58 }59 60 61 function Connect( $Host, $User, $Password, $Database)59 $this->Database = ''; 60 } 61 62 function Connect(string $Host, string $User, string $Password, string $Database): void 62 63 { 63 64 if ($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database; 64 65 else if ($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host; 65 66 else $ConnectionString = ''; 67 $this->Database = $Database; 66 68 try { 67 69 $this->PDO = new PDO($ConnectionString, $User, $Password); 68 69 70 } catch (Exception $E) 70 71 { … … 74 75 } 75 76 76 function Disconnect() 77 function Disconnect(): void 77 78 { 78 79 unset($this->PDO); 79 80 } 80 81 81 function Connected() 82 function Connected(): bool 82 83 { 83 84 return isset($this->PDO); 84 85 } 85 86 86 function select_db( $Database)87 function select_db(string $Database) 87 88 { 88 89 $this->query('USE `'.$Database.'`'); 89 90 } 90 91 91 function query($Query) 92 function query($Query): DatabaseResult 92 93 { 93 94 if (!$this->Connected()) throw new Exception(T('Not connected to database')); 94 95 if (($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) $QueryStartTime = microtime_float(); 95 $this->LastQuery = $Query; 96 $this->LastQuery = $Query; 97 //echo('a'.$this->ShowSQLQuery.'<'.$QueryStartTime.', '.microtime_float()); 96 98 if (($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) 97 $Duration = ' ; '.round(microtime_float() - $QueryStartTime, 4). ' s'; 98 if ($this->LogSQLQuery == true) 99 { 100 $Time = round(microtime_float() - $QueryStartTime, 4); 101 $Duration = ' ; '.$Time. ' s'; 102 } 103 if (($this->LogSQLQuery == true) and ($Time != 0)) 99 104 file_put_contents($this->LogFile, $Query.$Duration."\n", FILE_APPEND); 100 105 if ($this->ShowSQLQuery == true) … … 102 107 'padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.$Duration.'</div>'."\n"); 103 108 $Result = new DatabaseResult(); 104 $Result->PDOStatement = $this->PDO->query($Query); 105 if ($Result->PDOStatement) 106 { 107 $Result->num_rows = $Result->PDOStatement->rowCount(); 109 $Statement = $this->PDO->query($Query); 110 if ($Statement) 111 { 112 $Result->PDOStatement = $Statement; 113 $Result->num_rows = $Statement->rowCount(); 108 114 $this->insert_id = $this->PDO->lastInsertId(); 109 115 } else 110 { 111 $ this->Error = $this->PDO->errorInfo();112 $this->Error = $ this->Error[2];116 { 117 $Error = $this->PDO->errorInfo(); 118 $this->Error = $Error[2]; 113 119 if (($this->Error != '') and ($this->ShowSQLError == true)) 114 120 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>'); … … 118 124 } 119 125 120 function select( $Table, $What = '*', $Condition = 1)126 function select(string $Table, string $What = '*', string $Condition = '1'): DatabaseResult 121 127 { 122 128 return $this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition); 123 129 } 124 130 125 function delete( $Table, $Condition)131 function delete(string $Table, string $Condition): void 126 132 { 127 133 $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition); 128 134 } 129 135 130 function insert( $Table, $Data)136 function insert(string $Table, array $Data): int 131 137 { 132 138 $this->query($this->GetInsert($Table, $Data)); 133 139 $this->insert_id = $this->PDO->lastInsertId(); 134 } 135 136 function GetInsert($Table, $Data) 140 return $this->insert_id; 141 } 142 143 function IsFunction(string $Text): bool 144 { 145 $Pos = strpos($Text, '('); 146 return ($Pos !== false) && in_array(substr($Text, 0, $Pos + 1), $this->Functions); 147 } 148 149 function GetInsert(string $Table, array $Data): string 137 150 { 138 151 $Name = ''; … … 141 154 { 142 155 $Name .= ',`'.$Key.'`'; 143 if (!in_array($Value, $this->Functions)) 156 if (is_null($Value)) $Value = 'NULL'; 157 else if (!$this->IsFunction($Value)) 144 158 { 145 if (is_null($Value)) $Value = 'NULL'; 146 else $Value = $this->PDO->quote($Value); 159 $Value = $this->PDO->quote($Value); 147 160 } 148 161 $Values .= ','.$Value; … … 153 166 } 154 167 155 function update( $Table, $Condition, $Data)168 function update(string $Table, string $Condition, array $Data): void 156 169 { 157 170 $this->query($this->GetUpdate($Table, $Condition, $Data)); 158 171 } 159 160 function GetUpdate( $Table, $Condition, $Data)172 173 function GetUpdate(string $Table, string $Condition, array $Data): string 161 174 { 162 175 $Values = ''; 163 176 foreach ($Data as $Key => $Value) 164 177 { 165 if (!in_array($Value, $this->Functions)) 178 if (is_null($Value)) $Value = 'NULL'; 179 else if (!$this->IsFunction($Value)) 166 180 { 167 if (is_null($Value)) $Value = 'NULL'; 168 else $Value = $this->PDO->quote($Value); 181 $Value = $this->PDO->quote($Value); 169 182 } 170 183 $Values .= ', `'.$Key.'`='.$Value; … … 174 187 } 175 188 176 function replace( $Table, $Data)189 function replace(string $Table, array $Data): void 177 190 { 178 191 $Name = ''; … … 180 193 foreach ($Data as $Key => $Value) 181 194 { 182 if (!in_array($Value, $this->Functions)) 195 if (is_null($Value)) $Value = 'NULL'; 196 else if (!$this->IsFunction($Value)) 183 197 { 184 if (is_null($Value)) $Value = 'NULL'; 185 else $Value = $this->PDO->quote($Value); 198 $Value = $this->PDO->quote($Value); 186 199 } 187 200 $Name .= ',`'.$Key.'`'; … … 195 208 } 196 209 197 function charset( $Charset)210 function charset(string $Charset): void 198 211 { 199 212 $this->query('SET NAMES "'.$Charset.'"'); 200 213 } 201 214 202 function real_escape_string( $Text)215 function real_escape_string(string $Text): string 203 216 { 204 217 return addslashes($Text); 205 218 } 206 219 207 function quote( $Text)220 function quote(string $Text): string 208 221 { 209 222 return $this->PDO->quote($Text); 210 223 } 211 224 212 public function __sleep() 225 public function __sleep(): array 213 226 { 214 227 return array('LastQuery'); 215 228 } 216 229 217 public function __wakeup() 218 { 219 } 220 221 public function Transaction($Queries) 222 { 223 $this->PDO->beginTransaction(); 224 foreach ($Queries as $Query) 225 { 226 $Statement = $this->PDO->prepare($Query); 227 $Statement->execute(); 228 } 229 $this->PDO->commit(); 230 public function __wakeup(): void 231 { 232 } 233 234 public function Transaction(array $Queries): void 235 { 236 //echo('|'."\n"); 237 $this->PDO->beginTransaction(); 238 foreach ($Queries as $Query) 239 { 240 //echo('|'.$Query."\n"); 241 $Statement = $this->PDO->prepare($Query); 242 $Statement->execute(); 243 } 244 $this->PDO->commit(); 245 } 246 247 public function TableExists(string $Name): bool 248 { 249 $DbResult = $this->query('SELECT * FROM information_schema.tables WHERE table_schema = "'.$this->Database. 250 '" AND table_name = "'.$Name.'" LIMIT 1'); 251 return $DbResult->num_rows != 0; 230 252 } 231 253 }
Note:
See TracChangeset
for help on using the changeset viewer.