Skip to content

Add selectRaw, whereRaw, and orderByRaw to QueryBuilderInterface #77

@michalbiarda

Description

@michalbiarda

Problem

QueryBuilderInterface only exposes structured query methods (select, where, orderBy, etc.) and a low-level escape-hatch raw(). There is no way to inject a raw SQL expression into just the SELECT list, WHERE clause, or ORDER BY clause without dropping down to a full raw query. This blocks composite query specifications that need to mix structured conditions with expressions like COALESCE(a, b) or price > ?.

Proposed Solution

Add three raw-expression primitives to QueryBuilderInterface:

  • selectRaw(string $expression, array $bindings = []): static — appends a raw expression to the SELECT list, preserving any columns from select()
  • whereRaw(string $expression, array $bindings = []): static — AND-combines a raw condition with the regular WHERE clause; honored by aggregate methods
  • orderByRaw(string $expression, string $direction = 'ASC'): static — orders by an arbitrary expression

All three should validate the expression against a denylist (;, --, /*, */, backtick) and support positional ? bindings. Implement in MySqlQueryBuilder, PgSqlQueryBuilder, and RepositoryQueryBuilder.

Alternatives Considered

No response

Package

database

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions