00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 if (!function_exists('mysql_connect'))
00021 trigger_error('mysql_* functions not defined. Install php*-mysql.', E_USER_ERROR);
00022 require_once(dirname(__FILE__) . '/KjwSql.php');
00023 require_once(dirname(__FILE__) . '/KjwMyResultSet.php');
00024
00032 class KjwMySql extends KjwSql {
00033 var $_dbconn;
00044 function KjwMySql($hostname, $portnum, $username, $password, $database) {
00045 parent::KjwSql('mysql', $hostname, $portnum ? $portnum : 3306, $username, $password, $database);
00046 $this->_dbconn = null;
00047 }
00048
00049
00050 function destroy() {
00051 $this->disconnect();
00052 parent::destroy();
00053 }
00054
00055
00056 function connect() {
00057 if ($this->_dbconn)
00058 return true;
00059 $this->_dbconn = false;
00060 if (($res = @mysql_connect($this->_hostname . ':' . $this->_portnum, $this->_username, $this->_password, true)) == false)
00061 $this->croak('Database Connection Failure', 'mysql_connect: ' . mysql_error(), 2);
00062 if (mysql_select_db($this->_database, $res) == false)
00063 $this->croak('Database Connection Failure', 'mysql_select_db: ' . mysql_error($res), 2);
00064
00065
00066
00067 if (mysql_query("SET sql_mode = 'ANSI'", $res) === false)
00068 $this->trace('SET sql_mode = \'ANSI\', failed on mysql connection.', 'WARNING');
00069
00070 if (mysql_query("SET NAMES 'utf8'", $res) === false)
00071 $this->trace('SET NAMES \'utf8\', failed on mysql connection.', 'WARNING');
00072 $this->_dbconn = $res;
00073 return true;
00074 }
00075
00076
00077 function disconnect() {
00078 if (!$this->_dbconn)
00079 return true;
00080 if (($ret = @mysql_close($this->_dbconn)) == true)
00081 $this->_dbconn = null;
00082 return $ret;
00083 }
00084
00085
00086 function beginWork() {
00087
00088 return $this->execute('START TRANSACTION');
00089 }
00090
00091
00092 function execute($query) {
00093 $this->_setLastQuery($query);
00094 if ($this->_dbconn === null)
00095 $this->connect();
00096 if (($res = @mysql_query($query, $this->_dbconn)) === false)
00097 $this->croak('Database Data Transfer Failure', "mysql_query: " . mysql_error($this->_dbconn), 2);
00098 if (is_bool($res))
00099 return true;
00100 return new KjwMyResultSet($res);
00101 }
00102
00103
00104 function safeQuote($mixed) {
00105 if (is_string($mixed)) {
00106 if ($this->_dbconn === null)
00107 $this->connect();
00108 return "_utf8'" . mysql_real_escape_string($mixed, $this->_dbconn) . "'";
00109 }
00110 return parent::safeQuote($mixed);
00111 }
00112
00113
00114 function nameQuote($string) {
00115 return '`' . str_replace('`', '``', preg_replace("/[\x01-\x1f]/", '',
00116 str_replace("\x00", '', $string))) . '`';
00117 }
00118
00119
00120 function affectedRows() {
00121 return mysql_affected_rows($this->_dbconn);
00122 }
00123
00124
00125 function insertId() {
00126 return mysql_insert_id($this->_dbconn);
00127 }
00128 }
00129
00130 ?>