. ======================================================================*/ if (!function_exists('mysql_connect')) trigger_error('mysql_* functions not defined. Install php*-mysql.', E_USER_ERROR); require_once(dirname(__FILE__) . '/KjwSql.php'); require_once(dirname(__FILE__) . '/KjwMyResultSet.php'); /** @ingroup sql * * KjwMySql is the mysql implementation of the KjwSql abstract class. * * @todo See MsSql timeout/database fail 'transient_error' handling. * @todo mysql_set_charset to CHARSET or something.. (if php >= 5.2.3) */ class KjwMySql extends KjwSql { var $_dbconn; /**< Database resource */ /** * Construct a KjwMySql object. * * @param $hostname Server name or IP. * @param $portnum Port number of 0 if default. * @param $username User name. * @param $password Password. * @param $database Database name. */ function KjwMySql($hostname, $portnum, $username, $password, $database) { parent::KjwSql('mysql', $hostname, $portnum ? $portnum : 3306, $username, $password, $database); $this->_dbconn = null; // null = unconnected, false = failed connect } /* [INHERITED DOCS] */ function destroy() { $this->disconnect(); parent::destroy(); } /* [INHERITED DOCS] */ function connect() { if ($this->_dbconn) return true; $this->_dbconn = false; // If we fail, we shan't try to autoconnect again later. if (($res = @mysql_connect($this->_hostname . ':' . $this->_portnum, $this->_username, $this->_password, true)) == false) $this->croak('Database Connection Failure', 'mysql_connect: ' . mysql_error(), 2); if (mysql_select_db($this->_database, $res) == false) $this->croak('Database Connection Failure', 'mysql_select_db: ' . mysql_error($res), 2); // Go into "strict"-mode. If our user uses these classes, he will want us to // check our SQL-portability. if (mysql_query("SET sql_mode = 'ANSI'", $res) === false) $this->trace('SET sql_mode = \'ANSI\', failed on mysql connection.', 'WARNING'); // Most likely we'll be using UTF-8 all over. if (mysql_query("SET NAMES 'utf8'", $res) === false) $this->trace('SET NAMES \'utf8\', failed on mysql connection.', 'WARNING'); $this->_dbconn = $res; return true; } /* [INHERITED DOCS] */ function disconnect() { if (!$this->_dbconn) return true; if (($ret = @mysql_close($this->_dbconn)) == true) $this->_dbconn = null; return $ret; } /* [INHERITED DOCS] */ function beginWork() { // "START TRANSACTION is standard SQL syntax and is the recommended way to start an ad-hoc transaction." return $this->execute('START TRANSACTION'); } /* [INHERITED DOCS] */ function execute($query) { $this->_setLastQuery($query); if ($this->_dbconn === null) $this->connect(); // Try to autoconnect once. if (($res = @mysql_query($query, $this->_dbconn)) === false) $this->croak('Database Data Transfer Failure', "mysql_query: " . mysql_error($this->_dbconn), 2); if (is_bool($res)) return true; return new KjwMyResultSet($res); } /* [INHERITED DOCS] */ function safeQuote($mixed) { if (is_string($mixed)) { if ($this->_dbconn === null) $this->connect(); // Try to autoconnect once. return "_utf8'" . mysql_real_escape_string($mixed, $this->_dbconn) . "'"; } return parent::safeQuote($mixed); } /* [INHERITED DOCS] */ function nameQuote($string) { return '`' . str_replace('`', '``', preg_replace("/[\x01-\x1f]/", '', str_replace("\x00", '', $string))) . '`'; } /* [INHERITED DOCS] */ function affectedRows() { return mysql_affected_rows($this->_dbconn); } /* [INHERITED DOCS] */ function insertId() { return mysql_insert_id($this->_dbconn); } } ?>