class QueryBuilder implements IQueryBuilder, IQueryBuilderStrategy (View source)

The QueryBuilder class implements the IQueryBuilder and IQueryBuilderStrategy interfaces to provide a flexible query building mechanism using various database engines.

Utilizes the Singleton pattern to ensure a single instance. The class allows setting and retrieving query strategies based on the database engine, and provides methods for constructing SQL queries with various clauses such as SELECT, JOIN, WHERE, and ORDER. It also supports fetching results and metadata. It uses the Singleton pattern to ensure a single instance and provides a flexible query building mechanism using various database engines.

Methods:

  • __construct(IConnection $context = null): Initializes the query builder with a database connection context.
  • with(IConnection $context): Static initializer that sets the database connection context and returns the query builder instance.
  • setStrategy(IQueryBuilder $strategy): Sets the query strategy instance.
  • getStrategy(): Returns the query strategy instance.

Query Building Methods:

  • select(array|string ...$data): Adds a SELECT clause to the query.
  • distinct(array|string ...$data): Adds a DISTINCT clause to the query.
  • from(array|string ...$data): Adds a FROM clause to the query.
  • join(array|string ...$data): Adds a JOIN clause to the query.
  • selfJoin(array|string ...$data): Adds a SELF JOIN clause to the query.
  • leftJoin(array|string ...$data): Adds a LEFT JOIN clause to the query.
  • rightJoin(array|string ...$data): Adds a RIGHT JOIN clause to the query.
  • innerJoin(array|string ...$data): Adds an INNER JOIN clause to the query.
  • outerJoin(array|string ...$data): Adds an OUTER JOIN clause to the query.
  • crossJoin(array|string ...$data): Adds a CROSS JOIN clause to the query.
  • on(array|string ...$data): Adds an ON clause to the query.
  • andOn(array|string ...$data): Adds an AND ON clause to the query.
  • orOn(array|string ...$data): Adds an OR ON clause to the query.
  • where(array|string ...$data): Adds a WHERE clause to the query.
  • andWhere(array|string ...$data): Adds an AND WHERE clause to the query.
  • orWhere(array|string ...$data): Adds an OR WHERE clause to the query.
  • having(array|string ...$data): Adds a HAVING clause to the query.
  • andHaving(array|string ...$data): Adds an AND HAVING clause to the query.
  • orHaving(array|string ...$data): Adds an OR HAVING clause to the query.
  • group(array|string ...$data): Adds a GROUP BY clause to the query.
  • order(array|string ...$data): Adds an ORDER BY clause to the query.
  • orderAsc(array|string ...$data): Adds an ORDER BY ASC clause to the query.
  • orderDesc(array|string ...$data): Adds an ORDER BY DESC clause to the query.
  • limit(array|string ...$data): Adds a LIMIT clause to the query.

Query Execution Methods:

  • build(): Builds the query string.
  • buildRaw(): Builds the raw query string.
  • getValues(): Returns the query values.
  • getAllMetadata(): Returns the query metadata.
  • fetch(int $fetchStyle = null, mixed $fetchArgument = null, mixed $optArgs = null): Fetches a single row from the query result.
  • fetchAll(int $fetchStyle = null, mixed $fetchArgument = null, mixed $optArgs = null): Fetches all rows from the query result.

Properties:

  • $context: The database connection context.
  • $self: The singleton instance of the QueryBuilder.
  • $strategy: The current query strategy.

Traits

This trait provides methods to manage a single instance of a class or multiple instances identified by a unique hash. It includes methods to get, create, set, and clear instances.

Methods

static Singleton
getInstance(string|null $hash = null)

Create or obtain an instance If no hash is provided, works as classic singleton If hash is provided, creates/returns instance for that hash

static Singleton
newInstance(string|null $hash = null)

Create a new instance of the class and set it as the instance

static void
setInstance(Singleton $instance, string|null $hash = null)

Set a specific instance

static void
clearInstance(string|null $hash = null)

Clear instance(s)

static void
clearAllInstances()

Clear all instances including the classic singleton instance

__construct(IConnection|null $context = null)

Initializes the query builder with a database connection context.

static QueryBuilder
with(IConnection|null $context = null)

Static initializer that sets the database connection context and returns the query builder instance.

void
setStrategy(IQueryBuilder $strategy)

Sets the query strategy instance.

