Class: PostgreSQL_DBWorker

This is the class for the PostreSQL database using the extension pgsql.

Details

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.

Extends

Authors

Oleg Schildt

Package

Source code

See also

 

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.


Property: PostgreSQL_DBWorker::$connection

protected ?\PgSql\Connection $connection = null;

Internal PgSql\Connection object.

Type

?\PgSql\Connection

Authors

Oleg Schildt

Source code


Property: PostgreSQL_DBWorker::$field_names

protected ?array $field_names = null;

Internal variable for storing of the column names from the result of the last retrieving query.

Type

?array

Authors

Oleg Schildt

Source code


Property: PostgreSQL_DBWorker::$prepared_query

protected ?string $prepared_query = null;

Internal variable for storing of the last prepared query.

Type

?string

Authors

Oleg Schildt

Source code


Property: PostgreSQL_DBWorker::$read_only

protected bool $read_only = false;

Flag for setting the connection to read only.

Type

bool

Authors

Oleg Schildt

Source code


Property: PostgreSQL_DBWorker::$result

protected ?\PgSql\Result $result = null;

Internal \PgSql\Result object.

Type

?\PgSql\Result

Authors

Oleg Schildt

Source code


Property: PostgreSQL_DBWorker::$row

protected null|bool|array $row = null;

Internal variable for storing of the current fetched row from the result of the last retrieving query.

Type

null|bool|array

Authors

Oleg Schildt

Source code


Property: PostgreSQL_DBWorker::$statement

protected ?\PgSql\Result $statement = null;

Internal \PgSql\Result object for statements.

Type

?\PgSql\Result

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::__construct()

public function __construct();

Constructor.

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::__destruct()

public function __destruct(): void;

Destructor.

Returns

void

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::affected_count()

public function affected_count(): int;

Returns the number of the rows affected by the last modification query.

Returns

int

Returns the number of the rows affected by the last modification query.

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::build_select_query()

function build_select_query(string $table, array $fields, string $where_clause, string $order_clause, int $limit ) : string;

Builds simple select query based on parameters.

Details

It is used for building queries with limits.

Parameters

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

string

Returns the built query.

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::close_connection()

public function close_connection(): void;

Closes the currently opened connection.

Returns

void

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::commit_transaction()

public function commit_transaction(): void;

Commits the transation.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::connect()

public function connect(): void;

Establishes the connection to the database using the connection settings parameters specified by the initialization.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::create_clone()

public function create_clone(): \SmartFactory\DatabaseWorkers\PostgreSQL_DBWorker;

Creates a clone of the dbworker that is using the same open connection.

Details

This might be useful if you want to execute some additional queries while iteration through the active results of a select query.

Returns

\SmartFactory\DatabaseWorkers\PostgreSQL_DBWorker

Returns the clone of this dbworker.

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::escape()

public function escape(string $str): string;

Escapes the string so that it can be used in the query without causing an error.

Parameters

Name Pass type Value type Default value Description
$str by value string

The string to be escaped.

Returns

string

Returns the escaped string.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::execute_prepared_query()

public function execute_prepared_query(mixed ...$args): void;

Executes the prepared SQL query.

Parameters

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.

Returns

void

Throws

\Exception|\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::execute_procedure()

public function execute_procedure(string $procedure): void;

Executes the SQL stored procedure.

Parameters

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.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::execute_query()

public function execute_query(string $query_string): void;

Executes the SQL query.

Parameters

Name Pass type Value type Default value Description
$query_string by value string

The SQL query to be executed.

Returns

void

Throws

\Exception|\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::fetch_array()

public function fetch_array(array &$rows, ?array $dimension_keys = null): int;

Fetches all rows from the result into an array.

Parameters

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:

$rows = [];
$dbw->fetch_array($rows);

rows[n] = ["col1" => "val1", "col2" => "val2", "col3" => "val3", ...]

If the dimension columns are specified, their values are used for the dimensions.

Example:

$rows = [];
$dbw->fetch_array($rows, ["col1", "col2"]);

rows["val1"]["val2"] = ["col3" => "val3", ...]

Returns

int

Returns the number of the fetched rows. It might be also 0.

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::fetch_row()

public function fetch_row(): bool;

Fetches the next row of data from the result of the execution of the retrieving query.

Returns

bool

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();

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::fetched_count()

public function fetched_count(): int;

Returns the number of the rows fetched by the last retrieving query.

Returns

int

Returns the number of the rows fetched by the last retrieving query.

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::field_by_name()

public function field_by_name(string $name, int $type = self::DB_AS_IS): mixed;

Returns the value of a field specified by name.

Parameters

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

mixed

Returns the value of a field specified by name. In the case of any error returns null.

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::field_by_num()

public function field_by_num(int $num, int $type = self::DB_AS_IS): mixed;

Returns the value of a field specified by number.

Parameters

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

mixed

Returns the value of a field specified by number. In the case of any error returns null.

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::field_count()

public function field_count(): int;

Returns the number of the fields in the result of the last retrieving query.

Returns

int

Returns the number of the fields in the result of the last retrieving query.

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::field_info_by_num()

