josantonius / database
此包已被废弃且不再维护。没有建议的替代包。
库用于同时由多个提供者管理SQL数据库。
1.2.0
2018-04-19 00:01 UTC
Requires
- php: ^5.6 || ^7.0
- eliasis-framework/eliasis: ^1.1.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.3 || ^2.8
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^5.7 || ^6.0
- squizlabs/php_codesniffer: ^3.0
README
同时由多个提供者使用的SQL数据库管理库。
要求
此库支持PHP版本5.6或更高版本,且与HHVM版本3.0或更高版本兼容。
安装
安装此扩展的首选方式是通过Composer。
要安装PHP数据库库,只需
composer require Josantonius/Database
此命令只会安装必要的文件,如果您想下载整个源代码,可以使用
composer require Josantonius/Database --prefer-source
您还可以使用Git克隆整个存储库
git clone https://github.com/Josantonius/PHP-Database.git
或手动安装
下载Database.php,Provider.php,PDOprovider.php,MSSQLprovider.php和DBException.php
wget https://raw.githubusercontent.com/Josantonius/PHP-Database/master/src/Database.php
wget https://raw.githubusercontent.com/Josantonius/PHP-Database/master/src/Provider/Provider.php
wget https://raw.githubusercontent.com/Josantonius/PHP-Database/master/src/Provider/PDOprovider.php
wget https://raw.githubusercontent.com/Josantonius/PHP-Database/master/src/Provider/MSSQLprovider.php
wget https://raw.githubusercontent.com/Josantonius/PHP-Database/master/src/Exception/DBException.php
获取连接
- 获取连接
Database::getConnection($id, $provider, $host, $user, $name, $password, $settings);
属性 | 描述 | 类型 | 必需 | 默认值 |
---|---|---|---|---|
$id | 数据库唯一ID。 | 字符串 | 是 | |
$provider | 提供者类名称。 | 字符串 | 否 | null |
$host | 数据库主机。 | 字符串 | 否 | null |
$user | 数据库用户。 | 字符串 | 否 | null |
$name | 数据库名称。 | 字符串 | 否 | null |
$password | 数据库密码。 | 字符串 | 否 | null |
属性 | 键 | 描述 | 类型 | 必需 | 默认值 |
---|---|---|---|---|---|
$settings | 数据库选项。一旦建立连接,此配置将在数据库连接对象中可用:$db->settings。 | 数组 | 否 | null | |
$settings | 'port' | 数据库端口。 | 字符串 | 否 | |
$settings | 'charset' | 数据库字符集。 | 字符串 | 否 |
返回 (对象) → 包含连接的对象
$db = Database::getConnection( 'identifier', # Unique identifier 'PDOprovider', # Database provider name 'localhost', # Database server 'db-user', # Database user 'db-name', # Database name 'password', # Database password array('charset' => 'utf8') ); $externalDB = Database::getConnection( 'external', # Unique identifier 'PDOprovider', # Database provider name 'http://site.com', # Database server 'db-user', # Database user 'db-name', # Database name 'password', # Database password array('charset' => 'utf8') ); // And once the connection is established: $db = Database::getConnection('identifier'); $externalDB = Database::getConnection('external');
查询
- 处理查询并为提供者准备
$db->query($query, $statements, $result);
属性 | 描述 | 类型 | 必需 | 默认值 |
---|---|---|---|---|
$query | 查询。 | 字符串 | 是 | |
$statements | 语句。 | 数组 | 否 | null |
$result | 查询结果;'obj', 'array_num', 'array_assoc', 'rows', 'id'。 | 字符串 | 否 | 'obj' |
返回 (混合) → 以对象,数组,整数等返回结果
抛出 [DBException] → 无效的查询类型
$db->query( 'CREATE TABLE test ( id INT(6) PRIMARY KEY, name VARCHAR(30) NOT NULL, email VARCHAR(50) )' ); $db->query( 'SELECT id, name, email FROM test', false, 'array_assoc' // array_assoc, obj, array_num ); $statements[] = [1, "Many"]; $statements[] = [2, "many@email.com"]; $db->query( 'INSERT INTO test (name, email) VALUES (?, ?)', $statements, 'id' // id, rows );
CREATE TABLE
- CREATE TABLE 语句
$db->create($data) ->table($table) ->foreing($id) ->reference($table) ->on($table) ->actions($action) ->engine($type) ->charset($type) ->execute();
方法 | 属性 | 描述 | 类型 | 必需 | 默认值 |
---|---|---|---|---|---|
$data | 数据类型列名和配置。 | 数组 | 是 | ||
table() | 设置数据表名。 | method | 是 | ||
$table | 表名。 | 字符串 | 是 | ||
foreing() | 设置外键。 | method | 否 | ||
$id | 列id。 | 字符串 | 是 | ||
reference() | 设置外键的引用。 | method | 否 | ||
$table | 表名。 | 数组 | 是 | ||
on() | 设置数据表名。 | method | 否 | ||
$table | 表名。 | 数组 | 是 | ||
actions() | 设置外键删除或更新的操作。 | method | 否 | ||
$action | 删除或更新时的操作。 | 数组 | 是 | ||
engine() | 设置表引擎。 | method | 否 | ||
$type | 引擎类型。 | 字符串 | 是 | ||
charset() | 设置表字符集。 | method | 否 | ||
$type | 字符集类型。 | 字符串 | 是 | ||
execute() | 执行查询。 | method | 是 |
返回 (布尔值)
$params = [ 'id' => 'INT(6) PRIMARY KEY', 'name' => 'VARCHAR(30) NOT NULL', 'email' => 'VARCHAR(50)' ]; $query = $db->create($params) ->table('test') ->execute(); $db->create($params) ->table('test_two') ->foreing('id') ->reference('id') ->on('test') ->actions('ON DELETE CASCADE ON UPDATE CASCADE') ->engine('innodb') ->charset('utf8') ->execute();
SELECT
- SELECT 语句
$db->select($columns) ->from($table) ->where($clauses, $statements) ->order($type) ->limit($number) ->execute($result);
方法 | 属性 | 描述 | 类型 | 必需 | 默认值 |
---|---|---|---|---|---|
$columns | 列名。 | mixed | 否 | '*' | |
from() | 设置数据表名。 | method | 是 | ||
$table | 表名。 | 字符串 | 是 | ||
where() | WHERE 子句。 | method | 否 | ||
$clauses | 列名和值。 | mixed | 是 | ||
$statements | 语句。 | 数组 | 否 | null | |
order() | 排序。 | method | 否 | ||
$type | 查询排序参数。 | 字符串 | 是 | ||
limit() | 限制。 | method | 否 | ||
$number | 数字。 | int | 是 | ||
execute() | 执行查询。 | method | 是 | ||
$result | 查询结果;'obj', 'array_num', 'array_assoc', 'rows'。 | 字符串 | 否 | 'obj' |
返回 (mixed) → 查询结果(对象,数组,int...)或受影响的行数
#SELECT all $db->select() ->from('test') ->execute('array_num'); #SELECT with all params $db->select(['id', 'name']) ->from('test') ->where(['id = 4885', 'name = "Joe"']) ->order(['id DESC', 'name ASC']) ->limit(1) ->execute('obj'); #SELECT with statements $statements[] = [1, 3008]; $statements[] = [2, 'Manny']; $db->select('name') ->from('test') ->where('id = ? OR name = ?', $statements) ->execute('rows'); #Other version of SELECT with statements $statements[] = [':id', 8, 'int']; $statements[] = [':email', null, 'null']; $clauses = [ 'id = :id', 'email = :email' ]; $db->select('name') ->from('test') ->where($clauses, $statements) ->execute('rows');
INSERT INTO
- INSERT INTO 语句
$db->insert($data, $statements) ->in($table) ->execute($result);
方法 | 属性 | 描述 | 类型 | 必需 | 默认值 |
---|---|---|---|---|---|
$data | 列名和值。 | 数组 | 是 | ||
$statements | 语句。 | 数组 | 否 | null | |
in() | 设置数据表名。 | method | 是 | ||
$table | 表名。 | 字符串 | 是 | ||
execute() | 执行查询。 | method | 是 | ||
$result | 查询结果;'rows', 'id'。 | 字符串 | 否 | 'rows' |
返回 (int) → 受影响的行数或最后受影响的ID
#INSERT INTO basic example $data = [ "name" => "Isis", "email" => "isis@email.com", ]; $db->insert($data) ->in('test') ->execute(); #INSERT INTO with statements $data = [ "name" => "?", "email" => "?", ]; $statements[] = [1, "Isis"]; $statements[] = [2, "isis@email.com"]; $db->insert($data, $statements) ->in('test') ->execute('rows'); #Other version of INSERT INTO with statements $data = [ "name" => ":name", "email" => ":email", ]; $statements[] = [":name", "Isis", "str"]; $statements[] = [":email", "isis@email.com", "str"]; $db->insert($data, $statements) ->in('test') ->execute('id');
UPDATE
- UPDATE 语句
$db->update($data, $statements) ->in($table) ->where($clauses, $statements) ->execute();
方法 | 属性 | 描述 | 类型 | 必需 | 默认值 |
---|---|---|---|---|---|
$data | 列名和值。 | 数组 | 是 | ||
$statements | 语句。 | 数组 | 否 | null | |
in() | 设置数据表名。 | method | 是 | ||
$table | 表名。 | 字符串 | 是 | ||
where() | WHERE 子句。 | method | 否 | ||
$clauses | 列名和值。 | mixed | 是 | ||
$statements | 语句。 | 数组 | 否 | null | |
execute() | 执行查询。 | method | 是 |
返回 (int) → 受影响的行数
#UPDATE basic example $data = [ 'name' => 'Isis', 'email' => 'isis@email.com', ]; $db->update($data) ->in('test') ->execute(); #UPDATE with WHERE $data = [ 'name' => 'Manny', 'email' => 'manny@email.com', ]; $clauses = [ 'name = "isis"', 'email = "isis@email.com"' ]; $db->update($data) ->in('test') ->where($clauses) ->execute(); #UPDATE with statements $data = [ 'name' => '?', 'email' => '?', ]; $statements['data'][] = [1, 'Isis']; $statements['data'][] = [2, 'isis@email.com']; $clauses = 'id = ? AND name = ? OR name = ?'; $statements['clauses'][] = [3, 4883]; $statements['clauses'][] = [4, 'Isis']; $statements['clauses'][] = [5, 'Manny']; $db->update($data, $statements['data']) ->in('test') ->where($clauses, $statements['clauses']) ->execute(); #Other version of UPDATE with statements $data = [ 'name' => ':new_name', 'email' => ':new_email', ]; $statements['data'][] = [':new_name', 'Manny', 'str']; $statements['data'][] = [':new_email', 'manny@email.com', 'str']; $clauses = 'name = :name1 OR name = :name2'; $statements['clauses'][] = [':name1', 'Isis', 'str']; $statements['clauses'][] = [':name2', 'Manny', 'str']; $db->update($data, $statements['data']) ->in('test') ->where($clauses, $statements['clauses']) ->execute();
REPLACE
- 如果表中存在行则替换该行,否则插入新行
$db->replace($data, $statements) ->from($table) ->execute($result);
方法 | 属性 | 描述 | 类型 | 必需 | 默认值 |
---|---|---|---|---|---|
$data | 列名和值。 | 数组 | 是 | ||
$statements | 语句。 | 数组 | 否 | null | |
from() | 设置数据表名。 | method | 是 | ||
$table | 表名。 | 字符串 | 是 | ||
execute() | 执行查询。 | method | 是 | ||
$result | 查询结果;'rows', 'id'。 | 字符串 | 否 | 'rows' |
返回 (int) → 受影响的行数或最后受影响的ID
#REPLACE basic example $data = [ 'id' => 3008, 'name' => 'Manny', 'email' => 'manny@email.com', ]; $db->replace($data) ->from('test') ->execute(); #UPDATE with statements $data = [ 'id' => 4889, 'name' => ':name', 'email' => ':email', ]; $statements[] = [':name', 'Manny']; $statements[] = [':email', 'manny@email.com']; $db->replace($data, $statements) ->from('test') ->execute('rows'); #Other version of UPDATE with statements $data = [ 'id' => 2, 'name' => '?', 'email' => '?', ]; $statements[] = [1, 'Manny']; $statements[] = [2, 'manny@email.com']; $db->replace($data, $statements) ->from('test') ->execute('id');
DELETE
- DELETE 语句
$db->delete($data, $statements) ->from($table) ->where($clauses, $statements) ->execute();
方法 | 属性 | 描述 | 类型 | 必需 | 默认值 |
---|---|---|---|---|---|
$data | 列名和值。 | 数组 | 是 | ||
$statements | 语句。 | 数组 | 否 | null | |
from() | 设置数据表名。 | method | 是 | ||
$table | 表名。 | 字符串 | 是 | ||
where() | WHERE 子句。 | method | 否 | ||
$clauses | 列名和值。 | mixed | 是 | ||
$statements | 语句。 | 数组 | 否 | null | |
execute() | 执行查询。 | method | 是 |
返回 (int) → 受影响的行数
#DELETE all $db->delete() ->from('test') ->execute(); #DELETE with WHERE $clauses = [ 'id = 4884', 'name = "isis"', 'email = "isis@email.com"', ]; $db->delete() ->from('test') ->where($clauses) ->execute(); #DELETE with statements $clauses = 'id = :id AND name = :name1 OR name = :name2'; $statements[] = [':id', 4885]; $statements[] = [':name1', 'Isis']; $statements[] = [':name2', 'Manny']; $db->delete() ->from('test') ->where($clauses, $statements) ->execute(); #Other version of DELETE with statements $clauses = 'id = :id AND name = :name1 OR name = :name2'; $statements[] = [':id', 4886, 'int']; $statements[] = [':name1', 'Isis', 'src']; $statements[] = [':name2', 'Manny', 'src']; $db->delete() ->from('test_table') ->where($clauses, $statements) ->execute();
TRUNCATE TABLE
- TRUNCATE TABLE 语句
$db->truncate() ->table($table) ->execute();
方法 | 属性 | 描述 | 类型 | 必需 | 默认值 |
---|---|---|---|---|---|
table() | 设置数据表名。 | method | 是 | ||
$table | 表名。 | 字符串 | 是 | ||
execute() | 执行查询。 | method | 是 |
返回 (布尔值)
$db->truncate() ->table('test') ->execute();
DROP TABLE
- DROP TABLE 语句
$db->drop() ->table($table) ->execute();
方法 | 属性 | 描述 | 类型 | 必需 | 默认值 |
---|---|---|---|---|---|
table() | 设置数据表名。 | method | 是 | ||
$table | 表名。 | 字符串 | 是 | ||
execute() | 执行查询。 | method | 是 |
返回 (布尔值)
$db->drop() ->table('test') ->execute();
快速入门
使用 Composer 来使用此类
require __DIR__ . '/vendor/autoload.php'; use Josantonius\Database\Database;
或者如果你是手动安装的,可以使用它
require_once __DIR__ . '/Database.php'; use Josantonius\Database\Database;
预处理语句支持的数据类型
布尔值
- bool
- boolean
空值
- null
整数数字
- int
- 整数
文本字符串
- str
- 字符串
如果任何数据类型与上述类型不匹配,它将被验证为字符串。
如果没有指定数据类型,则准备好的查询中不会验证数据类型。
测试
git clone https://github.com/Josantonius/PHP-Database.git
cd PHP-Database
composer install
使用PHPUnit运行单元测试
composer phpunit
composer phpcs
运行PHP Mess Detector测试以检测代码样式的不一致性
composer phpmd
运行所有之前的测试
composer tests
赞助商
如果此项目帮助您减少了开发时间,您可以赞助我以支持我的开源工作 😊
许可
此存储库根据MIT 许可证授权。
版权 © 2017-2022, Josantonius