getStrategy()

Returns the query strategy instance.

static IQueryBuilder
select(array|string ...$data)

Adds a SELECT clause to the query

static IQueryBuilder
distinct(array|string ...$data)

Adds a DISTINCT clause to the query

static IQueryBuilder
from(array|string ...$data)

Adds a FROM clause to the query

static IQueryBuilder
join(array|string ...$data)

Adds a JOIN clause to the query

static IQueryBuilder
selfJoin(array|string ...$data)

Adds a SELF JOIN clause to the query

static IQueryBuilder
leftJoin(array|string ...$data)

Adds a LEFT JOIN clause to the query

static IQueryBuilder
rightJoin(array|string ...$data)

Adds a RIGHT JOIN clause to the query

static IQueryBuilder
innerJoin(array|string ...$data)

Adds a INNER JOIN clause to the query

static IQueryBuilder
outerJoin(array|string ...$data)

Adds a OUTER JOIN clause to the query

static IQueryBuilder
crossJoin(array|string ...$data)

Adds a CROSS JOIN clause to the query

static IQueryBuilder
on(array|string ...$data)

Adds a ON clause to the query

static IQueryBuilder
andOn(array|string ...$data)

Adds a AND ON clause to the query

static IQueryBuilder
orOn(array|string ...$data)

Adds a OR ON clause to the query

static IQueryBuilder
where(array|string ...$data)

Adds a WHERE clause to the query

static IQueryBuilder
andWhere(array|string ...$data)

Adds a AND WHERE clause to the query

static IQueryBuilder
orWhere(array|string ...$data)

Adds a OR WHERE clause to the query

static IQueryBuilder
having(array|string ...$data)

Adds a HAVING clause to the query

static IQueryBuilder
andHaving(array|string ...$data)

Adds a AND HAVING clause to the query

static IQueryBuilder
orHaving(array|string ...$data)

Adds a OR HAVING clause to the query

static IQueryBuilder
group(array|string ...$data)

Adds a GROUP clause to the query

static IQueryBuilder
order(array|string ...$data)

Adds a ORDER clause to the query

static IQueryBuilder
orderAsc(array|string ...$data)

Adds a GROUP ASC clause to the query

static IQueryBuilder
orderDesc(array|string ...$data)

Adds a ORDER DESC clause to the query

static IQueryBuilder
limit(array|string ...$data)

Adds a LIMIT clause to the query

string
build()

Summary of build

string
buildRaw()

Summary of buildRaw

array
getValues()

Returns the query values

object
getAllMetadata()

Returns the query metadata

mixed
fetch(int|null $fetchStyle = null, mixed $fetchArgument = null, mixed $optArgs = null)

Fetches a single row from the query result.

array|bool
fetchAll(int|null $fetchStyle = null, mixed $fetchArgument = null, mixed $optArgs = null)

Fetches all rows from the query result.

Details

static Singleton getInstance(string|null $hash = null)

Create or obtain an instance If no hash is provided, works as classic singleton If hash is provided, creates/returns instance for that hash

Parameters

string|null $hash

Optional unique identifier for multiple instances

Return Value

Singleton

static Singleton newInstance(string|null $hash = null)

Create a new instance of the class and set it as the instance

Parameters

string|null $hash

Optional unique identifier for multiple instances

Return Value

Singleton

The newly created instance

static void setInstance(Singleton $instance, string|null $hash = null)

Set a specific instance

Parameters

Singleton $instance

The instance to set

string|null $hash

Optional unique identifier for multiple instances

Return Value

void

static void clearInstance(string|null $hash = null)

Clear instance(s)

Parameters

string|null $hash

Optional unique identifier for multiple instances

Return Value

void

static void clearAllInstances()

Clear all instances including the classic singleton instance

Return Value

void

__construct(IConnection|null $context = null)

Initializes the query builder with a database connection context.

Parameters

IConnection|null $context

Exceptions

Exception

static QueryBuilder with(IConnection|null $context = null)

Static initializer that sets the database connection context and returns the query builder instance.

Parameters

IConnection|null $context

Return Value

QueryBuilder

Exceptions

Exception

void setStrategy(IQueryBuilder $strategy)

Sets the query strategy instance.

Parameters

IQueryBuilder $strategy

Return Value

void

IQueryBuilder getStrategy()

Returns the query strategy instance.

