00001 <?php
00015 class DBI {
00016
00017 private $username = "";
00018 private $password = "";
00019 private $database = "";
00020 private $dbServer = "";
00021 private $previousQuery = "";
00022 protected $dbObject = null;
00023 private $resultSet = null;
00024 private $errors = array();
00025 private $lastID = "";
00026 private $totalQueryTime = 0;
00027
00036 public function __construct ($user = "", $pass = "", $db = "", $server = "", $port = "3306") {
00037
00038 $this->username = $user;
00039 $this->password = $pass;
00040 $this->database = $db;
00041 $this->dbServer = "$server:$port";
00042
00043
00044 $this->dbObject = @mysql_pconnect($this->dbServer, $this->username, $this->password);
00045
00046
00047 if ($this->dbObject) {
00048
00049 $db = @mysql_select_db($this->database, $this->dbObject);
00050 if ($db) {
00051 return (true);
00052
00053 } else {
00054 $this->setError("Undefined database " . $this->database . " selected. Technical: " . @mysql_error());
00055 return (false);
00056 }
00057
00058 } else {
00059 $this->setError("Undefined database server " . $this->dbServer . " selected. Technical: " . @mysql_error());
00060 return (false);
00061 }
00062 }
00063
00069 public function __destruct() {
00070 if(SConfig::getOption('common.debugQueries'))
00071 print "Total DBI time: " . ($this->totalTime * 1000) . "<br />\n";
00072 }
00073
00074 private $oldQueries = array();
00075 private $qcount = 0;
00076 private $totalTime = 0;
00077
00078
00079
00080
00081
00082
00089 public function query($query, $queryInfo = false) {
00090
00091
00092 $db = mysql_select_db($this->database, $this->dbObject);
00093
00094
00095 $dbg = SConfig::getOption('common.debugQueries');
00096
00097
00098 if($dbg) {
00099 $start = array_sum(explode(' ', microtime()));
00100 if(isset($oldQueries[$query])) {
00101 SErrorManager::setWarning("Already ran query '$query' (debug message id $oldQueries[$query])");
00102 }
00103 }
00104
00105
00106 $sqlObj = new SQL($this, $query, $queryInfo);
00107
00108
00109 $sqlObj->doSQL();
00110
00111
00112 $this->previousQuery = $sqlObj;
00113
00114
00115 $this->lastID = $sqlObj->mysql_insert_id();
00116
00117
00118 $res = $this->getResult($query);
00119
00120
00121 if($dbg) {
00122 $end = array_sum(explode(' ', microtime()));
00123 $this->totalTime += ($end - $start);
00124 SErrorManager::sobj_setDebug("<i>DBI Query [$this->qcount]</i> <b>(TIME: " . substr((($end - $start) * 1000), 0, 6) . " ms)</b><br />$query", 2);
00125 $this->oldQueries[$query] = $this->qcount;
00126 $this->qcount++;
00127 }
00128
00129
00130 return $res;
00131 }
00132
00133
00134
00135
00136
00137
00142 public function setError($errorMessage) {
00143 array_push($this->errors, $errorMessage);
00144 }
00145
00151 public function getError($pos = "") {
00152 if ($pos == "") {
00153 return($this->errors);
00154 } else {
00155 if (isset($this->errors) && $this->errors[$pos] != "") {
00156 return $this->errors[$pos];
00157 }
00158 }
00159 }
00160
00168 public function hasError() {
00169 return (count($this->errors) > 0);
00170 }
00171
00175 public function clearErrors() {
00176 $this->errors = array();
00177 }
00178
00179
00180
00181
00182
00183
00189 public function setResult($resultIndex, $resultIn) {
00190 $this->resultSet[$resultIndex] = null;
00191 $this->resultSet[$resultIndex] = $resultIn;
00192 }
00193
00199 public function getResult($index = "") {
00200 if ($index != "") {
00201 if ($this->resultSet[$index] != "") {
00202 return ($this->resultSet[$index]);
00203 } else {
00204 return (array("0"));
00205 }
00206 } else {
00207 return($this->resultSet);
00208 }
00209 }
00210
00211
00212
00213
00214
00219 public function getDB() {
00220 return ($this->dbObject);
00221 }
00222
00230 public function getDBName() {
00231 return $this->database;
00232 }
00233
00238 public function getQuery() {
00239 if ($this->previousQuery != null) {
00240 return ($this->previousQuery->getQuery());
00241 } else {
00242 return ("");
00243 }
00244 }
00245
00250 public function mysql_insert_id() {
00251 return ($this->lastID);
00252 }
00253
00258 public function fieldName() {
00259
00260 $sqlObj = new SQL($this, $this->previousQuery, false);
00261
00262 if ($this->previousQuery != null) {
00263 return ($this->previousQuery->getFieldName());
00264 }
00265 }
00266 }
00267
00268
00269
00270
00271
00272
00276 class SQL {
00277
00278 private $dbObject = null;
00279 private $query = "";
00280 private $dbAccess;
00281 private $additionalQueryInfo;
00282 private $lastID = "";
00283 private $fieldName = array();
00284
00291 public function __construct($dbO = null, $queryIn = "", $queryInfo) {
00292 $this->dbObject = $dbO;
00293 $this->query = $queryIn;
00294 $this->dbAccess = $this->dbObject->getDB();
00295 $this->additionalQueryInfo = $queryInfo;
00296 }
00297
00308 public function doSQL() {
00309
00310 $results = array();
00311 $counter = 0;
00312
00313
00314 if(!mysql_select_db($this->dbObject->getDBName(), $this->dbAccess))
00315 $this->dbObject->setWarning("Could not select DB: " . mysql_error($this->dbAccess));
00316
00317 if (!($dbResult = mysql_query($this->query, $this->dbAccess))) {
00318
00319 $this->dbObject->setError("MySQL Error: " . mysql_error($this->dbAccess) . ". Associated query: " . $this->query);
00320 }
00321
00322
00323 if($dbResult) {
00324 if(SConfig::getOption('common.debugQueries') == true)
00325 SErrorManager::setDebug("Number of results: " . mysql_num_rows($dbResult));
00326 if ($dbResult != 1 && mysql_num_rows($dbResult) > 0 && $this->additionalQueryInfo) {
00327 $results["__count"] = @mysql_num_rows($dbResult);
00328 }
00329
00330 $dontCheckNumFields = false;
00331 if (eregi("^INSERT", $this->query) !== FALSE) {
00332 $dontCheckNumFields = true;
00333 $this->lastID = @mysql_insert_id($this->dbAccess);
00334 }
00335 if (eregi("^UPDATE", $this->query) !== FALSE) {
00336 $dontCheckNumFields = true;
00337 }
00338 if (eregi("^DELETE", $this->query) !== FALSE) {
00339 $dontCheckNumFields = true;
00340 }
00341
00342 if(!$dontCheckNumFields) {
00343 for ($i = 0; $i < @mysql_num_fields($dbResult); $i++) {
00344 @array_push($this->fieldName, @mysql_field_name($dbResult, $i));
00345 }
00346 while ($dbRow = @mysql_fetch_assoc($dbResult)) {
00347 $results[$counter] = $dbRow;
00348 $counter++;
00349 }
00350 }
00351
00352 if(SConfig::getOption('common.debugQueries') == true)
00353 SErrorManager::setDebug("Number of rows affected: " . mysql_affected_rows($this->dbAccess));
00354
00355 if (@mysql_affected_rows($this->dbAccess) > 0 && $this->additionalQueryInfo) {
00356 $results["__affected"] = @mysql_affected_rows($this->dbAccess);
00357 }
00358 }
00359 $this->dbObject->setResult($this->query, $results);
00360 }
00361
00366 public function getFieldName() {
00367 return($this->fieldName);
00368 }
00369
00374 public function getQuery() {
00375 if ($this->query != "") {
00376 return ($this->query);
00377 } else {
00378 return ("");
00379 }
00380 }
00381
00386 public function mysql_insert_id() {
00387 if ($this->query != "") {
00388 return ($this->lastID);
00389 } else {
00390 return ("");
00391 }
00392 }
00393 }
00394
00395 ?>