* modified the sql factory and null sql to fail gracefully on db problems
[vsc.git] / _res / _libs / mysqlim.class.php
blob82128a4b959b72ad8e38e26d5815f8e7fea6acd6
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 interfaceSql {
20 public $conn,
21 $link;
22 private $name,
23 $host,
24 $user,
25 $pass;
27 public function __construct( $dbHost = null, $dbUser = null, $dbPass = null ){
28 if (!empty ($dbHost))
29 $this->host = $dbHost;
30 elseif (defined('DB_HOST'))
31 $this->host = DB_HOST;
32 else
33 trigger_error('Database connection data missing!', E_USER_ERROR);
35 if (!empty ($dbUser))
36 $this->user = $dbUser;
37 elseif (defined('DB_USER'))
38 $this->user = DB_USER;
39 else
40 trigger_error('Database connection data missing!', E_USER_ERROR);
42 if(!empty($dbPass))
43 $this->pass = $dbPass;
44 elseif (defined('DB_PASS'))
45 $this->pass = DB_PASS;
46 else
47 trigger_error('Database connection data missing!', E_USER_ERROR);
49 if (!empty($this->host) && !empty($this->user) && !empty($this->pass)) {
50 $this->connect ();
54 public function __destruct() {
55 // var_dump($this->link);
56 // if (!empty ($this->link) && $this->link instanceof mysqli)
57 // $this->close();
61 /**
62 * wrapper for mysql_connect
64 * @return bool
66 private function connect (){
67 $this->link = @new mysqli ($this->host, $this->user, $this->pass);
68 $errNo = mysqli_connect_errno();
69 if (!empty($errNo)) {
70 $this->error = $errNo.' '.mysqli_connect_error();
71 // trigger_error ($this->link->error, E_USER_ERROR);
72 return false;
74 return true;
77 /**
78 * wrapper for mysql_close
80 * @return bool
82 public function close (){
83 if ($this->link instanceof mysqli)
84 $this->link->close ();
85 // dunno how smart it is to nullify an mysqli object
86 $this->link = null;
87 return true;
90 /**
91 * wrapper for mysql_select_db
93 * @param string $incData
94 * @return bool
96 public function selectDatabase ($incData){
97 $this->name = $incData;
98 if (($this->link instanceof mysqli) && $this->link->select_db($incData)) {
99 return true;
100 } else {
101 // trigger_error($this->link->error, E_USER_ERROR);
102 return false;
107 * wrapper for mysql_real_escape_string
109 * @param mixed $incData
110 * @return mixed
112 public function escape ($incData){
113 if (is_string($incData))
114 return $this->link->escape_string($incData);
115 else
116 return (int)$incData;
120 * wrapper for mysql_query
122 * @param string $query
123 * @return mixed
125 public function query ($query){
126 if (!is_a($this->link,'mysqli')) {
127 return false;
129 if (!empty($query)) {
130 // if (!preg_match("/insert|update|delete/i", $query))
131 $this->conn = $this->link->query($query);
132 echo htmlentities ($query).'<br/>'."\n";
133 if (isset($GLOBALS['qCnt']))
134 $GLOBALS['qCnt']++;
135 } else
136 return false;
138 if ($this->link->errno) {
139 trigger_error ($this->link->error.'<br/> '.$query);
140 return false;
143 if (stristr('select', $query))
144 // mysqli result
145 return $this->conn;
146 elseif (preg_match('/insert|update|replace|delete/i', $query))
147 return $this->link->affected_rows;
151 * wrapper for mysql_fetch_row
153 * @return array
155 public function getRow (){
156 if ($this->conn instanceof mysqli_result)
157 return $this->conn->fetch_row ();
160 // FIXME: for some reason the getAssoc and getArray work differently
161 public function getAssoc () {
162 if ($this->conn instanceof mysqli_result)
163 return $this->conn->fetch_assoc ();
167 * wrapper for mysql_fetch_row
169 * @return array
171 public function getObjects () {
172 $retArr = array ();
173 $i = 0;
174 if ($this->conn instanceof mysqli_result && $this->link instanceof mysqli ) {
175 while ($i < mysqli_field_count ($this->link)) {
176 $t = $this->conn->fetch_field_direct ($i++);
177 $retArr[] = $t;
181 return $retArr;
185 * wrapper for mysql_fetch_assoc
187 * @return array
189 public function getArray (){
190 $retArr = array();
191 if ($this->conn instanceof mysqli_result)
192 while (($r = $this->conn->fetch_assoc ())){
193 $retArr[] = $r;
196 return $retArr;
200 * getting the first result in the resultset
202 * @return mixed
204 public function getScalar() {
205 $retVal = $this->getRow();
206 if (is_array($retVal))
207 $retVal = current($retVal);
208 return $retVal;
213 * @param array $incObj = array (array('field1','alias1),array('field2','alias2),...)
214 * @return unknown
216 public function _SELECT ($incObj){
217 if (empty ($incObj))
218 return '';
220 $retStr = 'SELECT ';
221 return $retStr.' '.$incObj.' ';
224 public function _CREATE (){
225 return ' CREATE ';
228 public function _SET(){
229 return ' SET ';
232 public function _INSERT ($incData){
233 if (empty ($incData))
234 return '';
235 return ' INSERT INTO '.$incData;
238 public function _UPDATE ($incOb){
239 if (!is_array($incOb))
240 $incOb[] = array ($incOb);
241 return ' UPDATE '.$incOb[0].(!empty($incOb[1]) ? ' AS '.$incOb[1] : '');
245 * returns the FROM tabl...es part of the query
247 * @param string or array of strings $incData - table names
248 * @return string
250 public function _FROM ($incData){
251 if (empty ($incData))
252 return '';
253 if (is_array($incData))
254 $incData = implode(', ',$incData);
256 return ' FROM '.$incData.' ';
260 * @return string
262 public function _AND (){
263 return ' AND ';
267 * @return string
269 public function _OR (){
270 return ' OR ';
272 public function _JOIN ($type) {
277 * @return string
279 public function _AS ($str){
280 return ' AS '.$str;
283 public function _LIMIT ($start = 0, $end = 0){
284 if (!empty($start))
285 return ' LIMIT '.(int)$start.(!empty($end) ? ', '.(int)$end : '');
286 return '';
290 * TODO make it receive an array of tdoHabstractFields
291 * (see _SELECT)
293 * @param array of strings $colName
294 * @return string
296 public function _GROUP ($incObj = null){
297 if (empty ($incObj))
298 return '';
300 $retStr = ' GROUP BY ';
301 return $retStr.' '.$incObj;
304 public function _ORDER ($orderBys = null){
305 if (empty($orderBys))
306 return '';
307 $retStr = ' ORDER BY ';
309 return $retStr.$orderBys;
312 public function _WHERE ($clause) {
313 return ' WHERE '.$clause;