diff --git a/DatabaseMysql.php b/DatabaseMysql.php index d19366c..104abc5 100644 --- a/DatabaseMysql.php +++ b/DatabaseMysql.php @@ -6,42 +6,79 @@ * (c) Vitaliy Filippov, 2012 */ -define('MS_HASH', 0); -define('MS_LIST', 1); -define('MS_ROW', 2); -define('MS_ONE', 2); -define('MS_COL', 4); -define('MS_VALUE', 6); -define('MS_RESULT', 8); +if (!defined('MS_HASH')) +{ + define('MS_HASH', 0); + define('MS_LIST', 1); + define('MS_ROW', 2); + define('MS_ONE', 2); + define('MS_COL', 4); + define('MS_VALUE', 6); + define('MS_RESULT', 8); +} class DatabaseMysql { - var $dbName; + var $host, $port, $socket, $username, $password, $dbName; + var $link; var $tableNames = array(); var $queryCount = 0; var $queryLogFile; + /** + * Creates a MySQL connection object. + * + * @param string $host either 'host', 'host:port', or ':/var/run/mysqld/mysqld.sock' + * @param string $username + * @param string $password + * @param string $dbname + * @param string $port (optional) + * @param string $socket (optional) + */ function __construct($host, $username, $password, $dbname, $port = NULL, $socket = NULL) { - if ($socket !== NULL) + $host = explode(':', $host, 2); + if (!$host[0]) { - $this->link = new mysqli($host, $username, $password, $dbname, $port, $socket); - } - elseif ($port !== NULL) - { - $this->link = new mysqli($host, $username, $password, $dbname, $port); + $socket = $host[1]; + $host = 'localhost'; } else { - $this->link = new mysqli($host, $username, $password, $dbname); + if ($host[1]) + { + $port = $host[1]; + } + $host = $host[0]; } - $this->dbName = $dbname; + $this->host = $host; + $this->username = $username; + $this->password = $password; + $this->dbname = $dbname; + $this->connect(); + } + + function connect() + { + if ($this->socket !== NULL) + { + $this->link = new mysqli($this->host, $this->username, $this->password, $this->dbname, $this->port, $this->socket); + } + elseif ($this->port !== NULL) + { + $this->link = new mysqli($this->host, $this->username, $this->password, $this->dbname, $this->port); + } + else + { + $this->link = new mysqli($this->host, $this->username, $this->password, $this->dbname); + } + $this->link->set_charset('utf8'); } function getDBName() { - return $this->dbName; + return $this->dbname; } function quoteId($name) @@ -53,7 +90,7 @@ class DatabaseMysql { if ($value === NULL) return "NULL"; - return "'" . $this->link->real_escape_string($s) . "'"; + return "'" . $this->link->real_escape_string($value) . "'"; } function query($sql, $fetchMode = MYSQLI_STORE_RESULT) @@ -182,7 +219,7 @@ class DatabaseMysql { if (is_array($v)) { - $join = substr(strtolower($v[0]), 0, 4); + $join = strtolower(substr($v[0], 0, 4)); if ($join == 'righ') $join = 'RIGHT'; elseif ($join == 'left') @@ -326,6 +363,11 @@ class DatabaseMysql return NULL; } + function insert_id() + { + return $this->link->insert_id; + } + function update($table, $rows, $where = NULL, $options = NULL) { if (!$rows)