CPostgreSqlDb
in package
implements
IDBMS
Implements the IDBMS interface to perform all specific PostgreSQL-related basic operations.
Table of Contents
Interfaces
- IDBMS
- Interface for a generic Database Management System.
Properties
- $connection_manager : IConnectionManager|null
- $dbms_type : string
- $last_id : int
- $logger : ILogger|null
- $result : mixed
- $static_connection_manager : IConnectionManager|null
- $stmt : mixed
- $table : string
Methods
- __construct() : mixed
- Constructor
- BeginTransaction() : void
- Starts a DB transaction.
- Commit() : void
- Commits the current transaction.
- ConvertDBType2BindType() : string
- Converts a generic DB data type into a driver-specific bind type.
- Decrypt() : string
- Returns the DB instruction to decrypt a value.
- DeleteRecord() : bool
- Delete records from a DB table
- Disconnect() : void
- Releases the resource to connect to the DB
- DisconnectAll() : void
- Releases all the resources of the previously opened DBs
- Encrypt() : string
- Returns the DB instruction to encrypt a value.
- Execute() : void
- Executes a previously prepared statement.
- ExistsTable() : bool
- Checks if a table exists in the database.
- ExistsTableColumns() : bool
- Checks if a list of columns exists in a table.
- GetAffectedRows() : int
- Returns how many rows were affected by the last operation performed.
- GetAttributesFromType() : bool
- Gets field attributes based on a DB data type.
- GetColumnNames() : array<string|int, string>|null
- Returns an array containing the names of all columns for the last query executed.
- GetDataTable() : int|null
- Fetches the entire result set of a query into an array.
- GetDataValue() : string|null
- Returns a single value from a query result.
- GetDBResultAssoc() : int
- Returns the driver-specific value for an associative array fetch type.
- GetDBResultBoth() : int
- Returns the driver-specific value for a fetch type that includes both associative and numeric arrays.
- GetDBResultNum() : int
- Returns the driver-specific value for a numeric array fetch type.
- GetDBTable() : string
- Returns the name of the current main table
- GetFetchArray() : array<string|int, mixed>|null
- Fetches a result row as an array (both numeric and associative).
- GetFetchAssoc() : array<string|int, mixed>|null
- Fetches a result row as an associative array.
- GetFetchRow() : array<string|int, mixed>|null
- Fetches a result row as a numeric array.
- GetLastDBErrorNumber() : int
- Gets any error number issued by the DB.
- GetLastDBErrorString() : string
- Gets any error string issued by the DB.
- GetLastID() : int|null
- Returns the ID of the last inserted item
- GetNextID() : int|null
- Returns the next ID from a sequence or similar.
- GetTableColumnNames() : array<string|int, string>
- Returns an array containing the names of all columns of a table.
- GetTotalColumns() : int|bool
- Returns the number of columns of a previously executed query.
- GetTotalRows() : int|bool
- Reads the total number of rows from a previously executed query.
- InsertRecord() : bool
- Make an entry in the DB
- InsertRecordBatch() : bool
- Insert multiple records in a single batch operation
- LoadAllTables() : void
- Loads the schema of all tables into an array.
- Prepare() : void
- Prepares an SQL statement for execution.
- Query() : bool
- Executes a query on the DB.
- Rollback() : void
- Rolls back the current transaction.
- SetAutoCommit() : void
- Sets the autocommit mode.
- SetDBColumns() : void
- Loads the schema of a table's columns into an array.
- SetDBTable() : void
- Sets the name of the reference table for the instance.
- UpdateRecord() : bool
- Make an update in the DB
- ConvertPlaceholders() : string
- Convert prepared statement placeholders from ? to $1, $2, $3 format for PostgreSQL
- GetConnectionManager() : IConnectionManager
- Gets the connection manager instance.
Properties
$connection_manager
protected
IConnectionManager|null
$connection_manager
= null
The connection manager for this instance
$dbms_type
protected
string
$dbms_type
= ''
The DBMS type identifier
$last_id
protected
int
$last_id
= 0
The last inserted ID
$logger
protected
ILogger|null
$logger
= null
The logger instance
$result
protected
mixed
$result
= null
The result set from the last query
$static_connection_manager
protected
static IConnectionManager|null
$static_connection_manager
= null
Static connection manager for backward compatibility
$stmt
protected
mixed
$stmt
= null
The prepared statement handle
$table
protected
string
$table
= ''
The default table name for operations
Methods
__construct()
Constructor
public
__construct([string $table = '' ][, IConnectionManager|null $connection_manager = null ][, ILogger|null $logger = null ]) : mixed
Parameters
- $table : string = ''
-
name of the default db table
- $connection_manager : IConnectionManager|null = null
-
Optional connection manager
- $logger : ILogger|null = null
-
Optional logger instance
BeginTransaction()
Starts a DB transaction.
public
BeginTransaction() : void
Commit()
Commits the current transaction.
public
Commit() : void
ConvertDBType2BindType()
Converts a generic DB data type into a driver-specific bind type.
public
ConvertDBType2BindType(string|null $db_type) : string
Parameters
- $db_type : string|null
-
The DB data type.
Return values
string —The corresponding variable type for parameter binding.
Decrypt()
Returns the DB instruction to decrypt a value.
public
Decrypt(string $val[, string $name = '' ]) : string
Parameters
- $val : string
-
value or field name to decrypt
- $name : string = ''
-
alias for the returned field
Return values
string —DB instruction to decrypt $val
DeleteRecord()
Delete records from a DB table
public
DeleteRecord(string|null $table, string $conditions, array<string|int, mixed> $conditions_values, array<string|int, mixed> $types) : bool
Parameters
- $table : string|null
-
name of the table
- $conditions : string
-
of the where clause
- $conditions_values : array<string|int, mixed>
-
values present in the WHERE condition
- $types : array<string|int, mixed>
-
of DB table fields
Return values
bool —result of the operation, true if successful
Disconnect()
Releases the resource to connect to the DB
public
static Disconnect([string|null $dbkey = null ]) : void
Parameters
- $dbkey : string|null = null
-
key of db to disconnect (optional)
DisconnectAll()
Releases all the resources of the previously opened DBs
public
static DisconnectAll() : void
Encrypt()
Returns the DB instruction to encrypt a value.
public
Encrypt(string $val[, string $name = '' ]) : string
Parameters
- $val : string
-
value or field name to be encrypted
- $name : string = ''
-
alias for the returned field
Return values
string —DB instruction to encrypt $val
Execute()
Executes a previously prepared statement.
public
Execute([array<string|int, mixed> $bind_param_values = [] ][, array<string|int, mixed> $bind_param_types = [] ]) : void
Parameters
- $bind_param_values : array<string|int, mixed> = []
- $bind_param_types : array<string|int, mixed> = []
ExistsTable()
Checks if a table exists in the database.
public
ExistsTable(string $table) : bool
Parameters
- $table : string
-
name of the table
Return values
bool —true if it exists, false if it does not
ExistsTableColumns()
Checks if a list of columns exists in a table.
public
ExistsTableColumns(string|null $table, array<string|int, mixed> $names) : bool
Parameters
- $table : string|null
-
name of table
- $names : array<string|int, mixed>
-
list of field names to check
Return values
bool —true if all fields exist
GetAffectedRows()
Returns how many rows were affected by the last operation performed.
public
GetAffectedRows() : int
Return values
int —total of rows affected by the last operation
GetAttributesFromType()
Gets field attributes based on a DB data type.
public
GetAttributesFromType(string $type, array<string|int, mixed> &$values) : bool
Parameters
- $type : string
-
the db type to check (e.g., INT, VARCHAR)
- $values : array<string|int, mixed>
-
container for the attributes
Return values
bool —true if attributes were found
GetColumnNames()
Returns an array containing the names of all columns for the last query executed.
public
GetColumnNames() : array<string|int, string>|null
Return values
array<string|int, string>|null —the associative array of column names
GetDataTable()
Fetches the entire result set of a query into an array.
public
GetDataTable(array<string|int, mixed> &$data[, int|null $type = null ][, string|null $with_index = null ]) : int|null
Parameters
- $data : array<string|int, mixed>
-
array containing the records found
- $type : int|null = null
-
the fetch type (e.g., IDBMS::DB_ASSOC)
- $with_index : string|null = null
-
field name to use as the index of the main array
Return values
int|null —the total of records found or null on error
GetDataValue()
Returns a single value from a query result.
public
GetDataValue([int|string $name_col = '0' ][, int|null $pos_row = null ]) : string|null
Parameters
- $name_col : int|string = '0'
-
column name or numeric index
- $pos_row : int|null = null
-
position of the row
Return values
string|null —value of the indicated cell, or null if it does not exist
GetDBResultAssoc()
Returns the driver-specific value for an associative array fetch type.
public
GetDBResultAssoc() : int
Return values
intGetDBResultBoth()
Returns the driver-specific value for a fetch type that includes both associative and numeric arrays.
public
GetDBResultBoth() : int
Return values
intGetDBResultNum()
Returns the driver-specific value for a numeric array fetch type.
public
GetDBResultNum() : int
Return values
intGetDBTable()
Returns the name of the current main table
public
GetDBTable() : string
Return values
string —name of the main reference table
GetFetchArray()
Fetches a result row as an array (both numeric and associative).
public
GetFetchArray([int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
- $pos_row : int|null = null
-
position of the row to read
Return values
array<string|int, mixed>|null —the fetched row
GetFetchAssoc()
Fetches a result row as an associative array.
public
GetFetchAssoc([int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
- $pos_row : int|null = null
-
position of the row to read
Return values
array<string|int, mixed>|null —the fetched row
GetFetchRow()
Fetches a result row as a numeric array.
public
GetFetchRow([int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
- $pos_row : int|null = null
-
position of the row to read
Return values
array<string|int, mixed>|null —the fetched row
GetLastDBErrorNumber()
Gets any error number issued by the DB.
public
static GetLastDBErrorNumber() : int
Return values
int —integer value containing the error issued by the DB
GetLastDBErrorString()
Gets any error string issued by the DB.
public
static GetLastDBErrorString() : string
Return values
string —containing the error issued by the DB
GetLastID()
Returns the ID of the last inserted item
public
GetLastID() : int|null
Return values
int|null —The ID of the last inserted element, or null on failure.
GetNextID()
Returns the next ID from a sequence or similar.
public
GetNextID([string $table = '' ]) : int|null
Parameters
- $table : string = ''
-
the name of counter associated to SEQUENCE
Return values
int|null —the next ID of the table
GetTableColumnNames()
Returns an array containing the names of all columns of a table.
public
GetTableColumnNames(string $table) : array<string|int, string>
Parameters
- $table : string
Return values
array<string|int, string> —an array with all field names
GetTotalColumns()
Returns the number of columns of a previously executed query.
public
GetTotalColumns() : int|bool
Return values
int|bool —total of the columns or false on error
GetTotalRows()
Reads the total number of rows from a previously executed query.
public
GetTotalRows() : int|bool
Return values
int|bool —total number of rows or false on error
InsertRecord()
Make an entry in the DB
public
InsertRecord(string|null $table, array<string|int, mixed> $items, array<string|int, mixed> $types) : bool
Parameters
- $table : string|null
-
name of the table
- $items : array<string|int, mixed>
-
associative array FIELD_NAME => FIELD_VALUE
- $types : array<string|int, mixed>
-
of DB table fields
Return values
bool —true if the insertion has been made
InsertRecordBatch()
Insert multiple records in a single batch operation
public
InsertRecordBatch(string|null $table, array<string|int, mixed> $items, array<string|int, mixed> $batch_data, array<string|int, mixed> $types) : bool
Parameters
- $table : string|null
-
name of the table (if null uses the default table)
- $items : array<string|int, mixed>
-
associative array with field names as keys for the first record
- $batch_data : array<string|int, mixed>
-
array of arrays, each containing values for one record
- $types : array<string|int, mixed>
-
list of types mapped one to one with fields (it depends on used DBMS)
Return values
bool —true if successful, false otherwise
LoadAllTables()
Loads the schema of all tables into an array.
public
LoadAllTables(array<string|int, mixed> &$db_tables) : void
Parameters
- $db_tables : array<string|int, mixed>
-
list of tables to be returned
Prepare()
Prepares an SQL statement for execution.
public
Prepare(string $sql) : void
Parameters
- $sql : string
-
string containing the query to prepare
Query()
Executes a query on the DB.
public
Query(string $sql[, array<string|int, mixed> $bind_param_values = [] ][, array<string|int, mixed> $bind_param_types = [] ]) : bool
Parameters
- $sql : string
-
string containing the query to execute
- $bind_param_values : array<string|int, mixed> = []
-
deferred values for the query
- $bind_param_types : array<string|int, mixed> = []
-
DB data types
Return values
bool —true if the query was successful
Rollback()
Rolls back the current transaction.
public
Rollback() : void
SetAutoCommit()
Sets the autocommit mode.
public
SetAutoCommit([bool $bool = true ]) : void
Parameters
- $bool : bool = true
-
if is true the autocommit is running
SetDBColumns()
Loads the schema of a table's columns into an array.
public
SetDBColumns(array<string|int, mixed> &$db_cols[, string|null $table = null ]) : void
Parameters
- $db_cols : array<string|int, mixed>
-
the results to be returned
- $table : string|null = null
-
the table to be sampled
SetDBTable()
Sets the name of the reference table for the instance.
public
SetDBTable(string $table) : void
Parameters
- $table : string
-
The name of the main table to set.
UpdateRecord()
Make an update in the DB
public
UpdateRecord(string|null $table, string $conditions, array<string|int, mixed> $items, array<string|int, mixed> $conditions_values, array<string|int, mixed> $types) : bool
Parameters
- $table : string|null
-
name of the table
- $conditions : string
-
of the update
- $items : array<string|int, mixed>
-
associative array FIELD_NAME => FIELD_VALUE
- $conditions_values : array<string|int, mixed>
-
values of the conditions
- $types : array<string|int, mixed>
-
of DB table fields
Return values
bool —true if the update has been made
ConvertPlaceholders()
Convert prepared statement placeholders from ? to $1, $2, $3 format for PostgreSQL
protected
ConvertPlaceholders(string $sql) : string
Parameters
- $sql : string
-
SQL query with ? placeholders
Return values
string —SQL query with $1, $2, $3 placeholders
GetConnectionManager()
Gets the connection manager instance.
protected
GetConnectionManager() : IConnectionManager
Tags
Return values
IConnectionManager —The connection manager