1 <?php
2
3 #######################################################################################
4 ####
5 #### MAGRATHEA PHP
6 #### v. 1.0
7 ####
8 #### Magrathea by Paulo Henrique Martins
9 #### Platypus technology
10 ####
11 #######################################################################################
12 ####
13 #### Database Class
14 #### Database connection class
15 ####
16 #### created: 2012-12 by Paulo Martins
17 ####
18 #######################################################################################
19
20
21 require 'MDB2.php';
22
23 /**
24 * This class will provide a layer for connecting with mysql
25 * @deprecated
26 */
27 class Magdb_{
28 private $mdb2;
29 private $dsn = array();
30 private $pear;
31 private $fetchmode = MDB2_FETCHMODE_ASSOC;
32 public $count = 0;
33 protected static $inst = null;
34
35 /**
36 * This is a singleton!
37 * Constructor is private
38 */
39 private function __construct(){
40 $this->pear = new PEAR();
41 }
42
43 /**
44 * This is a singleton!
45 * Instance loader
46 * @return Magdb Instance of the object
47 */
48 public static function Instance(){
49 if (self::$inst === null) {
50 self::$inst = new Magdb();
51 }
52 return self::$inst;
53 }
54
55 /**
56 * This is a singleton!
57 * Should be called by private method Instance.
58 * Don't implement new ones
59 */
60 public function Magdb(){
61 }
62 /**
63 * Sets the connection array object
64 * @param array $dsn_arr array with connection data, as the sample:
65 * array(
66 * 'phptype' => 'mysql',
67 * 'hostspec' => $host,
68 * 'database' => $database,
69 * 'username' => $username,
70 * 'password' => $password,
71 * );
72 */
73 public function SetConnectionArray($dsn_arr){
74 $this->dsn = $dsn_arr;
75 }
76 /**
77 * Setups connection
78 * @param string $host host address for connection
79 * @param string $database database name
80 * @param string $username username for connection
81 * @param string $password password for connection
82 */
83 public function SetConnection($host, $database, $username, $password){
84 $this->dsn = array(
85 'phptype' => 'mysql',
86 'hostspec' => $host,
87 'database' => $database,
88 'username' => $username,
89 'password' => $password,
90 );
91 }
92
93 /**
94 * Sets fetchmode, according with MDB2 values. Default mode: assoc.
95 * @param string $fetch fetchmode for SQL returns
96 * options available:
97 * ordered:
98 * array with results ordered according with select statement
99 * assoc:
100 * array with keys as the column names
101 * object:
102 * object with columns as propertoies
103 * if anything different from those values is sent, "assoc" is used
104 */
105 public function SetFetchMode($fetch){
106 switch($fetch){
107 case "object":
108 $this->fetchmode = DB_FETCHMODE_OBJECT;
109 break;
110 case "ordered":
111 $this->fetchmode = MDB2_FETCHMODE_OBJECT;
112 break;
113 case "assoc":
114 default:
115 $this->fetchmode = MDB2_FETCHMODE_ASSOC;
116 break;
117 }
118 }
119
120 /**
121 * Open connection, please. Please! 0=)
122 * @return boolean true or false, if connection succedded
123 * @throws MagratheaDbException
124 */
125 public function OpenConnectionPlease(){
126 $options['use_transactions'] = true;
127 $options['default_table_type'] = 'InnoDB';
128 try{
129 $this->mdb2 = MDB2::factory($this->dsn, $options);
130 if ($this->pear->isError($this->mdb2)) {
131 $this->connectionErrorHandle("Could not connect to database!");
132 }
133 @$this->mdb2->setCharset("utf8");
134 @$this->mdb2->setFetchMode($this->fetchmode);
135 } catch (Exception $ex) {
136 p_r($ex);
137 throw new MagratheaDBException($ex->getMessage());
138 }
139 return true;
140 }
141 /**
142 * Already uses you.. Bye.
143 */
144 public function CloseConnectionThanks(){
145 $this->mdb2->disconnect();
146 }
147
148 /**
149 * Handle connection errors
150 * @throws MagratheaDbException
151 */
152 private function ConnectionErrorHandle($msg=""){
153 throw new MagratheaDBException($msg);
154 }
155 /**
156 * Handle errors
157 * @throws MagratheaDbException
158 */
159 private function ErrorHandle($result, $sql){
160 MagratheaLogger::Log(" ERROR!!! query error: [ ".$sql." ]");
161 MagratheaLogger::LogError($result, "log_mysqlerror");
162 }
163
164 /**
165 * Control Log
166 * @param string $sql Query to be logged
167 * @param object $values Values to be logged
168 */
169 private function LogControl($sql, $values=null){
170 MagratheaDebugger::Instance()->AddQuery($sql, $values);
171 }
172
173 // QUERY FUNCTIONS
174 /**
175 * executes the query and returns the full data
176 * @param string $sql Query to be executed
177 * @return object $result Result of the query
178 */
179 public function Query($sql){
180 $this->LogControl($sql);
181 $this->OpenConnectionPlease();
182 $result = $this->mdb2->query($sql);
183 if ($this->pear->isError($result)) {
184 $this->errorHandle($result, $sql);
185 }
186 $this->count = @$result->numRows();
187 $this->CloseConnectionThanks();
188 return $result;
189 }
190
191 /**
192 * executes the query and returns the full data in an array
193 * @param string $sql Query to be executed
194 * @return array $result Result of the query (one row for line result)
195 */
196 public function QueryAll($sql){
197 $this->LogControl($sql);
198 $this->OpenConnectionPlease();
199 $result = $this->mdb2->queryAll($sql);
200 p_r($result);
201 if ($this->pear->isError($result)) {
202 $this->ErrorHandle($result, $sql);
203 }
204 $this->CloseConnectionThanks();
205 $this->count = count($result);
206 return $result;
207 }
208
209 /**
210 * executes the query and returns only the first row of the result
211 * @param string $sql Query to be executed
212 * @return object $result First line of the query
213 */
214 public function QueryRow($sql){
215 $this->LogControl($sql);
216 $this->OpenConnectionPlease();
217 $result = $this->mdb2->queryRow($sql);
218 if ($this->pear->isError($result)) {
219 $this->errorHandle($result, $sql);
220 }
221 $this->CloseConnectionThanks();
222 $this->count = 1;
223 return $result;
224 }
225
226 /**
227 * executes the query and returns only the first value of the first row of the result
228 * @param string $sql Query to be executed
229 * @return object $result First value of the first line
230 */
231 public function QueryOne($sql){
232 $this->LogControl($sql);
233 $this->OpenConnectionPlease();
234 $result = $this->mdb2->queryOne($sql);
235 if ($this->pear->isError($result)) {
236 $this->errorHandle($result, $sql);
237 }
238 $this->CloseConnectionThanks();
239 $this->count = 1;
240 return $result;
241 }
242
243 /**
244 * receives an array of queries and executes them all
245 * @param array $query_array Array of queries to be executed
246 * @throws MagratheaDBException
247 */
248 public function QueryTransaction($query_array){
249 $error = false;
250 $this->OpenConnectionPlease();
251 if (!$this->mdb2->supports('transactions')) {
252 throw new MagratheaDBException("transactions not supported! why?");
253 }
254
255 $res = $this->mdb2->beginTransaction();
256 foreach( $query_array as $query ){
257 $this->LogControl($query);
258 if( $error ) break;
259 $result = $this->mdb2->query($query);
260 if ($this->pear->isError($result)) {
261 $error = true;
262 $this->ErrorHandle(null, $query);
263 }
264 }
265
266 if ($error) {
267 $res = $this->mdb2->rollback();
268 } else {
269 $res = $this->mdb2->commit();
270 }
271
272 $this->CloseConnectionThanks();
273 }
274
275 /**
276 * Prepares and execute a query and returns the inserted id (if any)
277 * @param string $query Query to be executed
278 * @param array $arrTypes Array of types from the values to be inserted
279 * @param array $arrValues Array of values to be inserted
280 */
281 public function PrepareAndExecute($query, $arrTypes, $arrValues){
282 $this->LogControl($query, $arrValues);
283 $this->OpenConnectionPlease();
284 $sth = $this->mdb2->prepare($query, $arrTypes, MDB2_PREPARE_MANIP);
285 $result = $sth->execute($arrValues);
286 $lastId = $this->mdb2->lastInsertID();
287 $sth->free();
288 if ($this->pear->isError($result)) {
289 $this->errorHandle($result, $query);
290 }
291 $this->CloseConnectionThanks();
292 return $lastId;
293 }
294
295 }
296