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 interfaceSql
{
27 public function __construct( $dbHost = null, $dbUser = null, $dbPass = null ){
29 $this->host
= $dbHost;
30 elseif (defined('DB_HOST'))
31 $this->host
= DB_HOST
;
33 trigger_error('Database connection data missing!', E_USER_ERROR
);
36 $this->user
= $dbUser;
37 elseif (defined('DB_USER'))
38 $this->user
= DB_USER
;
40 trigger_error('Database connection data missing!', E_USER_ERROR
);
43 $this->pass
= $dbPass;
44 elseif (defined('DB_PASS'))
45 $this->pass
= DB_PASS
;
47 trigger_error('Database connection data missing!', E_USER_ERROR
);
49 if (!empty($this->host
) && !empty($this->user
) && !empty($this->pass
)) {
54 public function __destruct() {
55 // var_dump($this->link);
56 // if (!empty ($this->link) && $this->link instanceof mysqli)
62 * wrapper for mysql_connect
66 private function connect (){
67 $this->link
= @new
mysqli ($this->host
, $this->user
, $this->pass
);
68 $errNo = mysqli_connect_errno();
70 $this->error
= $errNo.' '.mysqli_connect_error();
71 // trigger_error ($this->link->error, E_USER_ERROR);
78 * wrapper for mysql_close
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
91 * wrapper for mysql_select_db
93 * @param string $incData
96 public function selectDatabase ($incData){
97 $this->name
= $incData;
98 if (($this->link
instanceof mysqli
) && $this->link
->select_db($incData)) {
101 // trigger_error($this->link->error, E_USER_ERROR);
107 * wrapper for mysql_real_escape_string
109 * @param mixed $incData
112 public function escape ($incData){
113 if (is_string($incData))
114 return $this->link
->escape_string($incData);
116 return (int)$incData;
120 * wrapper for mysql_query
122 * @param string $query
125 public function query ($query){
126 if (!is_a($this->link
,'mysqli')) {
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']))
138 if ($this->link
->errno
) {
139 trigger_error ($this->link
->error
.'<br/> '.$query);
143 if (stristr('select', $query))
146 elseif (preg_match('/insert|update|replace|delete/i', $query))
147 return $this->link
->affected_rows
;
151 * wrapper for mysql_fetch_row
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
171 public function getObjects () {
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++
);
185 * wrapper for mysql_fetch_assoc
189 public function getArray (){
191 if ($this->conn
instanceof mysqli_result
)
192 while (($r = $this->conn
->fetch_assoc ())){
200 * getting the first result in the resultset
204 public function getScalar() {
205 $retVal = $this->getRow();
206 if (is_array($retVal))
207 $retVal = current($retVal);
213 * @param array $incObj = array (array('field1','alias1),array('field2','alias2),...)
216 public function _SELECT ($incObj){
221 return $retStr.' '.$incObj.' ';
224 public function _CREATE (){
228 public function _SET(){
232 public function _INSERT ($incData){
233 if (empty ($incData))
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
250 public function _FROM ($incData){
251 if (empty ($incData))
253 if (is_array($incData))
254 $incData = implode(', ',$incData);
256 return ' FROM '.$incData.' ';
262 public function _AND (){
269 public function _OR (){
272 public function _JOIN ($type) {
279 public function _AS ($str){
283 public function _LIMIT ($start = 0, $end = 0){
285 return ' LIMIT '.(int)$start.(!empty($end) ?
', '.(int)$end : '');
290 * TODO make it receive an array of tdoHabstractFields
293 * @param array of strings $colName
296 public function _GROUP ($incObj = null){
300 $retStr = ' GROUP BY ';
301 return $retStr.' '.$incObj;
304 public function _ORDER ($orderBys = null){
305 if (empty($orderBys))
307 $retStr = ' ORDER BY ';
309 return $retStr.$orderBys;
312 public function _WHERE ($clause) {
313 return ' WHERE '.$clause;