00001 <?php
00002
00015 class DBResult extends SObject implements ArrayAccess, Iterator, Countable {
00016 private $cookie;
00017 private $link;
00018 private $arrayResults = null;
00019 private $curRow = 0;
00020 private $numRows = null;
00021
00031 public function __construct($cookie, $link) {
00032 $this->cookie = $cookie;
00033 $this->link = $link;
00034 }
00035
00044 public function __destruct() {
00045 $this->free();
00046 }
00047
00057 public function getInsertId() {
00058 $id = @mysql_insert_id($this->link);
00059 if($id === false) {
00060 $this->setError('Could not get insert ID: ' . mysql_error($this->link));
00061 return false;
00062 }
00063 else
00064 return $id;
00065 }
00066
00072 public function numRows() {
00073 if($this->numRows != null)
00074 return $this->numRows;
00075 if(!is_resource($this->cookie)) {
00076 $this->setError('Cannot retrieve number of rows for non-SELECT query');
00077 return false;
00078 }
00079 $num = @mysql_num_rows($this->cookie);
00080 if($num === false) {
00081 $this->setError('Could not retrieve number of rows: ' . mysql_error($this->link));
00082 return false;
00083 }
00084 else {
00085 $this->numRows = $num;
00086 return $num;
00087 }
00088 }
00089
00101 public function nextRow() {
00102 if(!is_resource($this->cookie)) {
00103 $this->setError('Cannot retrieve row for non-SELECT query');
00104 return false;
00105 }
00106 if($this->curRow == $this->numRows())
00107 return false;
00108 $result = mysql_fetch_assoc($this->cookie);
00109 if($result) {
00110 $this->curRow++;
00111 return $result;
00112 }
00113 else {
00114 $this->setError('Failed to retrieve row: ' . mysql_error($this->link));
00115 return null;
00116 }
00117 }
00118
00127 public function reset() {
00128 if(!is_resource($this->cookie)) {
00129 $this->setError('Cannot seek for non-SELECT query');
00130 return false;
00131 }
00132 if(mysql_data_seek($this->cookie, 0)) {
00133 $this->curRow = 0;
00134 return true;
00135 }
00136 else {
00137 $this->setError('Failed to reset row position: ' . mysql_error($this->link));
00138 return false;
00139 }
00140 }
00141
00152 public function seek($n) {
00153 if(!is_resource($this->cookie)) {
00154 $this->setError('Cannot seek for non-SELECT query');
00155 return false;
00156 }
00157 if(mysql_data_seek($this->cookie, $n)) {
00158 $this->curRow = $n;
00159 return true;
00160 }
00161 else {
00162 $this->setError('Failed to reset row position: ' . mysql_error($this->link));
00163 return false;
00164 }
00165
00166 }
00167
00179 public function getRow($n) {
00180 if(!is_resource($this->cookie)) {
00181 $this->setError('Cannot retrieve row for non-SELECT query');
00182 return false;
00183 }
00184 if(mysql_data_seek($this->cookie, $n)) {
00185 $this->curRow = $n + 1;
00186 return mysql_fetch_assoc($this->cookie);
00187 }
00188 else {
00189 $this->setError('Failed to get row: ' . mysql_error($this->link));
00190 return null;
00191 }
00192 }
00193
00207 public function toArray() {
00208 if(!is_resource($this->cookie)) {
00209 $this->setError('Cannot retrieve rows for non-SELECT query');
00210 return false;
00211 }
00212 if($this->arrayResults != null)
00213 return $this->arrayResults;
00214
00215 if(@mysql_num_rows($this->cookie) == 0)
00216 return array();
00217
00218 if(!@mysql_data_seek($this->cookie, 0)) {
00219 $this->setError('Could not seek to beginning of result set: ' . mysql_error($this->link));
00220 return null;
00221 }
00222
00223 $out = array();
00224 while($result = mysql_fetch_assoc($this->cookie)) {
00225 $out[] = $result;
00226 }
00227
00228 mysql_data_seek($this->cookie, $this->curRow);
00229
00230 $this->arrayResults = $out;
00231
00232 return $out;
00233 }
00234
00243 public function free() {
00244 if(is_resource($this->cookie))
00245 mysql_free_result($this->cookie);
00246 $this->cookie = null;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 public function offsetSet($offset, $value) {
00258 $this->setError('You cannot modify resultsets!');
00259 }
00260
00261 public function offsetExists($offset) {
00262 if(!is_resource($this->cookie)) {
00263 $this->setError('Cannot retrieve rows for non-SELECT query');
00264 return false;
00265 }
00266 if($offset < 0 || $offset >= $this->numRows())
00267 return false;
00268 else
00269 return true;
00270 }
00271
00272 public function offsetUnset($offset) {
00273 $this->setError('You cannot modify resultsets!');
00274 }
00275
00276 public function offsetGet($offset) {
00277 if(!is_resource($this->cookie)) {
00278 $this->setError('Cannot retrieve rows for non-SELECT query');
00279 return false;
00280 }
00281 if($offset = $this->curRow + 1)
00282 return $this->nextRow();
00283 else
00284 return $this->getRow($offset);
00285 }
00286
00287
00288
00289 public function count() {
00290 if(!is_resource($this->cookie)) {
00291 $this->setError('Cannot retrieve rows for non-SELECT query');
00292 return false;
00293 }
00294 return $this->numRows();
00295 }
00296
00297
00298
00299 private $position = 0;
00300
00301 public function current() {
00302 if(!is_resource($this->cookie)) {
00303 $this->setError('Cannot retrieve rows for non-SELECT query');
00304 return false;
00305 }
00306 return $this->getRow($this->position);
00307 }
00308
00309 public function key() {
00310 if(!is_resource($this->cookie)) {
00311 $this->setError('Cannot retrieve rows for non-SELECT query');
00312 return false;
00313 }
00314 return $this->position;
00315 }
00316
00317 public function next() {
00318 if(!is_resource($this->cookie)) {
00319 $this->setError('Cannot retrieve rows for non-SELECT query');
00320 return false;
00321 }
00322 $this->position++;
00323 }
00324
00325 public function rewind() {
00326 if(!is_resource($this->cookie)) {
00327 $this->setError('Cannot retrieve rows for non-SELECT query');
00328 return false;
00329 }
00330 $this->position = 0;
00331 }
00332
00333 public function valid() {
00334 if(!is_resource($this->cookie)) {
00335 $this->setError('Cannot retrieve rows for non-SELECT query');
00336 return false;
00337 }
00338 if($this->position < 0 || $this->position >= $this->numRows())
00339 return false;
00340 else
00341 return true;
00342 }
00343 }
00344
00355 class DBTransaction extends SObject {
00356 private $DBI;
00357
00366 public function __construct($parDBI) {
00367 parent::__construct();
00368 $this->DBI = $parDBI;
00369 $this->DBI->startTransaction();
00370 }
00371
00379 public function __destruct() {
00380 $this->cancel();
00381 }
00382
00393 public function __call($name, $args) {
00394 switch($name) {
00395 case 'query':
00396 case 'select':
00397 case 'insert':
00398 case 'update':
00399 case 'delete':
00400 return call_user_func(array($this->DBI, $name), $args);
00401 default:
00402 $this->setError("Not forwarding function '$name' to DBI");
00403 return false;
00404 }
00405 }
00406
00412 public function commit() {
00413 return $this->DBI->commitTransaction();
00414 }
00415
00421 public function cancel() {
00422 return $this->DBI->cancelTransaction();
00423 }
00424
00431 public function addCallback($cb) {
00432 return $this->DBI->addTransactionCallback($cb);
00433 }
00434 }
00435
00452 class DBI2 extends SObject {
00453 private static $DBIS = array();
00454
00455 private $db_name;
00456 private $db_host;
00457 private $db_user;
00458 private $db_pass;
00459
00460 private $db_conn;
00461
00462 private $DBI2_name;
00463
00464 private $validAttributes = array();
00465 private $transactionDepth = 0;
00466 private $transactionCallbacks = array();
00467 private $transactionObject = null;
00468
00469 const NUM_RESULTS = true;
00470 const ACTUAL_RESULTS = false;
00471
00472 public static $DEBUG = false;
00473 public static $WARN_DBI_SAME_NAME = true;
00474
00475 private static $DEFAULT_OP = false;
00476 private $defaultOp = false;
00477
00478
00479
00481 const FORMAT_NICE_DATE = "%A, %B %e, %Y";
00483 const FORMAT_NICE_DATE_ABBR = "%B %e, %Y";
00485 const FORMAT_SHORT_DATE = "%D";
00487 const FORMAT_DATE_DB = "%F";
00489 const FORMAT_NICE_DATETIME = "%l:%M %p, %A, %B %e, %Y";
00491 const FORMAT_NICE_TIME = "%l:%M %p";
00492
00506 public function __construct($name, $db_host, $db_name, $db_user, $db_pass) {
00507 if($db_host == "" || $db_name == "" || $db_user == "" || $db_pass == "") {
00508 $this->setError("Invalid DB credentials given (host: '$db_host', name: '$db_name', user: '$db_user', pass: '$db_pass')");
00509 return;
00510 }
00511
00512 $this->DBI2_name = $name;
00513 $this->db_host = $db_host;
00514 $this->db_name = $db_name;
00515 $this->db_user = $db_user;
00516 $this->db_pass = $db_pass;
00517
00518 $this->db_conn = null;
00519
00520 if(count(self::$DBIS) == 0)
00521 register_shutdown_function(array('DBI2', 'cleanup'));
00522 if(isset(self::$DBIS[$name]) && self::$WARN_DBI_SAME_NAME)
00523 self::setStaticWarning("DBI with name '$name' already exists!");
00524 self::$DBIS[$name] = $this;
00525 }
00526
00527
00535 public static function cleanup() {
00536 foreach(self::$DBIS as $d) {
00537 $d->transactionCallbacks = array();
00538 $d->cancelTransaction();
00539 }
00540 }
00541
00548 public static function getDBI($name) {
00549 return isset(self::$DBIS[$name]) ? self::$DBIS[$name] : null;
00550 }
00551
00567 public function getDefaultOperator() {
00568 if(isset($this) && $this->defaultOp !== false) {
00569 return $this->defaultOp;
00570 }
00571 if(self::$DEFAULT_OP === false) {
00572 self::$DEFAULT_OP = SConfig::getDefault('dbi2.defaultOperator');
00573 }
00574 return self::$DEFAULT_OP;
00575 }
00576
00586 public function setDefaultOperator($op) {
00587 if(isset($this)) {
00588 $old = $this->getDefaultOperator();
00589 $this->defaultOp = $op;
00590 return $old;
00591 }
00592 else {
00593 $old = self::getDefaultOperator();
00594 self::$DEFAULT_OP = $op;
00595 return $old;
00596 }
00597 }
00598
00611 public function setValidAttributes($table, $attributes) {
00612 $this->validAttributes[$table] = $attributes;
00613 }
00614
00626 public function query($string) {
00627 if(!$this->db_conn) {
00628 if($this->db_conn === false) {
00629 $this->setError('Not re-attempting to connect to DB after previous failure.');
00630 return null;
00631 }
00632 $this->db_conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pass);
00633 if(!$this->db_conn) {
00634 $this->db_conn = false;
00635 $this->setError("Failed to connect to database server '$this->db_host': " . mysql_error());
00636 return null;
00637 }
00638 if(!mysql_select_db($this->db_name, $this->db_conn)) {
00639 $this->db_conn = false;
00640 $this->setError("Failed to select database '$this->db_name': " . mysql_error($this->db_conn));
00641 return null;
00642 }
00643 }
00644
00645 if(self::$DEBUG) {
00646 SErrorManager::setDebug("Query: $string");
00647 }
00648
00649 $cookie = mysql_query($string, $this->db_conn);
00650 if(!$cookie) {
00651 $this->setError('Query failed: ' . mysql_error($this->db_conn) . ' (query was: "' . $string . '")');
00652 return null;
00653 }
00654
00655 return new DBResult($cookie, $this->db_conn);
00656 }
00657
00672 public function select($from, $what = self::NUM_RESULTS, $constraints = array(), $limit = array(), $order = array(),
00673 $extraJoins = '', $groupBy = '')
00674 {
00675 if(isset($this->validAttributes[$from]))
00676 $attrs = $this->validAttributes[$from];
00677 else
00678 $attrs = null;
00679
00680 if($what === self::NUM_RESULTS)
00681 $query = 'SELECT COUNT(*) FROM ' . $from;
00682 else {
00683 $query = 'SELECT ';
00684 foreach($what as $k => $v) {
00685 if(ctype_digit((string) $k)) {
00686 if(is_array($attrs) && !in_array($v, $attrs))
00687 $this->setWarning("Field '$v' does not belong to table '$from'");
00688 $query .= "$v,";
00689 }
00690 else {
00691 if(is_array($attrs) && !in_array($k, $attrs))
00692 $this->setWarning("Field '$k' does not belong to table '$from'");
00693 $query .= "$k AS $v,";
00694 }
00695 }
00696 $query = rtrim($query, ',') . ' FROM ' . $from;
00697 }
00698 $constraintC = self::criteriaToString($constraints, $attrs, $this->getDefaultOperator());
00699 if($constraintC === false)
00700 return null;
00701 $limitC = self::limitToString($limit);
00702 if($limitC === false)
00703 return null;
00704 $orderC = self::orderToString($order, $attrs);
00705 if($orderC === false)
00706 return null;
00707
00708 if($extraJoins)
00709 $query .= " $extraJoins ";
00710 if($constraintC != '')
00711 $query .= ' WHERE ' . $constraintC;
00712 if($groupBy)
00713 $query .= " $groupBy ";
00714 $query .= ' ' . $orderC . ' ' . $limitC;
00715
00716 if($groupBy) {
00717 if(preg_match('/GROUP BY ([^\s]+)/', $groupBy, $matches)) {
00718 $gbc = $matches[1];
00719 $query = preg_replace('/^\s*SELECT COUNT\(\*\) FROM (.*)$/', "SELECT COUNT(*) FROM (SELECT $gbc FROM $1) AS t", $query);
00720 }
00721 }
00722
00723 $results = $this->query($query);
00724 if($what === self::NUM_RESULTS) {
00725 $row = $results->nextRow();
00726 return $row['COUNT(*)'];
00727 }
00728 else
00729 return $results;
00730 }
00731
00743 public function insert($into, $what) {
00744 $query = 'INSERT INTO ' . $into . ' (';
00745 $vals = '';
00746 foreach($what as $k => $v) {
00747 $query .= "$k,";
00748 if($v === null)
00749 $vals .= "NULL,";
00750 else if(is_array($v))
00751 $vals .= "$v[0],";
00752 else
00753 $vals .= "'" . mysql_escape_string($v) . "',";
00754 }
00755 $query = rtrim($query, ',') . ') VALUES (' . rtrim($vals, ',') . ')';
00756
00757 $result = $this->query($query);
00758 if($result)
00759 return $result->getInsertId();
00760 else
00761 return false;
00762 }
00763
00778 public function update($table, $what, $constraints = false) {
00779 if(isset($this->validAttributes[$table]))
00780 $attrs = $this->validAttributes[$table];
00781 else
00782 $attrs = null;
00783
00784 if($constraints === true)
00785 $constraintC = '';
00786 else if(!is_array($constraints) || count($constraints) == 0) {
00787 $this->setError('You must provide constraints to the update() function!');
00788 return false;
00789 }
00790 else {
00791 $constraintC = self::criteriaToString($constraints, $attrs, $this->getDefaultOperator());
00792 if($constraintC === false)
00793 return false;
00794 if($constraintC != '')
00795 $constraintC = " WHERE $constraintC";
00796 }
00797
00798 $query = 'UPDATE ' . $table . ' SET ';
00799 foreach($what as $k => $v) {
00800 $query .= "$k = ";
00801 if($v === null)
00802 $query .= "NULL";
00803 else if(is_array($v))
00804 $query .= $v[0];
00805 else
00806 $query .= "'" . mysql_escape_string($v) . "'";
00807 $query .= ',';
00808 }
00809 $query = rtrim($query, ',') . $constraintC;
00810
00811 $result = $this->query($query);
00812 if($result)
00813 return true;
00814 else
00815 return false;
00816 }
00817
00830 public function delete($table, $constraints = false) {
00831 if(isset($this->validAttributes[$table]))
00832 $attrs = $this->validAttributes[$table];
00833 else
00834 $attrs = null;
00835
00836 if($constraints === true)
00837 $constraintC = '';
00838 else if(!is_array($constraints) || count($constraints) == 0) {
00839 $this->setError('You must provide constraints to the delete() function!');
00840 return false;
00841 }
00842 else {
00843 $constraintC = self::criteriaToString($constraints, $attrs, $this->getDefaultOperator());
00844 if($constraintC === false)
00845 return false;
00846 if($constraintC != '')
00847 $constraintC = " WHERE $constraintC";
00848 }
00849
00850 $query = "DELETE FROM $table $constraintC";
00851 $result = $this->query($query);
00852 if($result)
00853 return true;
00854 else
00855 return false;
00856 }
00857
00910 public function startTransaction($callback = null) {
00911 if($this->transactionDepth == 0) {
00912 if(!$this->query('START TRANSACTION')) {
00913 $this->setError('Failed to start transaction');
00914 return false;
00915 }
00916 }
00917 if($callback)
00918 $this->transactionCallbacks[] = $callback;
00919 $this->transactionDepth++;
00920 return true;
00921 }
00922
00940 public function createTransaction($nest = false) {
00941 if($this->transactionObject === null)
00942 $this->transactionObject = new DBTransaction($this);
00943 else if($nest)
00944 $this->startTransaction();
00945 return $this->transactionObject;
00946 }
00947
00962 public function commitTransaction() {
00963 if($this->transactionDepth < 1) {
00964 $this->setError('Attempting to commit transaction when no transaction is pending');
00965 return false;
00966 }
00967 else if($this->transactionDepth == 1) {
00968 if(!$this->query('COMMIT')) {
00969 $this->setError('Failed to commit transaction');
00970 return false;
00971 }
00972 foreach($this->transactionCallbacks as $cb)
00973 if(is_callable($cb))
00974 call_user_func($cb, true);
00975 $this->transactionCallbacks = array();
00976 }
00977 $this->transactionDepth--;
00978 return true;
00979 }
00980
01001 public function cancelTransaction() {
01002 if($this->transactionDepth > 0) {
01003 if(!$this->query('ROLLBACK')) {
01004 $this->setError('Failed to cancel transaction');
01005 return false;
01006 }
01007 foreach($this->transactionCallbacks as $cb)
01008 if(is_callable($cb))
01009 call_user_func($cb, false);
01010 $this->transactionCallbacks = array();
01011 $this->transactionDepth = 0;
01012 }
01013 return true;
01014 }
01015
01024 public function inTransaction() {
01025 return $this->transactionDepth;
01026 }
01027
01034 public function addTransactionCallback($cb) {
01035 $this->transactionCallbacks[] = $cb;
01036 }
01037
01049 public static function formatTimestamp($dbTimestamp, $formatString) {
01050 $ts = strtotime($dbTimestamp);
01051 $out = strftime($formatString, $ts);
01052 return $out;
01053 }
01054
01066 public static function criteriaToString($arr, $attributes, $op = null, $conj = null) {
01067 if(is_string($arr))
01068 return $arr;
01069 else if(!is_array($arr)) {
01070 self::setStaticError('Criteria must be string or array!');
01071 return false;
01072 }
01073 else if(count($arr) == 0)
01074 return '';
01075
01076 $collect = "";
01077 $parOp = $op;
01078 $parConj = $conj;
01079 if($op == null)
01080 $op = self::getDefaultOperator();
01081 if($conj == null)
01082 $conj = "AND";
01083 foreach($arr as $k => $v) {
01084
01085 if(ctype_digit((string) $k)) {
01086
01087 if(is_string($v)) {
01088 switch(strtoupper($v)) {
01089 case 'AND':
01090 case 'OR':
01091 $conj = $v;
01092 break;
01093 case '=':
01094 case '>=':
01095 case '<=':
01096 case '<':
01097 case '>':
01098 case '<>':
01099 case 'LIKE':
01100 case 'LIKE BINARY':
01101 case 'IS':
01102 case 'IS NOT':
01103 case 'IS NULL':
01104 case 'IS NOT NULL':
01105 case 'IN':
01106 case 'NOT IN':
01107 $op = $v;
01108 break;
01109 default:
01110 self::setStaticError("Invalid operator or conjunction in criteria: $v");
01111 return false;
01112 }
01113 if(($parOp == 'IN' || $parOp == 'NOT IN') && ($op != $parOp || $conj != $parConj)) {
01114 self::setStaticError("Cannot use subconstraints with IN or NOT IN");
01115 return false;
01116 }
01117 }
01118
01119 else if(is_array($v)) {
01120 $res = self::criteriaToString($v, $attributes, $op, $conj);
01121 if($res === false)
01122 return false;
01123 if($collect != "")
01124
01125 $collect .= " $conj " . $res;
01126 else
01127 $collect .= $res;
01128 }
01129
01130 else {
01131 self::setStaticError("Invalid operator or conjunction in criteria: $v");
01132 return false;
01133 }
01134 }
01135
01136 else if(is_string($k)) {
01137
01138 $newConj = null;
01139 $newOp = null;
01140 switch(strtoupper($k)) {
01141 case 'AND':
01142 case 'OR':
01143 $newConj = strtoupper($k);
01144 break;
01145 case '=':
01146 case '>=':
01147 case '<=':
01148 case '<':
01149 case '>':
01150 case '<>':
01151 case 'LIKE':
01152 case 'LIKE BINARY':
01153 case 'IS':
01154 case 'IS NOT':
01155 case 'IS NULL':
01156 case 'IS NOT NULL':
01157 case 'IN':
01158 case 'NOT IN':
01159 $newOp = strtoupper($k);
01160 break;
01161 case 'SUBQUERY':
01162 if($collect != '') {
01163 self::setStaticError('SUBQUERY operations must be contained in their own array');
01164 return false;
01165 }
01166 if(!is_string($v)) {
01167 self::setStaticError('SUBQUERY must have a string value which is a valid MySQL subquery');
01168 return false;
01169 }
01170 return '(' . $v . ')';
01171 break;
01172 }
01173 if($newOp || $newConj) {
01174 if($op == 'IN' || $op == 'NOT IN') {
01175 self::setStaticError("You cannot use IN/NOT IN with subconstraints");
01176 return false;
01177 }
01178 $res = self::criteriaToString($v, $attributes, $newOp, $newConj);
01179 if($res === false)
01180 return false;
01181 if($collect != "")
01182
01183 $collect .= " $conj " . $res;
01184 else
01185 $collect .= $res;
01186 }
01187 else {
01188 if($attributes) {
01189 if(!isset($attributes[$k])) {
01190 self::setStaticError("Unknown field '$k' in criteria");
01191 return false;
01192 }
01193
01194 $k = $attributes[$k];
01195 }
01196
01197
01198
01199 if(is_array($v)) {
01200 if($op == 'IN') {
01201 if($collect != "")
01202 $collect .= " $conj $k IN(";
01203 else
01204 $collect = " $k IN(";
01205 }
01206 else if($op == 'NOT IN') {
01207 if($collect != "")
01208 $collect .= " $conj $k NOT IN(";
01209 else
01210 $collect = " $k NOT IN(";
01211 }
01212 foreach($v as $innerK => $value) {
01213
01214
01215
01216 if($op == 'IS NULL' || $op == 'IS NOT NULL') {
01217 if($collect != "")
01218 $collect .= " $conj $k $op";
01219 else
01220 $collect = "$k $op";
01221 }
01222 else if($op == 'IN' || $op == 'NOT IN') {
01223 if(strtoupper($innerK) == 'SUBQUERY') {
01224 if(substr($collect, -3) == 'IN(')
01225 $collect .= "$value";
01226 else
01227 $collect .= ", $value";
01228 }
01229 else {
01230 if(substr($collect, -3) == 'IN(')
01231 $collect .= "'$value'";
01232 else
01233 $collect .= ", '$value'";
01234 }
01235 }
01236 else {
01237
01238 $value = mysql_escape_string($value);
01239 if($collect != "")
01240 $collect .= " $conj $k $op '$value'";
01241 else
01242 $collect = "$k $op '$value'";
01243 }
01244 }
01245 if($op == 'IN' || $op == 'NOT IN')
01246 $collect .= ")";
01247 }
01248 else {
01249
01250 if($op == 'IS NULL' || $op == 'IS NOT NULL') {
01251 if($collect != "")
01252 $collect .= " $conj $k $op";
01253 else
01254 $collect = "$k $op";
01255 }
01256 else if($op == 'IN' || $op == 'NOT IN') {
01257 if($collect != "")
01258 $collect .= " $conj $k $op('$v')";
01259 else
01260 $collect = "$k $op('$v')";
01261 }
01262 else {
01263 $v = mysql_escape_string($v);
01264 if($collect != "")
01265 $collect .= " $conj $k $op '$v'";
01266 else
01267 $collect = "$k $op '$v'";
01268 }
01269 }
01270 }
01271 }
01272 }
01273
01274
01275 if($collect != "")
01276 $collect = "($collect)";
01277
01278 return $collect;
01279 }
01280
01290 public static function limitToString($limit) {
01291 if($limit == "" || count($limit) == 0)
01292 return '';
01293
01294 if(is_array($limit)) {
01295 if(count($limit) == 2) {
01296 if(!ctype_digit((string) $limit[0]) || $limit[0] < 0 || !ctype_digit((string) $limit[1]) || $limit[1] < 0) {
01297 self::setStaticError('Invalid limit values');
01298 return false;
01299 }
01300 return " LIMIT $limit[0], $limit[1]";
01301 }
01302 else if(count($limit) == 1) {
01303 if(!ctype_digit((string) $limit[0]) || $limit[0] < 0) {
01304 self::setStaticError('Invalid limit values');
01305 return false;
01306 }
01307 return " LIMIT $limit[0]";
01308 }
01309 }
01310 else if(ctype_digit((string) $limit)) {
01311 if($limit < 0) {
01312 self::setStaticError('Invalid limit values');
01313 return false;
01314 }
01315 return " LIMIT $limit";
01316 }
01317
01318 self::setStaticError('Invalid limit array!');
01319 return false;
01320 }
01321
01333 public static function orderToString($order, $attributes) {
01334 if($order == "" || count($order) == 0)
01335 return '';
01336
01337 if(is_array($order)) {
01338 $orderStr = ' ORDER BY ';
01339 foreach($order as $key => $value) {
01340 if(ctype_digit((string) $key)) {
01341 if(strtolower(substr($value, 0, 6)) != 'field(' && $attributes && !isset($attributes[$value])) {
01342 if(!isset($attributes[$value])) {
01343 self::setStaticError('Invalid attribute \'' . $value. '\'');
01344 return false;
01345 }
01346 $value = $attributes[$value];
01347 }
01348 $orderStr .= $value . ', ';
01349 }
01350 else {
01351 if($attributes) {
01352 if(!isset($attributes[$key])) {
01353 self::setStaticError('Invalid attribute \'' . $key . '\'');
01354 return false;
01355 }
01356 $key = $attributes[$key];
01357 }
01358 $value = strtoupper($value);
01359 if($value != 'ASC' && $value != 'DESC') {
01360 self::setStaticError('Ordering must be ASC or DESC');
01361 return false;
01362 }
01363 $orderStr .= $key . ' ' . $value . ', ';
01364 }
01365 }
01366 return rtrim($orderStr, ', ');
01367 }
01368 else if(is_string($order)) {
01369 return " ORDER BY $order";
01370 }
01371 else {
01372 self::setStaticError('Invalid order by array');
01373 return false;
01374 }
01375 }
01376
01377
01378
01386 public function mysql_insert_id() {
01387 $this->setWarning('DEPRECATION NOTICE: mysql_insert_id() is deprecated; use the query result object instead '
01388 . '(method getInsertId())');
01389 return @mysql_insert_id($this->conn);
01390 }
01391
01399 public function clearErrors() {
01400 $this->setWarning('DEPRECATION NOTICE: clearErrors() is deprecated; use resetError() instead');
01401 return $this->resetError();
01402 }
01403
01413 public function setResult($a, $b) {
01414 $this->setError('DEPRECATION NOTICE: setResult() is not implemented on new DBI2');
01415 return null;
01416 }
01417
01426 public function getResult($a = '') {
01427 $this->setError('DEPRECATION NOTICE: getResult() is not implemented on new DBI2');
01428 return null;
01429 }
01430
01438 public function getDB() {
01439 $this->setError('DEPRECATION NOTICE: getDB() is not implemented on new DBI2');
01440 return null;
01441 }
01442
01450 public function getQuery() {
01451 $this->setError('DEPRECATION NOTICE: getQuery() is not implemented on new DBI2');
01452 return null;
01453 }
01454
01462 public function getFieldName() {
01463 $this->setError('DEPRECATION NOTICE: getFieldName() is not implemented on new DBI2');
01464 return null;
01465 }
01466 }
01467
01468 ?>