You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.7 KiB

<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage db
* @since 2010-02-17
* @version SVN: $Id$
* @filesource $URL$
*/
/**
* @property MySQLi $connection
*/
class MySQLiDriver extends DbDriver
{
/**
* @param mixed $sql
* @return DbStatement
*/
public function prepare($sql)
{
return new MySQLiStatement($this, $sql);
}
public function getInsertId($table = null, $key = null)
{
return $this->connection->insert_id;
}
public function isConnected()
{
return ($this->connection instanceof MySQLi);
}
public function disconnect()
{
if ($this->isConnected()) {
$this->connection->close();
}
$this->connection = null;
}
protected function connect()
{
if ($this->connection) {
return;
}
$port = isset($this->config['port']) ? (int) $this->config['port'] : null;
$this->connection = mysqli_init();
$connected = @mysqli_real_connect($this->connection,
$this->config['hostname'],
$this->config['username'],
$this->config['password'],
$this->config['database'],
$port);
// Connection errors check
if (mysqli_connect_error()) {
throw new Exception(mysqli_connect_error(), mysqli_connect_errno());
}
$charset = (!empty($this->config['charset'])) ? $this->config['charset'] : 'utf8';
$this->connection->set_charset($charset);
}
protected function driverQuote($value)
{
if (is_int($value) || is_float($value)) {
return $value;
}
if (is_bool($value)) {
var_dump($value);
return (int) $value;
}
if ($value === null) {
return 'NULL';
}
$this->connect();
return '\'' . $this->connection->real_escape_string($value) . '\'';
}
protected function driverBeginTransaction()
{
$this->connect();
$this->connection->autocommit(false);
}
protected function driverCommitTransaction()
{
$this->connection->commit();
$this->connection->autocommit(true);
}
protected function driverRollbackTransaction()
{
$this->connection->rollback();
$this->connection->autocommit(true);
}
}