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

315 lines
8.8 KiB
PHP
Raw Normal View History

2006-06-04 23:06:33 +00:00
<?php
/**
2007-04-11 18:30:30 +00:00
* This file is part of the "dibi" project (http://dibi.texy.info/)
2006-06-04 23:06:33 +00:00
*
2007-06-24 23:49:57 +00:00
* @author David Grudl
* @copyright Copyright (c) 2005-2007 David Grudl aka -dgx- (http://www.dgx.cz)
* @license New BSD License
* @version $Revision$ $Date$
* @category Database
* @package Dibi
2006-06-04 23:06:33 +00:00
*/
// security - include dibi.php, not this file
if (!class_exists('dibi', FALSE)) die();
2006-06-04 23:06:33 +00:00
/**
* The dibi driver for MySQL database
*
*/
class DibiMySqlDriver extends DibiDriver
{
2006-06-04 23:06:33 +00:00
private
2006-06-04 23:09:53 +00:00
$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'",
);
/**
* @param array connect configuration
2007-06-24 23:49:57 +00:00
* @throws DibiException
2006-06-04 23:06:33 +00:00
*/
public function __construct($config)
2006-06-04 23:06:33 +00:00
{
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
// default values
if (empty($config['username'])) $config['username'] = ini_get('mysql.default_user');
if (empty($config['password'])) $config['password'] = ini_get('mysql.default_password');
if (empty($config['host'])) {
$config['host'] = ini_get('mysql.default_host');
if (empty($config['port'])) ini_get('mysql.default_port');
if (empty($config['host'])) $config['host'] = 'localhost';
}
2006-06-04 23:06:33 +00:00
parent::__construct($config);
}
protected function connect()
{
$config = $this->config;
2007-04-25 06:55:10 +00:00
if (isset($config['protocol']) && $config['protocol'] === 'unix') // host can be socket
$host = ':' . $config['host'];
else
$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']))
2007-06-25 17:02:12 +00:00
$connection = @mysql_connect($host, $config['username'], $config['password'], TRUE);
2006-06-04 23:06:33 +00:00
else
2007-06-25 17:02:12 +00:00
$connection = @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);
2007-06-25 17:02:12 +00:00
if (!is_resource($connection))
throw new DibiException("Connecting error (driver mysql)'", array(
2006-06-04 23:06:33 +00:00
'message' => mysql_error() ? mysql_error() : $php_errormsg,
'code' => mysql_errno(),
));
if (!empty($config['charset'])) {
2007-06-25 17:02:12 +00:00
@mysql_query("SET NAMES '" . $config['charset'] . "'", $connection);
2006-06-04 23:06:33 +00:00
// don't handle this error...
}
if (!empty($config['database'])) {
2007-06-25 17:02:12 +00:00
if (!@mysql_select_db($config['database'], $connection))
throw new DibiException("Connecting error (driver mysql)", array(
2007-06-25 17:02:12 +00:00
'message' => mysql_error($connection),
'code' => mysql_errno($connection),
2006-06-04 23:06:33 +00:00
));
}
2007-06-25 17:02:12 +00:00
return $connection;
2006-06-04 23:06:33 +00:00
}
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;
2007-06-25 17:02:12 +00:00
$connection = $this->getConnection();
$res = @mysql_query($sql, $connection);
2006-06-04 23:06:33 +00:00
if ($res === FALSE) return FALSE;
2007-06-25 17:02:12 +00:00
$this->affectedRows = mysql_affected_rows($connection);
2006-06-04 23:09:53 +00:00
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
2007-06-25 17:02:12 +00:00
$this->insertId = mysql_insert_id($connection);
2006-06-04 23:09:53 +00:00
if ($this->insertId < 1) $this->insertId = FALSE;
2007-03-26 06:22:53 +00:00
if (is_resource($res))
return new DibiMySqlResult($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()
{
2007-06-25 17:02:12 +00:00
return mysql_query('BEGIN', $this->getConnection());
2006-06-04 23:06:33 +00:00
}
public function commit()
{
2007-06-25 17:02:12 +00:00
return mysql_query('COMMIT', $this->getConnection());
2006-06-04 23:06:33 +00:00
}
public function rollback()
{
2007-06-25 17:02:12 +00:00
return mysql_query('ROLLBACK', $this->getConnection());
2006-06-04 23:06:33 +00:00
}
public function errorInfo()
{
2007-06-25 17:02:12 +00:00
$connection = $this->getConnection();
return array(
2007-06-25 17:02:12 +00:00
'message' => mysql_error($connection),
'code' => mysql_errno($connection),
);
}
public function escape($value, $appendQuotes=TRUE)
2006-06-04 23:06:33 +00:00
{
2007-06-25 17:02:12 +00:00
$connection = $this->getConnection();
2006-06-04 23:06:33 +00:00
return $appendQuotes
2007-06-25 17:02:12 +00:00
? "'" . mysql_real_escape_string($value, $connection) . "'"
: mysql_real_escape_string($value, $connection);
2006-06-04 23:06:33 +00:00
}
public function delimite($value)
2006-06-04 23:06:33 +00:00
{
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
2006-06-04 23:06:33 +00:00
} // DibiMySqlDriver
class DibiMySqlResult extends DibiResult
{
private $resource;
2006-06-04 23:06:33 +00:00
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);
}
/** 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
'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
2007-05-30 00:01:10 +00:00
// if ($info['type'] === dibi::FIELD_TEXT && $info['length'] > 255)
2006-06-08 01:35:44 +00:00
// $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