Class: DBWorker

abstract

This is the abstract base class for all dbworkers for different databases.

Details

This is a wrapper around the database connectivity. It offers a universal common way for working with databases of different types. Currently, MySQL 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.

Implements

Known inheritances

Authors

Oleg Schildt

Package

Source code

Used by

 

Constants
public

The constant for the returning field value as is.

public

The constant for the date type.

public

The constant for the date/time type.

public

The constant for the type geometry SRID 0.

public

The constant for the type geometry SRID 4326 (latitude, longitude).

public

The constant for the large objects.

public

The constant for the number type.

public

The constant for the string type.

public

The constant for the error: connection data is incomplete.

public

The constant for the error: connection to the server failed.

public

The constant for the error: database not found.

public

The constant for the error: host not reachable.

public

The constant for the error: not connected.

public

The constant for the error: query failed.

public

The constant for the error: stream invalid.

public

The constant for the error: wrong data format.

public

The constant for the error: wrong user credentials.

Properties
protected

Name of the database.

protected

Password of the database user.

protected

Port of the server.

protected

Name or IP address of the server.

protected

Name of the database user.

protected

Flag property for storing the state whether it is clone or not.

protected

This variable stores the last executed query.

protected

This variable stores the logging flag.

Methods
abstract public

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

abstract public

Builds simple select query based on parameters.

public

Check whether the connection exists and throws an exceptions if not.

abstract public

Closes the currently opened connection.

abstract public

Commits the transation.

abstract public

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

abstract public

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

public

Sets the logging flag. If set, query is logged to a log file.

abstract public

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

abstract public

Executes the prepared SQL query.

abstract public

Executes the SQL stored procedure.

abstract public

Executes the SQL query.

abstract public

Fetches all rows from the result into an array.

abstract public

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

abstract public

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

abstract public

Returns the value of a field specified by name.

abstract public

Returns the value of a field specified by number.

abstract public

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

abstract public

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

abstract public

Returns the name of the field by number.

abstract public

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

abstract public

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

abstract public

Frees the prepared query.

abstract public

Frees the result of the previously executed retrieving query.

abstract public

Returns the name of the required PHP extension.

public

Returns the last executed query.

abstract public

Returns the name of the supported database.

abstract public

Returns the name of the database schema if applicable.

abstract public

Initializes the dbworker with connection parameters.

abstract public

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

abstract public

Returns the connection state.

abstract public

Checks whether the extension is installed which is required for work with the corresponding database.

public

Checks that the value is a number and returns it, or returns the string NULL if the value is empty.

abstract public

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

abstract public

Prepares the SQL query with bindable variables.

abstract public

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

public

Escapes the string so that it can be used in the query without causing an error or returns the string NULL if the string is empty.

abstract public

Rolls back the translation.

abstract public

Starts the translation.

abstract public

Stores long data from a stream.

abstract public

Sets the database as working database.


Constant: DBWorker::DB_AS_IS

const DB_AS_IS = 0;

The constant for the returning field value as is.

Type

int

Authors

Oleg Schildt

Source code


Constant: DBWorker::DB_DATE

const DB_DATE = 3;

The constant for the date type.

Type

int

Authors

Oleg Schildt

Source code


Constant: DBWorker::DB_DATETIME

const DB_DATETIME = 4;

The constant for the date/time type.

Type

int

Authors

Oleg Schildt

Source code


Constant: DBWorker::DB_GEOMETRY

const DB_GEOMETRY = 5;

The constant for the type geometry SRID 0.

Type

int

Authors

Oleg Schildt

Source code


Constant: DBWorker::DB_GEOMETRY_4326

const DB_GEOMETRY_4326 = 6;

The constant for the type geometry SRID 4326 (latitude, longitude).

Type

int

Authors

Oleg Schildt

Source code


Constant: DBWorker::DB_LARGE_OBJECT_STREAM

const DB_LARGE_OBJECT_STREAM = 7;

The constant for the large objects.

Details

If a RDBMS supports stream reading, it is performed.

