Changeset 887 for trunk/Packages/Common/Database.php
- Timestamp:
- Nov 20, 2020, 12:08:12 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/Database.php
r874 r887 2 2 3 3 // Extended database class 4 // Date: 20 16-01-114 // Date: 2020-11-10 5 5 6 6 function microtime_float() … … 13 13 { 14 14 var $PDOStatement; 15 var$num_rows = 0;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 46 47 47 function __construct() … … 57 57 $this->LogFile = dirname(__FILE__).'/../../Query.log'; 58 58 } 59 60 61 function Connect($Host, $User, $Password, $Database) 59 60 function Connect(string $Host, string $User, string $Password, string $Database) 62 61 { 63 62 if ($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database; … … 79 78 } 80 79 81 function Connected() 80 function Connected(): bool 82 81 { 83 82 return isset($this->PDO); 84 83 } 85 84 86 function select_db( $Database)85 function select_db(string $Database) 87 86 { 88 87 $this->query('USE `'.$Database.'`'); 89 88 } 90 89 91 function query($Query) 90 function query($Query): DatabaseResult 92 91 { 93 92 if (!$this->Connected()) throw new Exception(T('Not connected to database')); … … 99 98 $Time = round(microtime_float() - $QueryStartTime, 4); 100 99 $Duration = ' ; '.$Time. ' s'; 101 } 100 } 102 101 if (($this->LogSQLQuery == true) and ($Time != 0)) 103 102 file_put_contents($this->LogFile, $Query.$Duration."\n", FILE_APPEND); … … 112 111 $this->insert_id = $this->PDO->lastInsertId(); 113 112 } else 114 { 115 $ this->Error = $this->PDO->errorInfo();116 $this->Error = $ this->Error[2];113 { 114 $Error = $this->PDO->errorInfo(); 115 $this->Error = $Error[2]; 117 116 if (($this->Error != '') and ($this->ShowSQLError == true)) 118 117 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>'); … … 122 121 } 123 122 124 function select( $Table, $What = '*', $Condition = 1)123 function select(string $Table, string $What = '*', string $Condition = '1'): DatabaseResult 125 124 { 126 125 return $this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition); 127 126 } 128 127 129 function delete( $Table,$Condition)128 function delete(string $Table, string $Condition) 130 129 { 131 130 $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition); 132 131 } 133 132 134 function insert( $Table,$Data)133 function insert(string $Table, array $Data) 135 134 { 136 135 $this->query($this->GetInsert($Table, $Data)); 137 136 $this->insert_id = $this->PDO->lastInsertId(); 138 137 } 139 140 function GetInsert( $Table, $Data)138 139 function GetInsert(string $Table, array $Data): string 141 140 { 142 141 $Name = ''; … … 157 156 } 158 157 159 function update( $Table, $Condition,$Data)158 function update(string $Table, string $Condition, array $Data) 160 159 { 161 160 $this->query($this->GetUpdate($Table, $Condition, $Data)); 162 161 } 163 164 function GetUpdate( $Table, $Condition, $Data)162 163 function GetUpdate(string $Table, string $Condition, array $Data): string 165 164 { 166 165 $Values = ''; … … 178 177 } 179 178 180 function replace( $Table,$Data)179 function replace(string $Table, array $Data) 181 180 { 182 181 $Name = ''; … … 199 198 } 200 199 201 function charset( $Charset)200 function charset(string $Charset) 202 201 { 203 202 $this->query('SET NAMES "'.$Charset.'"'); 204 203 } 205 204 206 function real_escape_string( $Text)205 function real_escape_string(string $Text): string 207 206 { 208 207 return addslashes($Text); 209 208 } 210 209 211 function quote( $Text)210 function quote(string $Text): string 212 211 { 213 212 return $this->PDO->quote($Text); … … 222 221 { 223 222 } 224 225 public function Transaction( $Queries)223 224 public function Transaction(array $Queries) 226 225 { 227 226 //echo('|'."\n");
Note:
See TracChangeset
for help on using the changeset viewer.