\CodeIgniter\Database\PostgreConnection

Connection for Postgre

Summary

Methods
Properties
Constants
__construct()
initialize()
connect()
close()
persistentConnect()
reconnect()
getConnection()
setDatabase()
getDatabase()
setPrefix()
getPrefix()
getError()
getPlatform()
getVersion()
setAliasedTables()
addTableAlias()
execute()
query()
simpleQuery()
transOff()
transStrict()
transStart()
transComplete()
transStatus()
transBegin()
transCommit()
transRollback()
table()
prepare()
getLastQuery()
showLastQuery()
getConnectStart()
getConnectDuration()
protectIdentifiers()
escapeIdentifiers()
prefixTable()
affectedRows()
escape()
escapeString()
escapeLikeString()
callFunction()
listTables()
tableExists()
getFieldNames()
fieldExists()
getFieldData()
getIndexData()
getForeignKeyData()
disableForeignKeyChecks()
enableForeignKeyChecks()
pretend()
resetDataCache()
error()
insertID()
_fieldData()
_indexData()
_foreignKeyData()
__get()
__isset()
$DBDriver
$connID
$resultID
$protectIdentifiers
$escapeChar
$likeEscapeStr
$likeEscapeChar
$dataCache
$transEnabled
$transStrict
$schema
No constants found
_close()
_transBegin()
_transCommit()
_transRollback()
_escapeString()
_listTables()
_listColumns()
_disableForeignKeyChecks()
_enableForeignKeyChecks()
buildDSN()
setClientEncoding()
$DSN
$port
$hostname
$username
$password
$database
$subdriver
$DBPrefix
$pConnect
$DBDebug
$cacheOn
$cacheDir
$charset
$DBCollat
$swapPre
$encrypt
$compress
$strictOn
$failover
$lastQuery
$reservedIdentifiers
$connectTime
$connectDuration
$pretend
$transDepth
$transStatus
$transFailure
$aliasedTables
N/A
No private methods found
No private properties found
N/A

Properties

$DBDriver

$DBDriver : string

Database driver

Type

string

$connID

$connID : object|resource

Connection ID

Type

object|resource

$resultID

$resultID : object|resource

Result ID

Type

object|resource

$protectIdentifiers

$protectIdentifiers : boolean

Protect identifiers flag

Type

boolean

$escapeChar

$escapeChar : string

Identifier escape character

Type

string

$likeEscapeStr

$likeEscapeStr : string

ESCAPE statement string

Type

string

$likeEscapeChar

$likeEscapeChar : string

ESCAPE character

Type

string

$dataCache

$dataCache : array

Holds previously looked up data for performance reasons.

Type

array

$transEnabled

$transEnabled : boolean

Transaction enabled flag

Type

boolean

$transStrict

$transStrict : boolean

Strict transaction mode flag

Type

boolean

$schema

$schema : string

Database schema

Type

string

$DSN

$DSN : string

Data Source Name / Connect string

Type

string

$port

$port : integer

Database port

Type

integer

$hostname

$hostname : string

Hostname

Type

string

$username

$username : string

Username

Type

string

$password

$password : string

Password

Type

string

$database

$database : string

Database name

Type

string

$subdriver

$subdriver : string

Sub-driver

Type

string

$DBPrefix

$DBPrefix : string

Table prefix

Type

string

$pConnect

$pConnect : boolean

Persistent connection flag

Type

boolean

$DBDebug

$DBDebug : boolean

Debug flag

Whether to display error messages.

Type

boolean

$cacheOn

$cacheOn : boolean

Should we cache results?

Type

boolean

$cacheDir

$cacheDir : string

Path to store cache files.

Type

string

$charset

$charset : string

Character set

Type

string

$DBCollat

$DBCollat : string

Collation

Type

string

$swapPre

$swapPre : string

Swap Prefix

Type

string

$encrypt

$encrypt : mixed

Encryption flag/data

Type

mixed

$compress

$compress : boolean

Compression flag

Type

boolean

$strictOn

$strictOn : boolean

Strict ON flag

Whether we're running in strict SQL mode.

Type

boolean

$failover

$failover : array

Settings for a failover connection.

Type

array

$lastQuery

$lastQuery : array

The last query object that was executed on this connection.

Type

array

$reservedIdentifiers

$reservedIdentifiers : array

List of reserved identifiers