Return Value

IQueryBuilder

static IQueryBuilder select(array|string ...$data)

Adds a SELECT clause to the query

Parameters

array|string ...$data

The columns to select.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder distinct(array|string ...$data)

Adds a DISTINCT clause to the query

Parameters

array|string ...$data

The columns to select distinctly.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder from(array|string ...$data)

Adds a FROM clause to the query

Parameters

array|string ...$data

The table(s) to select from.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder join(array|string ...$data)

Adds a JOIN clause to the query

Parameters

array|string ...$data

The join clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder selfJoin(array|string ...$data)

Adds a SELF JOIN clause to the query

Parameters

array|string ...$data

The self join clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder leftJoin(array|string ...$data)

Adds a LEFT JOIN clause to the query

Parameters

array|string ...$data

The left join clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder rightJoin(array|string ...$data)

Adds a RIGHT JOIN clause to the query

Parameters

array|string ...$data

The right join clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder innerJoin(array|string ...$data)

Adds a INNER JOIN clause to the query

Parameters

array|string ...$data

The inner join clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder outerJoin(array|string ...$data)

Adds a OUTER JOIN clause to the query

Parameters

array|string ...$data

The outer join clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder crossJoin(array|string ...$data)

Adds a CROSS JOIN clause to the query

Parameters

array|string ...$data

The cross join clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder on(array|string ...$data)

Adds a ON clause to the query

Parameters

array|string ...$data

The ON clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder andOn(array|string ...$data)

Adds a AND ON clause to the query

Parameters

array|string ...$data

The AND ON clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder orOn(array|string ...$data)

Adds a OR ON clause to the query

Parameters

array|string ...$data

The OR ON clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder where(array|string ...$data)

Adds a WHERE clause to the query

Parameters

array|string ...$data

The WHERE clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder andWhere(array|string ...$data)

Adds a AND WHERE clause to the query

Parameters

array|string ...$data

The AND WHERE clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder orWhere(array|string ...$data)

Adds a OR WHERE clause to the query

Parameters

array|string ...$data

The OR WHERE clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder having(array|string ...$data)

Adds a HAVING clause to the query

Parameters

array|string ...$data

The HAVING clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder andHaving(array|string ...$data)

Adds a AND HAVING clause to the query

Parameters

array|string ...$data

The AND HAVING clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder orHaving(array|string ...$data)

Adds a OR HAVING clause to the query

Parameters

array|string ...$data

The OR HAVING clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder group(array|string ...$data)

Adds a GROUP clause to the query

Parameters

array|string ...$data

The GROUP BY clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder order(array|string ...$data)

Adds a ORDER clause to the query

Parameters

array|string ...$data

The ORDER BY clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder orderAsc(array|string ...$data)

Adds a GROUP ASC clause to the query

Parameters

array|string ...$data

The ORDER BY clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder orderDesc(array|string ...$data)

Adds a ORDER DESC clause to the query

Parameters

array|string ...$data

The ORDER BY clause parameters.

Return Value

IQueryBuilder

The query builder instance.

static IQueryBuilder limit(array|string ...$data)

Adds a LIMIT clause to the query

Parameters

array|string ...$data

The LIMIT clause parameters.

Return Value

IQueryBuilder

The query builder instance.

string build()

Summary of build

Return Value

string

The SQL query string.

string buildRaw()

Summary of buildRaw

Return Value

string

The raw SQL query string.

array getValues()

Returns the query values

Return Value

array

The bound values.

object getAllMetadata()

Returns the query metadata

Return Value

object

The query metadata.

mixed fetch(int|null $fetchStyle = null, mixed $fetchArgument = null, mixed $optArgs = null)

Fetches a single row from the query result.

Parameters

int|null $fetchStyle

The fetch style to use.

mixed $fetchArgument

The fetch argument to use.

mixed $optArgs

Additional options for the fetch operation.

Return Value

mixed

The query result.

array|bool fetchAll(int|null $fetchStyle = null, mixed $fetchArgument = null, mixed $optArgs = null)

Fetches all rows from the query result.

Parameters

int|null $fetchStyle

The fetch style to use.

mixed $fetchArgument

The fetch argument to use.

mixed $optArgs

Additional options for the fetch operation.

Return Value

array|bool

The query results or false on failure.