ucscode/squery 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ucscode/squery

Composer 安装命令:

composer require ucscode/squery

包简介

SQL Syntax Generator for PHP

README 文档

README

SQL Syntax Generator for PHP

Introduction

Orignally made for User Synthetics, SQuery has progressed to the point of becoming an independent PHP library that simplifies the process of generating SQL syntax for your database queries. With sQuery, you can build SQL queries using an intuitive and fluent interface, reducing the need to write SQL directly. It supports various SQL operations like SELECT, FROM, JOIN, WHERE, GROUP BY, and more.

The SQuery class provides a simplified and efficient way to interact with databases in PHP, specifically by easing the CRUD (Create, Read, Update, Delete) operations.

Features

  • SQuery ensures the proper ordering of your SQL syntax, even if the methods are not called in order.
  • SQuery supports nearly all functions related to SQL keywords.
  • Build SQL queries in a structured and readable manner.
  • Supports common SQL operations such as SELECT, FROM, JOIN, WHERE, GROUP BY, ORDER BY, and LIMIT.
  • Generate complex queries with ease by chaining methods.
  • Designed to improve code maintainability and reduce the risk of SQL injection.

Requirements

  • PHP 8.1 or higher.
  • A compatible database (e.g., MySQL, MariaDB) and appropriate database extensions (e.g., MySQLi, PDO) to execute the generated queries.

Installation

composer require ucscode/squery

SELECT SQL EXAMPLE

use Ucscode\SQuery\SQuery;
use Ucscode\SQuery\Condition;

$squery = new SQuery();

$condition = new Condition();
$condition
   ->add("u.vendor", "ucscode")
   ->and("u.namespace", "SQuery")
   ->or("u.foundation", "Uss%", 'RLIKE');

$squery
   ->select("u.username")
   ->from("tablename", "u")
   ->where($condition)
   ->limit(2)
   ->groupBy('u.id', 'DESC')
   ;
   
echo $squery->build();

INSERT SQL EXAMPLE

$data = [
   'username' => 'Ucscode', 
   'password' => '12345',
   'role' => 'SUPER_ADMIN'
];

$squery = new SQuery();
$squery->insert('tablename', $data);

UPDATE SQL EXAMPLE

$data = [
   'username' => 'User Synthetics', 
   'password' => '54321',
   'role' => 'PROJECT'
];

$condition = new Condition();
$condition
   ->add("user_id", 1)
   ->and('role', 'SUPER_ADMIN')
   ->or('username', 'spider-man', 'NOT')
   ->and('finance', null, 'IS NOT');

$squery = new SQuery();
$squery
   ->update('tablename', $data)
   ->where($condition);

DELETE SQL EXAMPLE

  1. To generate a DELETE query, use the delete() method:
$squery = new SQuery();

$condition = new Condition();
$condition->add("username", "trouble-maker");

$squery
   ->delete()
   ->from('tablename')
   ->where($condition);

USING JOIN STATEMENT

SQuery uses an instance of a Join object to create and manage different types of join statements. For Example:

LEFT JOIN EXAMPLE

$squery = new SQuery();

$squery
   ->select([
      "t1.*",
      "t2.value",
      "t1.status",
   ])
   ->from('tablename', "t1");

// Left Join

$leftJoin = new Join('table_2');

$on = (new Condition())
   ->add("t2.user_id", "t1.id")
   ->and("t2.name", null);

$leftJoin->setOn($on);
$leftJoin->setAlias("t2");

$squery->leftJoin($leftJoin);

The same applies to other join statement like RIGHT JOIN, INNER JOIN etc

The Join object literally accepts an SQuery object as it's first parameter, making it capable of handling complex SQL statement.

EXAMPLE

$squery = new SQuery();

$squery
   ->select('*')
   ->from('table_1', 't1');

// Create Complex Join Statement;

$rightJoinQuery = (new SQuery())
   ->select("*")
   ->from("table_2")
   ->where(
      (new Condition())
         ->add("name", "ucscode")
         ->and("age", 3, ">")
   )
   ->limit(7);

$onCondition = (new Condition())
   ->add("t1.port", "t2.port", null, false)
   ->or("t1.status", null, 'IS NOT');

$alias = "t2";

$rightJoin = new Join($rightJoinQuery, $onCondition, $alias);

// Add the Right Join Statement

$squery->rightJoin($rightJoin);

Warning!

It is important to note that the SQuery library does not automatically sanitize user input. When using the library, it is crucial to sanitize any user-supplied data before passing it as input to the SQuery methods.

Note

Please note that the SQuery class only generates SQL query strings; it does not execute them against a database. To execute these queries, you would need to establish a database connection and use appropriate methods from the MySQLi or PDO libraries.

License

This project is licensed under the MIT License.

ucscode/squery 适用场景与选型建议

ucscode/squery 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 01 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ucscode/squery 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ucscode/squery 我们能提供哪些服务?
定制开发 / 二次开发

基于 ucscode/squery 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 21
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-02