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.
🌟 Features
- 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.
🛠 Requirements
- PHP 8.2 or higher
- Composer for managing dependencies
📦 Installation
To get started with MonkeyslegionDB, execute the following Composer command:
composer require monkeyslegion/monkeyslegiondb
🚀 Usage
Connection Pooling
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:
ConnectionPool::releaseConnection($connection);
Functions
-
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.
Query Building
use Monkeyslegion\\MonkeyslegionDB\\QueryBuilder;
$query = (new QueryBuilder())
.select('*')
.from('users')
.where('id', '=', 1)
.build();
Functions
-
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.
Transactions
use Monkeyslegion\\MonkeyslegionDB\\Transaction;
$transaction = new Transaction($connection);
$transaction->begin();
// ... perform operations
$transaction->commit();
Functions
-
begin()
: Start a new transaction. -
commit()
: Finalize the current transaction. -
rollback()
: Undo the current transaction.
Logging
use Monkeyslegion\\MonkeyslegionDB\\Logger;
$dbLogger = new Logger($yourPsr3Logger);
$dbLogger->logQuery('SELECT * FROM users WHERE id = ?', [1]);
Functions
-
logQuery(string $query, array $params = [])
: Log an executed SQL query. -
logError(string $message)
: Log any error messages.
📚 Documentation
For in-depth documentation, please visit our official documentation.
🤝 Contributing
We're open to contributions! Check out our contributing guidelines for more information.
📜 License
MonkeyslegionDB is open-source and licensed under the MIT license.
Top comments (0)