This is the abstract base class for all dbworkers for different databases.
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.
| 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. |
The constant for the returning field value as is.
The constant for the date type.
The constant for the date/time type.
The constant for the type geometry SRID 0.
The constant for the type geometry SRID 4326 (latitude, longitude).
The constant for the large objects.
If a RDBMS supports stream reading, it is performed.
The constant for the number type.
The constant for the string type.
The constant for the error: connection data is incomplete.
The constant for the error: connection to the server failed.
The constant for the error: database not found.
The constant for the error: host not reachable.
The constant for the error: not connected.
The constant for the error: query failed.
The constant for the error: stream invalid.
The constant for the error: wrong data format.
The constant for the error: wrong user credentials.
Name of the database.
Password of the database user.
Port of the server.
Name or IP address of the server.
Name of the database user.
Flag property for storing the state whether it is clone or not.
This variable stores the last executed query.
This variable stores the logging flag.
Returns the number of the rows affected by the last modification query.
Returns the number of the rows affected by the last modification query.
It might throw an exception in the case of any errors.
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.
Check whether the connection exists and throws an exceptions if not.
Closes the currently opened connection.
It might throw an exception in the case of any errors.
Commits the transation.
It might throw an exception in the case of any errors.
Establishes the connection to the database using the connection settings parameters specified by the initialization.
It might throw an exception in the case of any errors.
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.
Sets the logging flag. If set, query is logged to a log file.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $state | by value | bool |
The state of the logging. |
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 might throw 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. |
It might throw an exception in the case of any errors.
Executes the SQL query.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $query_string | by value | string |
The SQL query to be executed. |
It might throw 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 | 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. In the case of any error returns false.
It might throw an exception in the case of any errors.
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();It might throw an exception in the case of any errors.
Returns the number of the rows fetched by the last retrieving query.
Returns the number of the rows fetched by the last retrieving query.
It might throw an exception in the case of any errors.
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.
It might throw an exception in the case of any errors.
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.
It might throw an exception in the case of any errors.
Frees the result of the previously executed retrieving query.
It should be called only for the retrieving queries.
It might throw an exception in the case of any errors.
Returns the name of the required PHP extension.
Returns the name of the required PHP extension.
Returns the last executed query.
Returns the last executed query.
Returns the name of the supported database.
Returns the name of the supported database.
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 |
The parameters may vary for each database. |
It might throw an exception in the case of any errors.
Returns the value of the auto increment field by the last insertion.
Returns the value of the auto increment field by the last insertion.
It might throw an exception in the case of any errors.
Returns the connection state.
Returns true if the connection is open, otherwise false.
Checks whether the extension is installed which is required for work with the corresponding database.
The method should return true if the extension is installed, otherwise false.
Checks that the value is a number and returns it, or returns the string NULL if the value is empty.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $str | by value | string |
The string to be escaped. |
Returns the escaped string.
It might throw an exception in the case of any errors.
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. |
It might throw an exception in the case of any errors.
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.
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.
| Name | Pass type | Value type | Default value | Description |
|---|---|---|---|---|
| $str | by value | string |
The string to be escaped. |
Returns the escaped string.
Rolls back the translation.
It might throw an exception in the case of any errors.
Starts the translation.
It might throw an exception in the case of any errors.
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:
|
It might throw an exception in the case of any errors.
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. |
It might throw an exception in the case of any errors.