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
15 * OBS: maybe the static methods (_AND, _OR, sa.) can be conained into
16 * an external object. (??!)
19 class mySqlIm
extends fooSqlDriverA
{
22 $STRING_OPEN_QUOTE = '"',
23 $STRING_CLOSE_QUOTE = '"',
24 $FIELD_OPEN_QUOTE = '`',
25 $FIELD_CLOSE_QUOTE = '`',
33 public function __construct( $dbHost = null, $dbUser = null, $dbPass = null ){
34 if (!extension_loaded('mysqli')) {
38 $this->host
= $dbHost;
39 elseif (defined('DB_HOST'))
40 $this->host
= DB_HOST
;
42 throw new fooConnectionException ('Database connection data missing: [DB_HOST]');
45 $this->user
= $dbUser;
46 elseif (defined('DB_USER'))
47 $this->user
= DB_USER
;
49 throw new fooConnectionException ('Database connection data missing: [DB_USERNAME]');
52 $this->pass
= $dbPass;
53 elseif (defined('DB_PASS'))
54 $this->pass
= DB_PASS
;
56 throw new fooConnectionException ('Database connection data missing [DB_PASSWORD]');
58 if (!empty($this->host
) && !empty($this->user
) && !empty($this->pass
)) {
63 public function getEngine () {
67 public function getType () {
71 public function __destruct() {
72 // var_dump($this->link);
73 // if (!empty ($this->link) && $this->link instanceof mysqli)
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.');
100 return $this->query($sQuery);
104 * wrapper for mysql_connect
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);
121 * wrapper for mysql_close
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
134 * wrapper for mysql_select_db
136 * @param string $incData
139 public function selectDatabase ($incData){
140 $this->name
= $incData;
141 if (($this->link
instanceof mysqli
) && $this->link
->select_db($incData)) {
144 // trigger_error($this->link->error, E_USER_ERROR);
150 * wrapper for mysql_real_escape_string
152 * @param mixed $incData
155 public function escape ($incData){
156 if (is_string($incData))
157 return $this->link
->escape_string($incData);
163 * wrapper for mysql_query
165 * @param string $query
168 public function query ($query){
169 if (!($this->link
instanceof mysqli
)) {
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']))
183 if ($this->link
->errno
) {
184 throw new fooConnectionException ($this->link
->error
. nl() . $query . nl ());
188 if (stristr('select', $query))
191 elseif (preg_match('/insert|update|replace|delete/i', $query))
192 return $this->link
->affected_rows
;
198 * wrapper for mysql_fetch_row
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
218 public function getObjects () {
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++
);
232 * wrapper for mysql_fetch_assoc
236 public function getArray (){
238 if ($this->conn
instanceof mysqli_result
)
239 while (($r = $this->conn
->fetch_assoc ())){
247 * getting the first result in the resultset
251 public function getScalar() {
252 $retVal = $this->getRow();
253 if (is_array($retVal))
254 $retVal = current($retVal);
260 * @param array $incObj = array (array('field1','alias1),array('field2','alias2),...)
263 public function _SELECT ($incObj){
268 return $retStr . ' ' . $incObj . ' ';
271 public function _CREATE ($sName){
272 return ' CREATE TABLE ' . $sName . ' ';
275 public function _SET(){
279 public function _INSERT ($incData){
280 if (empty ($incData))
282 return ' INSERT INTO '.$incData . ' ';
285 public function _VALUES ($incData) {
286 if (empty ($incData))
289 if (is_array ($incData)) {
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)) {
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
317 public function _FROM ($incData){
318 if (empty ($incData))
320 if (is_array($incData))
321 $incData = implode(', ',$incData);
323 return ' FROM '.$incData.' ';
329 public function _AND (){
336 public function _OR (){
339 public function _JOIN ($type) {
346 public function _AS ($str){
350 public function _LIMIT ($start, $end = 0){
352 return ' LIMIT '.(int)$start . ', '.(int)$end;
353 elseif (!empty ($start))
354 return ' LIMIT '.(int)$start;
360 * TODO make it receive an array of tdoHabstractFields
363 * @param array of strings $colName
366 public function _GROUP ($incObj = null){
370 $retStr = ' GROUP BY ';
371 return $retStr.' '.$incObj;
374 public function _ORDER ($orderBys = null){
375 if (empty($orderBys))
377 $retStr = ' ORDER BY ';
379 return $retStr.$orderBys;
382 public function _WHERE ($clause) {
383 return ' WHERE '.$clause;