PostgresqlDriver
The PostgresqlDriver.php
file in the PHPFast framework defines a class for seamless interaction with a PostgreSQL database using PDO (PHP Data Objects). This class offers essential methods for establishing connections, executing queries, and handling data efficiently. Below is a comprehensive breakdown of each method, along with practical usage examples.
Configuaration
'db' => [
'db_driver' => 'pgsql',
'db_host' => 'localhost',
'db_port' => 5432,
'db_username' => 'root',
'db_password' => '',
'db_database' => 'cms.vn',
'db_charset' => 'utf8mb4',
'db_collate' => 'utf8mb4_unicode_ci',
],
__construct()
__construct()
<?php
public function __construct($config) {
try {
$dsn = 'pgsql:host=' . $config['db_host'] . ';dbname=' . $config['db_database'];
$this->pdo = new PDO($dsn, $config['db_username'], $config['db_password']);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
throw new \System\Core\AppException("Connect PostgresqlDriver failed: " . $e->getMessage(), 500);
}
}
Initializes the PostgreSQL connection using the provided configuration settings.
$config
: An array containing the database configuration settings.
query()
query()
<?php
public function query($query, $params = []) {}
Executes an arbitrary SQL query with optional parameters.
$query
: The SQL query string.$params
: An array of values corresponding to the parameters in the SQL query.return
: Result of the query (used for SELECT, INSERT, UPDATE, DELETE).
lastInsertId()
lastInsertId()
<?php
public function lastInsertId() {}
Retrieves the ID of the last inserted record.
return
: ID of the last inserted record.
count()
count()
<?php
public function count($table, $where = '', $params = []) {}
Counts the number of records in a table that match the specified filter criteria
$table
: The name of the table.$where
: The filter criteria (optional).$params
: An array of values corresponding to the parameters in the filter criteria (optional).return
: Number of records in the table.
fetchAll()
fetchAll()
<?php
public function fetchAll($table, $where = '', $params = [], $orderBy = '', $page = 1, $limit = null) {}
Executes a SELECT query on a table and retrieves multiple rows that match the specified filter criteria.
$table
: The name of the table.$where
: The filter criteria (optional).$params
: An array of values corresponding to the parameters in the filter criteria (optional).$orderBy
: The ORDER BY clause (optional).$page
: The current page number (optional).$limit
: The number of results per page (optional).return
: Array containing the query results.
fetchPagination()
fetchPagination()
<?php
public function fetchPagination($table, $where = '', $params = [], $orderBy = '', $page = 1, $limit = null) {}
Executes a SELECT query on a table and retrieves multiple rows with pagination support.
$table
: The name of the table.$where
: The filter criteria (optional).$params
: An array of values corresponding to the parameters in the filter criteria (optional).$orderBy
: The ORDER BY clause (optional).$page
: The current page number (optional).$limit
: The number of results per page (optional).return
: Query results and information about whether there is a next page.
fetchRow()
fetchRow()
<?php
public function fetchRow($table, $where = '', $params = [], $orderBy = '', $page = 1) {}
Executes a SELECT query on a table and retrieves a single row that matches the specified filter criteria.
$table
: The name of the table.$where
: The filter criteria (optional).$params
: An array of values corresponding to the parameters in the filter criteria (optional).$orderBy
: The ORDER BY clause (optional).$page
: The current page number (optional).return
: Array containing the query result or null if no result is found.
insert()
insert()
<?php
public function insert($table, $data) {}
Inserts a new row into a table with the specified data.
$table
: The name of the table.$data
: An array of data to insert (in the form of 'column' => 'value').return
: Returns true if the data is successfully inserted, otherwise false
update()
update()
<?php
public function update($table, $data, $where = '', $params = []) {}
Updates rows in a table that match the specified filter criteria with the provided data.
$table
: The name of the table.$data
: An array of data to update (in the form of 'column' => 'value').$where
: The filter criteria (optional).$params
: An array of values corresponding to the parameters in the filter criteria (optional).return
: Returns true if the update is successful, otherwise false.
delete()
delete()
<?php
public function delete($table, $where = '', $params = []) {}
Deletes rows from a table that match the specified filter criteria.
$table
: The name of the table.$where
: The filter criteria (optional).$params
: An array of values corresponding to the parameters in the filter criteria (optional).return
: Returns true if the deletion is successful, otherwise false
Summary
he PostgresqlDriver.php
file offers a robust collection of methods for seamless interaction with a PostgreSQL database using PDO. It facilitates essential operations such as querying, inserting, updating, and deleting records, along with transaction management and pagination support. Designed for efficiency, this implementation enables developers to handle database operations smoothly within their PHPFast applications.
Last updated
Was this helpful?