archangeldesign / archangeldb2
PHP数据库引擎封装器
Requires
- php: >=5.5
- zendframework/zend-db: 2.9.3
This package is auto-updated.
Last update: 2024-09-07 02:13:31 UTC
README
版权所有 (c) https://archangeldesign.github.io
** 简单但强大的PHP数据库引擎 **
Archangel DB的另一个版本引入了比之前版本更多的功能。这次不仅支持事务和预编译语句,而且与Postgres、Oracle、Sqlite、SQLServer和IBM DB2兼容。内置缓存系统和错误日志,易于错误处理和部署。
为Zend Framework 2(使用Zend\DB模块)设计,但可以应用于任何PHP 5.5项目。
优点
- 轻量级
- 非常简单易用,2行代码即可初始化
- 灵活的配置
- 可添加常用表前缀
- 轻松访问分析器(执行时间、完整查询、查询次数...)
- 无外部依赖
- 运行存储在文件系统中的查询(对报告很有用)
- 缓存系统
- 可运行存储在文件中的查询
- XML部署文件
- 数据库结构历史
- 自动数据库结构更新器
- 利用Zend\Db:稳定高效的模块
- 完全兼容ArchangelDB 1.2
目前仅在MySQL和PostgreSQL上进行了测试。
通过composer安装
安装composer(Linux)
curl -sS https://getcomposer.org/installer | php
在项目根目录中创建"composer.json"文件。在"require"部分中按如下提及ADB2
{
"name": "Your project anme",
"description": "your description",
"homepage": "http://arcangel-design.com or whatever",
"require": {
"php": "^5.6 || ^7.0",
"archangeldesign/archangeldb2" : "^1.3",
}
}
运行composer update/install
php composer.phar update
从vendor目录包含"autoload.php"即可完成。请注意,如果您使用composer,则不需要使用"autoload_register.php"文件,并且在以后的版本中将删除它。建议您始终使用composer来管理依赖项。
如何轻松开始使用Archangel DB 2的示例
============================================================================
<?php
// initialized with config file adbConfig.php in module or project root
require 'autoload_register.php';
$adb = new \ArchangelDB\ADB2();
$users = $adb->fetchAll('users', ['deleted' => 0, 'active' => 1]);
// $users now holds associative array of users
$adb->deleteRecords('users', ['deleted' => 1]);
// all users with column deleted set to 1 will be deleted from table
$adb->insert('user', ['name' => 'Archangel', 'surname' => 'Design', 'deleted' => 0]);
// user has been inserted to table users
$adb->updateRecords('user',
[
'name' ='Archangel',
'surname' = 'System',
'deleted' => 1
], 'name');
// record with name = 'Archangel' has been updated (or multiple records)
$idList = $adb->fetchList('users', 'id', ['deleted' => 1]);
// returns comma separated list of id as string ( like "1,2,3,4,5")
// for IN statements
?>
============================================================================
<?php
// initialized with array as argument
$config = [
'driver' => 'mysqli',
'database' => 'lop',
'username' => 'admin',
'password' => 'admin',
'prefix' => 'lop_', // not required, prefix added to all tables (automatically)
'profiler' => true, // recommended, if disabled some methods will not return results (like getLastQuery())
'allow_drop' => true, // if false or not set, no drop command is allowed
'deploy-file' => 'database-structure.xml',
'storage-dir' => __DIR__ . '/storage',
'enable-cache' => true,
'enable-storage' => true, // enables use of queries stored in files
'allow-deploy' => true, // whether to allow ADB to create the database, defaults to true
'cache-dir' => __DIR__ . '/cache',
'error-log-file' => __DIR__ . '/error.log',
'options' => [
'buffer_results' => true, // required for mysql and pgsql
// if enabled, all results will be buffered. This option is required for ADB to work
// properly, you can read about implications of this option on database provider site.
// if not set, it defaults to true
]
];
require 'autoload_register.php';
$adb = new \ArchangelDB\ADB2($config);
$users = $adb->fetchAll('users', ['deleted' => 0, 'active' => 1]);
// $users now holds associative array of users
?>
==============================================================================
// Executing custom SQL
$res = $adb->executeRawQuery("select * from {users} where deleted = ? and active = ?", [0, 1]);
// table prefix will be added automatically