CI_DB_postgre_driver Class Reference

Inheritance diagram for CI_DB_postgre_driver:
Collaboration diagram for CI_DB_postgre_driver:

List of all members.


Public Member Functions

 db_connect ()
 Non-persistent database connection.
 db_pconnect ()
 Persistent database connection.
 db_select ()
 Select the database.
 db_set_charset ($charset, $collation)
 Set client character set.
 _version ()
 Version number query string.
 _execute ($sql)
 Execute the query.
 _prep_query ($sql)
 Prep the query.
 trans_begin ($test_mode=FALSE)
 Begin Transaction.
 trans_commit ()
 Commit Transaction.
 trans_rollback ()
 Rollback Transaction.
 escape_str ($str)
 Escape String.
 affected_rows ()
 Affected Rows.
 insert_id ()
 Insert ID.
 count_all ($table= '')
 "Count All" query
 _list_tables ($prefix_limit=FALSE)
 Show table query.
 _list_columns ($table= '')
 Show column query.
 _field_data ($table)
 Field data query.
 _error_message ()
 The error message string.
 _error_number ()
 The error message number.
 _escape_table ($table)
 Escape Table Name.
 _protect_identifiers ($item, $first_word_only=FALSE)
 Protect Identifiers.
 _from_tables ($tables)
 From Tables.
 _insert ($table, $keys, $values)
 Insert statement.
 _update ($table, $values, $where, $orderby=array(), $limit=FALSE)
 Update statement.
 _truncate ($table)
 Truncate statement.
 _delete ($table, $where=array(), $like=array(), $limit=FALSE)
 Delete statement.
 _limit ($sql, $limit, $offset)
 Limit string.
 _close ($conn_id)
 Close DB Connection.

Public Attributes

 $_count_string = "SELECT COUNT(*) AS "
 The syntax to count rows is slightly different across different database engines, so this string appears in each driver and is used for the count_all() and count_all_results() functions.
 $_random_keyword = ' RANDOM()'

Detailed Description

Definition at line 31 of file postgre_driver.php.


Member Function Documentation

CI_DB_postgre_driver::_close ( conn_id  ) 

Close DB Connection.

public

Parameters:
resource 
Returns:
void

Definition at line 636 of file postgre_driver.php.

00637         {
00638                 @pg_close($conn_id);
00639         }

CI_DB_postgre_driver::_delete ( table,
where = array(),
like = array(),
limit = FALSE 
)

Delete statement.

Generates a platform-specific delete string from the supplied data

public

Parameters:
string the table name
array the where clause
string the limit clause
Returns:
string

Definition at line 582 of file postgre_driver.php.

00583         {
00584                 $conditions = '';
00585 
00586                 if (count($where) > 0 OR count($like) > 0)
00587                 {
00588                         $conditions = "\nWHERE ";
00589                         $conditions .= implode("\n", $this->ar_where);
00590 
00591                         if (count($where) > 0 && count($like) > 0)
00592                         {
00593                                 $conditions .= " AND ";
00594                         }
00595                         $conditions .= implode("\n", $like);
00596                 }
00597 
00598                 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
00599         
00600                 return "DELETE FROM ".$table.$conditions.$limit;
00601         }

CI_DB_postgre_driver::_error_message (  ) 

The error message string.

private

Returns:
string

Definition at line 377 of file postgre_driver.php.

00378         {
00379                 return pg_last_error($this->conn_id);
00380         }

CI_DB_postgre_driver::_error_number (  ) 

The error message number.

private

Returns:
integer

Definition at line 390 of file postgre_driver.php.

00391         {
00392                 return '';
00393         }

CI_DB_postgre_driver::_escape_table ( table  ) 

Escape Table Name.

This function adds backticks if the table name has a period in it. Some DBs will get cranky unless periods are escaped.

private

Parameters:
string the table name
Returns:
string

Definition at line 407 of file postgre_driver.php.

00408         {
00409                 if (strpos($table, '.') !== FALSE)
00410                 {
00411                         $table = '"' . str_replace('.', '"."', $table) . '"';
00412                 }
00413                 
00414                 return $table;
00415         }