Identifiers that must NOT be escaped.

Type

array

$connectTime

$connectTime : float

Microtime when connection was made

Type

float

$connectDuration

$connectDuration : float

How long it took to establish connection.

Type

float

$pretend

$pretend : boolean

If true, no queries will actually be ran against the database.

Type

boolean

$transDepth

$transDepth : integer

Transaction depth level

Type

integer

$transStatus

$transStatus : boolean

Transaction status flag

Used with transactions to determine if a rollback should occur.

Type

boolean

$transFailure

$transFailure : boolean

Transaction failure flag

Used with transactions to determine if a transaction has failed.

Type

boolean

$aliasedTables

$aliasedTables : array

Array of table aliases.

Type

array

Methods

__construct()

__construct(array  $params) 

Saves our connection settings.

Parameters

array $params

initialize()

initialize() : mixed|void

Initializes the database connection/settings.

Throws

\CodeIgniter\Database\Exceptions\DatabaseException

Returns

mixed|void

connect()

connect(boolean  $persistent = false) : mixed

Connect to the database.

Parameters

boolean $persistent

Returns

mixed

close()

close() : void

Close the database connection.

persistentConnect()

persistentConnect() : mixed

Create a persistent database connection.

Returns

mixed

reconnect()

reconnect() : void

Keep or establish the connection if no queries have been sent for a length of time exceeding the server's idle timeout.

getConnection()

getConnection(string|null  $alias = null) : mixed

Returns the actual connection object. If both a 'read' and 'write' connection has been specified, you can pass either term in to get that connection. If you pass either alias in and only a single connection is present, it must return the sole connection.

Parameters

string|null $alias

Returns

mixed

setDatabase()

setDatabase(string  $databaseName) : boolean

Select a specific database table to use.

Parameters

string $databaseName

Returns

boolean

getDatabase()

getDatabase() : string

Returns the name of the current database being used.

Returns

string

setPrefix()

setPrefix(string  $prefix = '') : string

Set DB Prefix

Set's the DB Prefix to something new without needing to reconnect

Parameters

string $prefix

The prefix

Returns

string

getPrefix()

getPrefix() : string

Returns the database prefix.

Returns

string

getError()

getError() : mixed

Returns the last error encountered by this connection.

Returns

mixed

getPlatform()

getPlatform() : string

The name of the platform in use (MySQLi, mssql, etc)

Returns

string

getVersion()

getVersion() : string

Returns a string containing the version of the database being used.

Returns

string

setAliasedTables()

setAliasedTables(array  $aliases) : $this

Sets the Table Aliases to use. These are typically collected during use of the Builder, and set here so queries are built correctly.

Parameters

array $aliases

Returns

$this

addTableAlias()

addTableAlias(string  $table) : $this

Add a table alias to our list.

Parameters

string $table

Returns

$this

execute()

execute(string  $sql) : resource

Executes the query against the database.

Parameters

string $sql

Returns

resource

query()

query(string  $sql, mixed  $binds = null, boolean  $setEscapeFlags = true, string  $queryClass = 'CodeIgniter\\Database\\Query') : \CodeIgniter\Database\BaseResult|\CodeIgniter\Database\Query|false

Orchestrates a query against the database. Queries must use Database\Statement objects to store the query and build it.

This method works with the cache.

Should automatically handle different connections for read/write queries if needed.

Parameters

string $sql
mixed $binds
boolean $setEscapeFlags
string $queryClass

Returns

\CodeIgniter\Database\BaseResult|\CodeIgniter\Database\Query|false

simpleQuery()

simpleQuery(string  $sql) : mixed

Performs a basic query against the database. No binding or caching is performed, nor are transactions handled. Simply takes a raw query string and returns the database-specific result id.

Parameters

string $sql

Returns

mixed

transOff()

transOff() : void

Disable Transactions

This permits transactions to be disabled at run-time.

transStrict()

transStrict(boolean  $mode = true) : $this

Enable/disable Transaction Strict Mode

When strict mode is enabled, if you are running multiple groups of transactions, if one group fails all subsequent groups will be rolled back.

If strict mode is disabled, each group is treated autonomously, meaning a failure of one group will not affect any others

Parameters

boolean $mode

= true

Returns

$this

transStart()

transStart(boolean  $test_mode = false) : boolean

Start Transaction

Parameters

boolean $test_mode

