Multi databases support, #12
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@113 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
104
model/MySQLiDriver.php
Normal file
104
model/MySQLiDriver.php
Normal file
@ -0,0 +1,104 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user