* having tests working at work...
[vsc.git] / _res / _libs / models / sqldrivers / mysqlim.class.php
blob97da41c13de5ef60a78d053740f6915c90af03e6
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 tsExceptionModel ('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 tsExceptionModel ('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 tsExceptionModel ('Database connection data missing [DB_PASSWORD]');
58 if (!empty($this->host) && !empty($this->user) && !empty($this->pass)) {
59 $this->connect ();
63 public function getType () {
64 return 'mysql';
67 public function __destruct() {
68 // var_dump($this->link);
69 // if (!empty ($this->link) && $this->link instanceof mysqli)
70 // $this->close();
74 /**
75 * wrapper for mysql_connect
77 * @return bool
79 private function connect (){
80 $this->link = @new mysqli ($this->host, $this->user, $this->pass);
81 $errNo = mysqli_connect_errno();
82 if (!empty($errNo)) {
83 $this->error = $errNo.' '.mysqli_connect_error();
84 throw new tsExceptionModel($this->error);
85 // trigger_error ($this->link->error, E_USER_ERROR);
86 return false;
88 return true;
91 /**
92 * wrapper for mysql_close
94 * @return bool
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
100 $this->link = null;
101 return true;
105 * wrapper for mysql_select_db
107 * @param string $incData
108 * @return bool
110 public function selectDatabase ($incData){
111 $this->name = $incData;
112 if (($this->link instanceof mysqli) && $this->link->select_db($incData)) {
113 return true;
114 } else {
115 // trigger_error($this->link->error, E_USER_ERROR);
116 return false;
121 * wrapper for mysql_real_escape_string
123 * @param mixed $incData
124 * @return mixed
126 public function escape ($incData){
127 if (is_string($incData))
128 return $this->link->escape_string($incData);
129 else
130 return $incData;
134 * wrapper for mysql_query
136 * @param string $query
137 * @return mixed
139 public function query ($query){
140 if (!($this->link instanceof mysqli)) {
141 return false;
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']))
150 $GLOBALS['qCnt']++;
151 } else
152 return false;
154 if ($this->link->errno) {
155 trigger_error ($this->link->error.'<br/> '.$query);
156 return false;
159 if (stristr('select', $query))
160 // mysqli result
161 return $this->conn;
162 elseif (preg_match('/insert|update|replace|delete/i', $query))
163 return $this->link->affected_rows;
167 * wrapper for mysql_fetch_row
169 * @return array
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
185 * @return array
187 public function getObjects () {
188 $retArr = array ();
189 $i = 0;
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++);
193 $retArr[] = $t;
197 return $retArr;
201 * wrapper for mysql_fetch_assoc
203 * @return array
205 public function getArray (){
206 $retArr = array();
207 if ($this->conn instanceof mysqli_result)
208 while (($r = $this->conn->fetch_assoc ())){
209 $retArr[] = $r;
212 return $retArr;
216 * getting the first result in the resultset
218 * @return mixed
220 public function getScalar() {
221 $retVal = $this->getRow();
222 if (is_array($retVal))
223 $retVal = current($retVal);
224 return $retVal;
229 * @param array $incObj = array (array('field1','alias1),array('field2','alias2),...)
230 * @return unknown
232 public function _SELECT ($incObj){
233 if (empty ($incObj))
234 return '';
236 $retStr = 'SELECT ';
237 return $retStr . ' ' . $incObj . ' ';
240 public function _CREATE ($sName){
241 return ' CREATE ' . $sName . ' ';
244 public function _SET(){
245 return ' SET ';
248 public function _INSERT ($incData){
249 if (empty ($incData))
250 return '';
251 return ' INSERT INTO '.$incData . ' ';
254 public function _VALUES ($incData) {
255 if (empty ($incData))
256 return '';
257 else {
258 if (is_array ($incData)) {
259 $ret = '';
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)) {
268 $ret = $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
284 * @return string
286 public function _FROM ($incData){
287 if (empty ($incData))
288 return '';
289 if (is_array($incData))
290 $incData = implode(', ',$incData);
292 return ' FROM '.$incData.' ';
296 * @return string
298 public function _AND (){
299 return ' AND ';
303 * @return string
305 public function _OR (){
306 return ' OR ';
308 public function _JOIN ($type) {
313 * @return string
315 public function _AS ($str){
316 return ' AS '.$str;
319 public function _LIMIT ($start, $end = 0){
320 if (!empty($end))
321 return ' LIMIT '.(int)$start . ', '.(int)$end;
322 elseif (!empty ($start))
323 return ' LIMIT '.(int)$start;
324 else
325 return '';
329 * TODO make it receive an array of tdoHabstractFields
330 * (see _SELECT)
332 * @param array of strings $colName
333 * @return string
335 public function _GROUP ($incObj = null){
336 if (empty ($incObj))
337 return '';
339 $retStr = ' GROUP BY ';
340 return $retStr.' '.$incObj;
343 public function _ORDER ($orderBys = null){
344 if (empty($orderBys))
345 return '';
346 $retStr = ' ORDER BY ';
348 return $retStr.$orderBys;
351 public function _WHERE ($clause) {
352 return ' WHERE '.$clause;