hraw/dbqb
Composer 安装命令:
composer require hraw/dbqb
包简介
A lightweight mysql query builder for PHP
README 文档
README
Query builder package to implement database queries easily in core PHP.
Requirements
- PHP 7.0 or higher
- Composer for installation
Installation
composer require hraw/dbqb
Implementation
Example: filename -> index.php
<?php
require_once __DIR__.'/vendor/autoload.php';
use Hraw\DBQB\DB;
//Connect to database
DB::connect([
'name' => 'default',
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'username',
'password' => 'password',
]);
//Fetch all the data from table
DB::all('tableName');
//Fetch data with conditions
DB::table('users')->where('id', '>', 1)->get();
//Fetch data with array of conditions
DB::table('users')->where([
'salary' => 10000,
'profile' => 'developer'
])
->get();
//Fetch only single record
DB::table('users')->where('id', '>', 1)->first();
//Print sql query
DB::table('users')->where('id', '>', 1)->toSql();
//Deleting a record
DB::table('users')->where('id', 10)->delete();
//Updating a record
DB::table('users')->where('role_id', 1)
->update([
'designation' => 'Admin'
]);
//Inserting a record
DB::table('users')
->insert([
'fname' => 'John',
'lname' => 'Doe'
]);
//Inserting bulk records
DB::table('users')
->batchInsert([
[
'fname' => 'John',
'lname' => 'Doe'
],
[
'fname' => 'peter',
'lname' => 'parker'
]
]);
Note:- batchInsert function does not return id of the inserted rows however insert function returns the id of the inserted row.
//Nested conditions
DB::table('users')->where('name', 'like', '%jo%')
->where(function($query){
$query->where('age', '>', 18)
->orWhere('state_code', 'UK');
})
->first();
//Group By, Order By and Limit
DB::table('users')->where('name', 'black tshirt')
->groupBy('size', 'sku')
->orderBy('id', 'desc')
->limit(20)
->get();
//Fetch data using union and unionAll
DB::table('users')
->select('id')
->where('fname', 'john')
->union(
DB::table('posts')
->select('id')
->where('id', 1)
)
->get();
DB::table('users')
->select('id')
->where('fname', 'john')
->unionAll(
DB::table('posts')
->select('id')
->where('id', 1)
)
->get();
//Fetch data using joins
DB::table('users')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'orders.*')
->get();
Note:- join method uses inner join for joining the tables.
Other supported methods:
1) leftJoin, 2) rightJoin, 3) outerJoin
DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
DB::table('users')
->rightJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
Transactions
DB::beginTransaction();
try {
DB::table('users')->where('id', '<', 10)->delete();
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
}
Multiple database connections
DB::connection('connection1Name')->all('tableName');
DB::connection('connection2Name')->all('tableName');
Supported methods
- where('columnName', 'operator', 'value') or where([$keyValues]) //operator is optional
- orWhere('columnName', 'operator', 'value') //operator is optional
- whereIn('columnName', 'values') //values must an array
- orWhereIn('columnName', 'values') //values must an array
- whereNotIn('columnName', 'values') //values must an array
- orWhereNotIn('columnName', 'values') //values must an array
- whereBetween('columnName', [val1, val2])
- whereNotBetween('columnName', [val1, val2])
- orWhereBetween('columnName', [val1, val2])
- orWhereNotBetween('columnName', [val1, val2])
- whereNull('columnName')
- orWhereNull('columnName')
- whereNotNull('columnName')
- orWhereNotNull('columnName')
- orderBy('columnName', type) // type can be ASC or DESC
- groupBy(column1, column2)
- limit(value)
- offset(value)
- raw(query, placeholders) //you can run raw queries through this query builder with or without prepare statements, for prepare statements all you have to do is pass array of values of in placeholders parameter(optional parameter).
- count('columnName') //optional
- min('columnName')
- max('columnName')
- avg('columnName')
- sum('columnName')
- insert([values]) //Insert single row in table, values must be an associative array
- batchInsert([values]) //Insert multiple records in table, values must array of arrays
- delete() //Delete record from database
- update([values]) //values must be an associative array
- having (Note:- All having methods are same as where methods)
Run Server
php -S localhost:8000
hraw/dbqb 适用场景与选型建议
hraw/dbqb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 566 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「mysql」 「query builder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hraw/dbqb 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hraw/dbqb 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hraw/dbqb 相关的其它包
同方向 / 同关键字的高下载量 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
Anax Database Active Record module for model classes.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 566
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-24