CI_DB_postgre_driver::_execute ( sql  ) 

Execute the query.

private called by the base class

Parameters:
string an SQL query
Returns:
resource

Definition at line 121 of file postgre_driver.php.

References _prep_query().

00122         {
00123                 $sql = $this->_prep_query($sql);
00124                 return @pg_query($this->conn_id, $sql);
00125         }

Here is the call graph for this function:

CI_DB_postgre_driver::_field_data ( table  ) 

Field data query.

Generates a platform-specific query so that the column data can be retrieved

public

Parameters:
string the table name
Returns:
object

Definition at line 364 of file postgre_driver.php.

00365         {
00366                 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
00367         }

CI_DB_postgre_driver::_from_tables ( tables  ) 

From Tables.

This function implicitly groups FROM tables so there is no confusion about operator precedence in harmony with SQL standards

public

Parameters:
type 
Returns:
type

Definition at line 489 of file postgre_driver.php.

00490         {
00491                 if ( ! is_array($tables))
00492                 {
00493                         $tables = array($tables);
00494                 }
00495                 
00496                 return implode(', ', $tables);
00497         }

CI_DB_postgre_driver::_insert ( table,
keys,
values 
)

Insert statement.

Generates a platform-specific insert string from the supplied data

public

Parameters:
string the table name
array the insert keys
array the insert values
Returns:
string

Definition at line 512 of file postgre_driver.php.

00513         {       
00514                 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
00515         }

CI_DB_postgre_driver::_limit ( sql,
limit,
offset 
)

Limit string.

Generates a platform-specific LIMIT clause

public

Parameters:
string the sql query string
integer the number of rows to limit the query to
integer the offset value
Returns:
string

Definition at line 615 of file postgre_driver.php.

00616         {       
00617                 $sql .= "LIMIT ".$limit;
00618         
00619                 if ($offset > 0)
00620                 {
00621                         $sql .= " OFFSET ".$offset;
00622                 }
00623                 
00624                 return $sql;
00625         }

CI_DB_postgre_driver::_list_columns ( table = ''  ) 

Show column query.

Generates a platform-specific query string so that the column names can be fetched

public

Parameters:
string the table name
Returns:
string

Definition at line 348 of file postgre_driver.php.

00349         {
00350                 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->_escape_table($table)."'";
00351         }

CI_DB_postgre_driver::_list_tables ( prefix_limit = FALSE  ) 

Show table query.

Generates a platform-specific query string so that the table names can be fetched

private

Parameters:
boolean 
Returns:
string

Definition at line 325 of file postgre_driver.php.

00326         {       
00327                 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";        
00328                 
00329                 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
00330                 {
00331                         $sql .= " AND table_name LIKE '".$this->dbprefix."%'";
00332                 }
00333                 
00334                 return $sql;
00335         }

CI_DB_postgre_driver::_prep_query ( sql  ) 

Prep the query.

If needed, each database adapter can prep the query string

private called by execute()

Parameters:
string an SQL query
Returns:
string

Definition at line 138 of file postgre_driver.php.

Referenced by _execute().

00139         {
00140                 return $sql;
00141         }

Here is the caller graph for this function:

CI_DB_postgre_driver::_protect_identifiers ( item,
first_word_only = FALSE 
)

Protect Identifiers.

This function adds backticks if appropriate based on db type

private

Parameters:
mixed the item to escape
boolean only affect the first word
Returns:
mixed the item with backticks

Definition at line 429 of file postgre_driver.php.

Referenced by count_all().

