Skip to content

QueryGrammar

Viames Marino edited this page Feb 23, 2026 · 1 revision

Pair framework: QueryGrammar

Pair\Orm\QueryGrammar compiles a Query object into raw SQL.

It handles clause compilation and identifier wrapping.

Main responsibilities

  • compile select/from/join/where/group/having/order/limit/offset
  • compile nested/exists/in/between/null where clauses
  • compile join clause variations from JoinClause
  • safely wrap identifiers and aliased identifiers

Primary method

compileSelect(Query $query): string

Produces final SQL for a select query.

Query::toSql() delegates to this compiler.

Important internal compilers

  • compileColumns()
  • compileFrom()
  • compileJoins()
  • compileWheres()
  • compileGroups()
  • compileHavings()
  • compileOrders()
  • compileLimit()
  • compileOffset()

Why this matters for developers

You usually do not call QueryGrammar directly, but understanding it helps when:

  • diagnosing generated SQL
  • mixing raw clauses with fluent builder
  • working on query builder internals

Implementation example (indirect use)

use Pair\Orm\Query;

$query = Query::table('users')
    ->where('active', 1)
    ->orderBy('created_at', 'desc')
    ->limit(10);

$sql = $query->toSql();
$bindings = $query->getBindings();

See also: Query, JoinClause, Database.

Clone this wiki locally