123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717 |
- <?php
- /**
- * Database connection wrapper. All database object instances are referenced
- * by a name. Queries are typically handled by [Database_Query], rather than
- * using the database object directly.
- *
- * @package Fuel/Database
- * @category Base
- * @author Kohana Team
- * @copyright (c) 2008-2010 Kohana Team
- * @license http://kohanaphp.com/license
- */
- namespace Fuel\Core;
- abstract class Database_Connection
- {
- /**
- * @var string default instance name
- */
- public static $default = 'default';
- /**
- * @var array Database instances
- */
- public static $instances = array();
- /**
- * Get a singleton Database instance. If configuration is not specified,
- * it will be loaded from the database configuration file using the same
- * group as the name.
- *
- * // Load the default database
- * $db = static::instance();
- *
- * // Create a custom configured instance
- * $db = static::instance('custom', $config);
- *
- * @param string instance name
- * @param array configuration parameters
- * @return Database_Connection
- */
- public static function instance($name = null, array $config = null)
- {
- \Config::load('db', true);
- if ($name === null)
- {
- // Use the default instance name
- $name = \Config::get('db.active');
- }
- if ( ! isset(static::$instances[$name]))
- {
- if ($config === null)
- {
- // Load the configuration for this database
- $config = \Config::get("db.{$name}");
- }
- if ( ! isset($config['type']))
- {
- throw new \FuelException("Database type not defined in {$name} configuration");
- }
- // Set the driver class name
- $driver = '\\Database_' . ucfirst($config['type']) . '_Connection';
- // Create the database connection instance
- new $driver($name, $config);
- }
- return static::$instances[$name];
- }
- /**
- * @var string the last query executed
- */
- public $last_query;
- /**
- * @var string Character that is used to quote identifiers
- */
- protected $_identifier = '"';
- /**
- * @var string Instance name
- */
- protected $_instance;
- /**
- * @var resource Raw server connection
- */
- protected $_connection;
- /**
- * @var array Configuration array
- */
- protected $_config;
- /**
- * Stores the database configuration locally and name the instance.
- *
- * [!!] This method cannot be accessed directly, you must use [static::instance].
- *
- * @return void
- */
- protected function __construct($name, array $config)
- {
- // Set the instance name
- $this->_instance = $name;
- // Store the config locally
- $this->_config = $config;
- // Store the database instance
- static::$instances[$name] = $this;
- }
- /**
- * Disconnect from the database when the object is destroyed.
- *
- * // Destroy the database instance
- * unset(static::instances[(string) $db], $db);
- *
- * [!!] Calling `unset($db)` is not enough to destroy the database, as it
- * will still be stored in `static::$instances`.
- *
- * @return void
- */
- final public function __destruct()
- {
- $this->disconnect();
- }
- /**
- * Returns the database instance name.
- *
- * echo (string) $db;
- *
- * @return string
- */
- final public function __toString()
- {
- return $this->_instance;
- }
- /**
- * Connect to the database. This is called automatically when the first
- * query is executed.
- *
- * $db->connect();
- *
- * @throws Database_Exception
- * @return void
- */
- abstract public function connect();
- /**
- * Disconnect from the database. This is called automatically by [static::__destruct].
- *
- * $db->disconnect();
- *
- * @return boolean
- */
- abstract public function disconnect();
- /**
- * Set the connection character set. This is called automatically by [static::connect].
- *
- * $db->set_charset('utf8');
- *
- * @throws Database_Exception
- * @param string character set name
- * @return void
- */
- abstract public function set_charset($charset);
- /**
- * Perform an SQL query of the given type.
- *
- * // Make a SELECT query and use objects for results
- * $db->query(static::SELECT, 'SELECT * FROM groups', true);
- *
- * // Make a SELECT query and use "Model_User" for the results
- * $db->query(static::SELECT, 'SELECT * FROM users LIMIT 1', 'Model_User');
- *
- * @param integer static::SELECT, static::INSERT, etc
- * @param string SQL query
- * @param mixed result object class, true for stdClass, false for assoc array
- * @return object Database_Result for SELECT queries
- * @return array list (insert id, row count) for INSERT queries
- * @return integer number of affected rows for all other queries
- */
- abstract public function query($type, $sql, $as_object);
- /**
- * Count the number of records in the last query, without LIMIT or OFFSET applied.
- *
- * // Get the total number of records that match the last query
- * $count = $db->count_last_query();
- *
- * @return integer
- */
- public function count_last_query()
- {
- if ($sql = $this->last_query)
- {
- $sql = trim($sql);
- if (stripos($sql, 'SELECT') !== 0)
- {
- return false;
- }
- if (stripos($sql, 'LIMIT') !== false)
- {
- // Remove LIMIT from the SQL
- $sql = preg_replace('/\sLIMIT\s+[^a-z]+/i', ' ', $sql);
- }
- if (stripos($sql, 'OFFSET') !== false)
- {
- // Remove OFFSET from the SQL
- $sql = preg_replace('/\sOFFSET\s+\d+/i', '', $sql);
- }
- // Get the total rows from the last query executed
- $result = $this->query
- (
- \DB::SELECT,
- 'SELECT COUNT(*) AS '.$this->quote_identifier('total_rows').' '.
- 'FROM ('.$sql.') AS '.$this->quote_table('counted_results'),
- true
- );
- // Return the total number of rows from the query
- return (int) $result->current()->total_rows;
- }
- return false;
- }
- /**
- * Per connection cache controlle setter/getter
- *
- * @param bool $bool wether to enable it [optional]
- * @return mixed cache boolean when getting, current instance when setting.
- */
- public function caching($bool = null)
- {
- if (is_bool($bool))
- {
- $this->_config['enable_cache'] = $bool;
- return $this;
- }
- return \Arr::get($this->_config, 'enable_cache', true);
- }
- /**
- * Count the number of records in a table.
- *
- * // Get the total number of records in the "users" table
- * $count = $db->count_records('users');
- *
- * @param mixed table name string or array(query, alias)
- * @return integer
- */
- public function count_records($table)
- {
- // Quote the table name
- $table = $this->quote_table($table);
- return $this->query(\DB::SELECT, 'SELECT COUNT(*) AS total_row_count FROM '.$table, false)
- ->get('total_row_count');
- }
- /**
- * Returns a normalized array describing the SQL data type
- *
- * $db->datatype('char');
- *
- * @param string SQL data type
- * @return array
- */
- public function datatype($type)
- {
- static $types = array
- (
- // SQL-92
- 'bit' => array('type' => 'string', 'exact' => true),
- 'bit varying' => array('type' => 'string'),
- 'char' => array('type' => 'string', 'exact' => true),
- 'char varying' => array('type' => 'string'),
- 'character' => array('type' => 'string', 'exact' => true),
- 'character varying' => array('type' => 'string'),
- 'date' => array('type' => 'string'),
- 'dec' => array('type' => 'float', 'exact' => true),
- 'decimal' => array('type' => 'float', 'exact' => true),
- 'double precision' => array('type' => 'float'),
- 'float' => array('type' => 'float'),
- 'int' => array('type' => 'int', 'min' => '-2147483648', 'max' => '2147483647'),
- 'integer' => array('type' => 'int', 'min' => '-2147483648', 'max' => '2147483647'),
- 'interval' => array('type' => 'string'),
- 'national char' => array('type' => 'string', 'exact' => true),
- 'national char varying' => array('type' => 'string'),
- 'national character' => array('type' => 'string', 'exact' => true),
- 'national character varying' => array('type' => 'string'),
- 'nchar' => array('type' => 'string', 'exact' => true),
- 'nchar varying' => array('type' => 'string'),
- 'numeric' => array('type' => 'float', 'exact' => true),
- 'real' => array('type' => 'float'),
- 'smallint' => array('type' => 'int', 'min' => '-32768', 'max' => '32767'),
- 'time' => array('type' => 'string'),
- 'time with time zone' => array('type' => 'string'),
- 'timestamp' => array('type' => 'string'),
- 'timestamp with time zone' => array('type' => 'string'),
- 'varchar' => array('type' => 'string'),
- // SQL:1999
- 'binary large object' => array('type' => 'string', 'binary' => true),
- 'blob' => array('type' => 'string', 'binary' => true),
- 'boolean' => array('type' => 'bool'),
- 'char large object' => array('type' => 'string'),
- 'character large object' => array('type' => 'string'),
- 'clob' => array('type' => 'string'),
- 'national character large object' => array('type' => 'string'),
- 'nchar large object' => array('type' => 'string'),
- 'nclob' => array('type' => 'string'),
- 'time without time zone' => array('type' => 'string'),
- 'timestamp without time zone' => array('type' => 'string'),
- // SQL:2003
- 'bigint' => array('type' => 'int', 'min' => '-9223372036854775808', 'max' => '9223372036854775807'),
- // SQL:2008
- 'binary' => array('type' => 'string', 'binary' => true, 'exact' => true),
- 'binary varying' => array('type' => 'string', 'binary' => true),
- 'varbinary' => array('type' => 'string', 'binary' => true),
- );
- if (isset($types[$type]))
- return $types[$type];
- return array();
- }
- /**
- * List all of the tables in the database. Optionally, a LIKE string can
- * be used to search for specific tables.
- *
- * // Get all tables in the current database
- * $tables = $db->list_tables();
- *
- * // Get all user-related tables
- * $tables = $db->list_tables('user%');
- *
- * @param string table to search for
- * @return array
- */
- abstract public function list_tables($like = null);
- /**
- * Lists all of the columns in a table. Optionally, a LIKE string can be
- * used to search for specific fields.
- *
- * // Get all columns from the "users" table
- * $columns = $db->list_columns('users');
- *
- * // Get all name-related columns
- * $columns = $db->list_columns('users', '%name%');
- *
- * @param string table to get columns from
- * @param string column to search for
- * @return array
- */
- abstract public function list_columns($table, $like = null);
- /**
- * Extracts the text between parentheses, if any.
- *
- * // Returns: array('CHAR', '6')
- * list($type, $length) = $db->_parse_type('CHAR(6)');
- *
- * @param string
- * @return array list containing the type and length, if any
- */
- protected function _parse_type($type)
- {
- if (($open = strpos($type, '(')) === false)
- {
- // No length specified
- return array($type, null);
- }
- // Closing parenthesis
- $close = strpos($type, ')', $open);
- // Length without parentheses
- $length = substr($type, $open + 1, $close - 1 - $open);
- // Type without the length
- $type = substr($type, 0, $open).substr($type, $close + 1);
- return array($type, $length);
- }
- /**
- * Return the table prefix defined in the current configuration.
- *
- * $prefix = $db->table_prefix();
- *
- * @return string
- */
- public function table_prefix($table = null)
- {
- if ($table !== null)
- {
- return $this->_config['table_prefix'] .$table;
- }
- return $this->_config['table_prefix'];
- }
- /**
- * Quote a value for an SQL query.
- *
- * $db->quote(null); // 'null'
- * $db->quote(10); // 10
- * $db->quote('fred'); // 'fred'
- *
- * Objects passed to this function will be converted to strings.
- * [Database_Expression] objects will use the value of the expression.
- * [Database_Query] objects will be compiled and converted to a sub-query.
- * All other objects will be converted using the `__toString` method.
- *
- * @param mixed any value to quote
- * @return string
- * @uses static::escape
- */
- public function quote($value)
- {
- if ($value === null)
- {
- return 'null';
- }
- elseif ($value === true)
- {
- return "'1'";
- }
- elseif ($value === false)
- {
- return "'0'";
- }
- elseif (is_object($value))
- {
- if ($value instanceof Database_Query)
- {
- // Create a sub-query
- return '('.$value->compile($this).')';
- }
- elseif ($value instanceof Database_Expression)
- {
- // Use a raw expression
- return $value->value();
- }
- else
- {
- // Convert the object to a string
- return $this->quote((string) $value);
- }
- }
- elseif (is_array($value))
- {
- return '('.implode(', ', array_map(array($this, __FUNCTION__), $value)).')';
- }
- elseif (is_int($value))
- {
- return (int) $value;
- }
- elseif (is_float($value))
- {
- // Convert to non-locale aware float to prevent possible commas
- return sprintf('%F', $value);
- }
- return $this->escape($value);
- }
- /**
- * Quote a database table name and adds the table prefix if needed.
- *
- * $table = $db->quote_table($table);
- *
- * @param mixed table name or array(table, alias)
- * @return string
- * @uses static::quote_identifier
- * @uses static::table_prefix
- */
- public function quote_table($value)
- {
- // Assign the table by reference from the value
- if (is_array($value))
- {
- $table =& $value[0];
- // Attach table prefix to alias
- $value[1] = $this->table_prefix().$value[1];
- }
- else
- {
- $table =& $value;
- }
- // deal with the sub-query objects first
- if ($table instanceof Database_Query)
- {
- // Create a sub-query
- $table = '('.$table->compile($this).')';
- }
- elseif (is_string($table))
- {
- if (strpos($table, '.') === false)
- {
- // Add the table prefix for tables
- $table = $this->quote_identifier($this->table_prefix().$table);
- }
- else
- {
- // Split the identifier into the individual parts
- $parts = explode('.', $table);
- if ($prefix = $this->table_prefix())
- {
- // Get the offset of the table name, 2nd-to-last part
- // This works for databases that can have 3 identifiers (Postgre)
- if (($offset = count($parts)) == 2)
- {
- $offset = 1;
- }
- else
- {
- $offset = $offset - 2;
- }
- // Add the table prefix to the table name
- $parts[$offset] = $prefix.$parts[$offset];
- }
- // Quote each of the parts
- $table = implode('.', array_map(array($this, 'quote_identifier'), $parts));
- }
- }
- // process the alias if present
- if (is_array($value))
- {
- // Separate the column and alias
- list ($value, $alias) = $value;
- return $value.' AS '.$this->quote_identifier($alias);
- }
- else
- {
- // return the value
- return $value;
- }
- }
- /**
- * Quote a database identifier, such as a column name. Adds the
- * table prefix to the identifier if a table name is present.
- *
- * $column = $db->quote_identifier($column);
- *
- * You can also use SQL methods within identifiers.
- *
- * // The value of "column" will be quoted
- * $column = $db->quote_identifier('COUNT("column")');
- *
- * Objects passed to this function will be converted to strings.
- * [Database_Expression] objects will use the value of the expression.
- * [Database_Query] objects will be compiled and converted to a sub-query.
- * All other objects will be converted using the `__toString` method.
- *
- * @param mixed any identifier
- * @return string
- * @uses static::table_prefix
- */
- public function quote_identifier($value)
- {
- if ($value === '*')
- {
- return $value;
- }
- elseif (is_object($value))
- {
- if ($value instanceof Database_Query)
- {
- // Create a sub-query
- return '('.$value->compile($this).')';
- }
- elseif ($value instanceof Database_Expression)
- {
- // Use a raw expression
- return $value->value();
- }
- else
- {
- // Convert the object to a string
- return $this->quote_identifier((string) $value);
- }
- }
- elseif (is_array($value))
- {
- // Separate the column and alias
- list ($value, $alias) = $value;
- return $this->quote_identifier($value).' AS '.$this->quote_identifier($alias);
- }
- if (strpos($value, '"') !== false)
- {
- // Quote the column in FUNC("ident") identifiers
- return preg_replace('/"(.+?)"/e', '$this->quote_identifier("$1")', $value);
- }
- elseif (preg_match("/^'(.*)?'$/", $value))
- {
- // return quoted values as-is
- return $value;
- }
- elseif (strpos($value, '.') !== false)
- {
- // Split the identifier into the individual parts
- $parts = explode('.', $value);
- if ($prefix = $this->table_prefix())
- {
- // Get the offset of the table name, 2nd-to-last part
- // This works for databases that can have 3 identifiers (Postgre)
- $offset = count($parts) - 2;
- // Add the table prefix to the table name
- $parts[$offset] = $prefix.$parts[$offset];
- }
- // Quote each of the parts
- return implode('.', array_map(array($this, __FUNCTION__), $parts));
- }
- else
- {
- return $this->_identifier.$value.$this->_identifier;
- }
- }
- /**
- * Sanitize a string by escaping characters that could cause an SQL
- * injection attack.
- *
- * $value = $db->escape('any string');
- *
- * @param string value to quote
- * @return string
- */
- abstract public function escape($value);
- /**
- * Whether or not the connection is in transaction mode
- *
- * $db->in_transaction();
- *
- * @return bool
- */
- abstract public function in_transaction();
- /**
- * Begins a transaction on instance
- *
- * $db->start_transaction();
- *
- * @return bool
- */
- abstract public function start_transaction();
- /**
- * Commits all pending transactional queries
- *
- * $db->commit_transaction();
- *
- * @return bool
- */
- abstract public function commit_transaction();
- /**
- * Rollsback all pending transactional queries
- *
- * $db->rollback_transaction();
- *
- * @return bool
- */
- abstract public function rollback_transaction();
- /**
- * Returns the raw connection object for custom method access
- *
- * $db->connection()->lastInsertId('id');
- *
- * @return resource
- */
- public function connection()
- {
- // Make sure the database is connected
- $this->_connection or $this->connect();
- return $this->_connection;
- }
- }
|