getMessage()}\nCode: {$this->getCode()}"; } } class DatabaseSqlException extends DatabaseException { protected $sql; public function __construct($sql, $message, $code = null) { parent::__construct($message, $code); $this->setSql($sql); } public function setSql($sql) { $this->sql = $sql; } public function getSql() { return $this->sql; } public function __toString() { return "Database Sql Error!\nSQL: {$this->getSql()}\nMessage: {$this->getMessage()}\nCode: {$this->getCode()}"; } } class Database extends mysqli { const FETCH_TYPE_ASSOC = 1; const FETCH_TYPE_ROW = 2; public function __construct($host = null, $username = null, $passwd = null, $dbname = null, $port = null, $socket = null) { parent::__construct($host, $username, $passwd, $dbname, $port, $socket); if ($this->errno) throw new DatabaseConnectException($this->error, $this->errno); } public function query($sql, $resultmode = MYSQLI_STORE_RESULT) { if (($result = parent::query($sql, $resultmode)) === false) throw new DatabaseSqlException($sql, $this->error, $this->errno); return $result; } public function fetchCol($sql) { $rows = array(); $result = $this->query($sql); while ($row = $result->fetch_row()) $rows[] = $row[0]; return $rows; } public function fetchOne($sql) { $result = $this->query($sql); if ($row = $result->fetch_row()) return $row[0]; return null; } public function fetchRow($sql, $type = self::FETCH_TYPE_ASSOC) { $result = $this->query($sql); if ($row = $this->createRow($result, $type)) return $row; return null; } public function fetchAll($sql, $type = self::FETCH_TYPE_ASSOC) { $rows = array(); $result = $this->query($sql); while ($row = $this->createRow($result, $type)) $rows[] = $row; return $rows; } protected function createRow($result, $type) { if ($type === self::FETCH_TYPE_ASSOC) return $result->fetch_assoc(); elseif ($type === self::FETCH_TYPE_ROW) return $result->fetch_row(); else throw new DatabaseException('Unknown fetch type: "' . $type . '"'); } public function insert($tableName, array $data) { $keys = $values = array(); foreach ($data as $k => $v) { $keys[] = $this->quoteIdentifier($k); $values[] = $this->quote($v); } $keys = implode(', ', $keys); $values = implode(', ', $values); $sql = "INSERT INTO " . $this->quoteIdentifier($tableName) . "($keys) VALUES($values)"; $this->query($sql); } public function update($tableName, array $data, $where = null) { $setters = array(); foreach ($data as $k => $v) $setters[] = $this->quoteIdentifier($k) . ' = ' . $this->quote($v); $setters = implode(', ', $setters); $sql = "UPDATE " . $this->quoteIdentifier($tableName) . " SET $setters"; if ($where !== null) $sql .= " WHERE $where"; $this->query($sql); } public function delete($tableName, $where = null) { $sql = "DELETE FROM " . $this->quoteIdentifier($tableName); if ($where !== null) $sql .= " WHERE $where"; $this->query($sql); } public function quote($value) { if ($value === null) return 'NULL'; if (is_bool($value)) return (int)$value; if (is_numeric($value)) return $value; if ($value instanceof DatabaseExpression) return $value->__toString(); return "'" . $this->real_escape_string((string)$value) . "'"; } public function quoteInto($sql, $value) { if (is_array($value)) { foreach ($value as $val) if (($pos = strpos($sql, '?')) !== false) $sql = substr_replace($sql, $this->quote($val), $pos, 1); else break; } else { $sql = str_replace('?', $this->quote($value), $sql); } return $sql; } public function quoteIdentifier($ident) { $ident = explode('.', $ident); $quoted = array(); foreach ($ident as $id) $quoted[] = '`' . $id . '`'; return implode('.', $quoted); } public function setEncoding($encoding) { $this->query('SET NAMES ' . $encoding); } } class DatabaseExpression { private $expression; public function __construct($expression) { $this->expression = (string)$expression; } public function __toString() { return $this->expression; } }