class PostgreSQLConnector extends DBConnector (View source)

PostgreSQL connector class using the PostgreSQL specific api

The connector doesn't know anything about schema selection, so code related to masking multiple databases as schemas should be handled in the database controller and schema manager.

Properties

protected resource $dbConn

Connection to the PG Database database

protected string $databaseName

Name of the currently selected database

protected resource $lastQuery

Reference to the last query result (for pg_affected_rows)

protected array $lastParameters

Last parameters used to connect

protected $lastRows

Methods

protected
databaseError(string $msg, int $errorLevel = E_USER_ERROR, string $sql = null, array $parameters = array())

Error handler for database errors.

public
bool
isQueryMutable(string $sql)

Determine if this SQL statement is a destructive operation (write or ddl)

public
bool
isQueryDDL(string $sql)

Determine if this SQL statement is a DDL operation

public
bool
isQueryWrite(string $sql)

Determine if this SQL statement is a write operation (alters content but not structure)

protected
isQueryType(string $sql, string|array $type)

Determine if a query is of the given type

protected
array
parameterValues(array $parameters)

Extracts only the parameter values for error reporting

public
connect(array $parameters, bool $selectDB = false)

Link this connector to the database given the specified parameters Will throw an exception rather than return a success state.

public
string
getVersion()

Query for the version of the currently connected database

public
string
escapeString(string $value)

Given a value escape this for use in a query for the current database connector. Note that this does not quote the value.

public
string
quoteString(string $value)

Given a value escape and quote this appropriately for the current database connector.

public
escapeIdentifier(string $value, string $separator = '.')

Escapes an identifier (table / database name). Typically the value is simply double quoted. Don't pass in already escaped identifiers in, as this will double escape the value!

public
query(string $sql, int $errorLevel = E_USER_ERROR)

Executes the following query with the specified error level.

public
preparedQuery(string $sql, array $parameters, int $errorLevel = E_USER_ERROR)

Execute the given SQL parameterised query with the specified arguments

public
bool
selectDatabase(string $name)

Select a database by name

public
string
getSelectedDatabase()

Retrieves the name of the currently selected database

public
unloadDatabase()

De-selects the currently selected database

public
string
getLastError()

Retrieves the last error generated from the database connection

public
int
getGeneratedID(string $table)

Determines the last ID generated from the specified table.

public
int
affectedRows()

Determines the number of affected rows from the last SQL query

public
bool
isActive()

Determines if we are connected to a server AND have a valid database selected.

protected
string
escapeParameter(array $parameters, string $key, string $name, mixed $default = null)

Escape a parameter to be used in the connection string

public
bool
checkStringTogglesLiteral(string $input)

Determines if the SQL fragment either breaks into or out of a string literal by counting single quotes

public
string
replacePlaceholders(string $sql)

Iteratively replaces all question marks with numerical placeholders E.g. "Title = ? AND Name = ?" becomes "Title = $1 AND Name = $2"

Details

protected databaseError(string $msg, int $errorLevel = E_USER_ERROR, string $sql = null, array $parameters = array())

Error handler for database errors.

All database errors will call this function to report the error. It isn't a static function; it will be called on the object itself and as such can be overridden in a subclass. Subclasses should run all errors through this function.

hook this into a more well-structured error handling system.

Parameters

string $msg

The error message.

int $errorLevel

The level of the error to throw.

string $sql

The SQL related to this query

array $parameters

Parameters passed to the query

Exceptions

SS_DatabaseException

bool isQueryMutable(string $sql)

Determine if this SQL statement is a destructive operation (write or ddl)

Parameters

string $sql

Return Value

bool

bool isQueryDDL(string $sql)

Determine if this SQL statement is a DDL operation

Parameters

string $sql

Return Value

bool

bool isQueryWrite(string $sql)

Determine if this SQL statement is a write operation (alters content but not structure)

Parameters

string $sql

Return Value

bool

protected isQueryType(string $sql, string|array $type)

Determine if a query is of the given type

Parameters