00430         {
00431                 if (is_array($item))
00432                 {
00433                         $escaped_array = array();
00434 
00435                         foreach($item as $k=>$v)
00436                         {
00437                                 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
00438                         }
00439 
00440                         return $escaped_array;
00441                 }       
00442 
00443                 // This function may get "item1 item2" as a string, and so
00444                 // we may need ""item1" "item2"" and not ""item1 item2""
00445                 if (ctype_alnum($item) === FALSE)
00446                 {
00447                         if (strpos($item, '.') !== FALSE)
00448                         {
00449                                 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
00450                                 $table_name =  substr($item, 0, strpos($item, '.')+1);
00451                                 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
00452                         }
00453 
00454                         // This function may get "field >= 1", and need it to return ""field" >= 1"
00455                         $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
00456 
00457                         $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1"$2"$3', $item);
00458                 }
00459                 else
00460                 {
00461                         return "\"{$item}\"";
00462                 }
00463 
00464                 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
00465                 
00466                 foreach ($exceptions as $exception)
00467                 {
00468                 
00469                         if (stristr($item, " \"{$exception}\" ") !== FALSE)
00470                         {
00471                                 $item = preg_replace('/ "('.preg_quote($exception).')" /i', ' $1 ', $item);
00472                         }
00473                 }
00474                 return $item;
00475         }

Here is the caller graph for this function:

CI_DB_postgre_driver::_truncate ( table  ) 

Truncate statement.

Generates a platform-specific truncate string from the supplied data If the database does not support the truncate() command This function maps to "DELETE FROM table"

public

Parameters:
string the table name
Returns:
string

Definition at line 564 of file postgre_driver.php.

00565         {
00566                 return "TRUNCATE ".$this->_escape_table($table);
00567         }

CI_DB_postgre_driver::_update ( table,
values,
where,
orderby = array(),
limit = FALSE 
)

Update statement.

Generates a platform-specific update string from the supplied data

public

Parameters:
string the table name
array the update data
array the where clause
array the orderby clause
array the limit clause
Returns:
string

Definition at line 532 of file postgre_driver.php.

00533         {
00534                 foreach($values as $key => $val)
00535                 {
00536                         $valstr[] = $key." = ".$val;
00537                 }
00538                 
00539                 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
00540                 
00541                 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
00542         
00543                 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
00544                 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
00545                 $sql .= $orderby.$limit;
00546                 
00547                 return $sql;
00548         }

CI_DB_postgre_driver::_version (  ) 

Version number query string.

public

Returns:
string

Definition at line 107 of file postgre_driver.php.

Referenced by insert_id().

00108         {
00109                 return "SELECT version() AS ver";
00110         }

Here is the caller graph for this function:

CI_DB_postgre_driver::affected_rows (  ) 

Affected Rows.

public

Returns:
integer

Definition at line 242 of file postgre_driver.php.

00243         {
00244                 return @pg_affected_rows($this->result_id);
00245         }

CI_DB_postgre_driver::count_all ( table = ''  ) 

"Count All" query

Generates a platform-specific query string that counts all records in the specified database

public

Parameters:
string 
Returns:
string

Definition at line 300 of file postgre_driver.php.

References _protect_identifiers().

00301         {
00302                 if ($table == '')
00303                         return '0';
00304 
00305                 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
00306                                 
00307                 if ($query->num_rows() == 0)
00308                         return '0';
00309 
00310                 $row = $query->row();
00311                 return $row->numrows;
00312         }

Here is the call graph for this function:

CI_DB_postgre_driver::db_connect (  ) 

Non-persistent database connection.

private called by the base class

Returns:
resource

Definition at line 47 of file postgre_driver.php.

00048         {
00049                 $port = ($this->port == '') ? '' : " port=".$this->port;
00050                 
00051                 return @pg_connect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
00052         }

CI_DB_postgre_driver::db_pconnect (  ) 

Persistent database connection.

private called by the base class

Returns:
resource

Definition at line 62 of file postgre_driver.php.

00063         {
00064                 $port = ($this->port == '') ? '' : " port=".$this->port;
00065 
00066                 return @pg_pconnect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
00067         }

CI_DB_postgre_driver::db_select (  ) 

Select the database.

private called by the base class

Returns:
resource

Definition at line 77 of file postgre_driver.php.

00078         {
00079                 // Not needed for Postgre so we'll return TRUE
00080                 return TRUE;
00081         }

