* making the fracking paginator work
[vsc.git] / _res / _libs / models / sqldrivers / mysqlim.class.php
blobaa5dc5590040e1a911e0cee5fa6b516533b2c942
1 <?php
2 /**
3 * At the moment we only have the habsql class:D, but:
4 * Here should be a _PACKAGE_ to include:
5 * <type>Sql - class to encapsulate the <something>sql_* functionality
6 * - it will be derived from tdoHabstract
7 * <type>SqlR - the sql resource of type <type> [might not be needed]
8 * - in case I need it, <type>Sql->conn will have this type
9 * <type>SqlOrder - a struct(class, yeah, yeah) to contain the ORDER BY
10 * pairs of stuff: string $field, bool $ASC = true
11 * <type>SqlJoin - class to handle joining of two <type>Sql classes
12 * - TODO: very important
13 * <type>SqlField
15 * OBS: maybe the static methods (_AND, _OR, sa.) can be conained into
16 * an external object. (??!)
19 class mySqlIm extends fooSqlDriverA {
20 public $conn,
21 $link,
22 $STRING_OPEN_QUOTE = '"',
23 $STRING_CLOSE_QUOTE = '"',
24 $FIELD_OPEN_QUOTE = '`',
25 $FIELD_CLOSE_QUOTE = '`',
26 $TRUE = '1',
27 $FALSE = '0';
28 private $name,
29 $host,
30 $user,
31 $pass;
33 public function __construct( $dbHost = null, $dbUser = null, $dbPass = null ){
34 if (!extension_loaded('mysqli')) {
35 return new nullSql();
37 if (!empty ($dbHost))
38 $this->host = $dbHost;
39 elseif (defined('DB_HOST'))
40 $this->host = DB_HOST;
41 else
42 throw new fooConnectionException ('Database connection data missing: [DB_HOST]');
44 if (!empty ($dbUser))
45 $this->user = $dbUser;
46 elseif (defined('DB_USER'))
47 $this->user = DB_USER;
48 else
49 throw new fooConnectionException ('Database connection data missing: [DB_USERNAME]');
51 if(!empty($dbPass))
52 $this->pass = $dbPass;
53 elseif (defined('DB_PASS'))
54 $this->pass = DB_PASS;
55 else
56 throw new fooConnectionException ('Database connection data missing [DB_PASSWORD]');
58 if (!empty($this->host) && !empty($this->user) && !empty($this->pass)) {
59 $this->connect ();
63 public function getEngine () {
64 return 'InnoDB';
67 public function getType () {
68 return 'mysql';
71 public function __destruct() {
72 // var_dump($this->link);
73 // if (!empty ($this->link) && $this->link instanceof mysqli)
74 // $this->close();
77 public function startTransaction ($bAutoCommit = false) {
78 if ($this->getEngine() != 'InnoDB')
79 throw new tsExceptionUnimplemented ('Unable to use transactions for the current MySQL engine.');
81 $sQuery = 'SET autocommit=' . ($bAutoCommit ? 1 : 0) . ';';
82 $this->query($sQuery);
83 $sQuery = 'START TRANSACTION;';
84 return $this->query($sQuery);
87 public function rollBackTransaction () {
88 if ($this->getEngine() != 'InnoDB')
89 throw new tsExceptionUnimplemented ('Unable to use transactions for the current MySQL engine.');
91 $sQuery = 'ROLLBACK;';
92 return $this->query($sQuery);
95 public function commitTransaction () {
96 if ($this->getEngine() != 'InnoDB')
97 throw new tsExceptionUnimplemented ('Unable to use transactions for the current MySQL engine.');
99 $sQuery = 'COMMIT;';
100 return $this->query($sQuery);
104 * wrapper for mysql_connect
106 * @return bool
108 private function connect (){
109 $this->link = @new mysqli ($this->host, $this->user, $this->pass);
110 $errNo = mysqli_connect_errno();
111 if (!empty($errNo)) {
112 $this->error = $errNo.' '.mysqli_connect_error();
113 throw new fooConnectionException($this->error);
114 // trigger_error ($this->link->error, E_USER_ERROR);
115 return false;
117 return true;
121 * wrapper for mysql_close
123 * @return bool
125 public function close (){
126 if ($this->link instanceof mysqli)
127 $this->link->close ();
128 // dunno how smart it is to nullify an mysqli object
129 $this->link = null;
130 return true;
134 * wrapper for mysql_select_db
136 * @param string $incData
137 * @return bool
139 public function selectDatabase ($incData){
140 $this->name = $incData;
141 if (($this->link instanceof mysqli) && $this->link->select_db($incData)) {
142 return true;
143 } else {
144 // trigger_error($this->link->error, E_USER_ERROR);
145 return false;
150 * wrapper for mysql_real_escape_string
152 * @param mixed $incData
153 * @return mixed
155 public function escape ($incData){
156 if (is_string($incData))
157 return $this->link->escape_string($incData);
158 else
159 return $incData;
163 * wrapper for mysql_query
165 * @param string $query
166 * @return mixed
168 public function query ($query){
169 if (!($this->link instanceof mysqli)) {
170 return false;
172 if (!empty($query)) {
173 $qst = microtime(true);
174 // if (!preg_match("/insert|update|delete/i", $query))
175 $this->conn = $this->link->query($query);
176 $qend = microtime(true);
177 // echo htmlentities ($query).' ['.number_format($qend-$qst, 5, ',', '.').'s]' . nlbr();
178 if (isset($GLOBALS['qCnt']))
179 $GLOBALS['qCnt']++;
180 } else
181 return false;
183 if ($this->link->errno) {
184 throw new fooConnectionException ($this->link->error. nl() . $query . nl ());
185 return false;
188 if (stristr('select', $query))
189 // mysqli result
190 return $this->conn;
191 elseif (preg_match('/insert|update|replace|delete/i', $query))
192 return $this->link->affected_rows;
194 return true;
198 * wrapper for mysql_fetch_row
200 * @return array
202 public function getRow (){
203 if ($this->conn instanceof mysqli_result)
204 return $this->conn->fetch_row ();
207 // FIXME: for some reason the getAssoc and getArray work differently
208 public function getAssoc () {
209 if ($this->conn instanceof mysqli_result)
210 return $this->conn->fetch_assoc ();
214 * wrapper for mysql_fetch_row
216 * @return array
218 public function getObjects () {
219 $retArr = array ();
220 $i = 0;
221 if ($this->conn instanceof mysqli_result && $this->link instanceof mysqli ) {
222 while ($i < mysqli_field_count ($this->link)) {
223 $t = $this->conn->fetch_field_direct ($i++);
224 $retArr[] = $t;
228 return $retArr;
232 * wrapper for mysql_fetch_assoc
234 * @return array
236 public function getArray (){
237 $retArr = array();
238 if ($this->conn instanceof mysqli_result)
239 while (($r = $this->conn->fetch_assoc ())){
240 $retArr[] = $r;
243 return $retArr;
247 * getting the first result in the resultset
249 * @return mixed
251 public function getScalar() {
252 $retVal = $this->getRow();
253 if (is_array($retVal))
254 $retVal = current($retVal);
255 return $retVal;
260 * @param array $incObj = array (array('field1','alias1),array('field2','alias2),...)
261 * @return unknown
263 public function _SELECT ($incObj){
264 if (empty ($incObj))
265 return '';
267 $retStr = 'SELECT ';
268 return $retStr . ' ' . $incObj . ' ';
271 public function _CREATE ($sName){
272 return ' CREATE TABLE ' . $sName . ' ';
275 public function _SET(){
276 return ' SET ';
279 public function _INSERT ($incData){
280 if (empty ($incData))
281 return '';
282 return ' INSERT INTO '.$incData . ' ';
285 public function _VALUES ($incData) {
286 if (empty ($incData))
287 return '';
288 else {
289 if (is_array ($incData)) {
290 $ret = '';
291 foreach ($incData as $value) {
292 if (is_numeric($value))
293 $ret .= $value . ', ';
294 elseif (is_string($value))
295 $ret .= "'" . $this->escape ($value) . "', ";
297 $ret = substr ($ret,0, -2);
298 } elseif (is_string ($incData)) {
299 $ret = $incData;
302 return ' VALUES (' . $ret . ' )';
305 public function _UPDATE ($incOb){
306 if (!is_array($incOb))
307 $incOb[] = array ($incOb);
308 return ' UPDATE '.$incOb[0].(!empty($incOb[1]) ? ' AS '.$incOb[1] : '');
312 * returns the FROM tabl...es part of the query
314 * @param string or array of strings $incData - table names
315 * @return string
317 public function _FROM ($incData){
318 if (empty ($incData))
319 return '';
320 if (is_array($incData))
321 $incData = implode(', ',$incData);
323 return ' FROM '.$incData.' ';
327 * @return string
329 public function _AND (){
330 return ' AND ';
334 * @return string
336 public function _OR (){
337 return ' OR ';
339 public function _JOIN ($type) {
344 * @return string
346 public function _AS ($str){
347 return ' AS '.$str;
350 public function _LIMIT ($start, $end = 0){
351 if (!empty($end))
352 return ' LIMIT '.(int)$start . ', '.(int)$end;
353 elseif (!empty ($start))
354 return ' LIMIT '.(int)$start;
355 else
356 return '';
360 * TODO make it receive an array of tdoHabstractFields
361 * (see _SELECT)
363 * @param array of strings $colName
364 * @return string
366 public function _GROUP ($incObj = null){
367 if (empty ($incObj))
368 return '';
370 $retStr = ' GROUP BY ';
371 return $retStr.' '.$incObj;
374 public function _ORDER ($orderBys = null){
375 if (empty($orderBys))
376 return '';
377 $retStr = ' ORDER BY ';
379 return $retStr.$orderBys;
382 public function _WHERE ($clause) {
383 return ' WHERE '.$clause;