= FALSE

Returns

boolean

transComplete()

transComplete() : boolean

Complete Transaction

Returns

boolean

transStatus()

transStatus() : boolean

Lets you retrieve the transaction flag to determine if it has failed

Returns

boolean

transBegin()

transBegin(boolean  $test_mode = false) : boolean

Begin Transaction

Parameters

boolean $test_mode

Returns

boolean

transCommit()

transCommit() : boolean

Commit Transaction

Returns

boolean

transRollback()

transRollback() : boolean

Rollback Transaction

Returns

boolean

table()

table(string|array  $tableName) : \CodeIgniter\Database\BaseBuilder

Returns an instance of the query builder for this connection.

Parameters

string|array $tableName

Table name.

Throws

\CodeIgniter\Database\Exceptions\DatabaseException

Returns

\CodeIgniter\Database\BaseBuilder

prepare()

prepare(\Closure  $func, array  $options = array()) : \CodeIgniter\Database\BasePreparedQuery|null

Creates a prepared statement with the database that can then be used to execute multiple statements against. Within the closure, you would build the query in any normal way, though the Query Builder is the expected manner.

Example: $stmt = $db->prepare(function($db) { return $db->table('users') ->where('id', 1) ->get(); })

Parameters

\Closure $func
array $options

Passed to the prepare() method

Returns

\CodeIgniter\Database\BasePreparedQuery|null

getLastQuery()

getLastQuery() : mixed

Returns the last query's statement object.

Returns

mixed

showLastQuery()

showLastQuery() : string

Returns a string representation of the last query's statement object.

Returns

string

getConnectStart()

getConnectStart() : float

Returns the time we started to connect to this database in seconds with microseconds.

Used by the Debug Toolbar's timeline.

Returns

float

getConnectDuration()

getConnectDuration(integer  $decimals = 6) : string

Returns the number of seconds with microseconds that it took to connect to the database.

Used by the Debug Toolbar's timeline.

Parameters

integer $decimals

Returns

string

protectIdentifiers()

protectIdentifiers(string|array  $item, boolean  $prefixSingle = false, boolean  $protectIdentifiers = null, boolean  $fieldExists = true) : string|array

Protect Identifiers

This function is used extensively by the Query Builder class, and by a couple functions in this class. It takes a column or table name (optionally with an alias) and inserts the table prefix onto it. Some logic is necessary in order to deal with column names that include the path. Consider a query like this:

SELECT hostname.database.table.column AS c FROM hostname.database.table

Or a query with aliasing:

SELECT m.member_id, m.member_name FROM members AS m

Since the column name can include up to four segments (host, DB, table, column) or also have an alias prefix, we need to do a bit of work to figure this out and insert the table prefix (if it exists) in the proper position, and escape only the correct identifiers.

Parameters

string|array $item
boolean $prefixSingle
boolean $protectIdentifiers
boolean $fieldExists

Returns

string|array

escapeIdentifiers()

escapeIdentifiers(mixed  $item) : mixed

Escape the SQL Identifiers

This function escapes column and table names

Parameters

mixed $item

Returns

mixed

prefixTable()

prefixTable(string  $table = '') : string

DB Prefix

Prepends a database prefix if one exists in configuration

Parameters

string $table

the table

Throws

\CodeIgniter\Database\Exceptions\DatabaseException

Returns

string

affectedRows()

affectedRows() : integer

Returns the total number of rows affected by this query.

Returns

integer

escape()

escape(mixed  $str) : mixed

"Smart" Escape String

Escapes data based on type

Parameters

mixed $str

Returns

mixed

escapeString()

escapeString(string|array<mixed,string>  $str, boolean  $like = false) : string|array<mixed,string>

Escape String

Parameters

string|array<mixed,string> $str

Input string

boolean $like

Whether or not the string will be used in a LIKE condition

Returns

string|array<mixed,string>

escapeLikeString()

escapeLikeString(  $str) : string|array<mixed,string>

Escape LIKE String

Calls the individual driver for platform specific escaping for LIKE conditions

Parameters

$str

Returns

string|array<mixed,string>

callFunction()

callFunction(string  $functionName, array  ...$params) : boolean

This function enables you to call PHP database functions that are not natively included in CodeIgniter, in a platform independent manner.

Parameters

string $functionName
array $params variadic

Throws

