castroitalo / echoquery
EchoQuery 便于在 PHP 代码中编写 SQL 查询
Requires
- php: ^8.2
Requires (Dev)
- mockery/mockery: ^1.6
- phpunit/phpunit: ^10.5
README
EchoQuery 为开发者提供了一种简洁直观的接口。它简化了复杂的 SQL 脚本编写,提高了可读性,并加快了开发速度,使任何规模的项目与数据库交互变得轻松高效。
基本用法
要使用 EchoQuery 包,您需要使用以下命令安装它
composer require castroitalo/echoquery
要使用 EchoQuery 生成查询字符串,只需使用可用的方法
- 查询示例
SELECT column_one AS co, column_two FROM table_one AS to WHERE column_one = 2 AND column_two = 5;
- 带有 EchoQuery 的 PHP 代码示例
// Importing library use CastroItalo\EchoQuery\Builder; // Instanciate class $echo_query = new Builder(); $query = $echo_query->select( ['column_one', 'co'], ['column_two'] ) ->from('table_one', 'to') ->where('column_one') ->equalsTo(2) ->and('column_two') ->equalsTo(5) ->getQuery();
功能
选择和 From 语句
任何 SQL 查询的基础是 SELECT 和 FROM 语句,您可以使用 echo query 轻松完成
让我们使用 EchoQuery 将此 SQL 代码创建成 PHP 代码
SELECT column_one FROM table_one
use CastroItalo\EchoQuery\Builder; $query = (new Builder())->select( ['column_one'] ) ->from('table_one') ->getQuery();
让我们使它更简单
SELECT * FROM table_one
use CastroItalo\EchoQuery\Builder; $query = (new Builder())->select( ['*'] ) ->from('table_one') ->getQuery();
在这种情况下,传递给 select 方法的每个数组都是一个列及其别名,您可以传递任意数量的列
use CastroItalo\EchoQuery\Builder; $query = (new Builder())->select( ['column_one', 'co'], ['column_two', 'ct'], ['column_three', 'ctr'] ) ->from('table_one') ->getQuery();
Where 语句
您可以逐个创建每个 WHERE 条件,如果您只想获取大于 10 的 column_one
数据,只需这样做
use CastroItalo\EchoQuery\Builder; $query = (new Builder())->select( ['column_one', 'co'], ['column_two', 'ct'], ['column_three', 'ctr'] ) ->from('table_one') ->where('column_on') ->greaterThan(10) ->getQuery();
您可以使用 ->where()
方法与
-
比较运算符
->equalsTo(mixed $value): Builder
->notEqualsTo(mixed $value, string $notEqualsToOperator = '!='): Builder
->lessThan(mixed $value): Builder
->lessThanEqualsTo(mixed $value): Builder
->greaterThan(mixed $value): Builder
->greaterThanEqualsTo(mixed $value): Builder
-
逻辑运算符
->and(string $columnName): Builder
->or(string $columnName): Builder
->not(string $columnName): Builder
-
模式匹配
->like(string $pattern): Builder
->notLike(string $pattern): Builder
-
范围条件
->between(mixed $start, mixed $end): Builder
->notBetween(mixed $start, mixed $end): Builder
-
列表条件
->in(array $list): Builder
->notIn(array $list): Builder
-
空值条件
->isNull(): Builder
->isNotNull(): Builder
连接(JOIN)
要使用连接,您需要调用所需的 JOIN 方法,并指定表和 JOIN 列,如下所示
SELECT a.column_one AS co, b.column_two AS ct FROM table_one AS a WHERE column_one > 10 INNER JOIN table_two AS b ON a.column_one = b.column_one
use CastroItalo\EchoQuery\Builder; $query = (new Builder())->select( ['a.column_one', 'co'], ['b.column_two', 'ct'], ) ->from('table_one', 'a') ->where('column_one') ->greaterThan(10) ->innerJoin( ['table_two', 'b'], ['a.column_one', 'b.column_one'] ) ->getQuery();
要使用子查询进行 JOIN,只需使用等效的子查询 JOIN 方法
SELECT a.column_one AS co, b.column_two AS ct FROM table_one AS a WHERE a.column_one > 10 INNER JOIN ( SELECT column_one, column_two FROM table_two ) AS b ON a.column_one = b.column_one
use CastroItalo\EchoQuery\Builder; $sub_query = (new Builder())->select( ['a.column_one', 'co'], ['b.column_two', 'ct'], ) ->from('table_one', 'a') ->where('column_one') ->getQuery(); $query = (new Builder())->select( ['a.column_one', 'co'], ['b.column_two', 'ct'], ) ->from('table_one', 'a') ->where('column_one') ->greaterThan(10) ->innerJoin( [$sub_query, 'b'], ['a.column_one', 'b.column_one'] ) ->getQuery();
-
内连接(INNER JOIN)
->innerJoin(array ...$joinInfo): Builder
->innerJoinSub(array ...$joinInfo): Builder
-
左连接(LEFT JOIN)
->leftJoin(array ...$joinInfo): Builder
->leftJoinSub(array ...$joinInfo): Builder
-
右连接(RIGHT JOIN)
->rightJoin(array ...$joinInfo): Builder
->rightJoinSub(array ...$joinInfo): Builder
-
全连接(FULL JOIN)
->fullJoin(array ...$joinInfo): Builder
->fullJoinSub(array ...$joinInfo): Builder
-
交叉连接(CROSS JOIN)
->crossJoin(array ...$joinInfo): Builder
->crossJoinSub(array ...$joinInfo): Builder
-
自连接(SELF JOIN)
->selfJoin(array ...$joinInfo): Builder
->selfJoinSub(array ...$joinInfo): Builder
-
自然连接(NATURAL JOIN)
->naturalJoin(array ...$joinInfo): Builder
->naturalJoinSub(array ...$joinInfo): Builder
联合(UNION)
要使用联合,只需使用 ->union(string $unionQuery): Builder
或 ->unionAll(string $unionQuery): Builder
,并将后续查询作为参数传递
SELECT column_one AS co, column_two AS ct, column_three AS ctr FROM table_one AS to WHERE column_one > 10 UNION SELECT column_four AS cfr, column_five AS cf, column_six AS cs FROM table_two AS tt WHERE column_five NOT IN (1, 3, 4, 6);
use CastroItalo\EchoQuery\Builder; $union_query = (new Builder())->select( ['column_four', 'cfr'], ['column_five', 'cf'], ['column_six', 'cs'] ) ->from('table_two', 'tt') ->where('column_five') ->notIn([1, 3, 4, 6]) ->getQuery(); $query = (new Builder())->select( ['column_one', 'co'], ['column_two', 'ct'], ['column_three', 'ctr'] ) ->from('table_one', 'to') ->where('column_one') ->greaterThan(10) ->union($union_query) ->getQuery();
分组(GROUP BY)
要使用 GROUP BY 对聚合函数进行分组,只需使用 ->groupBy(string ...$columns): Builder
方法,传递一个或多个列进行分组
SELECT COUNT(column_one) AS co SUM(column_two) AS ct FROM table_one AS to GROUP BY column_one, column_two
use CastroItalo\EchoQuery\Builder; $query = (new Builder())->select( ['COUNT(column_one)', 'co'], ['SUM(column_two)', 'ct'] ) ->from('table_one', 'to') ->groupBy('column_one', 'column_two') ->getQuery();
排序(ORDER BY)
要在列上使用 ORDER BY,只需使用 ->orderBy(array ...$columns): Builder
方法,传递一个或多个列以及相应的排序函数。
SELECT column_one AS co, column_two AS ct FROM table_one AS to ORDER BY column_one, column_two DESC
use CastroItalo\EchoQuery\Builder; $query = (new Builder())->select( ['column_one', 'co'], ['column_two', 'ct'] ) ->from('table_one', 'to') ->orderBy( ['column_one'], ['column_two', 'desc'] ) ->getQuery();
HAVING
要使用 HAVING,只需使用 ->having(string $having): Builder
并传递一个比较运算符。
SELECT COUNT(column_one) AS co, column_two AS ct, column_three AS ctr FROM table_one AS to WHERE column_one > 10 HAVING COUNT(column_one) > 10
use CastroItalo\EchoQuery\Builder; $query = (new Builder())->select( ['COUNT(column_one)', 'co'], ['column_two', 'ct'], ['column_three', 'ctr'] ) ->from('table_one', 'to') ->where('column_one') ->greaterThan(10) ->having('COUNT(column_one)') ->greaterThan(10) ->getQuery();
分页
要创建分页,只需使用 ->pagination(int $limit, ?int $offset = null): Builder
方法,传递限制数量和可选的偏移量。
SELECT column_one AS co, column_two AS ct FROM table_one AS to LIMIT 10
use CastroItalo\EchoQuery\Builder; $query = (new Builder())->select( ['COUNT(column_one)', 'co'], ['column_two', 'ct'], ) ->from('table_one', 'to') ->where('column_one') ->pagination(10) ->getQuery();
贡献
要为项目做出贡献,请确保您已阅读CONTRIBUTING部分。
更新日志
有关最近更改的详细信息,请参阅CHANGELOG。
贡献
有关详细信息,请参阅CONTRIBUTING和CODE_OF_CONDUCT。
鸣谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅许可文件。