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 tsExceptionModel ('Database connection data missing: [DB_HOST]');
45 $this->user
= $dbUser;
46 elseif (defined('DB_USER'))
47 $this->user
= DB_USER
;
49 throw new tsExceptionModel ('Database connection data missing: [DB_USERNAME]');
52 $this->pass
= $dbPass;
53 elseif (defined('DB_PASS'))
54 $this->pass
= DB_PASS
;
56 throw new tsExceptionModel ('Database connection data missing [DB_PASSWORD]');
58 if (!empty($this->host
) && !empty($this->user
) && !empty($this->pass
)) {
63 public function getType () {
67 public function __destruct() {
68 // var_dump($this->link);
69 // if (!empty ($this->link) && $this->link instanceof mysqli)
75 * wrapper for mysql_connect
79 private function connect (){
80 $this->link
= @new
mysqli ($this->host
, $this->user
, $this->pass
);
81 $errNo = mysqli_connect_errno();
83 $this->error
= $errNo.' '.mysqli_connect_error();
84 throw new tsExceptionModel($this->error
);
85 // trigger_error ($this->link->error, E_USER_ERROR);
92 * wrapper for mysql_close
96 public function close (){
97 if ($this->link
instanceof mysqli
)
98 $this->link
->close ();
99 // dunno how smart it is to nullify an mysqli object
105 * wrapper for mysql_select_db
107 * @param string $incData
110 public function selectDatabase ($incData){
111 $this->name
= $incData;
112 if (($this->link
instanceof mysqli
) && $this->link
->select_db($incData)) {
115 // trigger_error($this->link->error, E_USER_ERROR);
121 * wrapper for mysql_real_escape_string
123 * @param mixed $incData
126 public function escape ($incData){
127 if (is_string($incData))
128 return $this->link
->escape_string($incData);
134 * wrapper for mysql_query
136 * @param string $query
139 public function query ($query){
140 if (!($this->link
instanceof mysqli
)) {
143 if (!empty($query)) {
144 $qst = microtime(true);
145 // if (!preg_match("/insert|update|delete/i", $query))
146 $this->conn
= $this->link
->query($query);
147 $qend = microtime(true);
148 echo htmlentities ($query).' ['.number_format($qend-$qst, 5, ',', '.').'s]<br/>'."\n";
149 if (isset($GLOBALS['qCnt']))
154 if ($this->link
->errno
) {
155 trigger_error ($this->link
->error
.'<br/> '.$query);
159 if (stristr('select', $query))
162 elseif (preg_match('/insert|update|replace|delete/i', $query))
163 return $this->link
->affected_rows
;
167 * wrapper for mysql_fetch_row
171 public function getRow (){
172 if ($this->conn
instanceof mysqli_result
)
173 return $this->conn
->fetch_row ();
176 // FIXME: for some reason the getAssoc and getArray work differently
177 public function getAssoc () {
178 if ($this->conn
instanceof mysqli_result
)
179 return $this->conn
->fetch_assoc ();
183 * wrapper for mysql_fetch_row
187 public function getObjects () {
190 if ($this->conn
instanceof mysqli_result
&& $this->link
instanceof mysqli
) {
191 while ($i < mysqli_field_count ($this->link
)) {
192 $t = $this->conn
->fetch_field_direct ($i++
);
201 * wrapper for mysql_fetch_assoc
205 public function getArray (){
207 if ($this->conn
instanceof mysqli_result
)
208 while (($r = $this->conn
->fetch_assoc ())){
216 * getting the first result in the resultset
220 public function getScalar() {
221 $retVal = $this->getRow();
222 if (is_array($retVal))
223 $retVal = current($retVal);
229 * @param array $incObj = array (array('field1','alias1),array('field2','alias2),...)
232 public function _SELECT ($incObj){
237 return $retStr . ' ' . $incObj . ' ';
240 public function _CREATE ($sName){
241 return ' CREATE ' . $sName . ' ';
244 public function _SET(){
248 public function _INSERT ($incData){
249 if (empty ($incData))
251 return ' INSERT INTO '.$incData . ' ';
254 public function _VALUES ($incData) {
255 if (empty ($incData))
258 if (is_array ($incData)) {
260 foreach ($incData as $value) {
261 if (is_numeric($value))
262 $ret .= $value . ', ';
263 elseif (is_string($value))
264 $ret .= "'" . $this->escape ($value) . "', ";
266 $ret = substr ($ret,0, -2);
267 } elseif (is_string ($incData)) {
271 return ' VALUES (' . $ret . ' )';
274 public function _UPDATE ($incOb){
275 if (!is_array($incOb))
276 $incOb[] = array ($incOb);
277 return ' UPDATE '.$incOb[0].(!empty($incOb[1]) ?
' AS '.$incOb[1] : '');
281 * returns the FROM tabl...es part of the query
283 * @param string or array of strings $incData - table names
286 public function _FROM ($incData){
287 if (empty ($incData))
289 if (is_array($incData))
290 $incData = implode(', ',$incData);
292 return ' FROM '.$incData.' ';
298 public function _AND (){
305 public function _OR (){
308 public function _JOIN ($type) {
315 public function _AS ($str){
319 public function _LIMIT ($start, $end = 0){
321 return ' LIMIT '.(int)$start . ', '.(int)$end;
322 elseif (!empty ($start))
323 return ' LIMIT '.(int)$start;
329 * TODO make it receive an array of tdoHabstractFields
332 * @param array of strings $colName
335 public function _GROUP ($incObj = null){
339 $retStr = ' GROUP BY ';
340 return $retStr.' '.$incObj;
343 public function _ORDER ($orderBys = null){
344 if (empty($orderBys))
346 $retStr = ' ORDER BY ';
348 return $retStr.$orderBys;
351 public function _WHERE ($clause) {
352 return ' WHERE '.$clause;