承接 hasan-22/query-builder 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

hasan-22/query-builder

Composer 安装命令:

composer require hasan-22/query-builder

包简介

PHP SQL query builder using PDO

README 文档

README

PHP SQL query builder using PDO. It's a quick and light library featuring a smart join builder, which automatically creates table joins for you.

Features

  • Easy interface for creating robust queries
  • Supports any database compatible with PDO
  • Ability to build complex SELECT, INSERT, UPDATE & DELETE queries with little code
  • Type hinting for magic functions with code completion in smart IDEs

Install

composer require hasan-22/query-builder

Driver:

First make the config file exactly like this Config file is not necessary config.php

<?php

return [

    'default'=>'mysql',

    'driver'=>[
        'mysql'=>[
            'host'=>'localhost',
            'username'=>'root',
            'password'=>'',
            'dbname'=>'php',
            'charset'=>'UTF8',
        ]
    ]
];

Then create a custom driver class for the connection like this

Mysql.php

<?php

class Mysql implements \App\Database\ConnectionInterface
{
    private static $instance;

    private function __construct(){}

    /**
     * This method prevents the creation of duplicate object
     * @return mixed
     */
    public static function setInstance(){
        if(!static::$instance instanceof Mysql){
            static::$instance = new Mysql();
        }
        return static::$instance;
    }

    /**
     * Get database connection information in configuration
     * @return array
     */
    public function driver(): array{
        /**
        * If you have set the database in the config file, use this code.
        */
        // Path of your config file
        $config = require __DIR__.'/Config/Database.php';
        $config = $config['driver'][$config['default']];
        return [
            'dns'=>"mysql:host=".$config['host'].";dbname=".$config['dbname'].";charset:".$config['charset'],  
            'password'=>$config['password'], 
            'username'=>$config['username']
        ];

        /**
        * Otherwise, you can send the database information directly like this
        *
        * return ['dns'=>'Your dns','username'=>'','password'=>''];
        *
        */ 
    }

    /**
     * Connect to database with PDO
     * @return \PDO|void
     */
    public function connect(){
        // The attribute parameter is optional
        $attributes = [
            \PDO::ATTR_CASE=>\PDO::CASE_NATURAL
        ];
        return \App\Database\PDO::connect($this->driver(),$attributes);
    }
}

Then create new instance of DB

$db = new \App\Builder\DB("\App\Database\Mysql");

Set table and fetch all records

$db->table('users')->get()

In the get() function, you can specify which columns to get

$db->table('users')->get('name','password','email')

Or you can use all() function to get all records

$db->table('users')->all()

Execute queries for a single or many record by ID

$db->table('users')->find()
// or
$db->table('users')->find(1,2,3)

If you want add condition follow my lead

Some Where functions

$db->table('users')->where('name','=','armia')->get();

// for `or where`
$db->table('users')->orWhere('name','=','armia')->get();

// for `where in`
$db->table('users')->whereIn('id',[1,2,3])->get();

// for `or wher in`
$db->table('users')->orWhereIn('id',[1,2,3])->get();

// for `where between`
$db->table('users')->whereBetween('crated_at',['2022-12-12','2023-01-29'])->get();

// for `or where between`
$db->table('users')->orWhereBetween('crated_at',['2022-12-12','2023-01-29'])->get();

// for `exists`
$db->table('brands')->exists('users','users.id = brands.user_id')->get();

// And you can run all the functions one after the other like the example below
$db->table('users')
->where('name','=','armia')
->orWhere('name','=','armia')
->whereIn('id',[1,2,3])
->orWhereIn('id',[1,2,3])
->whereBetween('crated_at',['2022-12-12','2023-01-29'])
->orWhereBetween('crated_at',['2022-12-12','2023-01-29'])
->get()

Some Having functions

$db->table('users')->havin('name','=','armia')->get();

// for `or where`
$db->table('users')->orHaving('name','=','armia')->get();

// for `where in`
$db->table('users')->havingIn('id',[1,2,3])->get();

// for `or wher in`
$db->table('users')->orHavingIn('id',[1,2,3])->get();

// for `where between`
$db->table('users')->havingBetween('crated_at',['2022-12-12','2023-01-29'])->get();

// for `or where between`
$db->table('users')->orHavingBetween('crated_at',['2022-12-12','2023-01-29'])->get();