Type

int

Authors

Oleg Schildt

Source code


Constant: DBWorker::DB_NUMBER

const DB_NUMBER = 1;

The constant for the number type.

Type

int

Authors

Oleg Schildt

Source code


Constant: DBWorker::DB_STRING

const DB_STRING = 2;

The constant for the string type.

Type

int

Authors

Oleg Schildt

Source code


Constant: DBWorker::ERR_CONNECTION_DATA_INCOMPLETE

const ERR_CONNECTION_DATA_INCOMPLETE = "err_connection_data_incomplete";

The constant for the error: connection data is incomplete.

Type

string

Authors

Oleg Schildt

Source code


Constant: DBWorker::ERR_CONNECTION_FAILED

const ERR_CONNECTION_FAILED = "err_connection_failed";

The constant for the error: connection to the server failed.

Type

string

Authors

Oleg Schildt

Source code


Constant: DBWorker::ERR_DATABASE_NOT_FOUND

const ERR_DATABASE_NOT_FOUND = "err_database_not_found";

The constant for the error: database not found.

Type

string

Authors

Oleg Schildt

Source code


Constant: DBWorker::ERR_HOST_UNREACHABLE

const ERR_HOST_UNREACHABLE = "err_host_unreachable";

The constant for the error: host not reachable.

Type

string

Authors

Oleg Schildt

Source code


Constant: DBWorker::ERR_NOT_CONNECTED

const ERR_NOT_CONNECTED = "err_not_connected";

The constant for the error: not connected.

Type

string

Authors

Oleg Schildt

Source code


Constant: DBWorker::ERR_QUERY_FAILED

const ERR_QUERY_FAILED = "err_query_failed";

The constant for the error: query failed.

Type

string

Authors

Oleg Schildt

Source code


Constant: DBWorker::ERR_STREAM_ERROR

const ERR_STREAM_ERROR = "err_stream_error";

The constant for the error: stream invalid.

Type

string

Authors

Oleg Schildt

Source code


Constant: DBWorker::ERR_WRONG_DATA_FORMAT

const ERR_WRONG_DATA_FORMAT = "err_data_format";

The constant for the error: wrong data format.

Type

string

Authors

Oleg Schildt

Source code


Constant: DBWorker::ERR_WRONG_USER_CREDENTIALS

const ERR_WRONG_USER_CREDENTIALS = "err_wrong_user_credentials";

The constant for the error: wrong user credentials.

Type

string

Authors

Oleg Schildt

Source code


Property: DBWorker::$db_name

protected string $db_name;

Name of the database.

Type

string

Authors

Oleg Schildt

Source code


Property: DBWorker::$db_password

protected string $db_password;

Password of the database user.

Type

string

Authors

Oleg Schildt

Source code


Property: DBWorker::$db_port

protected string $db_port;

Port of the server.

Type

string

Authors

Oleg Schildt

Source code


Property: DBWorker::$db_server

protected string $db_server;

Name or IP address of the server.

Type

string

Authors

Oleg Schildt

Source code


Property: DBWorker::$db_user

protected string $db_user;

Name of the database user.

Type

string

Authors

Oleg Schildt

Source code


Property: DBWorker::$is_clone

protected bool $is_clone = false;

Flag property for storing the state whether it is clone or not.

Type

bool

Authors

Oleg Schildt

Source code


Property: DBWorker::$last_query

protected ?string $last_query = null;

This variable stores the last executed query.

Type

?string

Authors

Oleg Schildt

Source code


Property: DBWorker::$logging

protected bool $logging = false;

This variable stores the logging flag.

Type

bool

Authors

Oleg Schildt

Source code


Method: DBWorker::affected_count()

abstract 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 might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code


Method: DBWorker::build_select_query()

abstract 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.

Authors

Oleg Schildt

Source code


Method: DBWorker::check_connection()

public function check_connection(): void;

Check whether the connection exists and throws an exceptions if not.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It throws an exception if there is no connection.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::close_connection()