public function field_info_by_num(int $num): ?array;

Returns the meta information about the field as an object with properties.

Parameters

Name Pass type Value type Default value Description
$num by value int

The number of the field.

Returns

?array

Returns the associative array with properties. In the case of any error returns null.

  • $info["name"] - name of the field.
  • $info["type"] - type of the field.
  • $info["size"] - size of the field.
  • $info["binary"] - whether the filed is binary.
  • $info["numeric"] - whether the filed is numeric.
  • $info["datetime"] - whether the filed is datetime.

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::field_name()

public function field_name(int $num): ?string;

Returns the name of the field by number.

Parameters

Name Pass type Value type Default value Description
$num by value int

The number of the field.

Returns

?string

Returns the value of a field specified by number as an object with properties. In the case of any error returns null.

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::format_date()

public function format_date(int $date): string;

Formats the date to a string compatible for the corresponding database.

Parameters

Name Pass type Value type Default value Description
$date by value int

The date value as timestamp.

Returns

string

Returns the string representation of the date compatible for the corresponding database.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::format_datetime()

public function format_datetime(int $datetime): string;

Formats the date/time to a string compatible for the corresponding database.

Parameters

Name Pass type Value type Default value Description
$datetime by value int

The date/time value as timestamp.

Returns

string

Returns the string representation of the date/time compatible for the corresponding database.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::free_prepared_query()

public function free_prepared_query(): void;

Frees the prepared query.

Details

It should be called after all executions of the prepared query.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::free_result()

public function free_result(): void;

Frees the result of the previously executed retrieving query.

Details

It should be called only for the retrieving queries.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::get_extension_name()

public function get_extension_name(): string;

Returns the name of the required PHP extension - "pgsql".

Returns

string

Returns the name of the required PHP extension - "pgsql".

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::get_rdbms_name()

public function get_rdbms_name(): string;

Returns the name of the supported database - "PostgreSQL Server".

Returns

string

Returns the name of the supported database - "PostgreSQL Server".

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::get_schema()

public function get_schema(): string;

Returns the name of the database schema if applicable.

Returns

string

Returns the name of the database schema if applicable, or an empty string.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::init()

public function init(array $parameters): void;

Initializes the dbworker with connection parameters.

Parameters

Name Pass type Value type Default value Description
$parameters by value array

Connection settings as an associative array in the form key => value:

  • $parameters["db_server"] - server address.
  • $parameters["db_port"] - server port.
  • $parameters["db_name"] - database name.
  • $parameters["db_user"] - user name.
  • $parameters["db_password"] - user password.
  • $parameters["read_only"] - this parameter sets the connection to the read only mode.

Returns

void

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::insert_id()

public function insert_id(): ?int;

Returns the value of the auto increment field by the last insertion.

Details

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

?int

Returns the value of the auto increment field by the last insertion.

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::is_connected()

public function is_connected(): bool;

Returns the connection state.

Returns

bool

Returns true if the connection is open, otherwise false.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::is_extension_installed()

public function is_extension_installed(): bool;

Checks whether the extension (pgsql) is installed which is required for work with the PostreSQL database.

Returns

bool

The method should return true if the extension is installed, otherwise false.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::prepare_for_query()

function prepare_for_query(mixed $value, int $type): string;

Prepares the value for putting to a query depending on its type. It does escaping, formatting and quotation if necessary.

Parameters

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

string

Returns the prepared value.

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::prepare_query()

public function prepare_query(string $query_string): void;

Prepares the SQL query with bindable variables.

Parameters

Name Pass type Value type Default value Description
$query_string by value string

The SQL query to be prepared.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::qualify_name_with_schema()

public function qualify_name_with_schema(string $name): string;

Completes the name of a database object with the schema name if applicable.

Parameters

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

string

Returns the name of the database object with the schema name if applicable, otherwise the name of the database object remains unchanged.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::read_large_object()

protected function read_large_object(int $oid): string;

This auxiliary function reads the data from large object.

Parameters

Name Pass type Value type Default value Description
$oid by value int

The oid of the large object.

Returns

string

Returns the data read from the largeobject.

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::rollback_transaction()

public function rollback_transaction(): void;

Rolls back the transation.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::start_transaction()

public function start_transaction(): void;

Starts the transation.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: PostgreSQL_DBWorker::stream_long_data()

public function stream_long_data(string $query_string, resource $stream): void;

Stores long data from a stream.

Parameters

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:

$stream = fopen(".../large_binary.jpg", "rb");

if(!$dbw->stream_long_data("UPDATE LARGE_DATA SET BLOB_DATA = ? WHERE ID = 1", $stream))
{
  error reporting ...;
}

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::sys_get_errors()

protected function sys_get_errors(): string;

Retrieves the last errors occurred while execution of the query.

Returns

string

Returns the string of errors separated by the new line symbol.

Authors

Oleg Schildt

Source code


Method: PostgreSQL_DBWorker::use_database()

public function use_database(string $db_name): void;

Sets the database as working database.

Parameters

Name Pass type Value type Default value Description
$db_name by value string

The name of the database to be set as working database.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code