* removed the microtime_float function, microtime can do it
[vsc.git] / _res / _libs / mysql.class.php
blob6d12dda13a523acd61097477ab32e28a3f532eb6
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 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
13 * <type>SqlField
15 * OBS: maybe the static methods (_AND, _OR, sa.) can be conained into
16 * an external object. (??!)
19 class mySql 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() {
57 /**
58 * wrapper for mysql_connect
60 * @return bool
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);
66 return false;
68 return true;
71 /**
72 * wrapper for mysql_close
74 * @return bool
76 public function close(){
77 if(isDBLink($this->link)) {
78 mysql_close($this->link);
79 return true;
81 return false;
84 /**
85 * wrapper for mysql_select_db
87 * @param string $incData
88 * @return bool
90 public function selectDatabase($incData){
91 if (isDBLink($this->link)) {
92 $this->name = $incData;
93 return mysql_select_db($incData);
94 } else {
95 trigger_error(mysql_error(), E_USER_ERROR);
96 return false;
101 * wrapper for mysql_real_escape_string
103 * @param mixed $incData
104 * @return mixed
106 public function escape ($incData){
107 if (is_string($incData))
108 return mysql_real_escape_string($incData);
109 else
110 return (int)$incData;
114 * wrapper for mysql_query
116 * @param string $query
117 * @return mixed
119 public function query($query){
120 //echo $query;
121 if (!isDBLink($this->link)) {
122 return false;
124 if (!empty($query)) {
125 if (!preg_match("/insert|update|delete/i", $query))
126 $this->conn = mysql_query ($query);
127 echo $query.'<br/>';
128 } else {
129 return false;
131 $error = mysql_error();
133 if (!empty($error)) {
134 trigger_error($error.'<br/> '.$query);
135 return false;
137 if (preg_match("/insert|update|delete/i", $query))
138 return $this->conn;
139 else
140 return mysql_num_rows($this->conn);
144 * wrapper for mysql_fetch_row
146 * @return array
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
160 * @return array
162 public function getArray (){
163 $retArr = array();
164 while (($r = mysql_fetch_assoc($this->conn))){
165 $retArr[] = $r;
168 return $retArr;
171 * getting the first result in the resultset
173 * @return mixed
175 public function getScalar() {
176 $retVal = $this->getRow();
177 if (is_array($retVal))
178 $retVal = current($retVal);
179 return $retVal;
184 * @param array $incObj = array (array('field1','alias1),array('field2','alias2),...)
185 * @return unknown
187 public function _SELECT($incObj){
188 if (empty ($incObj))
189 return '';
191 $retStr = 'SELECT ';
192 return $retStr.' '.$incObj.' ';
195 public function _CREATE(){
196 return ' CREATE ';
199 public function _INSERT(){
200 return ' 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
213 * @return string
215 public function _FROM($incData){
216 if (empty ($incData))
217 return '';
218 if (is_array($incData))
219 $incData = implode('`, `',$incData);
221 return ' FROM `'.$incData.'` ';
225 * @return string
227 public function _AND(){
228 return ' AND ';
232 * @return string
234 public function _OR(){
235 return ' OR ';
237 public function _JOIN ($type) {
242 * @return string
244 public function _AS($str){
245 return ' AS '.$str;
248 public function _LIMIT($start = 0, $end = 0){
249 if (!empty($start))
250 return ' LIMIT '.(int)$start.(!empty($end) ? ', '.(int)$end : '');
251 return '';
255 * TODO make it receive an array of tdoHabstractFields
256 * (see _SELECT)
258 * @param array of strings $colName
259 * @return string
262 public function _GROUP($incObj = null){
263 if (empty ($incObj))
264 return '';
266 $retStr = ' GROUP BY ';
267 return $retStr.' '.$incObj;
270 public function _ORDER($orderBys = null){
271 if (empty($orderBys))
272 return '';
273 $retStr = ' ORDER BY ';
275 return $retStr.$orderBys;
278 public function _WHERE ($clause) {
279 return ' WHERE '.$clause;