source: branches/mvc/Base/Database.php

Last change on this file was 59, checked in by chronos, 10 years ago
  • Moved trunk as mvc branch.
File size: 5.8 KB
Line 
1<?php
2
3// Extended database class
4// Date: 2011-11-25
5
6class 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
27class 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
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 }
76
77 function query($Query)
78 {
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 }
97 return($Result);
98 }
99
100 function select($Table, $What = '*', $Condition = 1)
101 {
102 return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition));
103 }
104
105 function delete($Table, $Condition)
106 {
107 $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
108 }
109
110 function insert($Table, $Data)
111 {
112 $Name = '';
113 $Values = '';
114 foreach($Data as $Key => $Value)
115 {
116 $Name .= ',`'.$Key.'`';
117 if(!in_array($Value, $this->Functions))
118 {
119 if(is_null($Value)) $Value = 'NULL';
120 else $Value = $this->PDO->quote($Value);
121 }
122 $Values .= ','.$Value;
123 }
124 $Name = substr($Name, 1);
125 $Values = substr($Values, 1);
126 $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
127 $this->insert_id = $this->PDO->lastInsertId();
128 }
129
130 function update($Table, $Condition, $Data)
131 {
132 $Values = '';
133 foreach($Data as $Key => $Value)
134 {
135 if(!in_array($Value, $this->Functions))
136 {
137 if(is_null($Value)) $Value = 'NULL';
138 else $Value = $this->PDO->quote($Value);
139 }
140 $Values .= ', `'.$Key.'`='.$Value;
141 }
142 $Values = substr($Values, 2);
143 $this->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
144 }
145
146 function replace($Table, $Data)
147 {
148 $Name = '';
149 $Values = '';
150 foreach($Data as $Key => $Value)
151 {
152 if(!in_array($Value, $this->Functions))
153 {
154 if(is_null($Value)) $Value = 'NULL';
155 else $Value = $this->PDO->quote($Value);
156 }
157 $Name .= ',`'.$Key.'`';
158 $Values .= ','.$Value;
159 }
160 $Name = substr($Name, 1);
161 $Values = substr($Values, 1);
162 //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br />');
163 $this->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
164 //echo($this->error().'<br>');
165 }
166
167 function charset($Charset)
168 {
169 $this->query('SET NAMES "'.$Charset.'"');
170 }
171
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
197function TimeToMysqlDateTime($Time)
198{
199 if($Time == NULL) return(NULL);
200 else return(date('Y-m-d H:i:s', $Time));
201}
202
203function TimeToMysqlDate($Time)
204{
205 if($Time == NULL) return(NULL);
206 else return(date('Y-m-d', $Time));
207}
208
209function TimeToMysqlTime($Time)
210{
211 if($Time == NULL) return(NULL);
212 else return(date('H:i:s', $Time));
213}
214
215function 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
225function MysqlDateToTime($Date)
226{
227 if($Date == '') return(NULL);
228 return(MysqlDateTimeToTime($Date.' 0:0:0'));
229}
230
231function MysqlTimeToTime($Time)
232{
233 if($Time == '') return(NULL);
234 return(MysqlDateTimeToTime('0000-00-00 '.$Time));
235}
Note: See TracBrowser for help on using the repository browser.