kubricklabs / database
KubrickLabs 工具集合的数据库包
dev-main
2024-07-26 14:11 UTC
Requires
- php: ^7.4 || ^8.0
- ext-pdo: *
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-26 14:44:29 UTC
README
Database 是一个简单高效的数据库抽象层,建立在 PDO 之上。它提供了易于使用的通用数据库操作方法,如 CRUD 操作、事务管理和连接池。
安装
您可以通过 Composer 安装此包。运行以下命令:
composer require kubricklabs/database
使用方法
基本使用
以下是如何使用 Database 包的示例
require 'vendor/autoload.php'; use Database\Database; // Database connection details $dsn = 'your_dsn'; $username = 'your_username'; $password = 'your_password'; $options = []; // Get a Database instance $db = Database::getInstance($dsn, $username, $password, $options); // Example CRUD operations $result = $db->select('SELECT * FROM your_table'); $db->insert('your_table', ['column1' => 'value1', 'column2' => 'value2']); $db->update('your_table', ['column1' => 'new_value'], ['column2' => 'value2']); $db->delete('your_table', ['column1' => 'new_value']); // Transaction management $db->beginTransaction(); // Perform multiple CRUD operations $db->commit();
连接池
该库使用连接池来高效地管理数据库连接。您无需手动管理池;库会为您处理。
事务管理
通过 beginTransaction
、commit
和 rollBack
方法,提供了一种管理数据库事务的简单方法。
$db->beginTransaction(); try { // Perform multiple operations $db->insert('your_table', ['column1' => 'value1']); $db->update('your_table', ['column1' => 'new_value'], ['column2' => 'value2']); $db->commit(); } catch (Exception $e) { $db->rollBack(); echo "Failed: " . $e->getMessage(); }
类和方法
Database
主要类,扩展 PDO 并提供 CRUD 操作和事务管理的方法。
方法
select($query, $params = [], $fetchMode = PDO::FETCH_ASSOC)
insert($table, $data)
update($table, $data, $where)
delete($table, $where)
beginTransaction()
commit()
rollBack()
getInstance($dsn, $username = null, $password = null, $options = null)
CRUD 操作
执行 CRUD 操作的辅助类。
方法
select($query, $params = [], $fetchMode = PDO::FETCH_ASSOC)
insert($table, $data)
update($table, $data, $where)
delete($table, $where)
事务管理器
管理事务的辅助类。
方法
beginTransaction()
commit()
rollBack()
连接池
管理数据库连接池的辅助类。
方法
getConnection($dsn, $username, $password, $options)
releaseConnection($connection)
错误处理器
处理错误的辅助类。
方法
handleError(PDOException $e)
贡献
请随时提交问题和拉取请求。
许可
MIT 许可证。有关更多信息,请参阅LICENSE。