abstract public function close_connection(): void;

Closes the currently opened connection.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::commit_transaction()

abstract public function commit_transaction(): void;

Commits the transation.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::connect()

abstract 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 might throw an exception in the case of any errors.

  • if some parameters are missing.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::create_clone()

abstract public function create_clone(): \SmartFactory\DatabaseWorkers\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\DBWorker

Returns the clone of this dbworker.

Authors

Oleg Schildt

Source code


Method: DBWorker::enable_logging()

function enable_logging(bool $state): void;

Sets the logging flag. If set, query is logged to a log file.

Parameters

Name Pass type Value type Default value Description
$state by value bool

The state of the logging.

Returns

void

Authors

Oleg Schildt

Source code


Method: DBWorker::escape()

abstract 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.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::execute_prepared_query()

abstract 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

\SmartFactory\DatabaseWorkers\DBWorkerException

It might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::execute_procedure()

abstract 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 might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code


Method: DBWorker::execute_query()

abstract 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

\SmartFactory\DatabaseWorkers\DBWorkerException

It might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code


Method: DBWorker::fetch_array()

abstract public function fetch_array(array &$rows, array|null $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 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. In the case of any error returns false.

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code


Method: DBWorker::fetch_row()

abstract 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 might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code


Method: DBWorker::fetched_count()

abstract 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 might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code


Method: DBWorker::field_by_name()

abstract 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.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::field_by_num()

abstract 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.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::field_count()

abstract 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 might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code


Method: DBWorker::field_info_by_num()

abstract 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.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::field_name()

abstract 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.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::format_date()

abstract 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.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::format_datetime()

abstract 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.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::free_prepared_query()

abstract 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 might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::free_result()

abstract 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 might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code


Method: DBWorker::get_extension_name()

abstract public function get_extension_name(): string;

Returns the name of the required PHP extension.

Returns

string

Returns the name of the required PHP extension.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::get_last_query()

function get_last_query(): string;

Returns the last executed query.

Returns

string

Returns the last executed query.

Authors

Oleg Schildt

Source code


Method: DBWorker::get_rdbms_name()

abstract public function get_rdbms_name(): string;

Returns the name of the supported database.

Returns

string

Returns the name of the supported database.

Authors

Oleg Schildt

Source code


Method: DBWorker::get_schema()

abstract 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.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::init()

abstract 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

The parameters may vary for each database.

Returns

void

Throws

\Exception

It might throw an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: DBWorker::insert_id()

abstract public function insert_id(): ?int;

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

Returns

?int

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

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code


Method: DBWorker::is_connected()

abstract public function is_connected(): bool;

Returns the connection state.

Returns

bool

Returns true if the connection is open, otherwise false.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::is_extension_installed()

abstract public function is_extension_installed(): bool;

Checks whether the extension is installed which is required for work with the corresponding database.

Returns

bool

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

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::number_or_null()

function number_or_null(string $str): float|int|string;

Checks that the value is a number and returns it, or returns the string NULL if the value is empty.

Parameters

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

The string to be escaped.

Returns

float|int|string

Returns the escaped string.

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::prepare_for_query()

abstract 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.

Authors

Oleg Schildt

Source code


Method: DBWorker::prepare_query()

abstract 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 might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::qualify_name_with_schema()

abstract 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.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::quotes_or_null()

function quotes_or_null(string $str): string;

Escapes the string so that it can be used in the query without causing an error or returns the string NULL if the string is empty.

Parameters

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

The string to be escaped.

Returns

string

Returns the escaped string.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::rollback_transaction()

abstract public function rollback_transaction(): void;

Rolls back the translation.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::start_transaction()

abstract public function start_transaction(): void;

Starts the translation.

Returns

void

Throws

\SmartFactory\DatabaseWorkers\DBWorkerException

It might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code

See also


Method: DBWorker::stream_long_data()

abstract 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 might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code


Method: DBWorker::use_database()

abstract 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 might throw an exception in the case of any errors.

Authors

Oleg Schildt

Source code