MonkeyslegionDB serves as a specialized database connection manager tailored for the Monkeyslegion PHP framework. It offers an extensive toolkit for managing database connections, constructing queries, executing transactions, and maintaining logs.
- Connection Pooling: Optimize your database connections for reuse.
- Query Builder: Create SQL queries in a programmatic way.
- Transactions: Perform a set of operations as a single unit.
- Logging: Keep track of SQL queries and debug effortlessly.
- PHP 8.2 or higher
- Composer for managing dependencies
To get started with MonkeyslegionDB, execute the following Composer command:
composer require monkeyslegion/monkeyslegiondb
Before using the ConnectionPool, you'll need to set the database configuration. You can do this once, perhaps in your framework's bootstrap file.
use Monkeyslegion\\MonkeyslegionDB\\ConnectionPool; ConnectionPool::setConfig([ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'your_database', 'username' => 'your_username', 'password' => 'your_password', 'options' => [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ] ]);
To get a connection:
use Monkeyslegion\\MonkeyslegionDB\\ConnectionPool; $connection = ConnectionPool::getConnection();
After you're done with the connection, you can release it back to the pool:
setConfig(array $config): Configure your database settings.
getConnection(): Fetch a PDO connection from the pool.
releaseConnection(PDO $connection): Return a PDO connection to the pool.
use Monkeyslegion\\MonkeyslegionDB\\QueryBuilder; $query = (new QueryBuilder()) .select('*') .from('users') .where('id', '=', 1) .build();
select(string $columns): Choose the columns to select.
from(string $table): Specify the table to query.
where(string $column, string $operator, mixed $value): Add a WHERE clause.
build(): Generate the SQL query.
use Monkeyslegion\\MonkeyslegionDB\\Transaction; $transaction = new Transaction($connection); $transaction->begin(); // ... perform operations $transaction->commit();
begin(): Start a new transaction.
commit(): Finalize the current transaction.
rollback(): Undo the current transaction.
use Monkeyslegion\\MonkeyslegionDB\\Logger; $dbLogger = new Logger($yourPsr3Logger); $dbLogger->logQuery('SELECT * FROM users WHERE id = ?', );
logQuery(string $query, array $params = ): Log an executed SQL query.
logError(string $message): Log any error messages.
For in-depth documentation, please visit our official documentation.
We're open to contributions! Check out our contributing guidelines for more information.
MonkeyslegionDB is open-source and licensed under the MIT license.