1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-25 03:12:50 +01:00
php-dibi/dibi/drivers/mysql.php

340 lines
9.1 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://texy.info/dibi/
* @copyright Copyright (c) 2005-2006 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 MySQL database
*
*/
class DibiMySqlDriver extends DibiDriver {
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(
'NULL' => "NULL",
'TRUE' => "1",
'FALSE' => "0",
'date' => "'Y-m-d'",
'datetime' => "'Y-m-d H:i:s'",
);
/**
* Driver factory
2006-11-13 06:32:16 +00:00
* @throw DibiException
2006-06-04 23:06:33 +00:00
*/
public static function connect($config)
{
if (!extension_loaded('mysql'))
2006-11-13 06:32:16 +00:00
throw new DibiException("PHP extension 'mysql' is not loaded");
2006-06-04 23:06:33 +00:00
2006-08-25 18:10:30 +00:00
foreach (array('username', 'password', 'protocol') as $var)
if (!isset($config[$var])) $config[$var] = NULL;
2006-06-04 23:06:33 +00:00
if (empty($config['host'])) $config['host'] = 'localhost';
2006-08-25 18:10:30 +00:00
if ($config['protocol'] === 'unix') // host can be socket
2006-06-04 23:06:33 +00:00
$host = ':' . $config['host'];
else
2006-08-25 18:10:30 +00:00
$host = $config['host'] . (empty($config['port']) ? '' : ':'.$config['port']);
2006-06-04 23:06:33 +00:00
// some errors aren't handled. Must use $php_errormsg
if (function_exists('ini_set'))
$save = ini_set('track_errors', TRUE);
$php_errormsg = '';
if (empty($config['persistent']))
2006-08-25 18:10:30 +00:00
$conn = @mysql_connect($host, $config['username'], $config['password']);
2006-06-04 23:06:33 +00:00
else
2006-08-25 18:10:30 +00:00
$conn = @mysql_pconnect($host, $config['username'], $config['password']);
2006-06-04 23:06:33 +00:00
if (function_exists('ini_set'))
ini_set('track_errors', $save);
if (!is_resource($conn))
2006-11-13 06:32:16 +00:00
throw new DibiException("Connecting error", array(
2006-06-04 23:06:33 +00:00
'message' => mysql_error() ? mysql_error() : $php_errormsg,
'code' => mysql_errno(),
));
if (!empty($config['charset'])) {
$succ = @mysql_query('SET CHARACTER SET '.$config['charset'], $conn);
// don't handle this error...
}
if (!empty($config['database'])) {
if (!@mysql_select_db($config['database'], $conn))
2006-11-13 06:32:16 +00:00
throw new DibiException("Connecting error", array(
2006-06-04 23:06:33 +00:00
'message' => mysql_error($conn),
'code' => mysql_errno($conn),
));
}
$obj = new self($config);
$obj->conn = $conn;
return $obj;
}
public function query($sql)
{
2006-06-04 23:09:53 +00:00
$this->insertId = $this->affectedRows = FALSE;
2006-06-04 23:06:33 +00:00
$res = @mysql_query($sql, $this->conn);
if (is_resource($res))
return new DibiMySqlResult($res);
if ($res === FALSE)
return new DibiException("Query error", array(
'message' => mysql_error($this->conn),
'code' => mysql_errno($this->conn),
'sql' => $sql,
));
2006-06-04 23:09:53 +00:00
$this->affectedRows = mysql_affected_rows($this->conn);
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
$this->insertId = mysql_insert_id($this->conn);
if ($this->insertId < 1) $this->insertId = FALSE;
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 mysql_query('BEGIN', $this->conn);
}
public function commit()
{
return mysql_query('COMMIT', $this->conn);
}
public function rollback()
{
return mysql_query('ROLLBACK', $this->conn);
}
public function escape($value, $appendQuotes = FALSE)
{
return $appendQuotes
? "'" . mysql_real_escape_string($value, $this->conn) . "'"
: mysql_real_escape_string($value, $this->conn);
}
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)
2006-06-04 23:06:33 +00:00
{
2006-09-23 07:55:11 +00:00
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
}
2006-09-23 07:55:11 +00:00
/* is this really needed?
public function getResource()
2006-06-04 23:06:33 +00:00
{
2006-09-23 07:55:11 +00:00
return $this->conn;
2006-06-04 23:06:33 +00:00
}
*/
} // DibiMySqlDriver
class DibiMySqlResult extends DibiResult
{
private
$resource,
$meta;
public function __construct($resource)
{
$this->resource = $resource;
}
public function rowCount()
{
return mysql_num_rows($this->resource);
}
protected function doFetch()
{
return mysql_fetch_assoc($this->resource);
}
public function seek($row)
{
return mysql_data_seek($this->resource, $row);
}
protected function free()
{
mysql_free_result($this->resource);
}
public function getFields()
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return array_keys($this->meta);
}
protected function detectTypes()
{
if ($this->meta === NULL)
$this->createMeta();
}
/** this is experimental */
public function getMetaData($field)
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
}
/** this is experimental */
private function createMeta()
{
static $types = array(
2006-06-08 01:35:44 +00:00
'ENUM' => dibi::FIELD_TEXT, // eventually dibi::FIELD_INTEGER
'SET' => dibi::FIELD_TEXT, // eventually dibi::FIELD_INTEGER
'CHAR' => dibi::FIELD_TEXT,
'VARCHAR' => dibi::FIELD_TEXT,
'STRING' => dibi::FIELD_TEXT,
'TINYTEXT' => dibi::FIELD_TEXT,
'TEXT' => dibi::FIELD_TEXT,
'MEDIUMTEXT'=> dibi::FIELD_TEXT,
'LONGTEXT' => dibi::FIELD_TEXT,
'BINARY' => dibi::FIELD_BINARY,
'VARBINARY' => dibi::FIELD_BINARY,
'TINYBLOB' => dibi::FIELD_BINARY,
'BLOB' => dibi::FIELD_BINARY,
'MEDIUMBLOB'=> dibi::FIELD_BINARY,
'LONGBLOB' => dibi::FIELD_BINARY,
'DATE' => dibi::FIELD_DATE,
'DATETIME' => dibi::FIELD_DATETIME,
'TIMESTAMP' => dibi::FIELD_DATETIME,
'TIME' => dibi::FIELD_DATETIME,
'BIT' => dibi::FIELD_BOOL,
'YEAR' => dibi::FIELD_INTEGER,
'TINYINT' => dibi::FIELD_INTEGER,
'SMALLINT' => dibi::FIELD_INTEGER,
'MEDIUMINT' => dibi::FIELD_INTEGER,
'INT' => dibi::FIELD_INTEGER,
'INTEGER' => dibi::FIELD_INTEGER,
'BIGINT' => dibi::FIELD_INTEGER,
'FLOAT' => dibi::FIELD_FLOAT,
'DOUBLE' => dibi::FIELD_FLOAT,
'REAL' => dibi::FIELD_FLOAT,
'DECIMAL' => dibi::FIELD_FLOAT,
'NUMERIC' => dibi::FIELD_FLOAT,
2006-06-04 23:06:33 +00:00
);
$count = mysql_num_fields($this->resource);
$this->meta = $this->convert = array();
for ($index = 0; $index < $count; $index++) {
$info['native'] = $native = strtoupper(mysql_field_type($this->resource, $index));
$info['flags'] = explode(' ', mysql_field_flags($this->resource, $index));
$info['length'] = mysql_field_len($this->resource, $index);
$info['table'] = mysql_field_table($this->resource, $index);
if (in_array('auto_increment', $info['flags'])) // 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;
2006-06-04 23:06:33 +00:00
2006-06-08 01:35:44 +00:00
// if ($info['type'] == dibi::FIELD_TEXT && $info['length'] > 255)
// $info['type'] = dibi::FIELD_LONG_TEXT;
2006-06-04 23:06:33 +00:00
}
$name = mysql_field_name($this->resource, $index);
$this->meta[$name] = $info;
$this->convert[$name] = $info['type'];
}
}
} // class DibiMySqlResult