anax/database-query-builder
Composer 安装命令:
composer require anax/database-query-builder
包简介
Anax Database Query Builder module, create queries through methods.
关键字:
README 文档
README
Anax Database Query Builder module as an extension to anax/database to enable querying the datase using methods instead of SQL.
This module is used to implement the module Database Active Record anax\database-active-record.
The module is tested using MySQL and SQLite.
Table of content
- Install
- Development
- Class, interface, trait
- Exceptions
- DI service
- Access as framework service
- Basic usage
- Dependency
- License
Install
You can install the module from anax/database-query-builder on Packagist using composer.
composer require anax/database-query-builder
You can then copy the default configuration files as a start.
# In the root of your Anax installation
rsync -av vendor/anax/database-query-builder/config .
Development
To work as a developer you clone the repo and install the local environment through make. Then you can run the unit tests.
make install
make test
Class, interface, trait
The following classes, interfaces and traits exists.
The following parts are related to the feature of a SQL query builder.
| Class, interface, trait | Description |
|---|---|
Anax\Database\QueryBuilderTrait |
A trait implementing SQL query builder, based upon Anax\Database\Database. |
Anax\Database\DatabaseQueryBuilder |
A database class using the SQL query builder trait (used by the Active Record module) and extending the database class. |
Exceptions
All exceptions are in the namespace Anax\DatabaseQueryBuilder\Exception\. The following exceptions exists and may be thrown.
| Exception | Description |
|---|---|
BuildException |
When failing to build a SQL query. |
DI service
The database query builder is created as a framework service within $di. The following is a sample on how the database query builder service is created through config/di/dbqb.php.
/** * Configuration file for database query builder service. */ return [ // Services to add to the container. "services" => [ "dbqb" => [ "shared" => true, "callback" => function () { $obj = new \Anax\DatabaseQueryBuilder\DatabaseQueryBuilder(); // Load the configuration files $cfg = $this->get("configuration"); $config = $cfg->load("database"); // Set the database configuration $connection = $config["config"] ?? []; $db->setOptions($connection); $db->setDefaultsFromConfiguration(); return $db; } ], ], ];
Access as framework service
You can access the module as a framework service and use it as an ordinary database service.
$sql = "SELECT * FROM movie;"; $db = $di->get("dbqb"); $db->connect(); $res = $db->executeFetchAll($sql);
This is since the class \Anax\DatabaseQueryBuilder\DatabaseQueryBuilder extends the database class \Anax\Database\Database.
Basic usage
This is the basic usage of the query builder.
You start by creating a database object from the query builder class and connect to the database.
$this->db = new DatabaseQueryBuilder([ "dsn" => "sqlite::memory:", ]); $this->db->setDefaultsFromConfiguration(); $this->db->connect();
This is more or less the same as retrieving the class from the $di container.
You can now create a table.
// Create a table $this->db->createTable( 'user', [ 'id' => ['integer', 'primary key', 'not null'], 'age' => ['integer'], 'name' => ['varchar(10)'] ] )->execute();
The table is created.
You can now insert rows into the table.
$this->db->insert( "user", [ "age" => 3, "name" => "three", ] )->execute(); $last = $this->db->lastInsertId(); // 1 $rows = $this->db->rowCount(); // 1
You can now query the table.
$res = $this->db->select("*") ->from("user") ->where("id = 1") ->execute() ->fetch(); $res->id; // 1 $res->age; // 3 $res->name; // "three"
That is the basic usage and the idea is to create the SQL-queries using class methods and build tha actual SQL query behind the scene.
Dependency
This module depends upon, and extends, the database abstraction layer anax\database.
The module is usually used within an Anax installation but can also be used without Anax.
License
This software carries a MIT license. See LICENSE.txt for details.
.
..: Copyright (c) 2013 - 2018 Mikael Roos, mos@dbwebb.se
anax/database-query-builder 适用场景与选型建议
anax/database-query-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.31k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「query builder」 「dba」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 anax/database-query-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 anax/database-query-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 anax/database-query-builder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Query filtering in your frontend
A Sphinx Query Builder extension for the Laravel Database package
Anax Database Active Record module for model classes.
统计信息
- 总下载量: 6.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-01