00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 require_once('KjwObject.php');
00021
00032 class KjwSql extends KjwObject {
00033 var $_sqltype;
00034 var $_hostname;
00035 var $_portnum;
00036 var $_username;
00037 var $_password;
00038 var $_database;
00039 var $_debug_query_count;
00040 var $_last_query;
00052 function KjwSql($sqltype, $hostname, $portnum, $username, $password, $database) {
00053 parent::KjwObject();
00054 $this->_sqltype = $sqltype;
00055 $this->_hostname = $hostname;
00056 $this->_portnum = $portnum;
00057 $this->_username = $username;
00058 $this->_password = $password;
00059 $this->_database = $database;
00060 $this->_debug_query_count = 0;
00061 if (KJW_DEBUG)
00062 $this->_last_query = array();
00063 else
00064 $this->_last_query = '';
00065 }
00066
00067
00068 function destroy() {
00069 $this->disconnect();
00070 parent::destroy();
00071 }
00072
00078 function getType() {
00079 return $this->_sqltype;
00080 }
00081
00088 function connect() {
00089 return $this->notImplemented();
00090 }
00091
00097 function disconnect() {
00098 return $this->notImplemented();
00099 }
00100
00104 function beginWork() {
00105 return $this->execute('BEGIN WORK');
00106 }
00107
00111 function commitWork() {
00112 return $this->execute('COMMIT');
00113 }
00114
00118 function rollbackWork() {
00119 return $this->execute('ROLLBACK');
00120 }
00121
00128 function execute($query) {
00129 $this->_last_query = $query;
00130 return $this->notImplemented();
00131 }
00132
00143 function selectAll($query, $max = -1, $skip = 0) {
00144 $result = $this->execute("SELECT $query");
00145 if (!is_bool($result)) {
00146 $ret = array();
00147 $result->seekAbs($skip);
00148 while (($row = $result->getNext()) && $max--)
00149 array_push($ret, $row);
00150 $result->destroy();
00151 return $ret;
00152 } elseif ($result === false) {
00153 return false;
00154 }
00155 $this->croak('Database Data Transfer Failure', 'Execute returned \'true\'. Query was: ' . $this->_getLastQuery(), 2);
00156 }
00157
00165 function selectAtom($query) {
00166 $result = $this->execute("SELECT $query");
00167 if (!is_bool($result) && $result->size() === 1) {
00168 $arr = $result->getNext();
00169 $result->destroy();
00170 if (sizeof($arr) === 1)
00171 return array_shift($arr);
00172 } elseif ($result === false) {
00173 return false;
00174 }
00175 $this->croak('Database Data Transfer Failure', 'Atom: result count != 1. Query was: ' . $this->_getLastQuery(), 2);
00176 }
00177
00185 function selectAtomOrNull($query) {
00186 $result = $this->execute("SELECT $query");
00187 if (!is_bool($result)) {
00188 if ($result->size() === 0) {
00189 $result->destroy();
00190 return null;
00191 } elseif ($result->size() === 1) {
00192 $arr = $result->getNext();
00193 $result->destroy();
00194 if (sizeof($arr) === 1)
00195 return array_shift($arr);
00196 }
00197 } elseif ($result === false) {
00198 return false;
00199 }
00200 $this->croak('Database Data Transfer Failure', 'AtomOrNull: result count > 1. Query was: ' . $this->_getLastQuery(), 2);
00201 }
00202
00210 function selectAtomRow($query) {
00211 $result = $this->execute("SELECT $query");
00212 if (!is_bool($result) && $result->size() === 1) {
00213 $arr = $result->getNext();
00214 $result->destroy();
00215 return $arr;
00216 } elseif ($result === false) {
00217 return false;
00218 }
00219 $this->croak('Database Data Transfer Failure', 'AtomRow, result count != 1. Query was: ' . $this->_getLastQuery(), 2);
00220 }
00221
00229 function selectAtomRowOrNull($query) {
00230 $result = $this->execute("SELECT $query");
00231 if (!is_bool($result)) {
00232 if ($result->size() === 0) {
00233 $result->destroy();
00234 return null;
00235 } elseif ($result->size() === 1) {
00236 $arr = $result->getNext();
00237 $result->destroy();
00238 return $arr;
00239 }
00240 } elseif ($result === false) {
00241 return false;
00242 }
00243 $this->croak('Database Data Transfer Failure', 'AtomRowOrNull, result count > 1. Query was: ' . $this->_getLastQuery(), 2);
00244 }
00245
00256 function safeQuote($mixed) {
00257 if (is_string($mixed))
00258 return "'" . preg_replace("/[\x01-\x1f]/", '', str_replace("\x00", '', $mixed)) . "'";
00259 if (is_int($mixed) or is_float($mixed))
00260 return (double)$mixed;
00261 if (is_bool($mixed))
00262 return $mixed ? 1 : 0;
00263 if (is_null($mixed))
00264 return 'NULL';
00265 if (is_array($mixed) || is_object($mixed)) {
00266 $ret = array();
00267 foreach($mixed as $k => $v)
00268 $ret[$this->nameQuote($k)] = $this->safeQuote($v);
00269 return $ret;
00270 }
00271 return $this->croak('Library Component Error', 'Sql safeQuote fell through switch. Mixed parameter is "'
00272 . $mixed . '" as string, type is ' . gettype($mixed) . '.', 1);
00273 }
00274
00282 function nameQuote($string) {
00283 return $this->notImplemented();
00284 }
00285
00294 function insertArray($table, $args) {
00295 assert(is_array($args));
00296 if (is_string($table)) {
00297 $table = $this->nameQuote($table);
00298 } elseif (is_array($table)) {
00299 foreach ($table as $k => $v)
00300 $table[$k] = $this->nameQuote($v);
00301 $table = implode('.', $table);
00302 } else {
00303 assert(0);
00304 }
00305 $keys = array();
00306 $values = array();
00307 foreach ($args as $k => $v) {
00308 $keys[] = $this->nameQuote($k);
00309 if (is_array($v)) {
00310 $values[] = $v[0];
00311 } else {
00312 $values[] = $this->safeQuote($v);
00313 }
00314 }
00315
00316 $query = "INSERT INTO $table ("
00317 . implode(', ', $keys) . ") VALUES ("
00318 . implode(', ', $values) . ")";
00319 return $this->execute($query);
00320 }
00321
00334 function updateArray($table, $args, $where = null) {
00335 assert(is_array($args) && is_string($where));
00336 if (is_string($table)) {
00337 $table = $this->nameQuote($table);
00338 } elseif (is_array($table)) {
00339 foreach ($table as $k => $v)
00340 $table[$k] = $this->nameQuote($v);
00341 $table = implode('.', $table);
00342 } else {
00343 assert(0);
00344 }
00345 $setArgs = array();
00346 foreach($args AS $k => $v) {
00347 if (is_array($v)) {
00348 $setArgs[] = $this->nameQuote($k) . ' = ' . $v[0];
00349 } else {
00350 $setArgs[] = $this->nameQuote($k) . ' = ' . $this->safeQuote($v);
00351 }
00352 }
00353 return $this->execute(
00354 "UPDATE $table SET " . implode(', ', $setArgs) . ($where !== null ? " WHERE $where" : '')
00355 );
00356 }
00357
00366 function affectedRows() {
00367 return $this->notImplemented();
00368 }
00369
00377 function insertId() {
00378 return $this->notImplemented();
00379 }
00380
00386 function debugStat() {
00387 return array('query_count' => $this->_debug_query_count);
00388 }
00389
00395 function _getLastQuery() {
00396 if (KJW_DEBUG)
00397 return end($this->_last_query);
00398 return $this->_last_query;
00399 }
00400
00406 function _setLastQuery($query) {
00407 if (KJW_DEBUG) {
00408 array_push($this->_last_query, $query);
00409 $this->trace($query, 'DEBUG');
00410 } else {
00411 $this->_last_query = $query;
00412 }
00413 ++$this->_debug_query_count;
00414 }
00415 }
00416
00417
00436 function kjwsql_from_url($url, $create_new = false) {
00437 static $sqls = array();
00438 if ($create_new || !isset($sqls[$url])) {
00439 $ua = parse_url($url);
00440 $port = (int)@$ua['port'];
00441 switch (@$ua['scheme']) {
00442
00443
00444 case 'mysql': require_once(dirname(__FILE__) . '/KjwMySql.php');
00445 $sqls[$url] = new KjwMySql(@$ua['host'], $port, @$ua['user'], @$ua['pass'], substr(@$ua['path'], 1)); break;
00446 case 'mssql': require_once(dirname(__FILE__) . '/KjwMsSql.php');
00447 $sqls[$url] = new KjwMsSql(@$ua['host'], $port, @$ua['user'], @$ua['pass'], substr(@$ua['path'], 1)); break;
00448 case 'pgsql': require_once(dirname(__FILE__) . '/KjwPgSql.php');
00449 $sqls[$url] = new KjwPgSql(@$ua['host'], $port, @$ua['user'], @$ua['pass'], substr(@$ua['path'], 1)); break;
00450 case 'xmlsql': require_once(dirname(__FILE__) . '/KjwXmlSql.php');
00451 $sqls[$url] = new KjwXmlSql(@$ua['host'], $port, @$ua['user'], @$ua['pass'], substr(@$ua['path'], 1)); break;
00452 default: trigger_error('Unknown SQL type: ' . @$ua['scheme'], E_USER_ERROR);
00453 return null;
00454 }
00455 }
00456 return $sqls[$url];
00457 }
00458
00459 ?>