QueryBuilder
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
Create or obtain an instance If no hash is provided, works as classic singleton If hash is provided, creates/returns instance for that hash
Create a new instance of the class and set it as the instance
Clear all instances including the classic singleton instance
Initializes the query builder with a database connection context.
Static initializer that sets the database connection context and returns the query builder instance.
Returns the query strategy instance.
Adds a SELECT clause to the query
Adds a DISTINCT clause to the query
Adds a FROM clause to the query
Adds a JOIN clause to the query
Adds a SELF JOIN clause to the query
Adds a LEFT JOIN clause to the query
Adds a RIGHT JOIN clause to the query
Adds a INNER JOIN clause to the query
Adds a OUTER JOIN clause to the query
Adds a CROSS JOIN clause to the query
Adds a ON clause to the query
Adds a AND ON clause to the query
Adds a OR ON clause to the query
Adds a WHERE clause to the query
Adds a AND WHERE clause to the query
Adds a OR WHERE clause to the query
Adds a HAVING clause to the query
Adds a AND HAVING clause to the query
Adds a OR HAVING clause to the query
Adds a GROUP clause to the query
Adds a ORDER clause to the query
Adds a GROUP ASC clause to the query
Adds a ORDER DESC clause to the query
Adds a LIMIT clause to the query
Summary of build
Summary of buildRaw
Returns the query values
Returns the query metadata
Fetches a single row from the query result.
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
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.
IQueryBuilder
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.