1 | <?php
|
---|
2 |
|
---|
3 | // Extended database class
|
---|
4 | // Date: 2016-01-11
|
---|
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
|
---|
28 | {
|
---|
29 | var $Prefix;
|
---|
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 | var $LogSQLQuery;
|
---|
39 | var $LogFile;
|
---|
40 |
|
---|
41 | function __construct()
|
---|
42 | {
|
---|
43 | $this->Prefix = '';
|
---|
44 | $this->Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()');
|
---|
45 | $this->Type = 'mysql'; // mysql, pgsql
|
---|
46 | $this->Error = '';
|
---|
47 | $this->LastQuery = '';
|
---|
48 | $this->ShowSQLError = false;
|
---|
49 | $this->ShowSQLQuery = false;
|
---|
50 | $this->LogSQLQuery = false;
|
---|
51 | $this->LogFile = dirname(__FILE__).'/../../Query.log';
|
---|
52 | }
|
---|
53 |
|
---|
54 | function Connect($Host, $User, $Password, $Database)
|
---|
55 | {
|
---|
56 | if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database;
|
---|
57 | else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host;
|
---|
58 | else $ConnectionString = '';
|
---|
59 | try {
|
---|
60 | $this->PDO = new PDO($ConnectionString, $User, $Password);
|
---|
61 |
|
---|
62 | } catch (Exception $E)
|
---|
63 | {
|
---|
64 | unset($this->PDO);
|
---|
65 | throw new Exception($E->getMessage());
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | function Disconnect()
|
---|
70 | {
|
---|
71 | unset($this->PDO);
|
---|
72 | }
|
---|
73 |
|
---|
74 | function Connected()
|
---|
75 | {
|
---|
76 | return(isset($this->PDO));
|
---|
77 | }
|
---|
78 |
|
---|
79 | function select_db($Database)
|
---|
80 | {
|
---|
81 | $this->query('USE `'.$Database.'`');
|
---|
82 | }
|
---|
83 |
|
---|
84 | function query($Query)
|
---|
85 | {
|
---|
86 | if(!$this->Connected()) throw new Exception(T('Not connected to database'));
|
---|
87 | if(($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) $QueryStartTime = microtime();
|
---|
88 | $this->LastQuery = $Query;
|
---|
89 | if(($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true))
|
---|
90 | $Duration = ' ; '.round(microtime() - $QueryStartTime, 4). ' s';
|
---|
91 | if($this->LogSQLQuery == true)
|
---|
92 | file_put_contents($this->LogFile, $Query.$Duration."\n", FILE_APPEND);
|
---|
93 | if($this->ShowSQLQuery == true)
|
---|
94 | echo('<div style="border-bottom-width: 1px; border-bottom-style: solid; '.
|
---|
95 | 'padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.$Duration.'</div>'."\n");
|
---|
96 | $Result = new DatabaseResult();
|
---|
97 | $Result->PDOStatement = $this->PDO->query($Query);
|
---|
98 | if($Result->PDOStatement)
|
---|
99 | {
|
---|
100 | $Result->num_rows = $Result->PDOStatement->rowCount();
|
---|
101 | $this->insert_id = $this->PDO->lastInsertId();
|
---|
102 | } else
|
---|
103 | {
|
---|
104 | $this->Error = $this->PDO->errorInfo();
|
---|
105 | $this->Error = $this->Error[2];
|
---|
106 | if(($this->Error != '') and ($this->ShowSQLError == true))
|
---|
107 | echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>');
|
---|
108 | throw new Exception('SQL Error: '.$this->Error.', Query: '.$Query);
|
---|
109 | }
|
---|
110 | return($Result);
|
---|
111 | }
|
---|
112 |
|
---|
113 | function select($Table, $What = '*', $Condition = 1)
|
---|
114 | {
|
---|
115 | return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition));
|
---|
116 | }
|
---|
117 |
|
---|
118 | function delete($Table, $Condition)
|
---|
119 | {
|
---|
120 | $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
|
---|
121 | }
|
---|
122 |
|
---|
123 | function insert($Table, $Data)
|
---|
124 | {
|
---|
125 | $Name = '';
|
---|
126 | $Values = '';
|
---|
127 | foreach($Data as $Key => $Value)
|
---|
128 | {
|
---|
129 | $Name .= ',`'.$Key.'`';
|
---|
130 | if(!in_array($Value, $this->Functions))
|
---|
131 | {
|
---|
132 | if(is_null($Value)) $Value = 'NULL';
|
---|
133 | else $Value = $this->PDO->quote($Value);
|
---|
134 | }
|
---|
135 | $Values .= ','.$Value;
|
---|
136 | }
|
---|
137 | $Name = substr($Name, 1);
|
---|
138 | $Values = substr($Values, 1);
|
---|
139 | $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
|
---|
140 | $this->insert_id = $this->PDO->lastInsertId();
|
---|
141 | }
|
---|
142 |
|
---|
143 | function update($Table, $Condition, $Data)
|
---|
144 | {
|
---|
145 | $Values = '';
|
---|
146 | foreach($Data as $Key => $Value)
|
---|
147 | {
|
---|
148 | if(!in_array($Value, $this->Functions))
|
---|
149 | {
|
---|
150 | if(is_null($Value)) $Value = 'NULL';
|
---|
151 | else $Value = $this->PDO->quote($Value);
|
---|
152 | }
|
---|
153 | $Values .= ', `'.$Key.'`='.$Value;
|
---|
154 | }
|
---|
155 | $Values = substr($Values, 2);
|
---|
156 | $this->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
|
---|
157 | }
|
---|
158 |
|
---|
159 | function replace($Table, $Data)
|
---|
160 | {
|
---|
161 | $Name = '';
|
---|
162 | $Values = '';
|
---|
163 | foreach($Data as $Key => $Value)
|
---|
164 | {
|
---|
165 | if(!in_array($Value, $this->Functions))
|
---|
166 | {
|
---|
167 | if(is_null($Value)) $Value = 'NULL';
|
---|
168 | else $Value = $this->PDO->quote($Value);
|
---|
169 | }
|
---|
170 | $Name .= ',`'.$Key.'`';
|
---|
171 | $Values .= ','.$Value;
|
---|
172 | }
|
---|
173 | $Name = substr($Name, 1);
|
---|
174 | $Values = substr($Values, 1);
|
---|
175 | //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br />');
|
---|
176 | $this->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
|
---|
177 | //echo($this->error().'<br>');
|
---|
178 | }
|
---|
179 |
|
---|
180 | function charset($Charset)
|
---|
181 | {
|
---|
182 | $this->query('SET NAMES "'.$Charset.'"');
|
---|
183 | }
|
---|
184 |
|
---|
185 | function real_escape_string($Text)
|
---|
186 | {
|
---|
187 | return(addslashes($Text));
|
---|
188 | }
|
---|
189 |
|
---|
190 | function quote($Text)
|
---|
191 | {
|
---|
192 | return($this->PDO->quote($Text));
|
---|
193 | }
|
---|
194 |
|
---|
195 | public function __sleep()
|
---|
196 | {
|
---|
197 | return array('LastQuery');
|
---|
198 | }
|
---|
199 |
|
---|
200 | public function __wakeup()
|
---|
201 | {
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | function TimeToMysqlDateTime($Time)
|
---|
206 | {
|
---|
207 | if($Time == NULL) return(NULL);
|
---|
208 | else return(date('Y-m-d H:i:s', $Time));
|
---|
209 | }
|
---|
210 |
|
---|
211 | function TimeToMysqlDate($Time)
|
---|
212 | {
|
---|
213 | if($Time == NULL) return(NULL);
|
---|
214 | else return(date('Y-m-d', $Time));
|
---|
215 | }
|
---|
216 |
|
---|
217 | function TimeToMysqlTime($Time)
|
---|
218 | {
|
---|
219 | if($Time == NULL) return(NULL);
|
---|
220 | else return(date('H:i:s', $Time));
|
---|
221 | }
|
---|
222 |
|
---|
223 | function MysqlDateTimeToTime($DateTime)
|
---|
224 | {
|
---|
225 | if($DateTime == '') return(NULL);
|
---|
226 | $Parts = explode(' ', $DateTime);
|
---|
227 | $DateParts = explode('-', $Parts[0]);
|
---|
228 | $TimeParts = explode(':', $Parts[1]);
|
---|
229 | $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
|
---|
230 | return($Result);
|
---|
231 | }
|
---|
232 |
|
---|
233 | function MysqlDateToTime($Date)
|
---|
234 | {
|
---|
235 | if($Date == '') return(NULL);
|
---|
236 | return(MysqlDateTimeToTime($Date.' 0:0:0'));
|
---|
237 | }
|
---|
238 |
|
---|
239 | function MysqlTimeToTime($Time)
|
---|
240 | {
|
---|
241 | if($Time == '') return(NULL);
|
---|
242 | return(MysqlDateTimeToTime('0000-00-00 '.$Time));
|
---|
243 | }
|
---|