summaryrefslogtreecommitdiffstats
path: root/system/database/drivers/postgre
diff options
context:
space:
mode:
Diffstat (limited to 'system/database/drivers/postgre')
-rwxr-xr-x[-rw-r--r--]system/database/drivers/postgre/index.html0
-rwxr-xr-x[-rw-r--r--]system/database/drivers/postgre/postgre_driver.php122
-rwxr-xr-x[-rw-r--r--]system/database/drivers/postgre/postgre_forge.php117
-rwxr-xr-x[-rw-r--r--]system/database/drivers/postgre/postgre_result.php22
-rwxr-xr-x[-rw-r--r--]system/database/drivers/postgre/postgre_utility.php40
5 files changed, 158 insertions, 143 deletions
diff --git a/system/database/drivers/postgre/index.html b/system/database/drivers/postgre/index.html
index c942a79ce..c942a79ce 100644..100755
--- a/system/database/drivers/postgre/index.html
+++ b/system/database/drivers/postgre/index.html
diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php
index bac179d40..0bb7974d8 100644..100755
--- a/system/database/drivers/postgre/postgre_driver.php
+++ b/system/database/drivers/postgre/postgre_driver.php
@@ -2,11 +2,11 @@
/**
* CodeIgniter
*
- * An open source application development framework for PHP 4.3.2 or newer
+ * An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
- * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
+ * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
@@ -31,7 +31,7 @@
class CI_DB_postgre_driver extends CI_DB {
var $dbdriver = 'postgre';
-
+
var $_escape_char = '"';
// clause and character used for LIKE escape sequences
@@ -51,7 +51,7 @@ class CI_DB_postgre_driver extends CI_DB {
*
* @access private
* @return string
- */
+ */
function _connect_string()
{
$components = array(
@@ -61,7 +61,7 @@ class CI_DB_postgre_driver extends CI_DB {
'username' => 'user',
'password' => 'password'
);
-
+
$connect_string = "";
foreach ($components as $key => $val)
{
@@ -80,9 +80,9 @@ class CI_DB_postgre_driver extends CI_DB {
*
* @access private called by the base class
* @return resource
- */
+ */
function db_connect()
- {
+ {
return @pg_connect($this->_connect_string());
}
@@ -93,12 +93,12 @@ class CI_DB_postgre_driver extends CI_DB {
*
* @access private called by the base class
* @return resource
- */
+ */
function db_pconnect()
{
return @pg_pconnect($this->_connect_string());
}
-
+
// --------------------------------------------------------------------
/**
@@ -125,7 +125,7 @@ class CI_DB_postgre_driver extends CI_DB {
*
* @access private called by the base class
* @return resource
- */
+ */
function db_select()
{
// Not needed for Postgre so we'll return TRUE
@@ -149,7 +149,7 @@ class CI_DB_postgre_driver extends CI_DB {
}
// --------------------------------------------------------------------
-
+
/**
* Version number query string
*
@@ -169,13 +169,13 @@ class CI_DB_postgre_driver extends CI_DB {
* @access private called by the base class
* @param string an SQL query
* @return resource
- */
+ */
function _execute($sql)
{
$sql = $this->_prep_query($sql);
return @pg_query($this->conn_id, $sql);
}
-
+
// --------------------------------------------------------------------
/**
@@ -186,7 +186,7 @@ class CI_DB_postgre_driver extends CI_DB {
* @access private called by execute()
* @param string an SQL query
* @return string
- */
+ */
function _prep_query($sql)
{
return $sql;
@@ -198,15 +198,15 @@ class CI_DB_postgre_driver extends CI_DB {
* Begin Transaction
*
* @access public
- * @return bool
- */
+ * @return bool
+ */
function trans_begin($test_mode = FALSE)
{
if ( ! $this->trans_enabled)
{
return TRUE;
}
-
+
// When transactions are nested we only begin/commit/rollback the outermost ones
if ($this->_trans_depth > 0)
{
@@ -227,8 +227,8 @@ class CI_DB_postgre_driver extends CI_DB {
* Commit Transaction
*
* @access public
- * @return bool
- */
+ * @return bool
+ */
function trans_commit()
{
if ( ! $this->trans_enabled)
@@ -251,8 +251,8 @@ class CI_DB_postgre_driver extends CI_DB {
* Rollback Transaction
*
* @access public
- * @return bool
- */
+ * @return bool
+ */
function trans_rollback()
{
if ( ! $this->trans_enabled)
@@ -284,15 +284,15 @@ class CI_DB_postgre_driver extends CI_DB {
if (is_array($str))
{
foreach($str as $key => $val)
- {
+ {
$str[$key] = $this->escape_str($val, $like);
- }
-
- return $str;
- }
+ }
+
+ return $str;
+ }
$str = pg_escape_string($str);
-
+
// escape LIKE condition wildcards
if ($like === TRUE)
{
@@ -300,10 +300,10 @@ class CI_DB_postgre_driver extends CI_DB {
array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
$str);
}
-
+
return $str;
}
-
+
// --------------------------------------------------------------------
/**
@@ -316,7 +316,7 @@ class CI_DB_postgre_driver extends CI_DB {
{
return @pg_affected_rows($this->result_id);
}
-
+
// --------------------------------------------------------------------
/**
@@ -329,10 +329,10 @@ class CI_DB_postgre_driver extends CI_DB {
{
$v = $this->_version();
$v = $v['server'];
-
+
$table = func_num_args() > 0 ? func_get_arg(0) : null;
$column = func_num_args() > 1 ? func_get_arg(1) : null;
-
+
if ($table == null && $v >= '8.1')
{
$sql='SELECT LASTVAL() as ins_id';
@@ -400,17 +400,17 @@ class CI_DB_postgre_driver extends CI_DB {
* @return string
*/
function _list_tables($prefix_limit = FALSE)
- {
- $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
-
+ {
+ $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
+
if ($prefix_limit !== FALSE AND $this->dbprefix != '')
{
- $sql .= " AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_char);
+ $sql .= " AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
}
-
+
return $sql;
}
-
+
// --------------------------------------------------------------------
/**
@@ -455,7 +455,7 @@ class CI_DB_postgre_driver extends CI_DB {
{
return pg_last_error($this->conn_id);
}
-
+
// --------------------------------------------------------------------
/**
@@ -491,26 +491,26 @@ class CI_DB_postgre_driver extends CI_DB {
{
if (strpos($item, '.'.$id) !== FALSE)
{
- $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
-
+ $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
+
// remove duplicates if the user already included the escape
return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
- }
+ }
}
-
+
if (strpos($item, '.') !== FALSE)
{
- $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
+ $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
}
else
{
$str = $this->_escape_char.$item.$this->_escape_char;
}
-
+
// remove duplicates if the user already included the escape
return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
}
-
+
// --------------------------------------------------------------------
/**
@@ -529,12 +529,12 @@ class CI_DB_postgre_driver extends CI_DB {
{
$tables = array($tables);
}
-
+
return implode(', ', $tables);
}
// --------------------------------------------------------------------
-
+
/**
* Insert statement
*
@@ -547,10 +547,10 @@ class CI_DB_postgre_driver extends CI_DB {
* @return string
*/
function _insert($table, $keys, $values)
- {
+ {
return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
}
-
+
// --------------------------------------------------------------------
/**
@@ -572,17 +572,17 @@ class CI_DB_postgre_driver extends CI_DB {
{
$valstr[] = $key." = ".$val;
}
-
+
$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
-
+
$orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
-
+
$sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
$sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
$sql .= $orderby.$limit;
-
+
return $sql;
}
@@ -598,12 +598,12 @@ class CI_DB_postgre_driver extends CI_DB {
* @access public
* @param string the table name
* @return string
- */
+ */
function _truncate($table)
{
return "TRUNCATE ".$table;
}
-
+
// --------------------------------------------------------------------
/**
@@ -616,7 +616,7 @@ class CI_DB_postgre_driver extends CI_DB {
* @param array the where clause
* @param string the limit clause
* @return string
- */
+ */
function _delete($table, $where = array(), $like = array(), $limit = FALSE)
{
$conditions = '';
@@ -634,7 +634,7 @@ class CI_DB_postgre_driver extends CI_DB {
}
$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
-
+
return "DELETE FROM ".$table.$conditions.$limit;
}
@@ -651,14 +651,14 @@ class CI_DB_postgre_driver extends CI_DB {
* @return string
*/
function _limit($sql, $limit, $offset)
- {
+ {
$sql .= "LIMIT ".$limit;
-
+
if ($offset > 0)
{
$sql .= " OFFSET ".$offset;
}
-
+
return $sql;
}
diff --git a/system/database/drivers/postgre/postgre_forge.php b/system/database/drivers/postgre/postgre_forge.php
index c98ef425d..91a1c6861 100644..100755
--- a/system/database/drivers/postgre/postgre_forge.php
+++ b/system/database/drivers/postgre/postgre_forge.php
@@ -2,11 +2,11 @@
/**
* CodeIgniter
*
- * An open source application development framework for PHP 4.3.2 or newer
+ * An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
- * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
+ * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
@@ -66,12 +66,15 @@ class CI_DB_postgre_forge extends CI_DB_forge {
function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
{
$sql = 'CREATE TABLE ';
-
+
if ($if_not_exists === TRUE)
{
- $sql .= 'IF NOT EXISTS ';
+ if ($this->db->table_exists($table))
+ {
+ return "SELECT * FROM $table"; // Needs to return innocous but valid SQL statement
+ }
}
-
+
$sql .= $this->db->_escape_identifiers($table)." (";
$current_field_count = 0;
@@ -87,41 +90,81 @@ class CI_DB_postgre_forge extends CI_DB_forge {
else
{
$attributes = array_change_key_case($attributes, CASE_UPPER);
-
+
$sql .= "\n\t".$this->db->_protect_identifiers($field);
-
- $sql .= ' '.$attributes['TYPE'];
-
- if (array_key_exists('CONSTRAINT', $attributes))
+
+ $is_unsigned = (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE);
+
+ // Convert datatypes to be PostgreSQL-compatible
+ switch (strtoupper($attributes['TYPE']))
{
- $sql .= '('.$attributes['CONSTRAINT'].')';
+ case 'TINYINT':
+ $attributes['TYPE'] = 'SMALLINT';
+ break;
+ case 'SMALLINT':
+ $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT';
+ break;
+ case 'MEDIUMINT':
+ $attributes['TYPE'] = 'INTEGER';
+ break;
+ case 'INT':
+ $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER';
+ break;
+ case 'BIGINT':
+ $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT';
+ break;
+ case 'DOUBLE':
+ $attributes['TYPE'] = 'DOUBLE PRECISION';
+ break;
+ case 'DATETIME':
+ $attributes['TYPE'] = 'TIMESTAMP';
+ break;
+ case 'LONGTEXT':
+ $attributes['TYPE'] = 'TEXT';
+ break;
+ case 'BLOB':
+ $attributes['TYPE'] = 'BYTEA';
+ break;
+ }
+
+ // If this is an auto-incrementing primary key, use the serial data type instead
+ if (in_array($field, $primary_keys) && array_key_exists('AUTO_INCREMENT', $attributes)
+ && $attributes['AUTO_INCREMENT'] === TRUE)
+ {
+ $sql .= ' SERIAL';
}
-
- if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
+ else
{
- $sql .= ' UNSIGNED';
+ $sql .= ' '.$attributes['TYPE'];
}
-
+
+ // Modified to prevent constraints with integer data types
+ if (array_key_exists('CONSTRAINT', $attributes) && strpos($attributes['TYPE'], 'INT') === false)
+ {
+ $sql .= '('.$attributes['CONSTRAINT'].')';
+ }
+
if (array_key_exists('DEFAULT', $attributes))
{
$sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
}
-
+
if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
{
$sql .= ' NULL';
}
else
{
- $sql .= ' NOT NULL';
+ $sql .= ' NOT NULL';
}
-
- if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
+
+ // Added new attribute to create unqite fields. Also works with MySQL
+ if (array_key_exists('UNIQUE', $attributes) && $attributes['UNIQUE'] === TRUE)
{
- $sql .= ' AUTO_INCREMENT';
+ $sql .= ' UNIQUE';
}
}
-
+
// don't add a comma on the end of the last field
if (++$current_field_count < count($fields))
{
@@ -131,29 +174,37 @@ class CI_DB_postgre_forge extends CI_DB_forge {
if (count($primary_keys) > 0)
{
- $primary_keys = $this->db->_protect_identifiers($primary_keys);
+ // Something seems to break when passing an array to _protect_identifiers()
+ foreach ($primary_keys as $index => $key)
+ {
+ $primary_keys[$index] = $this->db->_protect_identifiers($key);
+ }
+
$sql .= ",\n\tPRIMARY KEY (" . implode(', ', $primary_keys) . ")";
}
-
+
+ $sql .= "\n);";
+
if (is_array($keys) && count($keys) > 0)
{
foreach ($keys as $key)
{
if (is_array($key))
{
- $key = $this->db->_protect_identifiers($key);
+ $key = $this->db->_protect_identifiers($key);
}
else
{
$key = array($this->db->_protect_identifiers($key));
}
-
- $sql .= ",\n\tFOREIGN KEY (" . implode(', ', $key) . ")";
+
+ foreach ($key as $field)
+ {
+ $sql .= "CREATE INDEX " . $table . "_" . str_replace(array('"', "'"), '', $field) . "_index ON $table ($field); ";
+ }
}
}
- $sql .= "\n);";
-
return $sql;
}
@@ -162,12 +213,12 @@ class CI_DB_postgre_forge extends CI_DB_forge {
/**
* Drop Table
*
- * @access private
- * @return bool
+ * @access private
+ * @return bool
*/
function _drop_table($table)
{
- return "DROP TABLE ".$this->db->_escape_identifiers($table)." CASCADE";
+ return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table)." CASCADE";
}
// --------------------------------------------------------------------
@@ -218,9 +269,9 @@ class CI_DB_postgre_forge extends CI_DB_forge {
{
$sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
}
-
+
return $sql;
-
+
}
// --------------------------------------------------------------------
diff --git a/system/database/drivers/postgre/postgre_result.php b/system/database/drivers/postgre/postgre_result.php
index 545f413e8..e9a1d1607 100644..100755
--- a/system/database/drivers/postgre/postgre_result.php
+++ b/system/database/drivers/postgre/postgre_result.php
@@ -2,11 +2,11 @@
/**
* CodeIgniter
*
- * An open source application development framework for PHP 4.3.2 or newer
+ * An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
- * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
+ * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
@@ -36,7 +36,7 @@ class CI_DB_postgre_result extends CI_DB_result {
{
return @pg_num_rows($this->result_id);
}
-
+
// --------------------------------------------------------------------
/**
@@ -67,7 +67,7 @@ class CI_DB_postgre_result extends CI_DB_result {
{
$field_names[] = pg_field_name($this->result_id, $i);
}
-
+
return $field_names;
}
@@ -86,16 +86,16 @@ class CI_DB_postgre_result extends CI_DB_result {
$retval = array();
for ($i = 0; $i < $this->num_fields(); $i++)
{
- $F = new stdClass();
- $F->name = pg_field_name($this->result_id, $i);
- $F->type = pg_field_type($this->result_id, $i);
+ $F = new stdClass();
+ $F->name = pg_field_name($this->result_id, $i);
+ $F->type = pg_field_type($this->result_id, $i);
$F->max_length = pg_field_size($this->result_id, $i);
$F->primary_key = 0;
$F->default = '';
$retval[] = $F;
}
-
+
return $retval;
}
@@ -105,7 +105,7 @@ class CI_DB_postgre_result extends CI_DB_result {
* Free the result
*
* @return null
- */
+ */
function free_result()
{
if (is_resource($this->result_id))
@@ -146,7 +146,7 @@ class CI_DB_postgre_result extends CI_DB_result {
{
return pg_fetch_assoc($this->result_id);
}
-
+
// --------------------------------------------------------------------
/**
@@ -161,7 +161,7 @@ class CI_DB_postgre_result extends CI_DB_result {
{
return pg_fetch_object($this->result_id);
}
-
+
}
diff --git a/system/database/drivers/postgre/postgre_utility.php b/system/database/drivers/postgre/postgre_utility.php
index dda22ddb0..741c52ea8 100644..100755
--- a/system/database/drivers/postgre/postgre_utility.php
+++ b/system/database/drivers/postgre/postgre_utility.php
@@ -2,11 +2,11 @@
/**
* CodeIgniter
*
- * An open source application development framework for PHP 4.3.2 or newer
+ * An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
- * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
+ * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
@@ -81,42 +81,6 @@ class CI_DB_postgre_utility extends CI_DB_utility {
// Currently unsupported
return $this->db->display_error('db_unsuported_feature');
}
-
- /**
- *
- * The functions below have been deprecated as of 1.6, and are only here for backwards
- * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation
- * is STRONGLY discouraged in favour if using dbforge.
- *
- */
-
- /**
- * Create database
- *
- * @access private
- * @param string the database name
- * @return bool
- */
- function _create_database($name)
- {
- return "CREATE DATABASE ".$name;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Drop database
- *
- * @access private
- * @param string the database name
- * @return bool
- */
- function _drop_database($name)
- {
- return "DROP DATABASE ".$name;
- }
-
-
}