string $sql

Raw SQL

string|array $type

Type or list of types (first word in the query). Must be lowercase

protected array parameterValues(array $parameters)

Extracts only the parameter values for error reporting

Parameters

array $parameters

Return Value

array

List of parameter values

connect(array $parameters, bool $selectDB = false)

Link this connector to the database given the specified parameters Will throw an exception rather than return a success state.

The connector should not select the database once connected until explicitly called by selectDatabase()

Parameters

array $parameters

List of parameters such as

  • type
  • server
  • username
  • password
  • database
  • path
bool $selectDB

By default database selection should be handled by the database controller (to enable database creation on the fly if necessary), but some interfaces require that the database is specified during connection (SQLite, Azure, etc).

string getVersion()

Query for the version of the currently connected database

Return Value

string

Version of this database

string escapeString(string $value)

Given a value escape this for use in a query for the current database connector. Note that this does not quote the value.

Parameters

string $value

The value to be escaped

Return Value

string

The appropritaely escaped string for value

string quoteString(string $value)

Given a value escape and quote this appropriately for the current database connector.

Parameters

string $value

The value to be injected into a query

Return Value

string

The appropriately escaped and quoted string for $value

escapeIdentifier(string $value, string $separator = '.')

Escapes an identifier (table / database name). Typically the value is simply double quoted. Don't pass in already escaped identifiers in, as this will double escape the value!

Parameters

string $value

The identifier to escape

string $separator

optional identifier splitter

query(string $sql, int $errorLevel = E_USER_ERROR)

Executes the following query with the specified error level.

Implementations of this function should respect previewWrite and benchmarkQuery

Parameters

string $sql

The SQL query to execute

int $errorLevel

For errors to this query, raise PHP errors using this error level.

SS_Query preparedQuery(string $sql, array $parameters, int $errorLevel = E_USER_ERROR)

Execute the given SQL parameterised query with the specified arguments

Parameters

string $sql

The SQL query to execute. The ? character will denote parameters.

array $parameters

An ordered list of arguments.

int $errorLevel

The level of error reporting to enable for the query

Return Value

SS_Query

bool selectDatabase(string $name)

Select a database by name

Parameters

string $name

Name of database

Return Value

bool

Flag indicating success

string getSelectedDatabase()

Retrieves the name of the currently selected database

Return Value

string

Name of the database, or null if none selected

unloadDatabase()

De-selects the currently selected database

string getLastError()

Retrieves the last error generated from the database connection

Return Value

string

The error message

int getGeneratedID(string $table)

Determines the last ID generated from the specified table.

Note that some connectors may not be able to return $table specific responses, and this parameter may be ignored.

Parameters

string $table

The target table to return the last generated ID for

Return Value

int

ID value

int affectedRows()

Determines the number of affected rows from the last SQL query

Return Value

int

Number of affected rows

bool isActive()

Determines if we are connected to a server AND have a valid database selected.

Return Value

bool

Flag indicating that a valid database is connected

protected string escapeParameter(array $parameters, string $key, string $name, mixed $default = null)

Escape a parameter to be used in the connection string

Parameters

array $parameters

All parameters

string $key

The key in $parameters to pull from

string $name

The connection string parameter name

mixed $default

The default value, or null if optional

Return Value

string

The completed fragment in the form name=value

bool checkStringTogglesLiteral(string $input)

Determines if the SQL fragment either breaks into or out of a string literal by counting single quotes

Handles double-quote escaped quotes as well as slash escaped quotes

Test this!

Parameters

string $input

The SQL fragment

Return Value

bool

True if the string breaks into or out of a string literal

See also

http://www.postgresql.org/docs/8.3/interactive/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS

string replacePlaceholders(string $sql)

Iteratively replaces all question marks with numerical placeholders E.g. "Title = ? AND Name = ?" becomes "Title = $1 AND Name = $2"

Better consider question marks in string literals

Parameters

string $sql

Paramaterised query using question mark placeholders

Return Value

string

Paramaterised query using numeric placeholders