PostgreSQLDBC is an abstract class designed to simplify handling PostgreSQL database connections. It provides a robust and secure way to use prepared statements, helping developers avoid SQL injection vulnerabilities. With this library, you can easily configure and execute prepared statements by associating them with meaningful names.
Features
- Secure Database Connections: Protects against SQL injection by enforcing the use of prepared statements.
- Easy Configuration: Allows you to configure prepared statements with meaningful names for reuse.
- Singleton Pattern: Ensures a single instance of the database connection throughout the application.
- Transaction Support: Includes methods for starting, committing, and rolling back transactions.
- Performance Monitoring: Tracks execution time and statistics for SQL queries.
Requirements
- PHP with PostgreSQL support (
pg_connect
and related functions).
- A PostgreSQL database.
Installation
- Clone or download this repository.
- Include the
PostgreSQLDatabase.php
file in your project.
- Extend the
PostgreSQLDatabase
class to create your own database connection class.
Documentation
See class PostgreSQLDatabase
documentation
Usage Example
Below is an example of how to use the library. This example assumes a PostgreSQL database with a users
table containing the following fields:
id SERIAL PRIMARY KEY
username TEXT NOT NULL
email TEXT NOT NULL
<?php
require_once 'PostgreSQLDatabase.php';
private static $instance = NULL;
public static function GetInstance() {
if (is_null(self::$instance)) {
self::$instance = new MyDBConnection();
}
return self::$instance;
}
parent::__construct();
global $host, $database, $user, $password;
$this->
Configure($host, $user, $password, $database);
parent::Connect();
$this->ConfigAllSTMTs();
}
private function ConfigAllSTMTs() {
$this->
ConfigureSTMT(
"getUserCount",
"SELECT COUNT(*) AS usercount FROM users");
$this->
ConfigureSTMT(
"createUser",
"INSERT INTO users(username,email) VALUES ($1,$2) RETURNING id");
}
public function GetUserCount() {
return $this->
ExecuteSTMT(
"getUserCount")[0][
'usercount'];
}
public function CreateUser($username, $email) {
return $this->
ExecuteSTMT(
"createUser", $username, $email)[0][
'id'];
}
}
$dbconn = MyDBConnection::GetInstance();
echo "Total number of users we have registered is: " . $dbconn->GetUserCount();
?>
Definition PostgreSQLDatabase.php:19
ConfigureSTMT($name, $query)
Definition PostgreSQLDatabase.php:251
Configure($server, $user, $pass, $db)
Definition PostgreSQLDatabase.php:182
__construct()
Definition PostgreSQLDatabase.php:134
ExecuteSTMT($name,... $params)
Definition PostgreSQLDatabase.php:371
License
This project is licensed under the [GNU General Public License v3.0](LICENSE).
Contributing
Contributions are welcome! Feel free to submit issues or pull requests to improve this library.
Author
Developed by David Carlos Manuelda. For more information, visit the GitHub repository.