\CodeIgniter\Database\Exceptions\DatabaseException

Returns

boolean

listTables()

listTables(boolean  $constrainByPrefix = false) : boolean|array

Returns an array of table names

Parameters

boolean $constrainByPrefix

= FALSE

Throws

\CodeIgniter\Database\Exceptions\DatabaseException

Returns

boolean|array

tableExists()

tableExists(string  $tableName) : boolean

Determine if a particular table exists

Parameters

string $tableName

Returns

boolean

getFieldNames()

getFieldNames(string  $table) : array|false

Fetch Field Names

Parameters

string $table

Table name

Throws

\CodeIgniter\Database\Exceptions\DatabaseException

Returns

array|false

fieldExists()

fieldExists(string  $fieldName, string  $tableName) : boolean

Determine if a particular field exists

Parameters

string $fieldName
string $tableName

Returns

boolean

getFieldData()

getFieldData(string  $table) : array|false

Returns an object with field data

Parameters

string $table

the table name

Returns

array|false

getIndexData()

getIndexData(string  $table) : array|false

Returns an object with key data

Parameters

string $table

the table name

Returns

array|false

getForeignKeyData()

getForeignKeyData(string  $table) : array|false

Returns an object with foreign key data

Parameters

string $table

the table name

Returns

array|false

disableForeignKeyChecks()

disableForeignKeyChecks() 

Disables foreign key checks temporarily.

enableForeignKeyChecks()

enableForeignKeyChecks() 

Enables foreign key checks temporarily.

pretend()

pretend(boolean  $pretend = true) : $this

Allows the engine to be set into a mode where queries are not actually executed, but they are still generated, timed, etc.

This is primarily used by the prepared query functionality.

Parameters

boolean $pretend

Returns

$this

resetDataCache()

resetDataCache() : $this

Empties our data cache. Especially helpful during testing.

Returns

$this

error()

error() : array

Returns the last error code and message.

Must return an array with keys 'code' and 'message':

return ['code' => null, 'message' => null);

Returns

array

insertID()

insertID() : integer

Insert ID

Returns

integer

_fieldData()

_fieldData(string  $table) : array<mixed,\stdClass>

Returns an array of objects with field data

Parameters

string $table

Throws

\CodeIgniter\Database\Exceptions\DatabaseException

Returns

array<mixed,\stdClass>

_indexData()

_indexData(string  $table) : array<mixed,\stdClass>

Returns an array of objects with index data

Parameters

string $table

Throws

\CodeIgniter\Database\Exceptions\DatabaseException

Returns

array<mixed,\stdClass>

_foreignKeyData()

_foreignKeyData(string  $table) : array<mixed,\stdClass>

Returns an array of objects with Foreign key data

Parameters

string $table

Throws

\CodeIgniter\Database\Exceptions\DatabaseException

Returns

array<mixed,\stdClass>

__get()

__get(string  $key) : mixed

Accessor for properties if they exist.

Parameters

string $key

Returns

mixed

__isset()

__isset(string  $key) : boolean

Checker for properties existence.

Parameters

string $key

Returns

boolean

_close()

_close() : void

Close the database connection.

_transBegin()

_transBegin() : boolean

Begin Transaction

Returns

boolean

_transCommit()

_transCommit() : boolean

Commit Transaction

Returns

boolean

_transRollback()

_transRollback() : boolean

Rollback Transaction

Returns

boolean

_escapeString()

_escapeString(string  $str) : string

Platform-dependant string escape

Will likely be overridden in child classes.

Parameters

string $str

Returns

string

_listTables()

_listTables(boolean  $prefixLimit = false) : string

Generates the SQL for listing tables in a platform-dependent manner.

Parameters

boolean $prefixLimit

Returns

string

_listColumns()

_listColumns(string  $table = '') : string

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

Parameters

string $table

Returns

string

_disableForeignKeyChecks()

_disableForeignKeyChecks() : string

Returns platform-specific SQL to disable foreign key checks.

Returns

string

_enableForeignKeyChecks()

_enableForeignKeyChecks() : string

Returns platform-specific SQL to enable foreign key checks.

Returns

string

buildDSN()

buildDSN() : void

Build a DSN from the provided parameters

setClientEncoding()

setClientEncoding(string  $charset) : boolean

Set client encoding

Parameters

string $charset

The client encoding to which the data will be converted.

Returns

boolean