Upgraded to KH 3.3.0
This commit is contained in:
12
includes/kohana/modules/database/classes/Config/Database.php
Normal file
12
includes/kohana/modules/database/classes/Config/Database.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
/**
|
||||
* Transparent extension for the Kohana_Config_Database class
|
||||
*
|
||||
* @package Kohana/Database
|
||||
* @category Configuration
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Config_Database extends Kohana_Config_Database {}
|
@@ -0,0 +1,15 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
/**
|
||||
* Transparent extension of the Kohana_Config_Database_Reader class
|
||||
*
|
||||
* @package Kohana/Database
|
||||
* @category Configuration
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Config_Database_Reader extends Kohana_Config_Database_Reader
|
||||
{
|
||||
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
/**
|
||||
* Transparent extension for the Kohana_Config_Database_Writer class
|
||||
*
|
||||
* @package Kohana/Database
|
||||
* @category Configuration
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Config_Database_Writer extends Kohana_Config_Database_Writer
|
||||
{
|
||||
|
||||
}
|
3
includes/kohana/modules/database/classes/DB.php
Normal file
3
includes/kohana/modules/database/classes/DB.php
Normal file
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class DB extends Kohana_DB {}
|
3
includes/kohana/modules/database/classes/Database.php
Normal file
3
includes/kohana/modules/database/classes/Database.php
Normal file
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
abstract class Database extends Kohana_Database {}
|
@@ -1,3 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Database_Exception extends Kohana_Database_Exception {}
|
@@ -1,3 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Database_Expression extends Kohana_Database_Expression {}
|
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Database_MySQL extends Kohana_Database_MySQL {}
|
@@ -1,3 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Database_MySQL_Result extends Kohana_Database_MySQL_Result {}
|
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Database_PDO extends Kohana_Database_PDO {}
|
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Database_Query extends Kohana_Database_Query {}
|
@@ -1,3 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
abstract class Database_Query_Builder extends Kohana_Database_Query_Builder {}
|
@@ -1,3 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Database_Query_Builder_Delete extends Kohana_Database_Query_Builder_Delete {}
|
@@ -1,3 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Database_Query_Builder_Insert extends Kohana_Database_Query_Builder_Insert {}
|
@@ -1,3 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Database_Query_Builder_Join extends Kohana_Database_Query_Builder_Join {}
|
@@ -1,3 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Database_Query_Builder_Select extends Kohana_Database_Query_Builder_Select {}
|
@@ -1,3 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Database_Query_Builder_Update extends Kohana_Database_Query_Builder_Update {}
|
@@ -1,3 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
abstract class Database_Query_Builder_Where extends Kohana_Database_Query_Builder_Where {}
|
@@ -1,3 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
abstract class Database_Result extends Kohana_Database_Result {}
|
@@ -1,3 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Database_Result_Cached extends Kohana_Database_Result_Cached {}
|
@@ -0,0 +1,15 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
/**
|
||||
* Backwards compatibility extension for the database writer.
|
||||
*
|
||||
* @package Kohana/Database
|
||||
* @category Configuration
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Kohana_Config_Database extends Kohana_Config_Database_Writer
|
||||
{
|
||||
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
/**
|
||||
* Database reader for the kohana config system
|
||||
*
|
||||
* @package Kohana/Database
|
||||
* @category Configuration
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Kohana_Config_Database_Reader implements Kohana_Config_Reader
|
||||
{
|
||||
protected $_db_instance;
|
||||
|
||||
protected $_table_name = 'config';
|
||||
|
||||
/**
|
||||
* Constructs the database reader object
|
||||
*
|
||||
* @param array Configuration for the reader
|
||||
*/
|
||||
public function __construct(array $config = NULL)
|
||||
{
|
||||
if (isset($config['instance']))
|
||||
{
|
||||
$this->_db_instance = $config['instance'];
|
||||
}
|
||||
elseif ($this->_db_instance === NULL)
|
||||
{
|
||||
$this->_db_instance = Database::$default;
|
||||
}
|
||||
|
||||
if (isset($config['table_name']))
|
||||
{
|
||||
$this->_table_name = $config['table_name'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load the specificed configuration group
|
||||
*
|
||||
* Returns FALSE if group does not exist or an array if it does
|
||||
*
|
||||
* @param string $group Configuration group
|
||||
* @return boolean|array
|
||||
*/
|
||||
public function load($group)
|
||||
{
|
||||
/**
|
||||
* Prevents the catch-22 scenario where the database config reader attempts to load the
|
||||
* database connections details from the database.
|
||||
*
|
||||
* @link http://dev.kohanaframework.org/issues/4316
|
||||
*/
|
||||
if ($group === 'database')
|
||||
return FALSE;
|
||||
|
||||
$query = DB::select('config_key', 'config_value')
|
||||
->from($this->_table_name)
|
||||
->where('group_name', '=', $group)
|
||||
->execute($this->_db_instance);
|
||||
|
||||
return count($query) ? array_map('unserialize', $query->as_array('config_key', 'config_value')) : FALSE;
|
||||
}
|
||||
}
|
@@ -0,0 +1,110 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
/**
|
||||
* Database writer for the config system
|
||||
*
|
||||
* @package Kohana
|
||||
* @category Configuration
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2007-2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Kohana_Config_Database_Writer extends Config_Database_Reader implements Kohana_Config_Writer
|
||||
{
|
||||
protected $_loaded_keys = array();
|
||||
|
||||
/**
|
||||
* Tries to load the specificed configuration group
|
||||
*
|
||||
* Returns FALSE if group does not exist or an array if it does
|
||||
*
|
||||
* @param string $group Configuration group
|
||||
* @return boolean|array
|
||||
*/
|
||||
public function load($group)
|
||||
{
|
||||
$config = parent::load($group);
|
||||
|
||||
if ($config !== FALSE)
|
||||
{
|
||||
$this->_loaded_keys[$group] = array_combine(array_keys($config), array_keys($config));
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the passed config for $group
|
||||
*
|
||||
* Returns chainable instance on success or throws
|
||||
* Kohana_Config_Exception on failure
|
||||
*
|
||||
* @param string $group The config group
|
||||
* @param string $key The config key to write to
|
||||
* @param array $config The configuration to write
|
||||
* @return boolean
|
||||
*/
|
||||
public function write($group, $key, $config)
|
||||
{
|
||||
$config = serialize($config);
|
||||
|
||||
// Check to see if we've loaded the config from the table already
|
||||
if (isset($this->_loaded_keys[$group][$key]))
|
||||
{
|
||||
$this->_update($group, $key, $config);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Attempt to run an insert query
|
||||
// This may fail if the config key already exists in the table
|
||||
// and we don't know about it
|
||||
try
|
||||
{
|
||||
$this->_insert($group, $key, $config);
|
||||
}
|
||||
catch (Database_Exception $e)
|
||||
{
|
||||
// Attempt to run an update instead
|
||||
$this->_update($group, $key, $config);
|
||||
}
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the config values into the table
|
||||
*
|
||||
* @param string $group The config group
|
||||
* @param string $key The config key to write to
|
||||
* @param array $config The serialized configuration to write
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _insert($group, $key, $config)
|
||||
{
|
||||
DB::insert($this->_table_name, array('group_name', 'config_key', 'config_value'))
|
||||
->values(array($group, $key, $config))
|
||||
->execute($this->_db_instance);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the config values in the table
|
||||
*
|
||||
* @param string $group The config group
|
||||
* @param string $key The config key to write to
|
||||
* @param array $config The serialized configuration to write
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _update($group, $key, $config)
|
||||
{
|
||||
DB::update($this->_table_name)
|
||||
->set(array('config_value' => $config))
|
||||
->where('group_name', '=', $group)
|
||||
->where('config_key', '=', $key)
|
||||
->execute($this->_db_instance);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Provides a shortcut to get Database related objects for [making queries](../database/query).
|
||||
*
|
||||
@@ -35,8 +35,8 @@ class Kohana_DB {
|
||||
* `Database::INSERT` queries will return the insert id and number of rows.
|
||||
* For all other queries, the number of affected rows is returned.
|
||||
*
|
||||
* @param integer type: Database::SELECT, Database::UPDATE, etc
|
||||
* @param string SQL statement
|
||||
* @param integer $type type: Database::SELECT, Database::UPDATE, etc
|
||||
* @param string $sql SQL statement
|
||||
* @return Database_Query
|
||||
*/
|
||||
public static function query($type, $sql)
|
||||
@@ -54,8 +54,7 @@ class Kohana_DB {
|
||||
* // SELECT id AS user_id
|
||||
* $query = DB::select(array('id', 'user_id'));
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param ...
|
||||
* @param mixed $columns column name or array($column, $alias) or object
|
||||
* @return Database_Query_Builder_Select
|
||||
*/
|
||||
public static function select($columns = NULL)
|
||||
@@ -69,7 +68,7 @@ class Kohana_DB {
|
||||
* // SELECT id, username
|
||||
* $query = DB::select_array(array('id', 'username'));
|
||||
*
|
||||
* @param array columns to select
|
||||
* @param array $columns columns to select
|
||||
* @return Database_Query_Builder_Select
|
||||
*/
|
||||
public static function select_array(array $columns = NULL)
|
||||
@@ -83,8 +82,8 @@ class Kohana_DB {
|
||||
* // INSERT INTO users (id, username)
|
||||
* $query = DB::insert('users', array('id', 'username'));
|
||||
*
|
||||
* @param string table to insert into
|
||||
* @param array list of column names or array($column, $alias) or object
|
||||
* @param string $table table to insert into
|
||||
* @param array $columns list of column names or array($column, $alias) or object
|
||||
* @return Database_Query_Builder_Insert
|
||||
*/
|
||||
public static function insert($table = NULL, array $columns = NULL)
|
||||
@@ -98,7 +97,7 @@ class Kohana_DB {
|
||||
* // UPDATE users
|
||||
* $query = DB::update('users');
|
||||
*
|
||||
* @param string table to update
|
||||
* @param string $table table to update
|
||||
* @return Database_Query_Builder_Update
|
||||
*/
|
||||
public static function update($table = NULL)
|
||||
@@ -112,7 +111,7 @@ class Kohana_DB {
|
||||
* // DELETE FROM users
|
||||
* $query = DB::delete('users');
|
||||
*
|
||||
* @param string table to delete from
|
||||
* @param string $table table to delete from
|
||||
* @return Database_Query_Builder_Delete
|
||||
*/
|
||||
public static function delete($table = NULL)
|
||||
@@ -128,12 +127,13 @@ class Kohana_DB {
|
||||
* $query = DB::update('users')->set(array('login_count' => DB::expr('login_count + 1')))->where('id', '=', $id);
|
||||
* $users = ORM::factory('user')->where(DB::expr("BINARY `hash`"), '=', $hash)->find();
|
||||
*
|
||||
* @param string expression
|
||||
* @param string $string expression
|
||||
* @param array parameters
|
||||
* @return Database_Expression
|
||||
*/
|
||||
public static function expr($string)
|
||||
public static function expr($string, $parameters = array())
|
||||
{
|
||||
return new Database_Expression($string);
|
||||
return new Database_Expression($string, $parameters);
|
||||
}
|
||||
|
||||
} // End DB
|
@@ -1,9 +1,9 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database connection wrapper/helper.
|
||||
*
|
||||
* You may get a database instance using `Database::instance('name')` where
|
||||
* name is the [config](database/config) group.
|
||||
* name is the [config](database/config) group.
|
||||
*
|
||||
* This class provides connection instance management via Database Drivers, as
|
||||
* well as quoting, escaping and other related functions. Querys are done using
|
||||
@@ -13,7 +13,7 @@
|
||||
* @package Kohana/Database
|
||||
* @category Base
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2008-2010 Kohana Team
|
||||
* @copyright (c) 2008-2012 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
abstract class Kohana_Database {
|
||||
@@ -45,8 +45,8 @@ abstract class Kohana_Database {
|
||||
* // Create a custom configured instance
|
||||
* $db = Database::instance('custom', $config);
|
||||
*
|
||||
* @param string instance name
|
||||
* @param array configuration parameters
|
||||
* @param string $name instance name
|
||||
* @param array $config configuration parameters
|
||||
* @return Database
|
||||
*/
|
||||
public static function instance($name = NULL, array $config = NULL)
|
||||
@@ -62,7 +62,7 @@ abstract class Kohana_Database {
|
||||
if ($config === NULL)
|
||||
{
|
||||
// Load the configuration for this database
|
||||
$config = Kohana::config('database')->$name;
|
||||
$config = Kohana::$config->load('database')->$name;
|
||||
}
|
||||
|
||||
if ( ! isset($config['type']))
|
||||
@@ -75,7 +75,10 @@ abstract class Kohana_Database {
|
||||
$driver = 'Database_'.ucfirst($config['type']);
|
||||
|
||||
// Create the database connection instance
|
||||
new $driver($name, $config);
|
||||
$driver = new $driver($name, $config);
|
||||
|
||||
// Store the database instance
|
||||
Database::$instances[$name] = $driver;
|
||||
}
|
||||
|
||||
return Database::$instances[$name];
|
||||
@@ -105,7 +108,7 @@ abstract class Kohana_Database {
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function __construct($name, array $config)
|
||||
public function __construct($name, array $config)
|
||||
{
|
||||
// Set the instance name
|
||||
$this->_instance = $name;
|
||||
@@ -113,8 +116,10 @@ abstract class Kohana_Database {
|
||||
// Store the config locally
|
||||
$this->_config = $config;
|
||||
|
||||
// Store the database instance
|
||||
Database::$instances[$name] = $this;
|
||||
if (empty($this->_config['table_prefix']))
|
||||
{
|
||||
$this->_config['table_prefix'] = '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,7 +133,7 @@ abstract class Kohana_Database {
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
final public function __destruct()
|
||||
public function __destruct()
|
||||
{
|
||||
$this->disconnect();
|
||||
}
|
||||
@@ -140,7 +145,7 @@ abstract class Kohana_Database {
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
final public function __toString()
|
||||
public function __toString()
|
||||
{
|
||||
return $this->_instance;
|
||||
}
|
||||
@@ -177,7 +182,7 @@ abstract class Kohana_Database {
|
||||
* $db->set_charset('utf8');
|
||||
*
|
||||
* @throws Database_Exception
|
||||
* @param string character set name
|
||||
* @param string $charset character set name
|
||||
* @return void
|
||||
*/
|
||||
abstract public function set_charset($charset);
|
||||
@@ -191,10 +196,10 @@ abstract class Kohana_Database {
|
||||
* // Make a SELECT query and use "Model_User" for the results
|
||||
* $db->query(Database::SELECT, 'SELECT * FROM users LIMIT 1', 'Model_User');
|
||||
*
|
||||
* @param integer Database::SELECT, Database::INSERT, etc
|
||||
* @param string SQL query
|
||||
* @param mixed result object class string, TRUE for stdClass, FALSE for assoc array
|
||||
* @param array object construct parameters for result class
|
||||
* @param integer $type Database::SELECT, Database::INSERT, etc
|
||||
* @param string $sql SQL query
|
||||
* @param mixed $as_object result object class string, TRUE for stdClass, FALSE for assoc array
|
||||
* @param array $params object construct parameters for result class
|
||||
* @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
|
||||
@@ -219,7 +224,7 @@ abstract class Kohana_Database {
|
||||
* $db->rollback();
|
||||
* }
|
||||
*
|
||||
* @param string transaction mode
|
||||
* @param string $mode transaction mode
|
||||
* @return boolean
|
||||
*/
|
||||
abstract public function begin($mode = NULL);
|
||||
@@ -250,7 +255,7 @@ abstract class Kohana_Database {
|
||||
* // Get the total number of records in the "users" table
|
||||
* $count = $db->count_records('users');
|
||||
*
|
||||
* @param mixed table name string or array(query, alias)
|
||||
* @param mixed $table table name string or array(query, alias)
|
||||
* @return integer
|
||||
*/
|
||||
public function count_records($table)
|
||||
@@ -267,7 +272,7 @@ abstract class Kohana_Database {
|
||||
*
|
||||
* $db->datatype('char');
|
||||
*
|
||||
* @param string SQL data type
|
||||
* @param string $type SQL data type
|
||||
* @return array
|
||||
*/
|
||||
public function datatype($type)
|
||||
@@ -342,7 +347,7 @@ abstract class Kohana_Database {
|
||||
* // Get all user-related tables
|
||||
* $tables = $db->list_tables('user%');
|
||||
*
|
||||
* @param string table to search for
|
||||
* @param string $like table to search for
|
||||
* @return array
|
||||
*/
|
||||
abstract public function list_tables($like = NULL);
|
||||
@@ -360,9 +365,9 @@ abstract class Kohana_Database {
|
||||
* // Get the columns from a table that doesn't use the table prefix
|
||||
* $columns = $db->list_columns('users', NULL, FALSE);
|
||||
*
|
||||
* @param string table to get columns from
|
||||
* @param string column to search for
|
||||
* @param boolean whether to add the table prefix automatically or not
|
||||
* @param string $table table to get columns from
|
||||
* @param string $like column to search for
|
||||
* @param boolean $add_prefix whether to add the table prefix automatically or not
|
||||
* @return array
|
||||
*/
|
||||
abstract public function list_columns($table, $like = NULL, $add_prefix = TRUE);
|
||||
@@ -373,7 +378,7 @@ abstract class Kohana_Database {
|
||||
* // Returns: array('CHAR', '6')
|
||||
* list($type, $length) = $db->_parse_type('CHAR(6)');
|
||||
*
|
||||
* @param string
|
||||
* @param string $type
|
||||
* @return array list containing the type and length, if any
|
||||
*/
|
||||
protected function _parse_type($type)
|
||||
@@ -385,7 +390,7 @@ abstract class Kohana_Database {
|
||||
}
|
||||
|
||||
// Closing parenthesis
|
||||
$close = strpos($type, ')', $open);
|
||||
$close = strrpos($type, ')', $open);
|
||||
|
||||
// Length without parentheses
|
||||
$length = substr($type, $open + 1, $close - 1 - $open);
|
||||
@@ -416,11 +421,11 @@ abstract class Kohana_Database {
|
||||
* $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_Expression] objects will be compiled.
|
||||
* [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
|
||||
* @param mixed $value any value to quote
|
||||
* @return string
|
||||
* @uses Database::escape
|
||||
*/
|
||||
@@ -447,8 +452,8 @@ abstract class Kohana_Database {
|
||||
}
|
||||
elseif ($value instanceof Database_Expression)
|
||||
{
|
||||
// Use a raw expression
|
||||
return $value->value();
|
||||
// Compile the expression
|
||||
return $value->compile($this);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -480,19 +485,27 @@ abstract class Kohana_Database {
|
||||
*
|
||||
* You can also use SQL methods within identifiers.
|
||||
*
|
||||
* // The value of "column" will be quoted
|
||||
* $column = $db->quote_column('COUNT("column")');
|
||||
* $column = $db->quote_column(DB::expr('COUNT(`column`)'));
|
||||
*
|
||||
* @param mixed column name or array(column, alias)
|
||||
* Objects passed to this function will be converted to strings.
|
||||
* [Database_Expression] objects will be compiled.
|
||||
* [Database_Query] objects will be compiled and converted to a sub-query.
|
||||
* All other objects will be converted using the `__toString` method.
|
||||
*
|
||||
* @param mixed $column column name or array(column, alias)
|
||||
* @return string
|
||||
* @uses Database::quote_identifier
|
||||
* @uses Database::table_prefix
|
||||
*/
|
||||
public function quote_column($column)
|
||||
{
|
||||
// Identifiers are escaped by repeating them
|
||||
$escaped_identifier = $this->_identifier.$this->_identifier;
|
||||
|
||||
if (is_array($column))
|
||||
{
|
||||
list($column, $alias) = $column;
|
||||
$alias = str_replace($this->_identifier, $escaped_identifier, $alias);
|
||||
}
|
||||
|
||||
if ($column instanceof Database_Query)
|
||||
@@ -502,23 +515,20 @@ abstract class Kohana_Database {
|
||||
}
|
||||
elseif ($column instanceof Database_Expression)
|
||||
{
|
||||
// Use a raw expression
|
||||
$column = $column->value();
|
||||
// Compile the expression
|
||||
$column = $column->compile($this);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Convert to a string
|
||||
$column = (string) $column;
|
||||
|
||||
$column = str_replace($this->_identifier, $escaped_identifier, $column);
|
||||
|
||||
if ($column === '*')
|
||||
{
|
||||
return $column;
|
||||
}
|
||||
elseif (strpos($column, '"') !== FALSE)
|
||||
{
|
||||
// Quote the column in FUNC("column") identifiers
|
||||
$column = preg_replace('/"(.+?)"/e', '$this->quote_column("$1")', $column);
|
||||
}
|
||||
elseif (strpos($column, '.') !== FALSE)
|
||||
{
|
||||
$parts = explode('.', $column);
|
||||
@@ -562,16 +572,25 @@ abstract class Kohana_Database {
|
||||
*
|
||||
* $table = $db->quote_table($table);
|
||||
*
|
||||
* @param mixed table name or array(table, alias)
|
||||
* Objects passed to this function will be converted to strings.
|
||||
* [Database_Expression] objects will be compiled.
|
||||
* [Database_Query] objects will be compiled and converted to a sub-query.
|
||||
* All other objects will be converted using the `__toString` method.
|
||||
*
|
||||
* @param mixed $table table name or array(table, alias)
|
||||
* @return string
|
||||
* @uses Database::quote_identifier
|
||||
* @uses Database::table_prefix
|
||||
*/
|
||||
public function quote_table($table)
|
||||
{
|
||||
// Identifiers are escaped by repeating them
|
||||
$escaped_identifier = $this->_identifier.$this->_identifier;
|
||||
|
||||
if (is_array($table))
|
||||
{
|
||||
list($table, $alias) = $table;
|
||||
$alias = str_replace($this->_identifier, $escaped_identifier, $alias);
|
||||
}
|
||||
|
||||
if ($table instanceof Database_Query)
|
||||
@@ -581,14 +600,16 @@ abstract class Kohana_Database {
|
||||
}
|
||||
elseif ($table instanceof Database_Expression)
|
||||
{
|
||||
// Use a raw expression
|
||||
$table = $table->value();
|
||||
// Compile the expression
|
||||
$table = $table->compile($this);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Convert to a string
|
||||
$table = (string) $table;
|
||||
|
||||
$table = str_replace($this->_identifier, $escaped_identifier, $table);
|
||||
|
||||
if (strpos($table, '.') !== FALSE)
|
||||
{
|
||||
$parts = explode('.', $table);
|
||||
@@ -630,18 +651,22 @@ abstract class Kohana_Database {
|
||||
* Quote a database identifier
|
||||
*
|
||||
* Objects passed to this function will be converted to strings.
|
||||
* [Database_Expression] objects will use the value of the expression.
|
||||
* [Database_Expression] objects will be compiled.
|
||||
* [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
|
||||
* @param mixed $value any identifier
|
||||
* @return string
|
||||
*/
|
||||
public function quote_identifier($value)
|
||||
{
|
||||
// Identifiers are escaped by repeating them
|
||||
$escaped_identifier = $this->_identifier.$this->_identifier;
|
||||
|
||||
if (is_array($value))
|
||||
{
|
||||
list($value, $alias) = $value;
|
||||
$alias = str_replace($this->_identifier, $escaped_identifier, $alias);
|
||||
}
|
||||
|
||||
if ($value instanceof Database_Query)
|
||||
@@ -651,14 +676,16 @@ abstract class Kohana_Database {
|
||||
}
|
||||
elseif ($value instanceof Database_Expression)
|
||||
{
|
||||
// Use a raw expression
|
||||
$value = $value->value();
|
||||
// Compile the expression
|
||||
$value = $value->compile($this);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Convert to a string
|
||||
$value = (string) $value;
|
||||
|
||||
$value = str_replace($this->_identifier, $escaped_identifier, $value);
|
||||
|
||||
if (strpos($value, '.') !== FALSE)
|
||||
{
|
||||
$parts = explode('.', $value);
|
||||
@@ -691,7 +718,7 @@ abstract class Kohana_Database {
|
||||
*
|
||||
* $value = $db->escape('any string');
|
||||
*
|
||||
* @param string value to quote
|
||||
* @param string $value value to quote
|
||||
* @return string
|
||||
*/
|
||||
abstract public function escape($value);
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database exceptions.
|
||||
*
|
@@ -0,0 +1,138 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database expressions can be used to add unescaped SQL fragments to a
|
||||
* [Database_Query_Builder] object.
|
||||
*
|
||||
* For example, you can use an expression to generate a column alias:
|
||||
*
|
||||
* // SELECT CONCAT(first_name, last_name) AS full_name
|
||||
* $query = DB::select(array(DB::expr('CONCAT(first_name, last_name)'), 'full_name')));
|
||||
*
|
||||
* More examples are available on the [Query Builder](database/query/builder#database-expressions) page
|
||||
*
|
||||
* @package Kohana/Database
|
||||
* @category Base
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
class Kohana_Database_Expression {
|
||||
|
||||
// Unquoted parameters
|
||||
protected $_parameters;
|
||||
|
||||
// Raw expression string
|
||||
protected $_value;
|
||||
|
||||
/**
|
||||
* Sets the expression string.
|
||||
*
|
||||
* $expression = new Database_Expression('COUNT(users.id)');
|
||||
*
|
||||
* @param string $value raw SQL expression string
|
||||
* @param array $parameters unquoted parameter values
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($value, $parameters = array())
|
||||
{
|
||||
// Set the expression string
|
||||
$this->_value = $value;
|
||||
$this->_parameters = $parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind a variable to a parameter.
|
||||
*
|
||||
* @param string $param parameter key to replace
|
||||
* @param mixed $var variable to use
|
||||
* @return $this
|
||||
*/
|
||||
public function bind($param, & $var)
|
||||
{
|
||||
$this->_parameters[$param] =& $var;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of a parameter.
|
||||
*
|
||||
* @param string $param parameter key to replace
|
||||
* @param mixed $value value to use
|
||||
* @return $this
|
||||
*/
|
||||
public function param($param, $value)
|
||||
{
|
||||
$this->_parameters[$param] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add multiple parameter values.
|
||||
*
|
||||
* @param array $params list of parameter values
|
||||
* @return $this
|
||||
*/
|
||||
public function parameters(array $params)
|
||||
{
|
||||
$this->_parameters = $params + $this->_parameters;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the expression value as a string.
|
||||
*
|
||||
* $sql = $expression->value();
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function value()
|
||||
{
|
||||
return (string) $this->_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value of the expression as a string.
|
||||
*
|
||||
* echo $expression;
|
||||
*
|
||||
* @return string
|
||||
* @uses Database_Expression::value
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile the SQL expression and return it. Replaces any parameters with
|
||||
* their given values.
|
||||
*
|
||||
* @param mixed Database instance or name of instance
|
||||
* @return string
|
||||
*/
|
||||
public function compile($db = NULL)
|
||||
{
|
||||
if ( ! is_object($db))
|
||||
{
|
||||
// Get the database instance
|
||||
$db = Database::instance($db);
|
||||
}
|
||||
|
||||
$value = $this->value();
|
||||
|
||||
if ( ! empty($this->_parameters))
|
||||
{
|
||||
// Quote all of the parameter values
|
||||
$params = array_map(array($db, 'quote'), $this->_parameters);
|
||||
|
||||
// Replace the values in the expression
|
||||
$value = strtr($value, $params);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
} // End Database_Expression
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* MySQL database connection.
|
||||
*
|
||||
@@ -59,14 +59,14 @@ class Kohana_Database_MySQL extends Database {
|
||||
$this->_connection = mysql_connect($hostname, $username, $password, TRUE);
|
||||
}
|
||||
}
|
||||
catch (ErrorException $e)
|
||||
catch (Exception $e)
|
||||
{
|
||||
// No connection exists
|
||||
$this->_connection = NULL;
|
||||
|
||||
throw new Database_Exception(':error',
|
||||
array(':error' => mysql_error()),
|
||||
mysql_errno());
|
||||
array(':error' => $e->getMessage()),
|
||||
$e->getCode());
|
||||
}
|
||||
|
||||
// \xFF is a better delimiter, but the PHP driver uses underscore
|
||||
@@ -79,12 +79,25 @@ class Kohana_Database_MySQL extends Database {
|
||||
// Set the character set
|
||||
$this->set_charset($this->_config['charset']);
|
||||
}
|
||||
|
||||
if ( ! empty($this->_config['connection']['variables']))
|
||||
{
|
||||
// Set session variables
|
||||
$variables = array();
|
||||
|
||||
foreach ($this->_config['connection']['variables'] as $var => $val)
|
||||
{
|
||||
$variables[] = 'SESSION '.$var.' = '.$this->quote($val);
|
||||
}
|
||||
|
||||
mysql_query('SET '.implode(', ', $variables), $this->_connection);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Select the database
|
||||
*
|
||||
* @param string Database
|
||||
* @param string $database Database
|
||||
* @return void
|
||||
*/
|
||||
protected function _select_db($database)
|
||||
@@ -157,7 +170,7 @@ class Kohana_Database_MySQL extends Database {
|
||||
// Make sure the database is connected
|
||||
$this->_connection or $this->connect();
|
||||
|
||||
if ( ! empty($this->_config['profiling']))
|
||||
if (Kohana::$profiling)
|
||||
{
|
||||
// Benchmark this query for the current instance
|
||||
$benchmark = Profiler::start("Database ({$this->_instance})", $sql);
|
||||
@@ -263,7 +276,7 @@ class Kohana_Database_MySQL extends Database {
|
||||
*
|
||||
* @link http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html
|
||||
*
|
||||
* @param string Isolation level
|
||||
* @param string $mode Isolation level
|
||||
* @return boolean
|
||||
*/
|
||||
public function begin($mode = NULL)
|
||||
@@ -284,7 +297,6 @@ class Kohana_Database_MySQL extends Database {
|
||||
/**
|
||||
* Commit a SQL transaction
|
||||
*
|
||||
* @param string Isolation level
|
||||
* @return boolean
|
||||
*/
|
||||
public function commit()
|
||||
@@ -298,7 +310,6 @@ class Kohana_Database_MySQL extends Database {
|
||||
/**
|
||||
* Rollback a SQL transaction
|
||||
*
|
||||
* @param string Isolation level
|
||||
* @return boolean
|
||||
*/
|
||||
public function rollback()
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* MySQL database result. See [Results](/database/results) for usage and examples.
|
||||
*
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* PDO database connection.
|
||||
*
|
||||
@@ -13,7 +13,7 @@ class Kohana_Database_PDO extends Database {
|
||||
// PDO uses no quoting for identifiers
|
||||
protected $_identifier = '';
|
||||
|
||||
protected function __construct($name, array $config)
|
||||
public function __construct($name, array $config)
|
||||
{
|
||||
parent::__construct($name, $config);
|
||||
|
||||
@@ -41,18 +41,18 @@ class Kohana_Database_PDO extends Database {
|
||||
unset($this->_config['connection']);
|
||||
|
||||
// Force PDO to use exceptions for all errors
|
||||
$attrs = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
|
||||
$options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
|
||||
|
||||
if ( ! empty($persistent))
|
||||
{
|
||||
// Make the connection persistent
|
||||
$attrs[PDO::ATTR_PERSISTENT] = TRUE;
|
||||
$options[PDO::ATTR_PERSISTENT] = TRUE;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Create a new PDO connection
|
||||
$this->_connection = new PDO($dsn, $username, $password, $attrs);
|
||||
$this->_connection = new PDO($dsn, $username, $password, $options);
|
||||
}
|
||||
catch (PDOException $e)
|
||||
{
|
||||
@@ -60,12 +60,51 @@ class Kohana_Database_PDO extends Database {
|
||||
array(':error' => $e->getMessage()),
|
||||
$e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty($this->_config['charset']))
|
||||
{
|
||||
// Set the character set
|
||||
$this->set_charset($this->_config['charset']);
|
||||
}
|
||||
/**
|
||||
* Create or redefine a SQL aggregate function.
|
||||
*
|
||||
* [!!] Works only with SQLite
|
||||
*
|
||||
* @link http://php.net/manual/function.pdo-sqlitecreateaggregate
|
||||
*
|
||||
* @param string $name Name of the SQL function to be created or redefined
|
||||
* @param callback $step Called for each row of a result set
|
||||
* @param callback $final Called after all rows of a result set have been processed
|
||||
* @param integer $arguments Number of arguments that the SQL function takes
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function create_aggregate($name, $step, $final, $arguments = -1)
|
||||
{
|
||||
$this->_connection or $this->connect();
|
||||
|
||||
return $this->_connection->sqliteCreateAggregate(
|
||||
$name, $step, $final, $arguments
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or redefine a SQL function.
|
||||
*
|
||||
* [!!] Works only with SQLite
|
||||
*
|
||||
* @link http://php.net/manual/function.pdo-sqlitecreatefunction
|
||||
*
|
||||
* @param string $name Name of the SQL function to be created or redefined
|
||||
* @param callback $callback Callback which implements the SQL function
|
||||
* @param integer $arguments Number of arguments that the SQL function takes
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function create_function($name, $callback, $arguments = -1)
|
||||
{
|
||||
$this->_connection or $this->connect();
|
||||
|
||||
return $this->_connection->sqliteCreateFunction(
|
||||
$name, $callback, $arguments
|
||||
);
|
||||
}
|
||||
|
||||
public function disconnect()
|
||||
@@ -79,9 +118,9 @@ class Kohana_Database_PDO extends Database {
|
||||
public function set_charset($charset)
|
||||
{
|
||||
// Make sure the database is connected
|
||||
$this->_connection or $this->connect();
|
||||
$this->_connection OR $this->connect();
|
||||
|
||||
// Execute a raw SET NAMES query
|
||||
// This SQL-92 syntax is not supported by all drivers
|
||||
$this->_connection->exec('SET NAMES '.$this->quote($charset));
|
||||
}
|
||||
|
||||
@@ -90,7 +129,7 @@ class Kohana_Database_PDO extends Database {
|
||||
// Make sure the database is connected
|
||||
$this->_connection or $this->connect();
|
||||
|
||||
if ( ! empty($this->_config['profiling']))
|
||||
if (Kohana::$profiling)
|
||||
{
|
||||
// Benchmark this query for the current instance
|
||||
$benchmark = Profiler::start("Database ({$this->_instance})", $sql);
|
@@ -1,6 +1,6 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database query wrapper. See [Prepared Statements](database/query/prepared) for usage and examples.
|
||||
* Database query wrapper. See [Parameterized Statements](database/query/parameterized) for usage and examples.
|
||||
*
|
||||
* @package Kohana/Database
|
||||
* @category Query
|
||||
@@ -13,6 +13,9 @@ class Kohana_Database_Query {
|
||||
// Query type
|
||||
protected $_type;
|
||||
|
||||
// Execute the query during a cache hit
|
||||
protected $_force_execute = FALSE;
|
||||
|
||||
// Cache lifetime
|
||||
protected $_lifetime = NULL;
|
||||
|
||||
@@ -31,8 +34,8 @@ class Kohana_Database_Query {
|
||||
/**
|
||||
* Creates a new SQL query of the specified type.
|
||||
*
|
||||
* @param integer query type: Database::SELECT, Database::INSERT, etc
|
||||
* @param string query string
|
||||
* @param integer $type query type: Database::SELECT, Database::INSERT, etc
|
||||
* @param string $sql query string
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($type, $sql)
|
||||
@@ -46,7 +49,7 @@ class Kohana_Database_Query {
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
final public function __toString()
|
||||
public function __toString()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -72,11 +75,12 @@ class Kohana_Database_Query {
|
||||
/**
|
||||
* Enables the query to be cached for a specified amount of time.
|
||||
*
|
||||
* @param integer number of seconds to cache
|
||||
* @param integer $lifetime number of seconds to cache, 0 deletes it from the cache
|
||||
* @param boolean whether or not to execute the query during a cache hit
|
||||
* @return $this
|
||||
* @uses Kohana::$cache_life
|
||||
*/
|
||||
public function cached($lifetime = NULL)
|
||||
public function cached($lifetime = NULL, $force = FALSE)
|
||||
{
|
||||
if ($lifetime === NULL)
|
||||
{
|
||||
@@ -84,6 +88,7 @@ class Kohana_Database_Query {
|
||||
$lifetime = Kohana::$cache_life;
|
||||
}
|
||||
|
||||
$this->_force_execute = $force;
|
||||
$this->_lifetime = $lifetime;
|
||||
|
||||
return $this;
|
||||
@@ -106,7 +111,8 @@ class Kohana_Database_Query {
|
||||
/**
|
||||
* Returns results as objects
|
||||
*
|
||||
* @param string classname or TRUE for stdClass
|
||||
* @param string $class classname or TRUE for stdClass
|
||||
* @param array $params
|
||||
* @return $this
|
||||
*/
|
||||
public function as_object($class = TRUE, array $params = NULL)
|
||||
@@ -125,8 +131,8 @@ class Kohana_Database_Query {
|
||||
/**
|
||||
* Set the value of a parameter in the query.
|
||||
*
|
||||
* @param string parameter key to replace
|
||||
* @param mixed value to use
|
||||
* @param string $param parameter key to replace
|
||||
* @param mixed $value value to use
|
||||
* @return $this
|
||||
*/
|
||||
public function param($param, $value)
|
||||
@@ -140,8 +146,8 @@ class Kohana_Database_Query {
|
||||
/**
|
||||
* Bind a variable to a parameter in the query.
|
||||
*
|
||||
* @param string parameter key to replace
|
||||
* @param mixed variable to use
|
||||
* @param string $param parameter key to replace
|
||||
* @param mixed $var variable to use
|
||||
* @return $this
|
||||
*/
|
||||
public function bind($param, & $var)
|
||||
@@ -155,7 +161,7 @@ class Kohana_Database_Query {
|
||||
/**
|
||||
* Add multiple parameters to the query.
|
||||
*
|
||||
* @param array list of parameters
|
||||
* @param array $params list of parameters
|
||||
* @return $this
|
||||
*/
|
||||
public function parameters(array $params)
|
||||
@@ -170,11 +176,17 @@ class Kohana_Database_Query {
|
||||
* Compile the SQL query and return it. Replaces any parameters with their
|
||||
* given values.
|
||||
*
|
||||
* @param object Database instance
|
||||
* @param mixed $db Database instance or name of instance
|
||||
* @return string
|
||||
*/
|
||||
public function compile(Database $db)
|
||||
public function compile($db = NULL)
|
||||
{
|
||||
if ( ! is_object($db))
|
||||
{
|
||||
// Get the database instance
|
||||
$db = Database::instance($db);
|
||||
}
|
||||
|
||||
// Import the SQL locally
|
||||
$sql = $this->_sql;
|
||||
|
||||
@@ -193,12 +205,14 @@ class Kohana_Database_Query {
|
||||
/**
|
||||
* Execute the current query on the given database.
|
||||
*
|
||||
* @param mixed Database instance or name of instance
|
||||
* @param mixed $db Database instance or name of instance
|
||||
* @param string result object classname, TRUE for stdClass or FALSE for array
|
||||
* @param array result object constructor arguments
|
||||
* @return object Database_Result for SELECT queries
|
||||
* @return mixed the insert id for INSERT queries
|
||||
* @return integer number of affected rows for all other queries
|
||||
*/
|
||||
public function execute($db = NULL)
|
||||
public function execute($db = NULL, $as_object = NULL, $object_params = NULL)
|
||||
{
|
||||
if ( ! is_object($db))
|
||||
{
|
||||
@@ -206,6 +220,16 @@ class Kohana_Database_Query {
|
||||
$db = Database::instance($db);
|
||||
}
|
||||
|
||||
if ($as_object === NULL)
|
||||
{
|
||||
$as_object = $this->_as_object;
|
||||
}
|
||||
|
||||
if ($object_params === NULL)
|
||||
{
|
||||
$object_params = $this->_object_params;
|
||||
}
|
||||
|
||||
// Compile the SQL query
|
||||
$sql = $this->compile($db);
|
||||
|
||||
@@ -214,17 +238,19 @@ class Kohana_Database_Query {
|
||||
// Set the cache key based on the database instance name and SQL
|
||||
$cache_key = 'Database::query("'.$db.'", "'.$sql.'")';
|
||||
|
||||
if (! is_null($result = Kohana::cache($cache_key, NULL, $this->_lifetime)))
|
||||
// Read the cache first to delete a possible hit with lifetime <= 0
|
||||
if (($result = Kohana::cache($cache_key, NULL, $this->_lifetime)) !== NULL
|
||||
AND ! $this->_force_execute)
|
||||
{
|
||||
// Return a cached result
|
||||
return new Database_Result_Cached($result, $sql, $this->_as_object, $this->_object_params);
|
||||
return new Database_Result_Cached($result, $sql, $as_object, $object_params);
|
||||
}
|
||||
}
|
||||
|
||||
// Execute the query
|
||||
$result = $db->query($this->_type, $sql, $this->_as_object, $this->_object_params);
|
||||
$result = $db->query($this->_type, $sql, $as_object, $object_params);
|
||||
|
||||
if (isset($cache_key))
|
||||
if (isset($cache_key) AND $this->_lifetime > 0)
|
||||
{
|
||||
// Cache the result array
|
||||
Kohana::cache($cache_key, $result->as_array(), $this->_lifetime);
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database query builder. See [Query Builder](/database/query/builder) for usage and examples.
|
||||
*
|
||||
@@ -13,8 +13,8 @@ abstract class Kohana_Database_Query_Builder extends Database_Query {
|
||||
/**
|
||||
* Compiles an array of JOIN statements into an SQL partial.
|
||||
*
|
||||
* @param object Database instance
|
||||
* @param array join statements
|
||||
* @param object $db Database instance
|
||||
* @param array $joins join statements
|
||||
* @return string
|
||||
*/
|
||||
protected function _compile_join(Database $db, array $joins)
|
||||
@@ -34,8 +34,8 @@ abstract class Kohana_Database_Query_Builder extends Database_Query {
|
||||
* Compiles an array of conditions into an SQL partial. Used for WHERE
|
||||
* and HAVING.
|
||||
*
|
||||
* @param object Database instance
|
||||
* @param array condition statements
|
||||
* @param object $db Database instance
|
||||
* @param array $conditions condition statements
|
||||
* @return string
|
||||
*/
|
||||
protected function _compile_conditions(Database $db, array $conditions)
|
||||
@@ -110,10 +110,6 @@ abstract class Kohana_Database_Query_Builder extends Database_Query {
|
||||
// Quote the min and max value
|
||||
$value = $min.' AND '.$max;
|
||||
}
|
||||
elseif ($op === 'IN' AND is_array($value))
|
||||
{
|
||||
$value = $db->quote($value);
|
||||
}
|
||||
elseif ((is_string($value) AND array_key_exists($value, $this->_parameters)) === FALSE)
|
||||
{
|
||||
// Quote the value, it is not a parameter
|
||||
@@ -122,8 +118,16 @@ abstract class Kohana_Database_Query_Builder extends Database_Query {
|
||||
|
||||
if ($column)
|
||||
{
|
||||
// Apply proper quoting to the column
|
||||
$column = $db->quote_column($column);
|
||||
if (is_array($column))
|
||||
{
|
||||
// Use the column name
|
||||
$column = $db->quote_identifier(reset($column));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Apply proper quoting to the column
|
||||
$column = $db->quote_column($column);
|
||||
}
|
||||
}
|
||||
|
||||
// Append the statement to the query
|
||||
@@ -140,8 +144,8 @@ abstract class Kohana_Database_Query_Builder extends Database_Query {
|
||||
/**
|
||||
* Compiles an array of set values into an SQL partial. Used for UPDATE.
|
||||
*
|
||||
* @param object Database instance
|
||||
* @param array updated values
|
||||
* @param object $db Database instance
|
||||
* @param array $values updated values
|
||||
* @return string
|
||||
*/
|
||||
protected function _compile_set(Database $db, array $values)
|
||||
@@ -167,11 +171,41 @@ abstract class Kohana_Database_Query_Builder extends Database_Query {
|
||||
return implode(', ', $set);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles an array of GROUP BY columns into an SQL partial.
|
||||
*
|
||||
* @param object $db Database instance
|
||||
* @param array $columns
|
||||
* @return string
|
||||
*/
|
||||
protected function _compile_group_by(Database $db, array $columns)
|
||||
{
|
||||
$group = array();
|
||||
|
||||
foreach ($columns as $column)
|
||||
{
|
||||
if (is_array($column))
|
||||
{
|
||||
// Use the column alias
|
||||
$column = $db->quote_identifier(end($column));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Apply proper quoting to the column
|
||||
$column = $db->quote_column($column);
|
||||
}
|
||||
|
||||
$group[] = $column;
|
||||
}
|
||||
|
||||
return 'GROUP BY '.implode(', ', $group);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles an array of ORDER BY statements into an SQL partial.
|
||||
*
|
||||
* @param object Database instance
|
||||
* @param array sorting columns
|
||||
* @param object $db Database instance
|
||||
* @param array $columns sorting columns
|
||||
* @return string
|
||||
*/
|
||||
protected function _compile_order_by(Database $db, array $columns)
|
||||
@@ -181,19 +215,24 @@ abstract class Kohana_Database_Query_Builder extends Database_Query {
|
||||
{
|
||||
list ($column, $direction) = $group;
|
||||
|
||||
if ($direction)
|
||||
if (is_array($column))
|
||||
{
|
||||
// Make the direction uppercase
|
||||
$direction = strtoupper($direction);
|
||||
// Use the column alias
|
||||
$column = $db->quote_identifier(end($column));
|
||||
}
|
||||
|
||||
if ($column)
|
||||
else
|
||||
{
|
||||
// Quote the column, if it has a value
|
||||
// Apply proper quoting to the column
|
||||
$column = $db->quote_column($column);
|
||||
}
|
||||
|
||||
$sort[] = trim($column.' '.$direction);
|
||||
if ($direction)
|
||||
{
|
||||
// Make the direction uppercase
|
||||
$direction = ' '.strtoupper($direction);
|
||||
}
|
||||
|
||||
$sort[] = $column.$direction;
|
||||
}
|
||||
|
||||
return 'ORDER BY '.implode(', ', $sort);
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database query builder for DELETE statements. See [Query Builder](/database/query/builder) for usage and examples.
|
||||
*
|
||||
@@ -16,7 +16,7 @@ class Kohana_Database_Query_Builder_Delete extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Set the table for a delete.
|
||||
*
|
||||
* @param mixed table name or array($table, $alias) or object
|
||||
* @param mixed $table table name or array($table, $alias) or object
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($table = NULL)
|
||||
@@ -34,7 +34,7 @@ class Kohana_Database_Query_Builder_Delete extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Sets the table to delete from.
|
||||
*
|
||||
* @param mixed table name or array($table, $alias) or object
|
||||
* @param mixed $table table name or array($table, $alias) or object
|
||||
* @return $this
|
||||
*/
|
||||
public function table($table)
|
||||
@@ -47,11 +47,17 @@ class Kohana_Database_Query_Builder_Delete extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Compile the SQL query and return it.
|
||||
*
|
||||
* @param object Database instance
|
||||
* @param mixed $db Database instance or name of instance
|
||||
* @return string
|
||||
*/
|
||||
public function compile(Database $db)
|
||||
public function compile($db = NULL)
|
||||
{
|
||||
if ( ! is_object($db))
|
||||
{
|
||||
// Get the database instance
|
||||
$db = Database::instance($db);
|
||||
}
|
||||
|
||||
// Start a deletion query
|
||||
$query = 'DELETE FROM '.$db->quote_table($this->_table);
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database query builder for INSERT statements. See [Query Builder](/database/query/builder) for usage and examples.
|
||||
*
|
||||
@@ -22,8 +22,8 @@ class Kohana_Database_Query_Builder_Insert extends Database_Query_Builder {
|
||||
/**
|
||||
* Set the table and columns for an insert.
|
||||
*
|
||||
* @param mixed table name or array($table, $alias) or object
|
||||
* @param array column names
|
||||
* @param mixed $table table name or array($table, $alias) or object
|
||||
* @param array $columns column names
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($table = NULL, array $columns = NULL)
|
||||
@@ -47,7 +47,7 @@ class Kohana_Database_Query_Builder_Insert extends Database_Query_Builder {
|
||||
/**
|
||||
* Sets the table to insert into.
|
||||
*
|
||||
* @param mixed table name or array($table, $alias) or object
|
||||
* @param mixed $table table name or array($table, $alias) or object
|
||||
* @return $this
|
||||
*/
|
||||
public function table($table)
|
||||
@@ -60,7 +60,7 @@ class Kohana_Database_Query_Builder_Insert extends Database_Query_Builder {
|
||||
/**
|
||||
* Set the columns that will be inserted.
|
||||
*
|
||||
* @param array column names
|
||||
* @param array $columns column names
|
||||
* @return $this
|
||||
*/
|
||||
public function columns(array $columns)
|
||||
@@ -73,7 +73,7 @@ class Kohana_Database_Query_Builder_Insert extends Database_Query_Builder {
|
||||
/**
|
||||
* Adds or overwrites values. Multiple value sets can be added.
|
||||
*
|
||||
* @param array values list
|
||||
* @param array $values values list
|
||||
* @param ...
|
||||
* @return $this
|
||||
*/
|
||||
@@ -95,7 +95,7 @@ class Kohana_Database_Query_Builder_Insert extends Database_Query_Builder {
|
||||
/**
|
||||
* Use a sub-query to for the inserted values.
|
||||
*
|
||||
* @param object Database_Query of SELECT type
|
||||
* @param object $query Database_Query of SELECT type
|
||||
* @return $this
|
||||
*/
|
||||
public function select(Database_Query $query)
|
||||
@@ -113,11 +113,17 @@ class Kohana_Database_Query_Builder_Insert extends Database_Query_Builder {
|
||||
/**
|
||||
* Compile the SQL query and return it.
|
||||
*
|
||||
* @param object Database instance
|
||||
* @param mixed $db Database instance or name of instance
|
||||
* @return string
|
||||
*/
|
||||
public function compile(Database $db)
|
||||
public function compile($db = NULL)
|
||||
{
|
||||
if ( ! is_object($db))
|
||||
{
|
||||
// Get the database instance
|
||||
$db = Database::instance($db);
|
||||
}
|
||||
|
||||
// Start an insertion query
|
||||
$query = 'INSERT INTO '.$db->quote_table($this->_table);
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database query builder for JOIN statements. See [Query Builder](/database/query/builder) for usage and examples.
|
||||
*
|
||||
@@ -26,8 +26,8 @@ class Kohana_Database_Query_Builder_Join extends Database_Query_Builder {
|
||||
* Creates a new JOIN statement for a table. Optionally, the type of JOIN
|
||||
* can be specified as the second parameter.
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param string type of JOIN: INNER, RIGHT, LEFT, etc
|
||||
* @param mixed $table column name or array($column, $alias) or object
|
||||
* @param string $type type of JOIN: INNER, RIGHT, LEFT, etc
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($table, $type = NULL)
|
||||
@@ -45,9 +45,9 @@ class Kohana_Database_Query_Builder_Join extends Database_Query_Builder {
|
||||
/**
|
||||
* Adds a new condition for joining.
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param string logic operator
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param mixed $c1 column name or array($column, $alias) or object
|
||||
* @param string $op logic operator
|
||||
* @param mixed $c2 column name or array($column, $alias) or object
|
||||
* @return $this
|
||||
*/
|
||||
public function on($c1, $op, $c2)
|
||||
@@ -65,8 +65,7 @@ class Kohana_Database_Query_Builder_Join extends Database_Query_Builder {
|
||||
/**
|
||||
* Adds a new condition for joining.
|
||||
*
|
||||
* @param string column name
|
||||
* @param ...
|
||||
* @param string $columns column name
|
||||
* @return $this
|
||||
*/
|
||||
public function using($columns)
|
||||
@@ -86,11 +85,17 @@ class Kohana_Database_Query_Builder_Join extends Database_Query_Builder {
|
||||
/**
|
||||
* Compile the SQL partial for a JOIN statement and return it.
|
||||
*
|
||||
* @param object Database instance
|
||||
* @param mixed $db Database instance or name of instance
|
||||
* @return string
|
||||
*/
|
||||
public function compile(Database $db)
|
||||
public function compile($db = NULL)
|
||||
{
|
||||
if ( ! is_object($db))
|
||||
{
|
||||
// Get the database instance
|
||||
$db = Database::instance($db);
|
||||
}
|
||||
|
||||
if ($this->_type)
|
||||
{
|
||||
$sql = strtoupper($this->_type).' JOIN';
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database query builder for SELECT statements. See [Query Builder](/database/query/builder) for usage and examples.
|
||||
*
|
||||
@@ -40,7 +40,7 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Sets the initial columns to select from.
|
||||
*
|
||||
* @param array column list
|
||||
* @param array $columns column list
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $columns = NULL)
|
||||
@@ -58,7 +58,7 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Enables or disables selecting only unique columns using "SELECT DISTINCT"
|
||||
*
|
||||
* @param boolean enable or disable distinct columns
|
||||
* @param boolean $value enable or disable distinct columns
|
||||
* @return $this
|
||||
*/
|
||||
public function distinct($value)
|
||||
@@ -71,8 +71,7 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Choose the columns to select from.
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param ...
|
||||
* @param mixed $columns column name or array($column, $alias) or object
|
||||
* @return $this
|
||||
*/
|
||||
public function select($columns = NULL)
|
||||
@@ -87,7 +86,7 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Choose the columns to select from, using an array.
|
||||
*
|
||||
* @param array list of column names or aliases
|
||||
* @param array $columns list of column names or aliases
|
||||
* @return $this
|
||||
*/
|
||||
public function select_array(array $columns)
|
||||
@@ -100,8 +99,7 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Choose the tables to select "FROM ..."
|
||||
*
|
||||
* @param mixed table name or array($table, $alias) or object
|
||||
* @param ...
|
||||
* @param mixed $table table name or array($table, $alias) or object
|
||||
* @return $this
|
||||
*/
|
||||
public function from($tables)
|
||||
@@ -116,8 +114,8 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Adds addition tables to "JOIN ...".
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param string join type (LEFT, RIGHT, INNER, etc)
|
||||
* @param mixed $table column name or array($column, $alias) or object
|
||||
* @param string $type join type (LEFT, RIGHT, INNER, etc)
|
||||
* @return $this
|
||||
*/
|
||||
public function join($table, $type = NULL)
|
||||
@@ -130,9 +128,9 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Adds "ON ..." conditions for the last created JOIN statement.
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param string logic operator
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param mixed $c1 column name or array($column, $alias) or object
|
||||
* @param string $op logic operator
|
||||
* @param mixed $c2 column name or array($column, $alias) or object
|
||||
* @return $this
|
||||
*/
|
||||
public function on($c1, $op, $c2)
|
||||
@@ -145,8 +143,7 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Adds "USING ..." conditions for the last created JOIN statement.
|
||||
*
|
||||
* @param string column name
|
||||
* @param ...
|
||||
* @param string $columns column name
|
||||
* @return $this
|
||||
*/
|
||||
public function using($columns)
|
||||
@@ -161,8 +158,7 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Creates a "GROUP BY ..." filter.
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param ...
|
||||
* @param mixed $columns column name or array($column, $alias) or object
|
||||
* @return $this
|
||||
*/
|
||||
public function group_by($columns)
|
||||
@@ -177,9 +173,9 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Alias of and_having()
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param string logic operator
|
||||
* @param mixed column value
|
||||
* @param mixed $column column name or array($column, $alias) or object
|
||||
* @param string $op logic operator
|
||||
* @param mixed $value column value
|
||||
* @return $this
|
||||
*/
|
||||
public function having($column, $op, $value = NULL)
|
||||
@@ -190,9 +186,9 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Creates a new "AND HAVING" condition for the query.
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param string logic operator
|
||||
* @param mixed column value
|
||||
* @param mixed $column column name or array($column, $alias) or object
|
||||
* @param string $op logic operator
|
||||
* @param mixed $value column value
|
||||
* @return $this
|
||||
*/
|
||||
public function and_having($column, $op, $value = NULL)
|
||||
@@ -205,9 +201,9 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Creates a new "OR HAVING" condition for the query.
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param string logic operator
|
||||
* @param mixed column value
|
||||
* @param mixed $column column name or array($column, $alias) or object
|
||||
* @param string $op logic operator
|
||||
* @param mixed $value column value
|
||||
* @return $this
|
||||
*/
|
||||
public function or_having($column, $op, $value = NULL)
|
||||
@@ -287,10 +283,10 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
|
||||
/**
|
||||
* Adds an other UNION clause.
|
||||
*
|
||||
* @param mixed $select if string, it must be the name of a table. Else
|
||||
*
|
||||
* @param mixed $select if string, it must be the name of a table. Else
|
||||
* must be an instance of Database_Query_Builder_Select
|
||||
* @param boolean $all decides if it's an UNION or UNION ALL clause
|
||||
* @param boolean $all decides if it's an UNION or UNION ALL clause
|
||||
* @return $this
|
||||
*/
|
||||
public function union($select, $all = TRUE)
|
||||
@@ -303,17 +299,17 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
throw new Kohana_Exception('first parameter must be a string or an instance of Database_Query_Builder_Select');
|
||||
$this->_union []= array('select' => $select, 'all' => $all);
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start returning results after "OFFSET ..."
|
||||
*
|
||||
* @param integer starting result number
|
||||
* @param integer $number starting result number or NULL to reset
|
||||
* @return $this
|
||||
*/
|
||||
public function offset($number)
|
||||
{
|
||||
$this->_offset = (int) $number;
|
||||
$this->_offset = $number;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -321,11 +317,17 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Compile the SQL query and return it.
|
||||
*
|
||||
* @param object Database instance
|
||||
* @param mixed $db Database instance or name of instance
|
||||
* @return string
|
||||
*/
|
||||
public function compile(Database $db)
|
||||
public function compile($db = NULL)
|
||||
{
|
||||
if ( ! is_object($db))
|
||||
{
|
||||
// Get the database instance
|
||||
$db = Database::instance($db);
|
||||
}
|
||||
|
||||
// Callback to quote columns
|
||||
$quote_column = array($db, 'quote_column');
|
||||
|
||||
@@ -372,8 +374,8 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
|
||||
if ( ! empty($this->_group_by))
|
||||
{
|
||||
// Add sorting
|
||||
$query .= ' GROUP BY '.implode(', ', array_map($quote_column, $this->_group_by));
|
||||
// Add grouping
|
||||
$query .= ' '.$this->_compile_group_by($db, $this->_group_by);
|
||||
}
|
||||
|
||||
if ( ! empty($this->_having))
|
||||
@@ -399,7 +401,7 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
// Add offsets
|
||||
$query .= ' OFFSET '.$this->_offset;
|
||||
}
|
||||
|
||||
|
||||
if ( ! empty($this->_union))
|
||||
{
|
||||
foreach ($this->_union as $u) {
|
||||
@@ -442,4 +444,3 @@ class Kohana_Database_Query_Builder_Select extends Database_Query_Builder_Where
|
||||
}
|
||||
|
||||
} // End Database_Query_Select
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database query builder for UPDATE statements. See [Query Builder](/database/query/builder) for usage and examples.
|
||||
*
|
||||
@@ -19,7 +19,7 @@ class Kohana_Database_Query_Builder_Update extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Set the table for a update.
|
||||
*
|
||||
* @param mixed table name or array($table, $alias) or object
|
||||
* @param mixed $table table name or array($table, $alias) or object
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($table = NULL)
|
||||
@@ -37,7 +37,7 @@ class Kohana_Database_Query_Builder_Update extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Sets the table to update.
|
||||
*
|
||||
* @param mixed table name or array($table, $alias) or object
|
||||
* @param mixed $table table name or array($table, $alias) or object
|
||||
* @return $this
|
||||
*/
|
||||
public function table($table)
|
||||
@@ -50,7 +50,7 @@ class Kohana_Database_Query_Builder_Update extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Set the values to update with an associative array.
|
||||
*
|
||||
* @param array associative (column => value) list
|
||||
* @param array $pairs associative (column => value) list
|
||||
* @return $this
|
||||
*/
|
||||
public function set(array $pairs)
|
||||
@@ -66,8 +66,8 @@ class Kohana_Database_Query_Builder_Update extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Set the value of a single column.
|
||||
*
|
||||
* @param mixed table name or array($table, $alias) or object
|
||||
* @param mixed column value
|
||||
* @param mixed $column table name or array($table, $alias) or object
|
||||
* @param mixed $value column value
|
||||
* @return $this
|
||||
*/
|
||||
public function value($column, $value)
|
||||
@@ -80,11 +80,17 @@ class Kohana_Database_Query_Builder_Update extends Database_Query_Builder_Where
|
||||
/**
|
||||
* Compile the SQL query and return it.
|
||||
*
|
||||
* @param object Database instance
|
||||
* @param mixed $db Database instance or name of instance
|
||||
* @return string
|
||||
*/
|
||||
public function compile(Database $db)
|
||||
public function compile($db = NULL)
|
||||
{
|
||||
if ( ! is_object($db))
|
||||
{
|
||||
// Get the database instance
|
||||
$db = Database::instance($db);
|
||||
}
|
||||
|
||||
// Start an update query
|
||||
$query = 'UPDATE '.$db->quote_table($this->_table);
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database query builder for WHERE statements. See [Query Builder](/database/query/builder) for usage and examples.
|
||||
*
|
||||
@@ -22,9 +22,9 @@ abstract class Kohana_Database_Query_Builder_Where extends Database_Query_Builde
|
||||
/**
|
||||
* Alias of and_where()
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param string logic operator
|
||||
* @param mixed column value
|
||||
* @param mixed $column column name or array($column, $alias) or object
|
||||
* @param string $op logic operator
|
||||
* @param mixed $value column value
|
||||
* @return $this
|
||||
*/
|
||||
public function where($column, $op, $value)
|
||||
@@ -35,9 +35,9 @@ abstract class Kohana_Database_Query_Builder_Where extends Database_Query_Builde
|
||||
/**
|
||||
* Creates a new "AND WHERE" condition for the query.
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param string logic operator
|
||||
* @param mixed column value
|
||||
* @param mixed $column column name or array($column, $alias) or object
|
||||
* @param string $op logic operator
|
||||
* @param mixed $value column value
|
||||
* @return $this
|
||||
*/
|
||||
public function and_where($column, $op, $value)
|
||||
@@ -50,9 +50,9 @@ abstract class Kohana_Database_Query_Builder_Where extends Database_Query_Builde
|
||||
/**
|
||||
* Creates a new "OR WHERE" condition for the query.
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param string logic operator
|
||||
* @param mixed column value
|
||||
* @param mixed $column column name or array($column, $alias) or object
|
||||
* @param string $op logic operator
|
||||
* @param mixed $value column value
|
||||
* @return $this
|
||||
*/
|
||||
public function or_where($column, $op, $value)
|
||||
@@ -97,7 +97,7 @@ abstract class Kohana_Database_Query_Builder_Where extends Database_Query_Builde
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes an open "AND WHERE (...)" grouping.
|
||||
* Closes an open "WHERE (...)" grouping.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
@@ -107,7 +107,27 @@ abstract class Kohana_Database_Query_Builder_Where extends Database_Query_Builde
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes an open "AND WHERE (...)" grouping.
|
||||
* Closes an open "WHERE (...)" grouping or removes the grouping when it is
|
||||
* empty.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function where_close_empty()
|
||||
{
|
||||
$group = end($this->_where);
|
||||
|
||||
if ($group AND reset($group) === '(')
|
||||
{
|
||||
array_pop($this->_where);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
return $this->where_close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes an open "WHERE (...)" grouping.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
@@ -119,7 +139,7 @@ abstract class Kohana_Database_Query_Builder_Where extends Database_Query_Builde
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes an open "OR WHERE (...)" grouping.
|
||||
* Closes an open "WHERE (...)" grouping.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
@@ -133,8 +153,8 @@ abstract class Kohana_Database_Query_Builder_Where extends Database_Query_Builde
|
||||
/**
|
||||
* Applies sorting with "ORDER BY ..."
|
||||
*
|
||||
* @param mixed column name or array($column, $alias) or object
|
||||
* @param string direction of sorting
|
||||
* @param mixed $column column name or array($column, $alias) or object
|
||||
* @param string $direction direction of sorting
|
||||
* @return $this
|
||||
*/
|
||||
public function order_by($column, $direction = NULL)
|
||||
@@ -147,12 +167,12 @@ abstract class Kohana_Database_Query_Builder_Where extends Database_Query_Builde
|
||||
/**
|
||||
* Return up to "LIMIT ..." results
|
||||
*
|
||||
* @param integer maximum results to return
|
||||
* @param integer $number maximum results to return or NULL to reset
|
||||
* @return $this
|
||||
*/
|
||||
public function limit($number)
|
||||
{
|
||||
$this->_limit = (int) $number;
|
||||
$this->_limit = $number;
|
||||
|
||||
return $this;
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database result wrapper. See [Results](/database/results) for usage and examples.
|
||||
*
|
||||
@@ -29,8 +29,10 @@ abstract class Kohana_Database_Result implements Countable, Iterator, SeekableIt
|
||||
/**
|
||||
* Sets the total number of rows and stores the result locally.
|
||||
*
|
||||
* @param mixed query result
|
||||
* @param string SQL query
|
||||
* @param mixed $result query result
|
||||
* @param string $sql SQL query
|
||||
* @param mixed $as_object
|
||||
* @param array $params
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($result, $sql, $as_object = FALSE, array $params = NULL)
|
||||
@@ -89,8 +91,8 @@ abstract class Kohana_Database_Result implements Countable, Iterator, SeekableIt
|
||||
* // Associative array of rows, "id" => "name"
|
||||
* $rows = $result->as_array('id', 'name');
|
||||
*
|
||||
* @param string column for associative keys
|
||||
* @param string column for values
|
||||
* @param string $key column for associative keys
|
||||
* @param string $value column for values
|
||||
* @return array
|
||||
*/
|
||||
public function as_array($key = NULL, $value = NULL)
|
||||
@@ -175,8 +177,8 @@ abstract class Kohana_Database_Result implements Countable, Iterator, SeekableIt
|
||||
* // Get the "id" value
|
||||
* $id = $result->get('id');
|
||||
*
|
||||
* @param string column to get
|
||||
* @param mixed default value if the column does not exist
|
||||
* @param string $name column to get
|
||||
* @param mixed $default default value if the column does not exist
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $default = NULL)
|
||||
@@ -217,6 +219,7 @@ abstract class Kohana_Database_Result implements Countable, Iterator, SeekableIt
|
||||
* // Row 10 exists
|
||||
* }
|
||||
*
|
||||
* @param int $offset
|
||||
* @return boolean
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
@@ -229,6 +232,7 @@ abstract class Kohana_Database_Result implements Countable, Iterator, SeekableIt
|
||||
*
|
||||
* $row = $result[10];
|
||||
*
|
||||
* @param int $offset
|
||||
* @return mixed
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
@@ -244,6 +248,8 @@ abstract class Kohana_Database_Result implements Countable, Iterator, SeekableIt
|
||||
*
|
||||
* [!!] You cannot modify a database result.
|
||||
*
|
||||
* @param int $offset
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
* @throws Kohana_Exception
|
||||
*/
|
||||
@@ -257,6 +263,7 @@ abstract class Kohana_Database_Result implements Countable, Iterator, SeekableIt
|
||||
*
|
||||
* [!!] You cannot modify a database result.
|
||||
*
|
||||
* @param int $offset
|
||||
* @return void
|
||||
* @throws Kohana_Exception
|
||||
*/
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Object used for caching the results of select queries. See [Results](/database/results#select-cached) for usage and examples.
|
||||
*
|
@@ -1,11 +1,11 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database Model base class.
|
||||
*
|
||||
* @package Kohana/Database
|
||||
* @category Models
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2008-2010 Kohana Team
|
||||
* @copyright (c) 2008-2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
abstract class Kohana_Model_Database extends Model {
|
||||
@@ -17,8 +17,8 @@ abstract class Kohana_Model_Database extends Model {
|
||||
*
|
||||
* $model = Model::factory($name);
|
||||
*
|
||||
* @param string model name
|
||||
* @param mixed Database instance object or string
|
||||
* @param string $name model name
|
||||
* @param mixed $db Database instance object or string
|
||||
* @return Model
|
||||
*/
|
||||
public static function factory($name, $db = NULL)
|
||||
@@ -30,23 +30,28 @@ abstract class Kohana_Model_Database extends Model {
|
||||
}
|
||||
|
||||
// Database instance
|
||||
protected $_db = 'default';
|
||||
protected $_db;
|
||||
|
||||
/**
|
||||
* Loads the database.
|
||||
*
|
||||
* $model = new Foo_Model($db);
|
||||
*
|
||||
* @param mixed Database instance object or string
|
||||
* @param mixed $db Database instance object or string
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($db = NULL)
|
||||
{
|
||||
if ($db !== NULL)
|
||||
if ($db)
|
||||
{
|
||||
// Set the database instance name
|
||||
// Set the instance or name
|
||||
$this->_db = $db;
|
||||
}
|
||||
elseif ( ! $this->_db)
|
||||
{
|
||||
// Use the default name
|
||||
$this->_db = Database::$default;
|
||||
}
|
||||
|
||||
if (is_string($this->_db))
|
||||
{
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* Database-based session class.
|
||||
*
|
||||
@@ -47,7 +47,7 @@ class Kohana_Session_Database extends Session {
|
||||
if ( ! isset($config['group']))
|
||||
{
|
||||
// Use the default group
|
||||
$config['group'] = 'default';
|
||||
$config['group'] = Database::$default;
|
||||
}
|
||||
|
||||
// Load the database
|
||||
@@ -176,6 +176,16 @@ class Kohana_Session_Database extends Session {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected function _restart()
|
||||
{
|
||||
$this->_regenerate();
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
protected function _destroy()
|
||||
{
|
||||
if ($this->_update_id === NULL)
|
@@ -1,3 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
abstract class Model_Database extends Kohana_Model_Database {}
|
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Session_Database extends Kohana_Session_Database {}
|
@@ -1,3 +0,0 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
class Config_Database extends Kohana_Config_Database {}
|
@@ -1,3 +0,0 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
abstract class Database extends Kohana_Database {}
|
@@ -1,3 +0,0 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
class Database_MySQL extends Kohana_Database_MySQL {}
|
@@ -1,3 +0,0 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
class Database_PDO extends Kohana_Database_PDO {}
|
@@ -1,3 +0,0 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
class Database_Query extends Kohana_Database_Query {}
|
@@ -1,3 +0,0 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
class DB extends Kohana_DB {}
|
@@ -1,97 +0,0 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
/**
|
||||
* Database-based configuration loader.
|
||||
*
|
||||
* Schema for configuration table:
|
||||
*
|
||||
* group_name varchar(128)
|
||||
* config_key varchar(128)
|
||||
* config_value text
|
||||
* primary key (group_name, config_key)
|
||||
*
|
||||
* @package Kohana/Database
|
||||
* @category Configuration
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
class Kohana_Config_Database extends Config_Reader {
|
||||
|
||||
protected $_database_instance = 'default';
|
||||
|
||||
protected $_database_table = 'config';
|
||||
|
||||
public function __construct(array $config = NULL)
|
||||
{
|
||||
if (isset($config['instance']))
|
||||
{
|
||||
$this->_database_instance = $config['instance'];
|
||||
}
|
||||
|
||||
if (isset($config['table']))
|
||||
{
|
||||
$this->_database_table = $config['table'];
|
||||
}
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Query the configuration table for all values for this group and
|
||||
* unserialize each of the values.
|
||||
*
|
||||
* @param string group name
|
||||
* @param array configuration array
|
||||
* @return $this clone of the current object
|
||||
*/
|
||||
public function load($group, array $config = NULL)
|
||||
{
|
||||
if ($config === NULL AND $group !== 'database')
|
||||
{
|
||||
// Load all of the configuration values for this group
|
||||
$query = DB::select('config_key', 'config_value')
|
||||
->from($this->_database_table)
|
||||
->where('group_name', '=', $group)
|
||||
->execute($this->_database_instance);
|
||||
|
||||
if (count($query) > 0)
|
||||
{
|
||||
// Unserialize the configuration values
|
||||
$config = array_map('unserialize', $query->as_array('config_key', 'config_value'));
|
||||
}
|
||||
}
|
||||
|
||||
return parent::load($group, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overload setting offsets to insert or update the database values as
|
||||
* changes occur.
|
||||
*
|
||||
* @param string array key
|
||||
* @param mixed new value
|
||||
* @return mixed
|
||||
*/
|
||||
public function offsetSet($key, $value)
|
||||
{
|
||||
if ( ! $this->offsetExists($key))
|
||||
{
|
||||
// Insert a new value
|
||||
DB::insert($this->_database_table, array('group_name', 'config_key', 'config_value'))
|
||||
->values(array($this->_configuration_group, $key, serialize($value)))
|
||||
->execute($this->_database_instance);
|
||||
}
|
||||
elseif ($this->offsetGet($key) !== $value)
|
||||
{
|
||||
// Update the value
|
||||
DB::update($this->_database_table)
|
||||
->value('config_value', serialize($value))
|
||||
->where('group_name', '=', $this->_configuration_group)
|
||||
->where('config_key', '=', $key)
|
||||
->execute($this->_database_instance);
|
||||
}
|
||||
|
||||
return parent::offsetSet($key, $value);
|
||||
}
|
||||
|
||||
} // End Kohana_Config_Database
|
@@ -1,62 +0,0 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
/**
|
||||
* Database expressions can be used to add unescaped SQL fragments to a
|
||||
* [Database_Query_Builder] object.
|
||||
*
|
||||
* For example, you can use an expression to generate a column alias:
|
||||
*
|
||||
* // SELECT CONCAT(first_name, last_name) AS full_name
|
||||
* $query = DB::select(array(DB::expr('CONCAT(first_name, last_name)'), 'full_name')));
|
||||
*
|
||||
* More examples are available on the [Query Builder](database/query/builder#database-expressions) page
|
||||
*
|
||||
* @package Kohana/Database
|
||||
* @category Base
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
class Kohana_Database_Expression {
|
||||
|
||||
// Raw expression string
|
||||
protected $_value;
|
||||
|
||||
/**
|
||||
* Sets the expression string.
|
||||
*
|
||||
* $expression = new Database_Expression('COUNT(users.id)');
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($value)
|
||||
{
|
||||
// Set the expression string
|
||||
$this->_value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the expression value as a string.
|
||||
*
|
||||
* $sql = $expression->value();
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function value()
|
||||
{
|
||||
return (string) $this->_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value of the expression as a string.
|
||||
*
|
||||
* echo $expression;
|
||||
*
|
||||
* @return string
|
||||
* @uses Database_Expression::value
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->value();
|
||||
}
|
||||
|
||||
} // End Database_Expression
|
@@ -1,3 +0,0 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
class Session_Database extends Kohana_Session_Database {}
|
Reference in New Issue
Block a user