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 interfaceSql
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 mySql
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() {
58 * wrapper for mysql_connect
62 private function connect(){
63 $this->link
= mysql_connect($this->host
, $this->user
, $this->pass
);
64 if(!isDBLink($this->link
)) {
65 trigger_error(mysql_error(), E_USER_ERROR
);
72 * wrapper for mysql_close
76 public function close(){
77 if(isDBLink($this->link
)) {
78 mysql_close($this->link
);
85 * wrapper for mysql_select_db
87 * @param string $incData
90 public function selectDatabase($incData){
91 if (isDBLink($this->link
)) {
92 $this->name
= $incData;
93 return mysql_select_db($incData);
95 trigger_error(mysql_error(), E_USER_ERROR
);
101 * wrapper for mysql_real_escape_string
103 * @param mixed $incData
106 public function escape ($incData){
107 if (is_string($incData))
108 return mysql_real_escape_string($incData);
110 return (int)$incData;
114 * wrapper for mysql_query
116 * @param string $query
119 public function query($query){
121 if (!isDBLink($this->link
)) {
124 if (!empty($query)) {
125 if (!preg_match("/insert|update|delete/i", $query))
126 $this->conn
= mysql_query ($query);
131 $error = mysql_error();
133 if (!empty($error)) {
134 trigger_error($error.'<br/> '.$query);
137 if (preg_match("/insert|update|delete/i", $query))
140 return mysql_num_rows($this->conn
);
144 * wrapper for mysql_fetch_row
148 public function getRow (){
149 return mysql_fetch_assoc($this->conn
);
152 public function getAssoc (){
153 //var_dump(mysql_fetch_assoc($this->conn));
154 return mysql_fetch_assoc($this->conn
);
158 * wrapper for mysql_fetch_assoc
162 public function getArray (){
164 while (($r = mysql_fetch_assoc($this->conn
))){
171 * getting the first result in the resultset
175 public function getScalar() {
176 $retVal = $this->getRow();
177 if (is_array($retVal))
178 $retVal = current($retVal);
184 * @param array $incObj = array (array('field1','alias1),array('field2','alias2),...)
187 public function _SELECT($incObj){
192 return $retStr.' '.$incObj.' ';
195 public function _CREATE(){
199 public function _INSERT(){
203 public function _UPDATE($incOb){
204 if (!is_array($incOb))
205 $incOb[] = array ($incOb);
206 return ' UPDATE `'.$incOb[0].'`'.(!empty($incOb[1]) ?
' AS `'.$incOb[1].'`' : '');
210 * returns the FROM `tabl...es` part of the query
212 * @param string or array of strings $incData - table names
215 public function _FROM($incData){
216 if (empty ($incData))
218 if (is_array($incData))
219 $incData = implode('`, `',$incData);
221 return ' FROM `'.$incData.'` ';
227 public function _AND(){
234 public function _OR(){
237 public function _JOIN ($type) {
244 public function _AS($str){
248 public function _LIMIT($start = 0, $end = 0){
250 return ' LIMIT '.(int)$start.(!empty($end) ?
', '.(int)$end : '');
255 * TODO make it receive an array of tdoHabstractFields
258 * @param array of strings $colName
262 public function _GROUP($incObj = null){
266 $retStr = ' GROUP BY ';
267 return $retStr.' '.$incObj;
270 public function _ORDER($orderBys = null){
271 if (empty($orderBys))
273 $retStr = ' ORDER BY ';
275 return $retStr.$orderBys;
278 public function _WHERE ($clause) {
279 return ' WHERE '.$clause;