kekos/query-builder

Small SQL query builder

v2.0.1 2018-10-12 18:11 UTC

This package is auto-updated.

Last update: 2024-11-09 10:33:50 UTC


README

A fork of Muhammad Usman's Pixie, a lightweight query builder for PHP. This library only supports a subset of Pixie's fetures:

  • Nested criterias (WHERE, HAVING and ON for joins)
  • Raw queries

Query Builder is not able to open database connections or execute the built queries.

Install

You can install Query Builder via Composer:

composer require kekos/query-builder

API

Start by configure Query Builder with an adapter:

QueryBuilder::setAdapter(new MySqlAdapter());

The adapter sets the correct sanitizer character. At this moment MySqlAdapter is provided with the library.

All builders have a toSql() method, which returns the SQL query and all parameters to be bound to the query, to be used with prepared statements.

Select

$result = QueryBuilder::select(['user', 'u'])
  ->columns(['u.id', 'uname' => 'username'])
  ->join(['user_permission', 'p'], QB::raw('p.user_id = u.id'))
  ->limit(5, 0)
  ->groupby(['u.id'])
  ->orderby(['username ASC', 'firstname ASC'])
  ->where('firstname', '=', 'Christoffer')
  ->whereNot('u.id', 'IN', array(2))
  ->toSql();

$result will be an object of type QueryBuilder\QueryBuilders\Raw.

Nested where or having

$result = QueryBuilder::select('user')
  ->orderby('id')
  ->where(function($qb) {
    $qb->where('name', 'LIKE', '%chris%')
      ->whereOr('username', 'LIKE', '%chris%');
  })
  ->where('active', '=', 1)
  ->toSql();

Insert

$result = QueryBuilder::insert('user')
  ->values([
      'username' => 'Kekos',
      'firstname' => 'Christoffer'
    ])
  ->toSql();

Update

$result = QueryBuilder::update('user')
  ->set([
      'username' => 'new_username',
      'firstname' => 'New firstname'
    ])
  ->where('id', '=', 2)
  ->toSql();

Delete

$result = QueryBuilder::delete('user')
  ->where('id', '=', 2)
  ->toSql();

Bugs and improvements

Report bugs in GitHub issues or feel free to make a pull request :-)

License

MIT