CI_DB_postgre_driver::db_set_charset ( charset,
collation 
)

Set client character set.

public

Parameters:
string 
string 
Returns:
resource

Definition at line 93 of file postgre_driver.php.

00094         {
00095                 // TODO - add support if needed
00096                 return TRUE;
00097         }

CI_DB_postgre_driver::escape_str ( str  ) 

Escape String.

public

Parameters:
string 
Returns:
string

Definition at line 229 of file postgre_driver.php.

00230         {       
00231                 return pg_escape_string($str);
00232         }

CI_DB_postgre_driver::insert_id (  ) 

Insert ID.

public

Returns:
integer

Definition at line 255 of file postgre_driver.php.

References _version().

00256         {
00257                 $v = $this->_version();
00258                 $v = $v['server'];
00259                 
00260                 $table  = func_num_args() > 0 ? func_get_arg(0) : null;
00261                 $column = func_num_args() > 1 ? func_get_arg(1) : null;
00262                 
00263                 if ($table == null && $v >= '8.1')
00264                 {
00265                         $sql='SELECT LASTVAL() as ins_id';
00266                 }
00267                 elseif ($table != null && $column != null && $v >= '8.0')
00268                 {
00269                         $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
00270                         $query = $this->query($sql);
00271                         $row = $query->row();
00272                         $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
00273                 }
00274                 elseif ($table != null)
00275                 {
00276                         // seq_name passed in table parameter
00277                         $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
00278                 }
00279                 else
00280                 {
00281                         return pg_last_oid($this->result_id);
00282                 }
00283                 $query = $this->query($sql);
00284                 $row = $query->row();
00285                 return $row->ins_id;
00286         }

Here is the call graph for this function:

CI_DB_postgre_driver::trans_begin ( test_mode = FALSE  ) 

Begin Transaction.

public

Returns:
bool

Definition at line 151 of file postgre_driver.php.

00152         {
00153                 if ( ! $this->trans_enabled)
00154                 {
00155                         return TRUE;
00156                 }
00157                 
00158                 // When transactions are nested we only begin/commit/rollback the outermost ones
00159                 if ($this->_trans_depth > 0)
00160                 {
00161                         return TRUE;
00162                 }
00163 
00164                 // Reset the transaction failure flag.
00165                 // If the $test_mode flag is set to TRUE transactions will be rolled back
00166                 // even if the queries produce a successful result.
00167                 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
00168 
00169                 return @pg_exec($this->conn_id, "begin");
00170         }

CI_DB_postgre_driver::trans_commit (  ) 

Commit Transaction.

public

Returns:
bool

Definition at line 180 of file postgre_driver.php.

00181         {
00182                 if ( ! $this->trans_enabled)
00183                 {
00184                         return TRUE;
00185                 }
00186 
00187                 // When transactions are nested we only begin/commit/rollback the outermost ones
00188                 if ($this->_trans_depth > 0)
00189                 {
00190                         return TRUE;
00191                 }
00192 
00193                 return @pg_exec($this->conn_id, "commit");
00194         }

CI_DB_postgre_driver::trans_rollback (  ) 

Rollback Transaction.

public

Returns:
bool

Definition at line 204 of file postgre_driver.php.

00205         {
00206                 if ( ! $this->trans_enabled)
00207                 {
00208                         return TRUE;
00209                 }
00210 
00211                 // When transactions are nested we only begin/commit/rollback the outermost ones
00212                 if ($this->_trans_depth > 0)
00213                 {
00214                         return TRUE;
00215                 }
00216 
00217                 return @pg_exec($this->conn_id, "rollback");
00218         }


Member Data Documentation

CI_DB_postgre_driver::$_count_string = "SELECT COUNT(*) AS "

The syntax to count rows is slightly different across different database engines, so this string appears in each driver and is used for the count_all() and count_all_results() functions.

Definition at line 38 of file postgre_driver.php.

CI_DB_postgre_driver::$_random_keyword = ' RANDOM()'

Definition at line 39 of file postgre_driver.php.


The documentation for this class was generated from the following file: