1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-23 18:33:45 +01:00
php-dibi/dibi/drivers/mysqli.php

273 lines
7.5 KiB
PHP
Raw Normal View History

2006-06-04 23:06:33 +00:00
<?php
/**
* dibi - Database Abstraction Layer according to dgx
* --------------------------------------------------
*
* This source file is subject to the GNU GPL license.
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2007 David Grudl
2006-08-04 14:04:26 +00:00
* @license GNU GENERAL PUBLIC LICENSE v2
2006-06-04 23:06:33 +00:00
* @package dibi
* @category Database
2006-06-07 11:48:20 +00:00
* @version $Revision$ $Date$
2006-06-04 23:06:33 +00:00
*/
// security - include dibi.php, not this file
2006-06-07 11:48:20 +00:00
if (!defined('DIBI')) die();
2006-06-04 23:06:33 +00:00
/**
* The dibi driver for MySQLi database
*
*/
class DibiMySqliDriver extends DibiDriver
{
2006-06-04 23:06:33 +00:00
private
2006-06-04 23:09:53 +00:00
$conn,
$insertId = FALSE,
$affectedRows = FALSE;
2006-06-04 23:06:33 +00:00
public
$formats = array(
'TRUE' => "1",
'FALSE' => "0",
'date' => "'Y-m-d'",
'datetime' => "'Y-m-d H:i:s'",
);
public static function connect($config)
{
if (!extension_loaded('mysqli'))
2006-11-13 06:32:16 +00:00
throw new DibiException("PHP extension 'mysqli' is not loaded");
2006-06-04 23:06:33 +00:00
// default values
if (empty($config['username'])) $config['username'] = ini_get('mysqli.default_user');
if (empty($config['password'])) $config['password'] = ini_get('mysqli.default_password');
if (empty($config['host'])) {
$config['host'] = ini_get('mysqli.default_host');
if (empty($config['port'])) ini_get('mysqli.default_port');
if (empty($config['host'])) $config['host'] = 'localhost';
}
if (!isset($config['database'])) $config['database'] = NULL;
2006-08-25 18:10:30 +00:00
$conn = @mysqli_connect($config['host'], $config['username'], $config['password'], $config['database'], $config['port']);
2006-06-04 23:06:33 +00:00
if (!$conn)
2006-11-13 06:32:16 +00:00
throw new DibiException("Connecting error", array(
2006-06-04 23:06:33 +00:00
'message' => mysqli_connect_error(),
'code' => mysqli_connect_errno(),
));
if (!empty($config['charset']))
mysqli_query($conn, "SET NAMES '" . $config['charset'] . "'");
2006-06-04 23:06:33 +00:00
$obj = new self($config);
$obj->conn = $conn;
return $obj;
}
public function nativeQuery($sql)
2006-06-04 23:06:33 +00:00
{
2006-06-04 23:09:53 +00:00
$this->insertId = $this->affectedRows = FALSE;
2006-06-04 23:06:33 +00:00
$res = @mysqli_query($this->conn, $sql);
if ($res === FALSE) return FALSE;
2006-06-04 23:09:53 +00:00
$this->affectedRows = mysqli_affected_rows($this->conn);
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
$this->insertId = mysqli_insert_id($this->conn);
if ($this->insertId < 1) $this->insertId = FALSE;
2007-03-26 06:22:53 +00:00
if (is_object($res))
return new DibiMySqliResult($res);
2006-06-04 23:06:33 +00:00
return TRUE;
}
public function affectedRows()
{
2006-06-04 23:09:53 +00:00
return $this->affectedRows;
2006-06-04 23:06:33 +00:00
}
public function insertId()
{
2006-06-04 23:09:53 +00:00
return $this->insertId;
2006-06-04 23:06:33 +00:00
}
public function begin()
{
return mysqli_autocommit($this->conn, FALSE);
}
public function commit()
{
$ok = mysqli_commit($this->conn);
mysqli_autocommit($this->conn, TRUE);
return $ok;
}
public function rollback()
{
$ok = mysqli_rollback($this->conn);
mysqli_autocommit($this->conn, TRUE);
return $ok;
}
public function errorInfo()
2006-06-04 23:06:33 +00:00
{
return array(
'message' => mysqli_error($this->conn),
'code' => mysqli_errno($this->conn),
);
}
public function escape($value, $appendQuotes = FALSE)
{
return $appendQuotes
? "'" . mysqli_real_escape_string($this->conn, $value) . "'"
: mysqli_real_escape_string($this->conn, $value);
}
public function quoteName($value)
{
return '`' . str_replace('.', '`.`', $value) . '`';
2006-06-04 23:06:33 +00:00
}
public function getMetaData()
{
trigger_error('Meta is not implemented yet.', E_USER_WARNING);
}
2006-09-23 07:55:11 +00:00
/**
* @see DibiDriver::applyLimit()
*/
public function applyLimit(&$sql, $limit, $offset = 0)
{
if ($limit < 0 && $offset < 1) return;
// see http://dev.mysql.com/doc/refman/5.0/en/select.html
$sql .= ' LIMIT ' . ($limit < 0 ? '18446744073709551615' : (int) $limit)
. ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
}
2006-06-04 23:06:33 +00:00
} // class DibiMySqliDriver
class DibiMySqliResult extends DibiResult
{
private $resource;
2006-06-04 23:06:33 +00:00
public function __construct($resource)
{
$this->resource = $resource;
}
public function rowCount()
{
return mysqli_num_rows($this->resource);
}
protected function doFetch()
{
return mysqli_fetch_assoc($this->resource);
}
public function seek($row)
{
return mysqli_data_seek($this->resource, $row);
}
protected function free()
{
mysqli_free_result($this->resource);
}
/** this is experimental */
protected function buildMeta()
2006-06-04 23:06:33 +00:00
{
static $types = array(
2006-06-08 01:35:44 +00:00
MYSQLI_TYPE_FLOAT => dibi::FIELD_FLOAT,
MYSQLI_TYPE_DOUBLE => dibi::FIELD_FLOAT,
MYSQLI_TYPE_DECIMAL => dibi::FIELD_FLOAT,
// MYSQLI_TYPE_NEWDECIMAL=> dibi::FIELD_FLOAT,
// MYSQLI_TYPE_BIT => dibi::FIELD_INTEGER,
MYSQLI_TYPE_TINY => dibi::FIELD_INTEGER,
MYSQLI_TYPE_SHORT => dibi::FIELD_INTEGER,
MYSQLI_TYPE_LONG => dibi::FIELD_INTEGER,
MYSQLI_TYPE_LONGLONG => dibi::FIELD_INTEGER,
MYSQLI_TYPE_INT24 => dibi::FIELD_INTEGER,
MYSQLI_TYPE_YEAR => dibi::FIELD_INTEGER,
MYSQLI_TYPE_GEOMETRY => dibi::FIELD_INTEGER,
MYSQLI_TYPE_DATE => dibi::FIELD_DATE,
MYSQLI_TYPE_NEWDATE => dibi::FIELD_DATE,
MYSQLI_TYPE_TIMESTAMP => dibi::FIELD_DATETIME,
MYSQLI_TYPE_TIME => dibi::FIELD_DATETIME,
MYSQLI_TYPE_DATETIME => dibi::FIELD_DATETIME,
MYSQLI_TYPE_ENUM => dibi::FIELD_TEXT, // eventually dibi::FIELD_INTEGER
MYSQLI_TYPE_SET => dibi::FIELD_TEXT, // eventually dibi::FIELD_INTEGER
MYSQLI_TYPE_STRING => dibi::FIELD_TEXT,
MYSQLI_TYPE_VAR_STRING=> dibi::FIELD_TEXT,
MYSQLI_TYPE_TINY_BLOB => dibi::FIELD_BINARY,
MYSQLI_TYPE_MEDIUM_BLOB=> dibi::FIELD_BINARY,
MYSQLI_TYPE_LONG_BLOB => dibi::FIELD_BINARY,
MYSQLI_TYPE_BLOB => dibi::FIELD_BINARY,
2006-06-04 23:06:33 +00:00
);
$count = mysqli_num_fields($this->resource);
$this->meta = $this->convert = array();
for ($index = 0; $index < $count; $index++) {
$info = (array) mysqli_fetch_field_direct($this->resource, $index);
$native = $info['native'] = $info['type'];
if ($info['flags'] & MYSQLI_AUTO_INCREMENT_FLAG) // or 'primary_key' ?
2006-06-08 01:35:44 +00:00
$info['type'] = dibi::FIELD_COUNTER;
2006-06-04 23:06:33 +00:00
else {
2006-06-08 01:35:44 +00:00
$info['type'] = isset($types[$native]) ? $types[$native] : dibi::FIELD_UNKNOWN;
// if ($info['type'] == dibi::FIELD_TEXT && $info['length'] > 255)
// $info['type'] = dibi::FIELD_LONG_TEXT;
2006-06-04 23:06:33 +00:00
}
$this->meta[$info['name']] = $info;
$this->convert[$info['name']] = $info['type'];
}
}
} // class DibiMySqliResult