// And you can run all the functions one after the other like the example below
$db->table('users')
->having('name','=','armia')
->orHaving('name','=','armia')
->havingIn('id',[1,2,3])
->orHavingIn('id',[1,2,3])
->havingBetween('crated_at',['2022-12-12','2023-01-29'])
->orHavingeBetween('crated_at',['2022-12-12','2023-01-29'])
->get()

Join functions

$db->table('users')->innerJoin('brands','brands.user_id','users.id')->get();

$db->table('users')->leftJoin('brands','brands.user_id','users.id')->get();

$db->table('users')->rightJoin('brands','brands.user_id','users.id')->get();

$db->table('users')->crossJoin('brands')->get();

You can use where or having condition with join functions

$db->table('users')
->innerJoin('brands','brands.user_id','users.id')
->where('users.id','=',14)->get();

CRUD functions

For insert data use create function

Insert single data

$data = ['name'=>'armia','email'=>'armiaevil@gmail.com','password'=>'123456'];
$db->table('users')->create($data);

Insert multiple data

$data = [
    ['name'=>'armia','email'=>'armiaevil@gmail.com','password'=>'123456'],
    ['name'=>'armia','email'=>'armiaevil@gmail.com','password'=>'123456'],
    ['name'=>'armia','email'=>'armiaevil@gmail.com','password'=>'123456'],
];
$db->table('users')->create($data);

Update record

$db->table('users')->where('id','=',1)->update(['name'=>'update name','email'=>'email@gmail.com']);
// or
$db->table('users')->update(['name'=>'update name','email'=>'email@gmail.com'],['id'=>1]);
// or
$db->table('users')->update(['name'=>'update name','email'=>'email@gmail.com'],['id'=>1,'status'=>'activated']);

Delete record

$db->table('users')->where('id',1)->delete();
// or
$db->table('users')->delete(['id',1]);
// or
$db->table('users')->delete(['id',1,'status'=>'activated']);

Transaction

$db->beginTransaction();
try{
    // TODO: code
    
    $db->commit();
}catch (Exception $e){
    $db->rollback();
    // TODO: code
}

Debug

Only works on functions whose return value is Object of class DB.

$db->table('users')
->innerJoin('brands','brands.user_id','users.id')
->where('users.id','=',14)->debug();

/**
Output:
SQL: [86] SELECT * FROM users INNER JOIN brands ON brands.user_id = users.id WHERE users.id = ?
Sent SQL: [89] SELECT * FROM users INNER JOIN brands ON brands.user_id = users.id WHERE users.id = '14'
Params:  1
Key: Position #0:
paramno=0
name=[0] ""
is_param=1
param_type=2
NULL
*/

Functions

Functions return
table Object of class DB
get Array
all Array
find object \ array
newQQuery Array
count object
latest Object of class DB
orderBy Object of class DB
rand Object of class DB
groupBy Object of class DB
limit Object of class DB
take Object of class DB
first object
last object
max object
min object
sum object
avg object
where Object of class DB
orWhere Object of class DB
whereNull Object of class DB
orWhereNull Object of class DB
whereNotNull Object of class DB
orWhereNotNull Object of class DB
whereIn Object of class DB
orWhereIn Object of class DB
whereNotIn Object of class DB
orWhereNotIn Object of class DB
whereBetween Object of class DB
orWhereBetween Object of class DB
whereNotBetween Object of class DB
orWhereNotBetween Object of class DB
exists Object of class DB
orExists Object of class DB
notExists Object of class DB
orNotExists Object of class DB
having Object of class DB
orHaving Object of class DB
havingNull Object of class DB
orHavingNull Object of class DB
havingNotNull Object of class DB
orHavingNotNull Object of class DB
havingIn Object of class DB
orHavingIn Object of class DB
havingNotIn Object of class DB
orHavingNotIn Object of class DB
havingBetween Object of class DB
orHavingBetween Object of class DB
havingNotBetween Object of class DB
orHavingNotBetween Object of class DB
innerJoin Object of class DB
leftJoin Object of class DB
rightJoin Object of class DB
crossJoin Object of class DB
create boolean
update boolean
delete boolean
beginTransaction void
rollback void
commit void
getDbName string
currentId object
lastInsertId object
emptyQuery void
debug string

hasan-22/query-builder 适用场景与选型建议

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

它主要适用于以下技术方向: 「php」 「pdo」 「query builder」 「hasan」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 hasan-22/query-builder 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-30