2017 © Pedro Peláez
 

library query-builder

Query Builder is a fast compiler for MySQL compatible queries written in PHP 5.4+.

image

repo2/query-builder

Query Builder is a fast compiler for MySQL compatible queries written in PHP 5.4+.

  • Sunday, February 21, 2016
  • by regeda
  • Repository
  • 3 Watchers
  • 9 Stars
  • 194 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 1 % Grown

The README.md

Query Builder

Build Status Latest Stable Version Total Downloads License Scrutinizer Code Quality Code Coverage, (*1)

Query Builder is a fast compiler for MySQL compatible queries written in PHP 5.4+. A syntax is more declarative and a simple than in another known libraries. The compiler is a micro framework that useful for development in PHP only without writing pure SQL., (*2)

use Repo2\QueryBuilder;

// build a query
$query = QueryBuilder\select('user', [
    'id',
    QueryBuilder\alias('timezone', 'tz')
])
->where(
    QueryBuilder\greater('age', 21),
    QueryBuilder\orWhere(
        QueryBuilder\equal('city', 'New York'),
        QueryBuilder\isNotNull('country')
    )
)
->orderBy(
    QueryBuilder\orderByDesc('created_at'),
    QueryBuilder\orderByAsc('age')
)
->limit(20);

// compile & run sql
$conn = new \mysqli('localhost', 'root', '', 'test');
$sql = $query->compile(new Driver\Mysqli($conn));
$conn->query($sql);

The query will be compiled into:, (*3)

SELECT `id`, `timezone` AS `tz`
FROM `user`
WHERE (`age` > "21" AND (`city` = "New York" OR `country` IS NOT NULL))
ORDER BY `created_at` DESC, `age` ASC
LIMIT 20

The heart of the framework is extensibility. You can create own query. All you need is implement ExpressionInterface., (*4)

Table of contents, (*5)

  1. Installation
  2. DDL
  3. Query
  4. Restrictions

Installation

Install it with Composer:, (*6)

{
    "require": {
        "repo2/query-builder": "*"
    }
}

DDL

DDL wrappers are located in Repo2\QueryBuilder\DDL namespace., (*7)

create

function DDL\create(string $table, array $columns)

returns \Repo2\QueryBuilder\DDL\CreateTable., (*8)

Example, (*9)

The code:, (*10)

use Repo2\QueryBuilder\DDL;

$query = DDL\create('user', [
    DDL\column('id')->integer()->primary(),
    DDL\column('name')->varchar(50)->required(),
    DDL\column('timezone')->varchar(100)->defval('UTC')
]);

will be compiled into:, (*11)

CREATE TABLE `user`(
    `id` INTEGER PRIMARY KEY,
    `name` VARCHAR(50) NOT NULL,
    `timezone` VARCHAR(100) DEFAULT "UTC"
)

drop

function DDL\drop(string $table)

returns \Repo2\QueryBuilder\DDL\DropTable., (*12)

Example, (*13)

The code:, (*14)

use Repo2\QueryBuilder\DDL;

$query = DDL\drop('user');

will be compiled into:, (*15)

DROP TABLE `user`

Query

CRUD wrappers are located in Repo2\QueryBuilder namespace., (*16)

insert

function QueryBuilder\insert(string $table, array $firstRow = [])

returns \Repo2\QueryBuilder\Query\InsertQuery., (*17)

Example, (*18)

The code:, (*19)

use Repo2\QueryBuilder;

$query = QueryBuilder\insert('something', ['foo' => 1, 'bar' => 2]);

will be compiled into:, (*20)

INSERT INTO `something`(`foo`, `bar`) VALUES ("1", "2")

You can create multi insert statement also. The code:, (*21)

use Repo2\QueryBuilder;

$query = QueryBuilder\insert('something')
    ->pipe(['foo' => 1, 'bar' => 2])
    ->pipe(['foo' => 3, 'bar' => 4]);

will be compiled into:, (*22)

INSERT INTO `something`(`foo`, `bar`) VALUES ("1", "2"), ("3", "4")

select

function QueryBuilder\select(string $table, array $columns)

returns \Repo2\QueryBuilder\Query\SelectQuery., (*23)

The SelectQuery inherits FilterQuery. See filter., (*24)

Example, (*25)

The code:, (*26)

use Repo2\QueryBuilder;

$query = QueryBuilder\select('something', ['foo']);

will be compiled into:, (*27)

SELECT `foo` FROM `something`

You can create aliases in columns list. The code:, (*28)

use Repo2\QueryBuilder;

$query = QueryBuilder\select(
    'something',
    [
       'foo',
       QueryBuilder\alias('timezone', 'tz')
    ]
);

will be compiled into:, (*29)

SELECT `foo`, `timezone` AS `tz` FROM `something`

update

function QueryBuilder\update(string $table, array $values)

returns \Repo2\QueryBuilder\Query\UpdateQuery., (*30)

The UpdateQuery inherits FilterQuery. See filter., (*31)

Example, (*32)

The code:, (*33)

use Repo2\QueryBuilder;

$query = QueryBuilder\update('something', ['foo' => 1, 'bar' => 'baz']);

will be compiled into:, (*34)

UPDATE `something` SET `foo` = "1", `bar` = "baz"

You can use values incrementations for update. The code:, (*35)

use Repo2\QueryBuilder;

$query = QueryBuilder\update(
    'something',
    [
        QueryBuilder\inc('foo', 10),
        QueryBuilder\inc('bar', -10)
    ]
);

will be compiled into:, (*36)

UPDATE `something` SET `foo` = `foo` + 10, `bar` = `bar` - 10

delete

function QueryBuilder\delete(string $table)

returns \Repo2\QueryBuilder\Query\DeleteQuery., (*37)

The DeleteQuery inherits FilterQuery. See filter., (*38)

Example, (*39)

The code:, (*40)

use Repo2\QueryBuilder;

$query = QueryBuilder\delete('something');

will be compiled into:, (*41)

DELETE FROM `something`

filter

The abstract class FilterQuery implements data filtering, sorting and slicing., (*42)

where, (*43)

FilterQuery::where(ExpressionInterface ...$conditions)

You can use expressions for the method: - equal - notEqual - greater - greaterOrEqual - less - lessOrEqual - isNull - isNotNull - inArray - notInArray - orWhere - andWhere, (*44)

orderBy, (*45)

FilterQuery::orderBy(ExpressionInterface ...$conditions)

You can use expressions for the method: - orderByAsc - orderByDesc, (*46)

limit, (*47)

FilterQuery::limit(int $limit, int $offset = 0)

Restrictions

The framework has some restrictions: - No prepared statements. - No joins. - No subqueries. - The query SELECT * FROM is not allowed. - mysqli driver implementation only., (*48)

Source: https://github.com/Repo2/query-builder, (*49)

The Versions

21/02 2016

dev-master

9999999-dev

Query Builder is a fast compiler for MySQL compatible queries written in PHP 5.4+.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

sql query mysql builder

22/01 2015

v1.3

1.3.0.0

Query Builder is a fast compiler for MySQL compatible queries written in PHP 5.4+.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

sql query mysql builder

20/12 2014

v1.2

1.2.0.0

Query Builder is a fast compiler for MySQL compatible queries written in PHP 5.4+.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

sql query mysql builder

13/12 2014

v1.1

1.1.0.0

Query Builder is a fast compiler written in PHP for MySQL compatible queries.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

sql query mysqli builder

12/12 2014

v1.0

1.0.0.0

Query Builder is a fast compiler written in PHP for MySQL compatible queries.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

sql query mysqli builder