summaryrefslogtreecommitdiff
path: root/kohana/libraries/drivers/Database
diff options
context:
space:
mode:
Diffstat (limited to 'kohana/libraries/drivers/Database')
-rw-r--r--kohana/libraries/drivers/Database/Mssql.php456
-rw-r--r--kohana/libraries/drivers/Database/Mysql.php492
-rw-r--r--kohana/libraries/drivers/Database/Mysqli.php369
-rw-r--r--kohana/libraries/drivers/Database/Pdosqlite.php473
-rw-r--r--kohana/libraries/drivers/Database/Pgsql.php552
5 files changed, 2342 insertions, 0 deletions
diff --git a/kohana/libraries/drivers/Database/Mssql.php b/kohana/libraries/drivers/Database/Mssql.php
new file mode 100644
index 00000000..644d4a13
--- /dev/null
+++ b/kohana/libraries/drivers/Database/Mssql.php
@@ -0,0 +1,456 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * MSSQL Database Driver
+ *
+ * @package Core
+ * @author Kohana Team
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class Database_Mssql_Driver extends Database_Driver
+{
+ /**
+ * Database connection link
+ */
+ protected $link;
+
+ /**
+ * Database configuration
+ */
+ protected $db_config;
+
+ /**
+ * Sets the config for the class.
+ *
+ * @param array database configuration
+ */
+ public function __construct($config)
+ {
+ $this->db_config = $config;
+
+ Kohana::log('debug', 'MSSQL Database Driver Initialized');
+ }
+
+ /**
+ * Closes the database connection.
+ */
+ public function __destruct()
+ {
+ is_resource($this->link) and mssql_close($this->link);
+ }
+
+ /**
+ * Make the connection
+ *
+ * @return return connection
+ */
+ public function connect()
+ {
+ // Check if link already exists
+ if (is_resource($this->link))
+ return $this->link;
+
+ // Import the connect variables
+ extract($this->db_config['connection']);
+
+ // Persistent connections enabled?
+ $connect = ($this->db_config['persistent'] == TRUE) ? 'mssql_pconnect' : 'mssql_connect';
+
+ // Build the connection info
+ $host = isset($host) ? $host : $socket;
+ $port = (isset($port) AND is_string($port)) ? ':'.$port : '';
+
+ // Make the connection and select the database
+ if (($this->link = $connect($host.$port, $user, $pass, TRUE)) AND mssql_select_db($database, $this->link))
+ {
+ /* This is being removed so I can use it, will need to come up with a more elegant workaround in the future...
+ *
+ if ($charset = $this->db_config['character_set'])
+ {
+ $this->set_charset($charset);
+ }
+ */
+
+ // Clear password after successful connect
+ $this->config['connection']['pass'] = NULL;
+
+ return $this->link;
+ }
+
+ return FALSE;
+ }
+
+ public function query($sql)
+ {
+ // Only cache if it's turned on, and only cache if it's not a write statement
+ if ($this->db_config['cache'] AND ! preg_match('#\b(?:INSERT|UPDATE|REPLACE|SET)\b#i', $sql))
+ {
+ $hash = $this->query_hash($sql);
+
+ if ( ! isset(self::$query_cache[$hash]))
+ {
+ // Set the cached object
+ self::$query_cache[$hash] = new Mssql_Result(mssql_query($sql, $this->link), $this->link, $this->db_config['object'], $sql);
+ }
+
+ // Return the cached query
+ return self::$query_cache[$hash];
+ }
+
+ return new Mssql_Result(mssql_query($sql, $this->link), $this->link, $this->db_config['object'], $sql);
+ }
+
+ public function escape_table($table)
+ {
+ if (stripos($table, ' AS ') !== FALSE)
+ {
+ // Force 'AS' to uppercase
+ $table = str_ireplace(' AS ', ' AS ', $table);
+
+ // Runs escape_table on both sides of an AS statement
+ $table = array_map(array($this, __FUNCTION__), explode(' AS ', $table));
+
+ // Re-create the AS statement
+ return implode(' AS ', $table);
+ }
+ return '['.str_replace('.', '[.]', $table).']';
+ }
+
+ public function escape_column($column)
+ {
+ if (!$this->db_config['escape'])
+ return $column;
+
+ if (strtolower($column) == 'count(*)' OR $column == '*')
+ return $column;
+
+ // This matches any modifiers we support to SELECT.
+ if ( ! preg_match('/\b(?:rand|all|distinct(?:row)?|high_priority|sql_(?:small_result|b(?:ig_result|uffer_result)|no_cache|ca(?:che|lc_found_rows)))\s/i', $column))
+ {
+ if (stripos($column, ' AS ') !== FALSE)
+ {
+ // Force 'AS' to uppercase
+ $column = str_ireplace(' AS ', ' AS ', $column);
+
+ // Runs escape_column on both sides of an AS statement
+ $column = array_map(array($this, __FUNCTION__), explode(' AS ', $column));
+
+ // Re-create the AS statement
+ return implode(' AS ', $column);
+ }
+
+ return preg_replace('/[^.*]+/', '[$0]', $column);
+ }
+
+ $parts = explode(' ', $column);
+ $column = '';
+
+ for ($i = 0, $c = count($parts); $i < $c; $i++)
+ {
+ // The column is always last
+ if ($i == ($c - 1))
+ {
+ $column .= preg_replace('/[^.*]+/', '[$0]', $parts[$i]);
+ }
+ else // otherwise, it's a modifier
+ {
+ $column .= $parts[$i].' ';
+ }
+ }
+ return $column;
+ }
+
+ /**
+ * Limit in SQL Server 2000 only uses the keyword
+ * 'TOP'; 2007 may have an offset keyword, but
+ * I am unsure - for pagination style limit,offset
+ * functionality, a fancy query needs to be built.
+ *
+ * @param unknown_type $limit
+ * @return unknown
+ */
+ public function limit($limit, $offset=null)
+ {
+ return 'TOP '.$limit;
+ }
+
+ public function compile_select($database)
+ {
+ $sql = ($database['distinct'] == TRUE) ? 'SELECT DISTINCT ' : 'SELECT ';
+ $sql .= (count($database['select']) > 0) ? implode(', ', $database['select']) : '*';
+
+ if (count($database['from']) > 0)
+ {
+ // Escape the tables
+ $froms = array();
+ foreach ($database['from'] as $from)
+ $froms[] = $this->escape_column($from);
+ $sql .= "\nFROM ";
+ $sql .= implode(', ', $froms);
+ }
+
+ if (count($database['join']) > 0)
+ {
+ foreach($database['join'] AS $join)
+ {
+ $sql .= "\n".$join['type'].'JOIN '.implode(', ', $join['tables']).' ON '.$join['conditions'];
+ }
+ }
+
+ if (count($database['where']) > 0)
+ {
+ $sql .= "\nWHERE ";
+ }
+
+ $sql .= implode("\n", $database['where']);
+
+ if (count($database['groupby']) > 0)
+ {
+ $sql .= "\nGROUP BY ";
+ $sql .= implode(', ', $database['groupby']);
+ }
+
+ if (count($database['having']) > 0)
+ {
+ $sql .= "\nHAVING ";
+ $sql .= implode("\n", $database['having']);
+ }
+
+ if (count($database['orderby']) > 0)
+ {
+ $sql .= "\nORDER BY ";
+ $sql .= implode(', ', $database['orderby']);
+ }
+
+ if (is_numeric($database['limit']))
+ {
+ $sql .= "\n";
+ $sql .= $this->limit($database['limit']);
+ }
+
+ return $sql;
+ }
+
+ public function escape_str($str)
+ {
+ if (!$this->db_config['escape'])
+ return $str;
+
+ is_resource($this->link) or $this->connect();
+ //mssql_real_escape_string($str, $this->link); <-- this function doesn't exist
+
+ $characters = array('/\x00/', '/\x1a/', '/\n/', '/\r/', '/\\\/', '/\'/');
+ $replace = array('\\\x00', '\\x1a', '\\n', '\\r', '\\\\', "''");
+ return preg_replace($characters, $replace, $str);
+ }
+
+ public function list_tables(Database $db)
+ {
+ $sql = 'SHOW TABLES FROM ['.$this->db_config['connection']['database'].']';
+ $result = $this->query($sql)->result(FALSE, MSSQL_ASSOC);
+
+ $retval = array();
+ foreach ($result as $row)
+ {
+ $retval[] = current($row);
+ }
+
+ return $retval;
+ }
+
+ public function show_error()
+ {
+ return mssql_get_last_message($this->link);
+ }
+
+ public function list_fields($table)
+ {
+ static $tables;
+
+ if (empty($tables[$table]))
+ {
+ foreach ($this->field_data($table) as $row)
+ {
+ // Make an associative array
+ $tables[$table][$row->Field] = $this->sql_type($row->Type);
+ }
+ }
+
+ return $tables[$table];
+ }
+
+ public function field_data($table)
+ {
+ $columns = array();
+
+ if ($query = MSSQL_query('SHOW COLUMNS FROM '.$this->escape_table($table), $this->link))
+ {
+ if (MSSQL_num_rows($query) > 0)
+ {
+ while ($row = MSSQL_fetch_object($query))
+ {
+ $columns[] = $row;
+ }
+ }
+ }
+
+ return $columns;
+ }
+}
+
+/**
+ * MSSQL Result
+ */
+class Mssql_Result extends Database_Result {
+
+ // Fetch function and return type
+ protected $fetch_type = 'mssql_fetch_object';
+ protected $return_type = MSSQL_ASSOC;
+
+ /**
+ * Sets up the result variables.
+ *
+ * @param resource query result
+ * @param resource database link
+ * @param boolean return objects or arrays
+ * @param string SQL query that was run
+ */
+ public function __construct($result, $link, $object = TRUE, $sql)
+ {
+ $this->result = $result;
+
+ // If the query is a resource, it was a SELECT, SHOW, DESCRIBE, EXPLAIN query
+ if (is_resource($result))
+ {
+ $this->current_row = 0;
+ $this->total_rows = mssql_num_rows($this->result);
+ $this->fetch_type = ($object === TRUE) ? 'mssql_fetch_object' : 'mssql_fetch_array';
+ }
+ elseif (is_bool($result))
+ {
+ if ($result == FALSE)
+ {
+ // SQL error
+ throw new Kohana_Database_Exception('database.error', mssql_get_last_message($link).' - '.$sql);
+ }
+ else
+ {
+ // Its an DELETE, INSERT, REPLACE, or UPDATE querys
+ $last_id = mssql_query('SELECT @@IDENTITY AS last_id', $link);
+ $result = mssql_fetch_assoc($last_id);
+ $this->insert_id = $result['last_id'];
+ $this->total_rows = mssql_rows_affected($link);
+ }
+ }
+
+ // Set result type
+ $this->result($object);
+
+ // Store the SQL
+ $this->sql = $sql;
+ }
+
+ /**
+ * Destruct, the cleanup crew!
+ */
+ public function __destruct()
+ {
+ if (is_resource($this->result))
+ {
+ mssql_free_result($this->result);
+ }
+ }
+
+ public function result($object = TRUE, $type = MSSQL_ASSOC)
+ {
+ $this->fetch_type = ((bool) $object) ? 'mssql_fetch_object' : 'mssql_fetch_array';
+
+ // This check has to be outside the previous statement, because we do not
+ // know the state of fetch_type when $object = NULL
+ // NOTE - The class set by $type must be defined before fetching the result,
+ // autoloading is disabled to save a lot of stupid overhead.
+ if ($this->fetch_type == 'mssql_fetch_object')
+ {
+ $this->return_type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ else
+ {
+ $this->return_type = $type;
+ }
+
+ return $this;
+ }
+
+ public function as_array($object = NULL, $type = MSSQL_ASSOC)
+ {
+ return $this->result_array($object, $type);
+ }
+
+ public function result_array($object = NULL, $type = MSSQL_ASSOC)
+ {
+ $rows = array();
+
+ if (is_string($object))
+ {
+ $fetch = $object;
+ }
+ elseif (is_bool($object))
+ {
+ if ($object === TRUE)
+ {
+ $fetch = 'mssql_fetch_object';
+
+ // NOTE - The class set by $type must be defined before fetching the result,
+ // autoloading is disabled to save a lot of stupid overhead.
+ $type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ else
+ {
+ $fetch = 'mssql_fetch_array';
+ }
+ }
+ else
+ {
+ // Use the default config values
+ $fetch = $this->fetch_type;
+
+ if ($fetch == 'mssql_fetch_object')
+ {
+ $type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ }
+
+ if (mssql_num_rows($this->result))
+ {
+ // Reset the pointer location to make sure things work properly
+ mssql_data_seek($this->result, 0);
+
+ while ($row = $fetch($this->result, $type))
+ {
+ $rows[] = $row;
+ }
+ }
+
+ return isset($rows) ? $rows : array();
+ }
+
+ public function list_fields()
+ {
+ $field_names = array();
+ while ($field = mssql_fetch_field($this->result))
+ {
+ $field_names[] = $field->name;
+ }
+
+ return $field_names;
+ }
+
+ public function seek($offset)
+ {
+ if ( ! $this->offsetExists($offset))
+ return FALSE;
+
+ return mssql_data_seek($this->result, $offset);
+ }
+
+} // End mssql_Result Class \ No newline at end of file
diff --git a/kohana/libraries/drivers/Database/Mysql.php b/kohana/libraries/drivers/Database/Mysql.php
new file mode 100644
index 00000000..fd20a08e
--- /dev/null
+++ b/kohana/libraries/drivers/Database/Mysql.php
@@ -0,0 +1,492 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * MySQL Database Driver
+ *
+ * $Id$
+ *
+ * @package Core
+ * @author Kohana Team
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class Database_Mysql_Driver extends Database_Driver {
+
+ /**
+ * Database connection link
+ */
+ protected $link;
+
+ /**
+ * Database configuration
+ */
+ protected $db_config;
+
+ /**
+ * Sets the config for the class.
+ *
+ * @param array database configuration
+ */
+ public function __construct($config)
+ {
+ $this->db_config = $config;
+
+ Kohana::log('debug', 'MySQL Database Driver Initialized');
+ }
+
+ /**
+ * Closes the database connection.
+ */
+ public function __destruct()
+ {
+ is_resource($this->link) and mysql_close($this->link);
+ }
+
+ public function connect()
+ {
+ // Check if link already exists
+ if (is_resource($this->link))
+ return $this->link;
+
+ // Import the connect variables
+ extract($this->db_config['connection']);
+
+ // Persistent connections enabled?
+ $connect = ($this->db_config['persistent'] == TRUE) ? 'mysql_pconnect' : 'mysql_connect';
+
+ // Build the connection info
+ $host = isset($host) ? $host : $socket;
+ $port = isset($port) ? ':'.$port : '';
+
+ // Make the connection and select the database
+ if (($this->link = $connect($host.$port, $user, $pass, TRUE)) AND mysql_select_db($database, $this->link))
+ {
+ if ($charset = $this->db_config['character_set'])
+ {
+ $this->set_charset($charset);
+ }
+
+ // Clear password after successful connect
+ $this->config['connection']['pass'] = NULL;
+
+ return $this->link;
+ }
+
+ return FALSE;
+ }
+
+ public function query($sql)
+ {
+ // Only cache if it's turned on, and only cache if it's not a write statement
+ if ($this->db_config['cache'] AND ! preg_match('#\b(?:INSERT|UPDATE|REPLACE|SET)\b#i', $sql))
+ {
+ $hash = $this->query_hash($sql);
+
+ if ( ! isset(self::$query_cache[$hash]))
+ {
+ // Set the cached object
+ self::$query_cache[$hash] = new Mysql_Result(mysql_query($sql, $this->link), $this->link, $this->db_config['object'], $sql);
+ }
+
+ // Return the cached query
+ return self::$query_cache[$hash];
+ }
+
+ return new Mysql_Result(mysql_query($sql, $this->link), $this->link, $this->db_config['object'], $sql);
+ }
+
+ public function set_charset($charset)
+ {
+ $this->query('SET NAMES '.$this->escape_str($charset));
+ }
+
+ public function escape_table($table)
+ {
+ if (!$this->db_config['escape'])
+ return $table;
+
+ if (stripos($table, ' AS ') !== FALSE)
+ {
+ // Force 'AS' to uppercase
+ $table = str_ireplace(' AS ', ' AS ', $table);
+
+ // Runs escape_table on both sides of an AS statement
+ $table = array_map(array($this, __FUNCTION__), explode(' AS ', $table));
+
+ // Re-create the AS statement
+ return implode(' AS ', $table);
+ }
+ return '`'.str_replace('.', '`.`', $table).'`';
+ }
+
+ public function escape_column($column)
+ {
+ if (!$this->db_config['escape'])
+ return $column;
+
+ if (strtolower($column) == 'count(*)' OR $column == '*')
+ return $column;
+
+ // This matches any modifiers we support to SELECT.
+ if ( ! preg_match('/\b(?:rand|all|distinct(?:row)?|high_priority|sql_(?:small_result|b(?:ig_result|uffer_result)|no_cache|ca(?:che|lc_found_rows)))\s/i', $column))
+ {
+ if (stripos($column, ' AS ') !== FALSE)
+ {
+ // Force 'AS' to uppercase
+ $column = str_ireplace(' AS ', ' AS ', $column);
+
+ // Runs escape_column on both sides of an AS statement
+ $column = array_map(array($this, __FUNCTION__), explode(' AS ', $column));
+
+ // Re-create the AS statement
+ return implode(' AS ', $column);
+ }
+
+ return preg_replace('/[^.*]+/', '`$0`', $column);
+ }
+
+ $parts = explode(' ', $column);
+ $column = '';
+
+ for ($i = 0, $c = count($parts); $i < $c; $i++)
+ {
+ // The column is always last
+ if ($i == ($c - 1))
+ {
+ $column .= preg_replace('/[^.*]+/', '`$0`', $parts[$i]);
+ }
+ else // otherwise, it's a modifier
+ {
+ $column .= $parts[$i].' ';
+ }
+ }
+ return $column;
+ }
+
+ public function regex($field, $match = '', $type = 'AND ', $num_regexs)
+ {
+ $prefix = ($num_regexs == 0) ? '' : $type;
+
+ return $prefix.' '.$this->escape_column($field).' REGEXP \''.$this->escape_str($match).'\'';
+ }
+
+ public function notregex($field, $match = '', $type = 'AND ', $num_regexs)
+ {
+ $prefix = $num_regexs == 0 ? '' : $type;
+
+ return $prefix.' '.$this->escape_column($field).' NOT REGEXP \''.$this->escape_str($match) . '\'';
+ }
+
+ public function merge($table, $keys, $values)
+ {
+ // Escape the column names
+ foreach ($keys as $key => $value)
+ {
+ $keys[$key] = $this->escape_column($value);
+ }
+ return 'REPLACE INTO '.$this->escape_table($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
+ }
+
+ public function limit($limit, $offset = 0)
+ {
+ return 'LIMIT '.$offset.', '.$limit;
+ }
+
+ public function compile_select($database)
+ {
+ $sql = ($database['distinct'] == TRUE) ? 'SELECT DISTINCT ' : 'SELECT ';
+ $sql .= (count($database['select']) > 0) ? implode(', ', $database['select']) : '*';
+
+ if (count($database['from']) > 0)
+ {
+ // Escape the tables
+ $froms = array();
+ foreach ($database['from'] as $from)
+ {
+ $froms[] = $this->escape_column($from);
+ }
+ $sql .= "\nFROM ";
+ $sql .= implode(', ', $froms);
+ }
+
+ if (count($database['join']) > 0)
+ {
+ foreach($database['join'] AS $join)
+ {
+ $sql .= "\n".$join['type'].'JOIN '.implode(', ', $join['tables']).' ON '.$join['conditions'];
+ }
+ }
+
+ if (count($database['where']) > 0)
+ {
+ $sql .= "\nWHERE ";
+ }
+
+ $sql .= implode("\n", $database['where']);
+
+ if (count($database['groupby']) > 0)
+ {
+ $sql .= "\nGROUP BY ";
+ $sql .= implode(', ', $database['groupby']);
+ }
+
+ if (count($database['having']) > 0)
+ {
+ $sql .= "\nHAVING ";
+ $sql .= implode("\n", $database['having']);
+ }
+
+ if (count($database['orderby']) > 0)
+ {
+ $sql .= "\nORDER BY ";
+ $sql .= implode(', ', $database['orderby']);
+ }
+
+ if (is_numeric($database['limit']))
+ {
+ $sql .= "\n";
+ $sql .= $this->limit($database['limit'], $database['offset']);
+ }
+
+ return $sql;
+ }
+
+ public function escape_str($str)
+ {
+ if (!$this->db_config['escape'])
+ return $str;
+
+ is_resource($this->link) or $this->connect();
+
+ return mysql_real_escape_string($str, $this->link);
+ }
+
+ public function list_tables(Database $db)
+ {
+ static $tables;
+
+ if (empty($tables) AND $query = $db->query('SHOW TABLES FROM '.$this->escape_table($this->db_config['connection']['database'])))
+ {
+ foreach ($query->result(FALSE) as $row)
+ {
+ $tables[] = current($row);
+ }
+ }
+
+ return $tables;
+ }
+
+ public function show_error()
+ {
+ return mysql_error($this->link);
+ }
+
+ public function list_fields($table)
+ {
+ static $tables;
+
+ if (empty($tables[$table]))
+ {
+ foreach ($this->field_data($table) as $row)
+ {
+ // Make an associative array
+ $tables[$table][$row->Field] = $this->sql_type($row->Type);
+
+ if ($row->Key === 'PRI' AND $row->Extra === 'auto_increment')
+ {
+ // For sequenced (AUTO_INCREMENT) tables
+ $tables[$table][$row->Field]['sequenced'] = TRUE;
+ }
+
+ if ($row->Null === 'YES')
+ {
+ // Set NULL status
+ $tables[$table][$row->Field]['null'] = TRUE;
+ }
+ }
+ }
+
+ if (!isset($tables[$table]))
+ throw new Kohana_Database_Exception('database.table_not_found', $table);
+
+ return $tables[$table];
+ }
+
+ public function field_data($table)
+ {
+ $columns = array();
+
+ if ($query = mysql_query('SHOW COLUMNS FROM '.$this->escape_table($table), $this->link))
+ {
+ if (mysql_num_rows($query) > 0)
+ {
+ while ($row = mysql_fetch_object($query))
+ {
+ $columns[] = $row;
+ }
+ }
+ }
+
+ return $columns;
+ }
+
+} // End Database_Mysql_Driver Class
+
+/**
+ * MySQL Result
+ */
+class Mysql_Result extends Database_Result {
+
+ // Fetch function and return type
+ protected $fetch_type = 'mysql_fetch_object';
+ protected $return_type = MYSQL_ASSOC;
+
+ /**
+ * Sets up the result variables.
+ *
+ * @param resource query result
+ * @param resource database link
+ * @param boolean return objects or arrays
+ * @param string SQL query that was run
+ */
+ public function __construct($result, $link, $object = TRUE, $sql)
+ {
+ $this->result = $result;
+
+ // If the query is a resource, it was a SELECT, SHOW, DESCRIBE, EXPLAIN query
+ if (is_resource($result))
+ {
+ $this->current_row = 0;
+ $this->total_rows = mysql_num_rows($this->result);
+ $this->fetch_type = ($object === TRUE) ? 'mysql_fetch_object' : 'mysql_fetch_array';
+ }
+ elseif (is_bool($result))
+ {
+ if ($result == FALSE)
+ {
+ // SQL error
+ throw new Kohana_Database_Exception('database.error', mysql_error($link).' - '.$sql);
+ }
+ else
+ {
+ // Its an DELETE, INSERT, REPLACE, or UPDATE query
+ $this->insert_id = mysql_insert_id($link);
+ $this->total_rows = mysql_affected_rows($link);
+ }
+ }
+
+ // Set result type
+ $this->result($object);
+
+ // Store the SQL
+ $this->sql = $sql;
+ }
+
+ /**
+ * Destruct, the cleanup crew!
+ */
+ public function __destruct()
+ {
+ if (is_resource($this->result))
+ {
+ mysql_free_result($this->result);
+ }
+ }
+
+ public function result($object = TRUE, $type = MYSQL_ASSOC)
+ {
+ $this->fetch_type = ((bool) $object) ? 'mysql_fetch_object' : 'mysql_fetch_array';
+
+ // This check has to be outside the previous statement, because we do not
+ // know the state of fetch_type when $object = NULL
+ // NOTE - The class set by $type must be defined before fetching the result,
+ // autoloading is disabled to save a lot of stupid overhead.
+ if ($this->fetch_type == 'mysql_fetch_object' AND $object === TRUE)
+ {
+ $this->return_type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ else
+ {
+ $this->return_type = $type;
+ }
+
+ return $this;
+ }
+
+ public function as_array($object = NULL, $type = MYSQL_ASSOC)
+ {
+ return $this->result_array($object, $type);
+ }
+
+ public function result_array($object = NULL, $type = MYSQL_ASSOC)
+ {
+ $rows = array();
+
+ if (is_string($object))
+ {
+ $fetch = $object;
+ }
+ elseif (is_bool($object))
+ {
+ if ($object === TRUE)
+ {
+ $fetch = 'mysql_fetch_object';
+
+ $type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ else
+ {
+ $fetch = 'mysql_fetch_array';
+ }
+ }
+ else
+ {
+ // Use the default config values
+ $fetch = $this->fetch_type;
+
+ if ($fetch == 'mysql_fetch_object')
+ {
+ $type = (is_string($this->return_type) AND Kohana::auto_load($this->return_type)) ? $this->return_type : 'stdClass';
+ }
+ }
+
+ if (mysql_num_rows($this->result))
+ {
+ // Reset the pointer location to make sure things work properly
+ mysql_data_seek($this->result, 0);
+
+ while ($row = $fetch($this->result, $type))
+ {
+ $rows[] = $row;
+ }
+ }
+
+ return isset($rows) ? $rows : array();
+ }
+
+ public function list_fields()
+ {
+ $field_names = array();
+ while ($field = mysql_fetch_field($this->result))
+ {
+ $field_names[] = $field->name;
+ }
+
+ return $field_names;
+ }
+
+ public function seek($offset)
+ {
+ if ($this->offsetExists($offset) AND mysql_data_seek($this->result, $offset))
+ {
+ // Set the current row to the offset
+ $this->current_row = $offset;
+
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+ }
+
+} // End Mysql_Result Class \ No newline at end of file
diff --git a/kohana/libraries/drivers/Database/Mysqli.php b/kohana/libraries/drivers/Database/Mysqli.php
new file mode 100644
index 00000000..206d42f2
--- /dev/null
+++ b/kohana/libraries/drivers/Database/Mysqli.php
@@ -0,0 +1,369 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * MySQLi Database Driver
+ *
+ * $Id$
+ *
+ * @package Core
+ * @author Kohana Team
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class Database_Mysqli_Driver extends Database_Mysql_Driver {
+
+ // Database connection link
+ protected $link;
+ protected $db_config;
+ protected $statements = array();
+
+ /**
+ * Sets the config for the class.
+ *
+ * @param array database configuration
+ */
+ public function __construct($config)
+ {
+ $this->db_config = $config;
+
+ Kohana::log('debug', 'MySQLi Database Driver Initialized');
+ }
+
+ /**
+ * Closes the database connection.
+ */
+ public function __destruct()
+ {
+ is_object($this->link) and $this->link->close();
+ }
+
+ public function connect()
+ {
+ // Check if link already exists
+ if (is_object($this->link))
+ return $this->link;
+
+ // Import the connect variables
+ extract($this->db_config['connection']);
+
+ // Build the connection info
+ $host = isset($host) ? $host : $socket;
+
+ // Make the connection and select the database
+ if ($this->link = new mysqli($host, $user, $pass, $database, $port))
+ {
+ if ($charset = $this->db_config['character_set'])
+ {
+ $this->set_charset($charset);
+ }
+
+ // Clear password after successful connect
+ $this->config['connection']['pass'] = NULL;
+
+ return $this->link;
+ }
+
+ return FALSE;
+ }
+
+ public function query($sql)
+ {
+ // Only cache if it's turned on, and only cache if it's not a write statement
+ if ($this->db_config['cache'] AND ! preg_match('#\b(?:INSERT|UPDATE|REPLACE|SET)\b#i', $sql))
+ {
+ $hash = $this->query_hash($sql);
+
+ if ( ! isset(self::$query_cache[$hash]))
+ {
+ // Set the cached object
+ self::$query_cache[$hash] = new Kohana_Mysqli_Result($this->link, $this->db_config['object'], $sql);
+ }
+
+ // Return the cached query
+ return self::$query_cache[$hash];
+ }
+
+ return new Kohana_Mysqli_Result($this->link, $this->db_config['object'], $sql);
+ }
+
+ public function set_charset($charset)
+ {
+ if ($this->link->set_charset($charset) === FALSE)
+ throw new Kohana_Database_Exception('database.error', $this->show_error());
+ }
+
+ public function stmt_prepare($sql = '')
+ {
+ is_object($this->link) or $this->connect();
+ return new Kohana_Mysqli_Statement($sql, $this->link);
+ }
+
+ public function escape_str($str)
+ {
+ if (!$this->db_config['escape'])
+ return $str;
+
+ is_object($this->link) or $this->connect();
+
+ return $this->link->real_escape_string($str);
+ }
+
+ public function show_error()
+ {
+ return $this->link->error;
+ }
+
+ public function field_data($table)
+ {
+ $query = $this->link->query('SHOW COLUMNS FROM '.$this->escape_table($table));
+
+ $table = array();
+ while ($row = $query->fetch_object())
+ {
+ $table[] = $row;
+ }
+
+ return $table;
+ }
+
+} // End Database_Mysqli_Driver Class
+
+/**
+ * MySQLi Result
+ */
+class Kohana_Mysqli_Result extends Database_Result {
+
+ // Database connection
+ protected $link;
+
+ // Data fetching types
+ protected $fetch_type = 'mysqli_fetch_object';
+ protected $return_type = MYSQLI_ASSOC;
+
+ /**
+ * Sets up the result variables.
+ *
+ * @param object database link
+ * @param boolean return objects or arrays
+ * @param string SQL query that was run
+ */
+ public function __construct($link, $object = TRUE, $sql)
+ {
+ $this->link = $link;
+
+ if ( ! $this->link->multi_query($sql))
+ {
+ // SQL error
+ throw new Kohana_Database_Exception('database.error', $this->link->error.' - '.$sql);
+ }
+ else
+ {
+ $this->result = $this->link->store_result();
+
+ // If the query is an object, it was a SELECT, SHOW, DESCRIBE, EXPLAIN query
+ if (is_object($this->result))
+ {
+ $this->current_row = 0;
+ $this->total_rows = $this->result->num_rows;
+ $this->fetch_type = ($object === TRUE) ? 'fetch_object' : 'fetch_array';
+ }
+ elseif ($this->link->error)
+ {
+ // SQL error
+ throw new Kohana_Database_Exception('database.error', $this->link->error.' - '.$sql);
+ }
+ else
+ {
+ // Its an DELETE, INSERT, REPLACE, or UPDATE query
+ $this->insert_id = $this->link->insert_id;
+ $this->total_rows = $this->link->affected_rows;
+ }
+ }
+
+ // Set result type
+ $this->result($object);
+
+ // Store the SQL
+ $this->sql = $sql;
+ }
+
+ /**
+ * Magic __destruct function, frees the result.
+ */
+ public function __destruct()
+ {
+ if (is_object($this->result))
+ {
+ $this->result->free_result();
+
+ // this is kinda useless, but needs to be done to avoid the "Commands out of sync; you
+ // can't run this command now" error. Basically, we get all results after the first one
+ // (the one we actually need) and free them.
+ if (is_resource($this->link) AND $this->link->more_results())
+ {
+ do
+ {
+ if ($result = $this->link->store_result())
+ {
+ $result->free_result();
+ }
+ } while ($this->link->next_result());
+ }
+ }
+ }
+
+ public function result($object = TRUE, $type = MYSQLI_ASSOC)
+ {
+ $this->fetch_type = ((bool) $object) ? 'fetch_object' : 'fetch_array';
+
+ // This check has to be outside the previous statement, because we do not
+ // know the state of fetch_type when $object = NULL
+ // NOTE - The class set by $type must be defined before fetching the result,
+ // autoloading is disabled to save a lot of stupid overhead.
+ if ($this->fetch_type == 'fetch_object')
+ {
+ $this->return_type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ else
+ {
+ $this->return_type = $type;
+ }
+
+ return $this;
+ }
+
+ public function as_array($object = NULL, $type = MYSQLI_ASSOC)
+ {
+ return $this->result_array($object, $type);
+ }
+
+ public function result_array($object = NULL, $type = MYSQLI_ASSOC)
+ {
+ $rows = array();
+
+ if (is_string($object))
+ {
+ $fetch = $object;
+ }
+ elseif (is_bool($object))
+ {
+ if ($object === TRUE)
+ {
+ $fetch = 'fetch_object';
+
+ // NOTE - The class set by $type must be defined before fetching the result,
+ // autoloading is disabled to save a lot of stupid overhead.
+ $type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ else
+ {
+ $fetch = 'fetch_array';
+ }
+ }
+ else
+ {
+ // Use the default config values
+ $fetch = $this->fetch_type;
+
+ if ($fetch == 'fetch_object')
+ {
+ $type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ }
+
+ if ($this->result->num_rows)
+ {
+ // Reset the pointer location to make sure things work properly
+ $this->result->data_seek(0);
+
+ while ($row = $this->result->$fetch($type))
+ {
+ $rows[] = $row;
+ }
+ }
+
+ return isset($rows) ? $rows : array();
+ }
+
+ public function list_fields()
+ {
+ $field_names = array();
+ while ($field = $this->result->fetch_field())
+ {
+ $field_names[] = $field->name;
+ }
+
+ return $field_names;
+ }
+
+ public function seek($offset)
+ {
+ if ( ! $this->offsetExists($offset))
+ return FALSE;
+
+ $this->result->data_seek($offset);
+
+ return TRUE;
+ }
+
+ public function offsetGet($offset)
+ {
+ if ( ! $this->seek($offset))
+ return FALSE;
+
+ // Return the row
+ $fetch = $this->fetch_type;
+ return $this->result->$fetch($this->return_type);
+ }
+
+} // End Mysqli_Result Class
+
+/**
+ * MySQLi Prepared Statement (experimental)
+ */
+class Kohana_Mysqli_Statement {
+
+ protected $link = NULL;
+ protected $stmt;
+ protected $var_names = array();
+ protected $var_values = array();
+
+ public function __construct($sql, $link)
+ {
+ $this->link = $link;
+
+ $this->stmt = $this->link->prepare($sql);
+
+ return $this;
+ }
+
+ public function __destruct()
+ {
+ $this->stmt->close();
+ }
+
+ // Sets the bind parameters
+ public function bind_params($param_types, $params)
+ {
+ $this->var_names = array_keys($params);
+ $this->var_values = array_values($params);
+ call_user_func_array(array($this->stmt, 'bind_param'), array_merge($param_types, $var_names));
+
+ return $this;
+ }
+
+ public function bind_result($params)
+ {
+ call_user_func_array(array($this->stmt, 'bind_result'), $params);
+ }
+
+ // Runs the statement
+ public function execute()
+ {
+ foreach ($this->var_names as $key => $name)
+ {
+ $$name = $this->var_values[$key];
+ }
+ $this->stmt->execute();
+ return $this->stmt;
+ }
+} \ No newline at end of file
diff --git a/kohana/libraries/drivers/Database/Pdosqlite.php b/kohana/libraries/drivers/Database/Pdosqlite.php
new file mode 100644
index 00000000..5a512877
--- /dev/null
+++ b/kohana/libraries/drivers/Database/Pdosqlite.php
@@ -0,0 +1,473 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/*
+ * Class: Database_PdoSqlite_Driver
+ * Provides specific database items for Sqlite.
+ *
+ * Connection string should be, eg: "pdosqlite://path/to/database.db"
+ *
+ * Version 1.0 alpha
+ * author - Doutu, updated by gregmac
+ * copyright - (c) BSD
+ * license - <no>
+ */
+
+class Database_Pdosqlite_Driver extends Database_Driver {
+
+ // Database connection link
+ protected $link;
+ protected $db_config;
+
+ /*
+ * Constructor: __construct
+ * Sets up the config for the class.
+ *
+ * Parameters:
+ * config - database configuration
+ *
+ */
+ public function __construct($config)
+ {
+ $this->db_config = $config;
+
+ Kohana::log('debug', 'PDO:Sqlite Database Driver Initialized');
+ }
+
+ public function connect()
+ {
+ // Import the connect variables
+ extract($this->db_config['connection']);
+
+ try
+ {
+ $this->link = new PDO('sqlite:'.$socket.$database, $user, $pass,
+ array(PDO::ATTR_PERSISTENT => $this->db_config['persistent']));
+
+ $this->link->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
+ $this->link->query('PRAGMA count_changes=1;');
+
+ if ($charset = $this->db_config['character_set'])
+ {
+ $this->set_charset($charset);
+ }
+ }
+ catch (PDOException $e)
+ {
+ throw new Kohana_Database_Exception('database.error', $e->getMessage());
+ }
+
+ // Clear password after successful connect
+ $this->db_config['connection']['pass'] = NULL;
+
+ return $this->link;
+ }
+
+ public function query($sql)
+ {
+ try
+ {
+ $sth = $this->link->prepare($sql);
+ }
+ catch (PDOException $e)
+ {
+ throw new Kohana_Database_Exception('database.error', $e->getMessage());
+ }
+ return new Pdosqlite_Result($sth, $this->link, $this->db_config['object'], $sql);
+ }
+
+ public function set_charset($charset)
+ {
+ $this->link->query('PRAGMA encoding = '.$this->escape_str($charset));
+ }
+
+ public function escape_table($table)
+ {
+ if ( ! $this->db_config['escape'])
+ return $table;
+
+ return '`'.str_replace('.', '`.`', $table).'`';
+ }
+
+ public function escape_column($column)
+ {
+ if ( ! $this->db_config['escape'])
+ return $column;
+
+ if (strtolower($column) == 'count(*)' OR $column == '*')
+ return $column;
+
+ // This matches any modifiers we support to SELECT.
+ if ( ! preg_match('/\b(?:rand|all|distinct(?:row)?|high_priority|sql_(?:small_result|b(?:ig_result|uffer_result)|no_cache|ca(?:che|lc_found_rows)))\s/i', $column))
+ {
+ if (stripos($column, ' AS ') !== FALSE)
+ {
+ // Force 'AS' to uppercase
+ $column = str_ireplace(' AS ', ' AS ', $column);
+
+ // Runs escape_column on both sides of an AS statement
+ $column = array_map(array($this, __FUNCTION__), explode(' AS ', $column));
+
+ // Re-create the AS statement
+ return implode(' AS ', $column);
+ }
+
+ return preg_replace('/[^.*]+/', '`$0`', $column);
+ }
+
+ $parts = explode(' ', $column);
+ $column = '';
+
+ for ($i = 0, $c = count($parts); $i < $c; $i++)
+ {
+ // The column is always last
+ if ($i == ($c - 1))
+ {
+ $column .= preg_replace('/[^.*]+/', '`$0`', $parts[$i]);
+ }
+ else // otherwise, it's a modifier
+ {
+ $column .= $parts[$i].' ';
+ }
+ }
+ return $column;
+ }
+
+ public function limit($limit, $offset = 0)
+ {
+ return 'LIMIT '.$offset.', '.$limit;
+ }
+
+ public function compile_select($database)
+ {
+ $sql = ($database['distinct'] == TRUE) ? 'SELECT DISTINCT ' : 'SELECT ';
+ $sql .= (count($database['select']) > 0) ? implode(', ', $database['select']) : '*';
+
+ if (count($database['from']) > 0)
+ {
+ $sql .= "\nFROM ";
+ $sql .= implode(', ', $database['from']);
+ }
+
+ if (count($database['join']) > 0)
+ {
+ foreach($database['join'] AS $join)
+ {
+ $sql .= "\n".$join['type'].'JOIN '.implode(', ', $join['tables']).' ON '.$join['conditions'];
+ }
+ }
+
+ if (count($database['where']) > 0)
+ {
+ $sql .= "\nWHERE ";
+ }
+
+ $sql .= implode("\n", $database['where']);
+
+ if (count($database['groupby']) > 0)
+ {
+ $sql .= "\nGROUP BY ";
+ $sql .= implode(', ', $database['groupby']);
+ }
+
+ if (count($database['having']) > 0)
+ {
+ $sql .= "\nHAVING ";
+ $sql .= implode("\n", $database['having']);
+ }
+
+ if (count($database['orderby']) > 0)
+ {
+ $sql .= "\nORDER BY ";
+ $sql .= implode(', ', $database['orderby']);
+ }
+
+ if (is_numeric($database['limit']))
+ {
+ $sql .= "\n";
+ $sql .= $this->limit($database['limit'], $database['offset']);
+ }
+
+ return $sql;
+ }
+
+ public function escape_str($str)
+ {
+ if ( ! $this->db_config['escape'])
+ return $str;
+
+ if (function_exists('sqlite_escape_string'))
+ {
+ $res = sqlite_escape_string($str);
+ }
+ else
+ {
+ $res = str_replace("'", "''", $str);
+ }
+ return $res;
+ }
+
+ public function list_tables(Database $db)
+ {
+ $sql = "SELECT `name` FROM `sqlite_master` WHERE `type`='table' ORDER BY `name`;";
+ try
+ {
+ $result = $db->query($sql)->result(FALSE, PDO::FETCH_ASSOC);
+ $tables = array();
+ foreach ($result as $row)
+ {
+ $tables[] = current($row);
+ }
+ }
+ catch (PDOException $e)
+ {
+ throw new Kohana_Database_Exception('database.error', $e->getMessage());
+ }
+ return $tables;
+ }
+
+ public function show_error()
+ {
+ $err = $this->link->errorInfo();
+ return isset($err[2]) ? $err[2] : 'Unknown error!';
+ }
+
+ public function list_fields($table, $query = FALSE)
+ {
+ static $tables;
+ if (is_object($query))
+ {
+ if (empty($tables[$table]))
+ {
+ $tables[$table] = array();
+
+ foreach ($query->result() as $row)
+ {
+ $tables[$table][] = $row->name;
+ }
+ }
+
+ return $tables[$table];
+ }
+ else
+ {
+ $result = $this->link->query( 'PRAGMA table_info('.$this->escape_table($table).')' );
+
+ foreach ($result as $row)
+ {
+ $tables[$table][$row['name']] = $this->sql_type($row['type']);
+ }
+
+ return $tables[$table];
+ }
+ }
+
+ public function field_data($table)
+ {
+ Kohana::log('error', 'This method is under developing');
+ }
+ /**
+ * Version number query string
+ *
+ * @access public
+ * @return string
+ */
+ function version()
+ {
+ return $this->link->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
+ }
+
+} // End Database_PdoSqlite_Driver Class
+
+/*
+ * PDO-sqlite Result
+ */
+class Pdosqlite_Result extends Database_Result {
+
+ // Data fetching types
+ protected $fetch_type = PDO::FETCH_OBJ;
+ protected $return_type = PDO::FETCH_ASSOC;
+
+ /**
+ * Sets up the result variables.
+ *
+ * @param resource query result
+ * @param resource database link
+ * @param boolean return objects or arrays
+ * @param string SQL query that was run
+ */
+ public function __construct($result, $link, $object = TRUE, $sql)
+ {
+ if (is_object($result) OR $result = $link->prepare($sql))
+ {
+ // run the query
+ try
+ {
+ $result->execute();
+ }
+ catch (PDOException $e)
+ {
+ throw new Kohana_Database_Exception('database.error', $e->getMessage());
+ }
+
+ if (preg_match('/^SELECT|PRAGMA|EXPLAIN/i', $sql))
+ {
+ $this->result = $result;
+ $this->current_row = 0;
+
+ $this->total_rows = $this->sqlite_row_count();
+
+ $this->fetch_type = ($object === TRUE) ? PDO::FETCH_OBJ : PDO::FETCH_ASSOC;
+ }
+ elseif (preg_match('/^DELETE|INSERT|UPDATE/i', $sql))
+ {
+ $this->insert_id = $link->lastInsertId();
+ }
+ }
+ else
+ {
+ // SQL error
+ throw new Kohana_Database_Exception('database.error', $link->errorInfo().' - '.$sql);
+ }
+
+ // Set result type
+ $this->result($object);
+
+ // Store the SQL
+ $this->sql = $sql;
+ }
+
+ private function sqlite_row_count()
+ {
+ $count = 0;
+ while ($this->result->fetch())
+ {
+ $count++;
+ }
+
+ // The query must be re-fetched now.
+ $this->result->execute();
+
+ return $count;
+ }
+
+ /*
+ * Destructor: __destruct
+ * Magic __destruct function, frees the result.
+ */
+ public function __destruct()
+ {
+ if (is_object($this->result))
+ {
+ $this->result->closeCursor();
+ $this->result = NULL;
+ }
+ }
+
+ public function result($object = TRUE, $type = PDO::FETCH_BOTH)
+ {
+ $this->fetch_type = (bool) $object ? PDO::FETCH_OBJ : PDO::FETCH_BOTH;
+
+ if ($this->fetch_type == PDO::FETCH_OBJ)
+ {
+ $this->return_type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ else
+ {
+ $this->return_type = $type;
+ }
+
+ return $this;
+ }
+
+ public function as_array($object = NULL, $type = PDO::FETCH_ASSOC)
+ {
+ return $this->result_array($object, $type);
+ }
+
+ public function result_array($object = NULL, $type = PDO::FETCH_ASSOC)
+ {
+ $rows = array();
+
+ if (is_string($object))
+ {
+ $fetch = $object;
+ }
+ elseif (is_bool($object))
+ {
+ if ($object === TRUE)
+ {
+ $fetch = PDO::FETCH_OBJ;
+
+ // NOTE - The class set by $type must be defined before fetching the result,
+ // autoloading is disabled to save a lot of stupid overhead.
+ $type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ else
+ {
+ $fetch = PDO::FETCH_OBJ;
+ }
+ }
+ else
+ {
+ // Use the default config values
+ $fetch = $this->fetch_type;
+
+ if ($fetch == PDO::FETCH_OBJ)
+ {
+ $type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ }
+ try
+ {
+ while ($row = $this->result->fetch($fetch))
+ {
+ $rows[] = $row;
+ }
+ }
+ catch(PDOException $e)
+ {
+ throw new Kohana_Database_Exception('database.error', $e->getMessage());
+ return FALSE;
+ }
+ return $rows;
+ }
+
+ public function list_fields()
+ {
+ $field_names = array();
+ for ($i = 0, $max = $this->result->columnCount(); $i < $max; $i++)
+ {
+ $info = $this->result->getColumnMeta($i);
+ $field_names[] = $info['name'];
+ }
+ return $field_names;
+ }
+
+ public function seek($offset)
+ {
+ // To request a scrollable cursor for your PDOStatement object, you must
+ // set the PDO::ATTR_CURSOR attribute to PDO::CURSOR_SCROLL when you
+ // prepare the statement.
+ Kohana::log('error', get_class($this).' does not support scrollable cursors, '.__FUNCTION__.' call ignored');
+
+ return FALSE;
+ }
+
+ public function offsetGet($offset)
+ {
+ try
+ {
+ return $this->result->fetch($this->fetch_type, PDO::FETCH_ORI_ABS, $offset);
+ }
+ catch(PDOException $e)
+ {
+ throw new Kohana_Database_Exception('database.error', $e->getMessage());
+ }
+ }
+
+ public function rewind()
+ {
+ // Same problem that seek() has, see above.
+ return $this->seek(0);
+ }
+
+} // End PdoSqlite_Result Class \ No newline at end of file
diff --git a/kohana/libraries/drivers/Database/Pgsql.php b/kohana/libraries/drivers/Database/Pgsql.php
new file mode 100644
index 00000000..ff0a5ed5
--- /dev/null
+++ b/kohana/libraries/drivers/Database/Pgsql.php
@@ -0,0 +1,552 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * PostgreSQL 8.1+ Database Driver
+ *
+ * $Id$
+ *
+ * @package Core
+ * @author Kohana Team
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class Database_Pgsql_Driver extends Database_Driver {
+
+ // Database connection link
+ protected $link;
+ protected $db_config;
+
+ /**
+ * Sets the config for the class.
+ *
+ * @param array database configuration
+ */
+ public function __construct($config)
+ {
+ $this->db_config = $config;
+
+ Kohana::log('debug', 'PgSQL Database Driver Initialized');
+ }
+
+ public function connect()
+ {
+ // Check if link already exists
+ if (is_resource($this->link))
+ return $this->link;
+
+ // Import the connect variables
+ extract($this->db_config['connection']);
+
+ // Persistent connections enabled?
+ $connect = ($this->db_config['persistent'] == TRUE) ? 'pg_pconnect' : 'pg_connect';
+
+ // Build the connection info
+ $port = isset($port) ? 'port=\''.$port.'\'' : '';
+ $host = isset($host) ? 'host=\''.$host.'\' '.$port : ''; // if no host, connect with the socket
+
+ $connection_string = $host.' dbname=\''.$database.'\' user=\''.$user.'\' password=\''.$pass.'\'';
+ // Make the connection and select the database
+ if ($this->link = $connect($connection_string))
+ {
+ if ($charset = $this->db_config['character_set'])
+ {
+ echo $this->set_charset($charset);
+ }
+
+ // Clear password after successful connect
+ $this->config['connection']['pass'] = NULL;
+
+ return $this->link;
+ }
+
+ return FALSE;
+ }
+
+ public function query($sql)
+ {
+ // Only cache if it's turned on, and only cache if it's not a write statement
+ if ($this->db_config['cache'] AND ! preg_match('#\b(?:INSERT|UPDATE|SET)\b#i', $sql))
+ {
+ $hash = $this->query_hash($sql);
+
+ if ( ! isset(self::$query_cache[$hash]))
+ {
+ // Set the cached object
+ self::$query_cache[$hash] = new Pgsql_Result(pg_query($this->link, $sql), $this->link, $this->db_config['object'], $sql);
+ }
+
+ return self::$query_cache[$hash];
+ }
+
+ return new Pgsql_Result(pg_query($this->link, $sql), $this->link, $this->db_config['object'], $sql);
+ }
+
+ public function set_charset($charset)
+ {
+ $this->query('SET client_encoding TO '.pg_escape_string($this->link, $charset));
+ }
+
+ public function escape_table($table)
+ {
+ if (!$this->db_config['escape'])
+ return $table;
+
+ return '"'.str_replace('.', '"."', $table).'"';
+ }
+
+ public function escape_column($column)
+ {
+ if (!$this->db_config['escape'])
+ return $column;
+
+ if (strtolower($column) == 'count(*)' OR $column == '*')
+ return $column;
+
+ // This matches any modifiers we support to SELECT.
+ if ( ! preg_match('/\b(?:all|distinct)\s/i', $column))
+ {
+ if (stripos($column, ' AS ') !== FALSE)
+ {
+ // Force 'AS' to uppercase
+ $column = str_ireplace(' AS ', ' AS ', $column);
+
+ // Runs escape_column on both sides of an AS statement
+ $column = array_map(array($this, __FUNCTION__), explode(' AS ', $column));
+
+ // Re-create the AS statement
+ return implode(' AS ', $column);
+ }
+
+ return preg_replace('/[^.*]+/', '"$0"', $column);
+ }
+
+ $parts = explode(' ', $column);
+ $column = '';
+
+ for ($i = 0, $c = count($parts); $i < $c; $i++)
+ {
+ // The column is always last
+ if ($i == ($c - 1))
+ {
+ $column .= preg_replace('/[^.*]+/', '"$0"', $parts[$i]);
+ }
+ else // otherwise, it's a modifier
+ {
+ $column .= $parts[$i].' ';
+ }
+ }
+ return $column;
+ }
+
+ public function regex($field, $match = '', $type = 'AND ', $num_regexs)
+ {
+ $prefix = ($num_regexs == 0) ? '' : $type;
+
+ return $prefix.' '.$this->escape_column($field).' REGEXP \''.$this->escape_str($match).'\'';
+ }
+
+ public function notregex($field, $match = '', $type = 'AND ', $num_regexs)
+ {
+ $prefix = $num_regexs == 0 ? '' : $type;
+
+ return $prefix.' '.$this->escape_column($field).' NOT REGEXP \''.$this->escape_str($match) . '\'';
+ }
+
+ public function limit($limit, $offset = 0)
+ {
+ return 'LIMIT '.$limit.' OFFSET '.$offset;
+ }
+
+ public function stmt_prepare($sql = '')
+ {
+ is_object($this->link) or $this->connect();
+ return new Kohana_Mysqli_Statement($sql, $this->link);
+ }
+
+ public function compile_select($database)
+ {
+ $sql = ($database['distinct'] == TRUE) ? 'SELECT DISTINCT ' : 'SELECT ';
+ $sql .= (count($database['select']) > 0) ? implode(', ', $database['select']) : '*';
+
+ if (count($database['from']) > 0)
+ {
+ $sql .= "\nFROM ";
+ $sql .= implode(', ', $database['from']);
+ }
+
+ if (count($database['join']) > 0)
+ {
+ foreach($database['join'] AS $join)
+ {
+ $sql .= "\n".$join['type'].'JOIN '.implode(', ', $join['tables']).' ON '.$join['conditions'];
+ }
+ }
+
+ if (count($database['where']) > 0)
+ {
+ $sql .= "\nWHERE ";
+ }
+
+ $sql .= implode("\n", $database['where']);
+
+ if (count($database['groupby']) > 0)
+ {
+ $sql .= "\nGROUP BY ";
+ $sql .= implode(', ', $database['groupby']);
+ }
+
+ if (count($database['having']) > 0)
+ {
+ $sql .= "\nHAVING ";
+ $sql .= implode("\n", $database['having']);
+ }
+
+ if (count($database['orderby']) > 0)
+ {
+ $sql .= "\nORDER BY ";
+ $sql .= implode(', ', $database['orderby']);
+ }
+
+ if (is_numeric($database['limit']))
+ {
+ $sql .= "\n";
+ $sql .= $this->limit($database['limit'], $database['offset']);
+ }
+
+ return $sql;
+ }
+
+ public function escape_str($str)
+ {
+ if (!$this->db_config['escape'])
+ return $str;
+
+ is_resource($this->link) or $this->connect();
+
+ return pg_escape_string($this->link, $str);
+ }
+
+ public function list_tables(Database $db)
+ {
+ $sql = 'SELECT table_schema || \'.\' || table_name FROM information_schema.tables WHERE table_schema NOT IN (\'pg_catalog\', \'information_schema\')';
+ $result = $db->query($sql)->result(FALSE, PGSQL_ASSOC);
+
+ $retval = array();
+ foreach ($result as $row)
+ {
+ $retval[] = current($row);
+ }
+
+ return $retval;
+ }
+
+ public function show_error()
+ {
+ return pg_last_error($this->link);
+ }
+
+ public function list_fields($table, $query = FALSE)
+ {
+ static $tables;
+
+ if (is_object($query))
+ {
+ if (empty($tables[$table]))
+ {
+ $tables[$table] = array();
+
+ foreach ($query as $row)
+ {
+ $tables[$table][] = $row->Field;
+ }
+ }
+
+ return $tables[$table];
+ }
+
+ // WOW...REALLY?!?
+ // Taken from http://www.postgresql.org/docs/7.4/interactive/catalogs.html
+ $query = $this->query('SELECT
+ -- Field
+ pg_attribute.attname AS "Field",
+ -- Type
+ CASE pg_type.typname
+ WHEN \'int2\' THEN \'smallint\'
+ WHEN \'int4\' THEN \'int\'
+ WHEN \'int8\' THEN \'bigint\'
+ WHEN \'varchar\' THEN \'varchar(\' || pg_attribute.atttypmod-4 || \')\'
+ ELSE pg_type.typname
+ END AS "Type",
+ -- Null
+ CASE WHEN pg_attribute.attnotnull THEN \'NO\'
+ ELSE \'YES\'
+ END AS "Null",
+ -- Default
+ CASE pg_type.typname
+ WHEN \'varchar\' THEN substring(pg_attrdef.adsrc from \'^(.*).*$\')
+ ELSE pg_attrdef.adsrc
+ END AS "Default"
+FROM pg_class
+ INNER JOIN pg_attribute
+ ON (pg_class.oid=pg_attribute.attrelid)
+ INNER JOIN pg_type
+ ON (pg_attribute.atttypid=pg_type.oid)
+ LEFT JOIN pg_attrdef
+ ON (pg_class.oid=pg_attrdef.adrelid AND pg_attribute.attnum=pg_attrdef.adnum)
+WHERE pg_class.relname=\''.$this->escape_str($table).'\' AND pg_attribute.attnum>=1 AND NOT pg_attribute.attisdropped
+ORDER BY pg_attribute.attnum');
+
+ // Load the result as objects
+ $query->result(TRUE);
+
+ $fields = array();
+ foreach ($query as $row)
+ {
+ $fields[$row->Field] = $row->Type;
+ }
+
+ return $fields;
+ }
+
+ public function field_data($table)
+ {
+ // TODO: This whole function needs to be debugged.
+ $query = pg_query('SELECT * FROM '.$this->escape_table($table).' LIMIT 1', $this->link);
+ $fields = pg_num_fields($query);
+ $table = array();
+
+ for ($i=0; $i < $fields; $i++)
+ {
+ $table[$i]['type'] = pg_field_type($query, $i);
+ $table[$i]['name'] = pg_field_name($query, $i);
+ $table[$i]['len'] = pg_field_prtlen($query, $i);
+ }
+
+ return $table;
+ }
+
+} // End Database_Pgsql_Driver Class
+
+/**
+ * PostgreSQL Result
+ */
+class Pgsql_Result extends Database_Result {
+
+ // Data fetching types
+ protected $fetch_type = 'pgsql_fetch_object';
+ protected $return_type = PGSQL_ASSOC;
+
+ /**
+ * Sets up the result variables.
+ *
+ * @param resource query result
+ * @param resource database link
+ * @param boolean return objects or arrays
+ * @param string SQL query that was run
+ */
+ public function __construct($result, $link, $object = TRUE, $sql)
+ {
+ $this->result = $result;
+
+ // If the query is a resource, it was a SELECT, SHOW, DESCRIBE, EXPLAIN query
+ if (is_resource($result))
+ {
+ // Its an DELETE, INSERT, REPLACE, or UPDATE query
+ if (preg_match('/^(?:delete|insert|replace|update)\b/iD', trim($sql), $matches))
+ {
+ $this->insert_id = (strtolower($matches[0]) == 'insert') ? $this->insert_id() : FALSE;
+ $this->total_rows = pg_affected_rows($this->result);
+ }
+ else
+ {
+ $this->current_row = 0;
+ $this->total_rows = pg_num_rows($this->result);
+ $this->fetch_type = ($object === TRUE) ? 'pg_fetch_object' : 'pg_fetch_array';
+ }
+ }
+ else
+ {
+ throw new Kohana_Database_Exception('database.error', pg_last_error().' - '.$sql);
+ }
+
+ // Set result type
+ $this->result($object);
+
+ // Store the SQL
+ $this->sql = $sql;
+ }
+
+ /**
+ * Magic __destruct function, frees the result.
+ */
+ public function __destruct()
+ {
+ if (is_resource($this->result))
+ {
+ pg_free_result($this->result);
+ }
+ }
+
+ public function result($object = TRUE, $type = PGSQL_ASSOC)
+ {
+ $this->fetch_type = ((bool) $object) ? 'pg_fetch_object' : 'pg_fetch_array';
+
+ // This check has to be outside the previous statement, because we do not
+ // know the state of fetch_type when $object = NULL
+ // NOTE - The class set by $type must be defined before fetching the result,
+ // autoloading is disabled to save a lot of stupid overhead.
+ if ($this->fetch_type == 'pg_fetch_object')
+ {
+ $this->return_type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ else
+ {
+ $this->return_type = $type;
+ }
+
+ return $this;
+ }
+
+ public function as_array($object = NULL, $type = PGSQL_ASSOC)
+ {
+ return $this->result_array($object, $type);
+ }
+
+ public function result_array($object = NULL, $type = PGSQL_ASSOC)
+ {
+ $rows = array();
+
+ if (is_string($object))
+ {
+ $fetch = $object;
+ }
+ elseif (is_bool($object))
+ {
+ if ($object === TRUE)
+ {
+ $fetch = 'pg_fetch_object';
+
+ // NOTE - The class set by $type must be defined before fetching the result,
+ // autoloading is disabled to save a lot of stupid overhead.
+ $type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ else
+ {
+ $fetch = 'pg_fetch_array';
+ }
+ }
+ else
+ {
+ // Use the default config values
+ $fetch = $this->fetch_type;
+
+ if ($fetch == 'pg_fetch_object')
+ {
+ $type = (is_string($type) AND Kohana::auto_load($type)) ? $type : 'stdClass';
+ }
+ }
+
+ while ($row = $fetch($this->result, NULL, $type))
+ {
+ $rows[] = $row;
+ }
+
+ return $rows;
+ }
+
+ public function insert_id()
+ {
+ if ($this->insert_id === NULL)
+ {
+ $query = 'SELECT LASTVAL() AS insert_id';
+
+ // Disable error reporting for this, just to silence errors on
+ // tables that have no serial column.
+ $ER = error_reporting(0);
+
+ $result = pg_query($query);
+ $insert_id = pg_fetch_array($result, NULL, PGSQL_ASSOC);
+
+ $this->insert_id = $insert_id['insert_id'];
+
+ // Reset error reporting
+ error_reporting($ER);
+ }
+
+ return $this->insert_id;
+ }
+
+ public function seek($offset)
+ {
+ if ( ! $this->offsetExists($offset))
+ return FALSE;
+
+ return pg_result_seek($this->result, $offset);
+ }
+
+ public function list_fields()
+ {
+ $field_names = array();
+ while ($field = pg_field_name($this->result))
+ {
+ $field_names[] = $field->name;
+ }
+
+ return $field_names;
+ }
+
+ /**
+ * ArrayAccess: offsetGet
+ */
+ public function offsetGet($offset)
+ {
+ if ( ! $this->seek($offset))
+ return FALSE;
+
+ // Return the row by calling the defined fetching callback
+ $fetch = $this->fetch_type;
+ return $fetch($this->result, NULL, $this->return_type);
+ }
+
+} // End Pgsql_Result Class
+
+/**
+ * PostgreSQL Prepared Statement (experimental)
+ */
+class Kohana_Pgsql_Statement {
+
+ protected $link = NULL;
+ protected $stmt;
+
+ public function __construct($sql, $link)
+ {
+ $this->link = $link;
+
+ $this->stmt = $this->link->prepare($sql);
+
+ return $this;
+ }
+
+ public function __destruct()
+ {
+ $this->stmt->close();
+ }
+
+ // Sets the bind parameters
+ public function bind_params()
+ {
+ $argv = func_get_args();
+ return $this;
+ }
+
+ // sets the statement values to the bound parameters
+ public function set_vals()
+ {
+ return $this;
+ }
+
+ // Runs the statement
+ public function execute()
+ {
+ return $this;
+ }
+} \ No newline at end of file