SQL Query Builder
=================
[![Build Status](https://travis-ci.org/nilportugues/php-sql-query-builder.svg)](https://travis-ci.org/nilportugues/php-sql-query-builder) [![Coverage Status](https://img.shields.io/coveralls/nilportugues/sql-query-builder.svg)](https://coveralls.io/r/nilportugues/sql-query-builder) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nilportugues/sql-query-builder/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/nilportugues/sql-query-builder/?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/89ec1003-4227-43a2-8432-67a9fc2d3ba3/mini.png)](https://insight.sensiolabs.com/projects/89ec1003-4227-43a2-8432-67a9fc2d3ba3) [![Latest Stable Version](https://poser.pugx.org/nilportugues/sql-query-builder/v/stable)](https://packagist.org/packages/nilportugues/sql-query-builder) [![Total Downloads](https://poser.pugx.org/nilportugues/sql-query-builder/downloads)](https://packagist.org/packages/nilportugues/sql-query-builder) [![License](https://poser.pugx.org/nilportugues/sql-query-builder/license)](https://packagist.org/packages/nilportugues/sql-query-builder)
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://paypal.me/nilportugues)
An elegant lightweight and efficient SQL Query Builder with fluid interface SQL syntax supporting bindings and complicated query generation. **Works without establishing a connection to the database.**
<a name="index_block"></a>
* [1. Installation](#block1)
* [2. The Builder](#block2)
* [2.1. Generic Builder](#block2.1)
* [2.2. MySQL Builder](#block2.2)
* [2.3. Human Readable Output](#block2.3)
* [3. Building Queries](#block3)
* [3.1. SELECT Statement](#block3.1)
* [3.1.1. Basic SELECT statement](#block3.1.1)
* [3.1.2. Aliased SELECT statement](#block3.1.2)
* [3.1.3. SELECT with WHERE statement](#block3.1.3)
* [3.1.4. Complex WHERE conditions](#block3.1.4)
* [3.1.5. JOIN & LEFT/RIGHT/INNER/CROSS JOIN SELECT statements](#block3.1.5)
* [3.1.6. COUNT rows](#block3.1.6)
* [3.2. INSERT Statement](#block3.2)
* [3.2.1. Basic INSERT statement](#block3.2.1)
* [3.3. UPDATE Statement](#block3.3)
* [3.3.1. Basic UPDATE statement](#block3.3.1)
* [3.3.2. Elaborated UPDATE statement](#block3.3.2)
* [3.4. DELETE Statement](#block3.4)
* [3.4.1. Empty table with DELETE statement](#block3.4.1)
* [3.4.2. Basic DELETE statement](#block3.4.2)
* [3.4.3. Elaborated DELETE statement](#block3.4.3)
* [3.5. INTERSECT Statement](#block3.5)
* [3.6. MINUS Statement](#block3.6)
* [3.7. UNION Statement](#block3.7)
* [3.8. UNION ALL Statement](#block3.8)
* [4. Advanced Quering](#block4)
* [4.1. Filtering using WHERE](#block4.1)
* [4.1.1. Changing WHERE logical operator](#block4.2)
* [4.1.2. Writing complicated WHERE conditions](#block4.2)
* [4.3. Grouping with GROUP BY and HAVING](#block4.3)
* [4.3.1 Available HAVING operators](#block4.3.1)
* [4.4. Changing HAVING logical operator](#block4.4)
* [4.5. Columns as SELECT statements](#block4.5)
* [4.6. Columns being Values](#block4.6)
* [4.7. Columns using FUNCTIONS](#block4.7)
* [5. Commenting queries](#block5)
* [6. Quality Code](#block6)
* [7. Author](#block7)
* [8. License](#block8)
<a name="block1"></a>
## 1. Installation [↑](#index_block)
The recommended way to install the SQL Query Builder is through [Composer](http://getcomposer.org). Run the following command to install it:
```sh
php composer.phar require nilportugues/sql-query-builder
```
<a name="block2"></a>
## 2. The Builder [↑](#index_block)
The SQL Query Builder allows to generate complex SQL queries standard using the `SQL-2003` dialect (default) and the `MySQL` dialect, that extends the `SQL-2003` dialect.
<a name="block2.1"></a>
### 2.1. Generic Builder [↑](#index_block)
The Generic Query Builder is the default builder for this class and writes standard SQL-2003.
**All column aliases are escaped using the `'` sign by default.**
#### Usage:
```php
<?php
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
$builder = new GenericBuilder();
$query = $builder->select()->setTable('user');
echo $builder->write($query);
```
#### Output:
```sql
SELECT user.* FROM user
```
<a name="block2.2"></a>
### 2.2. MySQL Builder [↑](#index_block)
The MySQL Query Builder has its own class, that inherits from the SQL-2003 builder. All columns will be wrapped with the tilde **`** sign.
**All table and column aliases are escaped using the tilde sign by default.**
#### Usage:
```php
<?php
use NilPortugues\Sql\QueryBuilder\Builder\MySqlBuilder;
$builder = new MySqlBuilder();
$query = $builder->select()->setTable('user');
echo $builder->write($query);
```
#### Output:
```sql
SELECT user.* FROM `user`
```
<a name="block2.3"></a>
#### 2.3. Human Readable Output [↑](#index_block)
Both Generic and MySQL Query Builder can write complex SQL queries.
Every developer out there needs at some point revising the output of a complicated query, the SQL Query Builder includes a human-friendly output method, and therefore the `writeFormatted` method is there to aid the developer when need.
Keep in mind `writeFormatted` is to be avoided at all cost in production mode as it adds unneeded overhead due to parsing and re-formatting of the generated statement.
#### Usage:
```php
<?php
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
$builder = new GenericBuilder();
$query = $builder->select()->setTable('user');
echo $builder->writeFormatted($query);
```
#### Output:
```sql
SELECT
user.*
FROM
user
```
More complicated examples can be found in the documentation.
<a name="block3"></a>
## 3. Building Queries [↑](#index_block)
<a name="block3.1"></a>
### 3.1. SELECT Statement [↑](#index_block)
<a name="block3.1.1"></a>
#### 3.1.1. Basic SELECT statement [↑](#index_block)
#### Usage:
```php
<?php
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
$builder = new GenericBuilder();
$query = $builder->select()
->setTable('user')
->setColumns(['user_id','name','email']);
echo $builder->write($query);
```
#### Output:
```sql
SELECT user.user_id, user.name, user.email FROM user
```
<a name="block3.1.2"></a>
#### 3.1.2. Aliased SELECT statement [↑](#index_block)
#### Usage:
```php
<?php
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
$builder = new GenericBuilder();
$query = $builder->select()
->setTable('user')
->setColumns(['userId' => 'user_id', 'username' => 'name', 'email' => 'email']);
echo $builder->write($query);
```
#### Output:
```sql
SELECT user.user_id AS 'userId', user.name AS 'username', user.email AS 'email' FROM user
```
<a name="block3.1.3"></a>
#### 3.1.3. SELECT with WHERE statement [↑](#index_block)
Default logical operator for filtering using `WHERE` conditions is `AND`.
#### Usage:
```php
<?php
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
$builder = new GenericBuilder();
$query = $builder->select()
->setTable('user')
->setColumns([
'userId' => 'user_id',
'username' => 'name',
'email' => 'email'
])
->where()
->greaterThan('user_id', 5)
->notLike('username', 'John');
echo $builder->writeFormatted($query);
```
#### Output:
```sql
SELECT
user.user_id AS 'userId',
user.name AS 'username',
user.email AS 'email'
FROM
user
WHERE
(user.user_id < :v1)
AND (user.username NOT LIKE :v2)
```
<a name="block3.1.4"></a>
#### 3.1.4. Complex WHERE conditions [↑](#index_block)
#### Usage:
```php
<?php
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
$builder = new GenericBuilder();
$query = $builder->select()
->setTable('user')
->where()
->equal
没有合适的资源?快使用搜索试试~ 我知道了~
php-sql-query-builder-master.zip_php sql
共78个文件
php:69个
md:3个
yml:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 191 浏览量
2022-09-24
08:32:11
上传
评论
收藏 80KB ZIP 举报
温馨提示
this code is explaele php engin
资源推荐
资源详情
资源评论
收起资源包目录
php-sql-query-builder-master.zip (78个子文件)
php-sql-query-builder-master
composer.json 1KB
.travis.yml 225B
tests
Builder
GenericBuilderTest.php 7KB
MySqlBuilderTest.php 2KB
Syntax
InsertWriterTest.php 3KB
PlaceholderWriterTest.php 2KB
DeleteWriterTest.php 2KB
UnionAllWriterTest.php 2KB
WhereWriterTest.php 16KB
UpdateWriterTest.php 4KB
WriterFactoryTest.php 4KB
SelectWriterTest.php 18KB
IntersectWriterTest.php 2KB
MinusWriterTest.php 2KB
UnionWriterTest.php 2KB
ColumnWriterTest.php 5KB
Syntax
OrderByTest.php 2KB
WhereTest.php 10KB
ColumnTest.php 2KB
TableTest.php 2KB
Manipulation
InsertTest.php 1KB
UnionAllTest.php 2KB
QueryFactoryTest.php 3KB
Resources
DummyQuery.php 561B
DeleteTest.php 1KB
MinusTest.php 2KB
UpdateTest.php 1KB
SelectTest.php 3KB
BaseQueryTest.php 2KB
UnionTest.php 2KB
IntersectTest.php 2KB
CONTRIBUTING.md 1KB
src
Builder
MySqlBuilder.php 2KB
GenericBuilder.php 10KB
Syntax
WhereWriter.php 11KB
WriterFactory.php 3KB
DeleteWriter.php 1KB
ColumnWriter.php 4KB
UnionAllWriter.php 659B
MinusWriter.php 985B
AbstractSetWriter.php 1KB
PlaceholderWriter.php 3KB
InsertWriter.php 2KB
UnionWriter.php 628B
IntersectWriter.php 672B
SelectWriter.php 10KB
UpdateWriter.php 2KB
AbstractBaseWriter.php 2KB
BuilderException.php 379B
BuilderInterface.php 698B
Syntax
SyntaxFactory.php 2KB
Table.php 2KB
OrderBy.php 2KB
QueryPartInterface.php 434B
Column.php 2KB
Where.php 12KB
Manipulation
AbstractCreationalQuery.php 1KB
Minus.php 2KB
QueryInterface.php 754B
Update.php 894B
Insert.php 739B
AbstractSetQuery.php 2KB
ColumnQuery.php 5KB
Intersect.php 2KB
Select.php 11KB
UnionAll.php 513B
QueryException.php 380B
QueryFactory.php 2KB
Union.php 495B
JoinQuery.php 6KB
AbstractBaseQuery.php 5KB
Delete.php 1004B
.coveralls.yml 46B
.gitignore 34B
CHANGELOG.md 949B
phpunit.xml.dist 1KB
README.md 28KB
LICENSE.txt 1KB
共 78 条
- 1
资源评论
weixin_42653672
- 粉丝: 93
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功