slicks / fluentdb
fluentdb 允许以表达式的形式编写数据库查询和例程。fluentdb 是流畅的,即使刚开始使用 fluentdb,您也可以几乎猜到下一步应该是什么。fluentdb 不是一个 ORM。它是为了使来自关系数据库背景的人能够编写具有对象交互的表达式查询而开发的。在本版本中已测试了 MySQL;将在完成时添加其他数据库的测试。
Requires
- php: >=5.3
Requires (Dev)
- peridot-php/leo: ~1.0
- peridot-php/peridot: ~1.15
This package is not auto-updated.
Last update: 2024-09-28 20:51:00 UTC
README
fluentdb 允许以表达式的形式编写数据库查询和例程。fluentdb 是流畅的,即使刚开始使用 fluentdb,您也可以几乎猜到下一步应该是什么。fluentdb 不是一个 ORM。它是为了使来自关系数据库背景的人能够编写具有对象交互的表达式查询而开发的。在本版本中已测试了 MySQL;将在完成时添加其他数据库的测试。
## fluentdb 选项 fluentdb 接受所有由 PDO
允许的选项/配置。有关详细信息,请参阅 https://php.ac.cn/manual/en/book.pdo.php。它还包含一个额外的 debug_db
选项,可以是 true/false
。当设置为 true 时,debug_db
启用将原始查询记录到控制台的功能,这在开发过程中非常有用。
安装
composer require slicks/fluentdb
用法
使用 fluentdb 是一件纯粹快乐的事情
use slicks\db\DbConnectionFactory as DbFac; $options = [ 'host' => 'devmac', 'port' => '3306', 'username' => 'tester', 'database' => 'todo_db', 'password' => 'tester' ]; //Init Db factory; DbFac::init($options); //Let us now connect and get a db object $db = DbFac::getDb(); //Do db stuffs here
fluentdb 在行动
现在我们有一个有效的 db
对象,我们如何使用它?好吧,看看下面的
fetch
记录
$db->fetch('todo', function ($e, $rows) { if ($e) { throw new Exception($e); } print_r($rows); });
上述方法用于需要所有记录字段的情况。然而,如果只对部分字段感兴趣,则可以使用 select
与 from
和 fetch
。
select
记录
$db->select('id, task') ->from('todo') ->fetch(function ($e, $rows) { if ($e) { throw new Exception($e); } print_r($rows); });
query
记录使用 query
$q = "insert into todo (task, task_owner) values ('Vacuum the floor',1),('Iron my shirt', 1)"; $this->db->query($q, function ($e, $res) { if ($e) { throw new Exception($e); } print_r($res); });
注意:仅使用 fetch
或与 select
和 from
结合使用不会改变结果。我认为这取决于您喜欢的口味或手头的需要。话虽如此,所有示例都是用一种或另一种风格编写的,但一种风格中完成的工作可以用另一种风格同样完成。
where
$db->where('id', 1) ->fetch('todo', function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
$db->where('id >', 1) ->fetch('todo', function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
$db->where('id <', 10) ->fetch('todo', function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
$db->where('id >=', 1) ->fetch('todo', function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
$db->where('id <=', 10) ->fetch('todo', function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
where
、orWhere
、whereIn
、orWhereIn
、whereNotIn
、orWhereNotIn
条件
请注意,适用于 where
的所有变体也适用于以下内容:orWhere
、whereIn
、orWhereIn
、whereNotIn
、orWhereNotIn
。
orWhere
$db->where('id', 10) ->orWhere('task_owner', 1) ->fetch('todo', function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
whereIn
$db->select('todo.*') //I could have used fetch directly here too ->from('todo') ->whereIn('id', [1,3]) ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
orWhereIn
$db->select('todo.*') //I could have used fetch directly here too ->from('todo') ->where('id', 2) ->orWhereIn('id', [1,3]) ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
whereNotIn
$db->select('todo.*') //I could have used fetch directly here too ->from('todo') ->whereNotIn('id', "1,2,3") ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
orWhereNotIn
$db->select('todo.*') //I could have used fetch directly here too ->from('todo') ->where('id', 2) ->orWhereNotIn('id', "1,3") ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
like
生成 task like %vacuum%
,允许 b
或 both
两端。
$db->select('todo.*') //I could have used fetch directly here too ->from('todo') ->like('task', 'vacuum', 'b') ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
orLike
生成 task like '%vacuum' or task like 'iron%'
,允许 l
或 left
左端,同时允许 r
或 right
右端。
$d->select('todo.*') //I could have used fetch directly here too ->from('todo') ->like('task', 'vacuum', 'l') ->orLike('task', 'iron', 'r') ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
notLike
生成 task NOT like '%vacuum%'
,允许 b
或 both
两端。
$db->select('todo.*') //I could have used fetch directly here too ->from('todo') ->notLike('task', 'vacuum', 'b') ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
orNotLike
生成 OR task NOT like '%dishes'
,允许 l
或 left
左端。
$db->select('todo.*') //I could have used fetch directly here too ->from('todo') ->where('id', 2) ->orNotLike('task', 'dishes', 'l') ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
limit
$db->limit(2) //I could have used select, from + fetch here too ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
带有 offset
的 limit
$db->limit(2, 0) //I could have used select, from + fetch here too ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
orderBy (desc)
$db>orderBy('id', 'desc') //I could have used select, from + fetch here too ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
orderBy ([asc])
如果希望升序排列,方向是可选的
$db->orderBy('id', 'asc') //I could have used select, from + fetch here too ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
与下面相同
$db->orderBy('id') //I could have used select, from + fetch here too ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
join
表
$db->select('t.*, o.name') ->from('todo t') //'left', for left join, also 'right', 'outer' etc are allowed ->join('task_owners o', 't.task_owner = o.id', 'left') ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
groupBy
用于聚合
$db->select('o.name, count(*) tasks') ->from('task_owners o') ->join('todo t', 't.task_owner = o.id', 'left') ->groupBy('o.name') ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
having
用于聚合
$db->select('o.name, count(*) tasks') ->from('task_owners o') ->join('todo t', 't.task_owner = o.id', 'left') ->groupBy('o.name') ->having('tasks >', 2) ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
orHaving
用于聚合
$db->select('o.name, count(*) tasks') ->from('task_owners o') ->join('todo t', 't.task_owner = o.id', 'left') ->groupBy('o.name') ->having('tasks >', 2) ->orHaving('tasks', 3) ->fetch(function ($err, $rows) { if ($err) { throw new Exception($err); } print_r($rows); });
insert
插入记录
insert
- 每次插入单个记录
$db->insert('task_owners', ['name' => 'Test owner'], function ($e, $res) { if ($e) { throw new Exception($e); } echo($res->id); });
使用 query
插入多条记录
$q = "insert into todo (task, task_owner) values ('Vacuum the floor',1),('Iron my shirt', 1)"; $db->query($q, function ($e, $res) { if ($e) { throw new Exception($e); } echo($res->affectedRows); });
update
更新记录
$db->set('task', 'Updated Todo') ->set('earnings', 0.99) ->whereIn('id', [1,3]) ->update('todo', function ($e, $res) { if ($e) { throw new Exception($e); } echo($res->affectedRows); });
delete
删除记录
$db->where('id', 2) ->delete('todo', function ($e, $res) { if ($e) { throw new Exception($e); } echo($res->affectedRows); });
测试
在运行测试之前,将包含的脚本 test_scripts.sql 加载到您的 MySQL 数据库中。确保以 'root' 用户加载脚本,因为您需要授权。更新 tests/specs.php 中的数据库参数。然后运行;
vendor/bin/peridot tests/specs