This is the class for the PostreSQL database using the extension pgsql.
This is a wrapper around the database connectivity. It offers a universal common way for working with databases of different types. Currently, MySQL, PostgreSQL and MS SQL are supported. If in the future, there will be a better solution, or the current solution turns out to be inefficient in a new version of PHP, we can easily reimplement the DB wrapper without touching the business logic code. Adding support for new database types is also much easier with this wrapping approach.
| Properties | |
|---|---|
|
protected
|
Internal PgSql\Connection object. |
|
protected
|
Internal variable for storing of the column names from the result of the last retrieving query. |
|
protected
|
Internal variable for storing of the last prepared query. |
|
protected
|
Flag for setting the connection to read only. |
|
protected
|
Internal \PgSql\Result object. |
|
protected
|
Internal variable for storing of the current fetched row from the result of the last retrieving query. |
|
protected
|
Internal \PgSql\Result object for statements. |
| Methods | |
|---|---|
|
public
|
Constructor. |
|
public
|
Destructor. |
|
public
|
Returns the number of the rows affected by the last modification query. |
|
public
|
Builds simple select query based on parameters. |
|
public
|
Closes the currently opened connection. |
|
public
|
Commits the transation. |
|
public
|
Establishes the connection to the database using the connection settings parameters specified by the initialization. |
|
public
|
Creates a clone of the dbworker that is using the same open connection. |
|
public
|
Escapes the string so that it can be used in the query without causing an error. |
|
public
|
Executes the prepared SQL query. |
|
public
|
Executes the SQL stored procedure. |
|
public
|
Executes the SQL query. |
|
public
|
Fetches all rows from the result into an array. |
|
public
|
Fetches the next row of data from the result of the execution of the retrieving query. |
|
public
|
Returns the number of the rows fetched by the last retrieving query. |
|
public
|
Returns the value of a field specified by name. |
|
public
|
Returns the value of a field specified by number. |
|
public
|
Returns the number of the fields in the result of the last retrieving query. |
|
public
|
Returns the meta information about the field as an object with properties. |
|
public
|
Returns the name of the field by number. |
|
public
|
Formats the date to a string compatible for the corresponding database. |
|
public
|
Formats the date/time to a string compatible for the corresponding database. |
|
public
|
Frees the prepared query. |
|
public
|
Frees the result of the previously executed retrieving query. |
|
public
|
Returns the name of the required PHP extension - "pgsql". |
|
public
|
Returns the name of the supported database - "PostgreSQL Server". |
|
public
|
Returns the name of the database schema if applicable. |
|
public
|
Initializes the dbworker with connection parameters. |
|
public
|
Returns the value of the auto increment field by the last insertion. |
|
public
|
Returns the connection state. |
|
public
|
Checks whether the extension (pgsql) is installed which is required for work with the PostreSQL database. |
|
public
|
Prepares the value for putting to a query depending on its type. It does escaping, formatting and quotation if necessary. |
|
public
|
Prepares the SQL query with bindable variables. |
|
public
|
Completes the name of a database object with the schema name if applicable. |
|
protected
|
This auxiliary function reads the data from large object. |
|
public
|
Rolls back the transation. |
|
public
|
Starts the transation. |
|
public
|
Stores long data from a stream. |
|
protected
|
Retrieves the last errors occurred while execution of the query. |
|
public
|
Sets the database as working database. |
Internal PgSql\Connection object.
Internal variable for storing of the column names from the result of the last retrieving query.
Internal variable for storing of the last prepared query.
Flag for setting the connection to read only.
Internal \PgSql\Result object.
Internal variable for storing of the current fetched row from the result of the last retrieving query.
Internal \PgSql\Result object for statements.
Constructor.
Destructor.
Returns the number of the rows affected by the last modification query.
Returns the number of the rows affected by the last modification query.
Builds simple select query based on parameters.
It is used for building queries with limits.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $table | by value | string |
The name of the table. |
|
| $fields | by value | array |
The list of request fields. |
|
| $where_clause | by value | string |
The where clause that should restrict the result. |
|
| $order_clause | by value | string |
The order clause to sort the results. |
|
| $limit | by value | int |
The limit how many records should be loaded. 0 for unlimited. |
Returns the built query.
Closes the currently opened connection.
Commits the transation.
Establishes the connection to the database using the connection settings parameters specified by the initialization.
Creates a clone of the dbworker that is using the same open connection.
This might be useful if you want to execute some additional queries while iteration through the active results of a select query.
Escapes the string so that it can be used in the query without causing an error.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $str | by value | string |
The string to be escaped. |
Returns the escaped string.
Executes the prepared SQL query.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $args | by value, variadic | mixed |
The number of parameters may vary and be zero. An array can also be passed. These are parameters of the prepared query. |
It throws an exception in the case of any errors.
Executes the SQL stored procedure.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $procedure | by value | string |
The name of the SQL stored procedure. All subsequent parameters are the parameters of the SQL stored procedure. |
Executes the SQL query.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $query_string | by value | string |
The SQL query to be executed. |
It throws an exception in the case of any errors.
Fetches all rows from the result into an array.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $rows | by reference | array |
Target array for loading the results. |
|
| $dimension_keys | by value | ?array | null |
Array of the column names that should be used as dimensions. Per default, the rows are fetched as two-dimensional array. Example:
If the dimension columns are specified, their values are used for the dimensions. Example:
|
Returns the number of the fetched rows. It might be also 0.
Fetches the next row of data from the result of the execution of the retrieving query.
Returns true if the next row exists and has been successfully fetched, otherwise false.
Example:
if(!$dbw->execute_query("SELECT FIRST_NAME, LAST_NAME FROM USERS"))
{
error reporting ...;
}
while($dbw->fetch_row())
{
echo $dbw->field_by_name("FIRST_NAME") . " " . $dbw->field_by_name("LAST_NAME") . "<br>";
}
$dbw->free_result();Returns the number of the rows fetched by the last retrieving query.
Returns the number of the rows fetched by the last retrieving query.
Returns the value of a field specified by name.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $name | by value | string |
The name of the field. |
|
| $type | by value | int | self::DB_AS_IS |
The type of the field. |
Returns the value of a field specified by name. In the case of any error returns null.
Returns the value of a field specified by number.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $num | by value | int |
The number of the field. |
|
| $type | by value | int | self::DB_AS_IS |
The type of the field. |
Returns the value of a field specified by number. In the case of any error returns null.
Returns the number of the fields in the result of the last retrieving query.
Returns the number of the fields in the result of the last retrieving query.
Returns the meta information about the field as an object with properties.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $num | by value | int |
The number of the field. |
Returns the associative array with properties. In the case of any error returns null.
Returns the name of the field by number.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $num | by value | int |
The number of the field. |
Returns the value of a field specified by number as an object with properties. In the case of any error returns null.
Formats the date to a string compatible for the corresponding database.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $date | by value | int |
The date value as timestamp. |
Returns the string representation of the date compatible for the corresponding database.
Formats the date/time to a string compatible for the corresponding database.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $datetime | by value | int |
The date/time value as timestamp. |
Returns the string representation of the date/time compatible for the corresponding database.
Frees the prepared query.
It should be called after all executions of the prepared query.
Frees the result of the previously executed retrieving query.
It should be called only for the retrieving queries.
Returns the name of the required PHP extension - "pgsql".
Returns the name of the required PHP extension - "pgsql".
Returns the name of the supported database - "PostgreSQL Server".
Returns the name of the supported database - "PostgreSQL Server".
Returns the name of the database schema if applicable.
Returns the name of the database schema if applicable, or an empty string.
Initializes the dbworker with connection parameters.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $parameters | by value | array |
Connection settings as an associative array in the form key => value:
|
Returns the value of the auto increment field by the last insertion.
WARNING! This method only works if there are no triggers with insertion commands.
PostgreSQL recommends using the sequences like Oracle. If you want to use sequences, generate ID values with passing to \SmartFactory\DatabaseWorkers\PostgreSQL_DBWorker::execute_query() the sequence generation commands and write your inserts with explicit ID values.
Returns the value of the auto increment field by the last insertion.
Returns the connection state.
Returns true if the connection is open, otherwise false.
Checks whether the extension (pgsql) is installed which is required for work with the PostreSQL database.
The method should return true if the extension is installed, otherwise false.
Prepares the value for putting to a query depending on its type. It does escaping, formatting and quotation if necessary.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $value | by value | mixed |
The value to be formatted. |
|
| $type | by value | int |
The type of the value. |
Returns the prepared value.
Prepares the SQL query with bindable variables.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $query_string | by value | string |
The SQL query to be prepared. |
Completes the name of a database object with the schema name if applicable.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $name | by value | string |
The name of the database object to be completed with the schema name. |
Returns the name of the database object with the schema name if applicable, otherwise the name of the database object remains unchanged.
This auxiliary function reads the data from large object.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $oid | by value | int |
The oid of the large object. |
Returns the data read from the largeobject.
Rolls back the transation.
Starts the transation.
Stores long data from a stream.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $query_string | by value | string |
The SQL query to be used for storing the long data. |
|
| $stream | by value | resource |
The opened valid stream for reading the long data. Example:
|
Retrieves the last errors occurred while execution of the query.
Returns the string of errors separated by the new line symbol.
Sets the database as working database.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $db_name | by value | string |
The name of the database to be set as working database. |