josantonius/database

此包已被弃用,不再维护。未建议替代包。

适用于同时使用多个提供商的SQL数据库管理库。

1.2.0 2018-04-19 00:01 UTC

This package is auto-updated.

Last update: 2022-08-18 17:30:02 UTC


README

Latest Stable Version License

西班牙语版本

同时适用于多个提供商的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.phpProvider.phpPDOprovider.phpMSSQLprovider.phpDBException.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

- 创建表语句

$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
  • integer

文本字符串

  • str
  • 字符串

如果任何类型的数据与上述不符,它将被验证为字符串。

如果没有指定数据类型,则准备好的查询中不会验证数据类型。

测试

要运行测试,您只需要 composer 并执行以下操作

git clone https://github.com/Josantonius/PHP-Database.git

cd PHP-Database

composer install

使用 PHPUnit 运行单元测试

composer phpunit

使用 PSR2 代码标准测试与 PHPCS

composer phpcs

运行 PHP Mess Detector 测试以检测代码风格的不一致性

composer phpmd

运行所有之前的测试

composer tests

赞助

如果这个项目帮助您减少了开发时间,您可以 赞助我 以支持我的开源工作 😊

许可

此存储库受 MIT 许可证 许可。

版权所有 © 2017-2022,Josantonius