atk4 / dsql
该软件包已被弃用且不再维护。没有建议的替代软件包。
PHP 动态 SQL 构建器
2.4.0
2021-04-16 10:36 UTC
Requires
- php: >=7.3.0
- ext-pdo: *
- atk4/core: ~2.4.0
- doctrine/dbal: ^2.10 || ^3.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.13
- friendsofphp/php-cs-fixer: ^2.17
- johnkary/phpunit-speedtrap: ^3.2
- phpstan/phpstan: ^0.12.58
- phpunit/phpcov: *
- phpunit/phpunit: >=9.1
Suggests
- dev-develop
- 2.4.0
- 2.3.5
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0-beta
- 1.0.0-alpha2
- 1.0.0-alpha
- 0.1.1
- dev-archived_message
- dev-test_for_data_774
- dev-drop_php73
- dev-fix_oracle_limit_type
- dev-phpstan_l6
- dev-feature/expression-implements-expressionable
- dev-release/2.4
- dev-release/2.3
- dev-release/2.2
- dev-action_to_toquery
- dev-release/2.1
- dev-legacy/2.0
- dev-refactor/organize-folder-structure-driver
- dev-legacy/v1
This package is auto-updated.
Last update: 2021-05-11 13:29:07 UTC
README
DSQL 是一个可组合的 SQL 查询构建器。您可以在 PHP 中编写多供应商查询,从而获得更好的安全性、干净的语法并避免人为错误。
等等!为什么还需要另一个查询构建器?
显然是因为现有的不够好。DSQL 试图以不同的方式做事
- 可组合性。与其他库不同,我们递归地渲染查询,允许多层子查询。
- 小型足迹。我们不为所有供应商重复查询代码,而是使用巧妙的模板系统。
- 可扩展性。我们有 3 种不同的方式来扩展 DSQL,以及第三方供应商驱动程序支持。
- 任何查询 - 任何复杂度的查询都可以通过 DSQL 表达。
- 几乎没有依赖关系。在任何 PHP 应用程序或框架中使用 DSQL。
- NoSQL 支持。除了支持 PDO,DSQL 还可以扩展以处理兼容 SQL 的 NoSQL 服务器。
DSQL 是稳定的!
DSQL 自 2006 年以来一直在生产环境中使用,最初包含在 AModules2 中,后来包含在 Agile Toolkit 中。我们只是将它分叉并为您清理了一下
DSQL 简单而强大
$query = new Atk4\Dsql\Query(); $query ->table('employees') ->where('birth_date','1961-05-02') ->field('count(*)') ; echo "Employees born on May 2, 1961: ".$query->getOne();
如果基本查询不有趣,那么更复杂的查询呢?
// Establish a query looking for a maximum salary $salary = new Atk4\Dsql\Query(['connection'=>$pdo]); // Create few expression objects $e_ms = $salary->expr('max(salary)'); $e_df = $salary->expr('TimeStampDiff(month, from_date, to_date)'); // Configure our basic query $salary ->table('salary') ->field(['emp_no', 'max_salary'=>$e_ms, 'months'=>$e_df]) ->group('emp_no') ->order('-max_salary') // Define sub-query for employee "id" with certain birth-date $employees = $salary->dsql() ->table('employees') ->where('birth_date','1961-05-02') ->field('emp_no') ; // use sub-select to condition salaries $salary->where('emp_no', $employees); // Join with another table for more data $salary ->join('employees.emp_id','emp_id') ->field('employees.first_name'); // finally, fetch result foreach ($salary as $row) { echo "Data: ".json_encode($row)."\n"; }
这构建并执行了一个看起来像这样的单个查询
SELECT `emp_no`, max(salary) `max_salary`, TimeStampDiff(month, from_date, to_date) `months` FROM `salary` JOIN `employees` on `employees`.`emp_id` = `salary`.`emp_id` WHERE `salary`.`emp_no` in (select `id` from `employees` where `birth_date` = :a) GROUP BY `emp_no` ORDER BY max_salary desc :a = "1961-05-02"
DSQL 是 Agile Data 的一部分
构建 SQL 查询可能很有趣,但为什么不将其提升到下一个层次呢?
领域模型
Agile Data 是我的另一个项目,它在 DSQL 的基础上实现了领域模型持久化。您仍然可以控制您的查询,同时还可以从数据库抽象中受益。
下一个示例使用 Agile Data 的方法 "action()" 来预先填充 DSQL 对象
$m = new Client($db); echo $m->addCondition('vip', true) ->ref('Order') ->ref('Line') ->action('fx', ['sum', 'total']) ->getDebugQuery();
select sum(`price`*`qty`) from `order_line` `O_L` where `order_id` in ( select `id` from `order` `O` where `client_id` in ( select `id` from `client` where `vip` = :a ) )
用户界面
Agile UI 是我的另一个项目,它专注于数据可视化。
如果您想知道如何在页面上以最高效的方式显示这样的表格,使用 Agile UI、Agile Data 和 DSQL,您可以在 不到 10 行 内完成它
require 'vendor/autoload.php'; $db = new \Atk4\Data\Persistence_SQL('mysql:dbname=atkui;host=localhost','root','root'); $app = new \Atk4\Ui\App('My First App'); $app->initLayout('Centered'); $g = $layout->add(new \Atk4\Ui\Grid()); $g->setModel(new Employee($db), false);
DSQL 的局限性
我们的团队有意保持 DSQL 简单。以下功能是故意排除的
- 不了解您的数据库模式(请参阅 https://github.com/atk4/schema)。
- 不依赖于您的数据库中的任何使用模式或特定表的存在。
- 不根据提供的数据值做出决策。
- 没有活动记录或对象关系映射
如果您需要上述功能,我强烈建议您查看Agile Data。
文档速查表
DSQL在http://dsql.readthedocs.org有详尽的文档,但以下链接了一些常见主题