Piql
in package
Uses
InteractsWithTime
Pionia Query Builder Provides base-strategy to perform all database queries in all RDBMS
This should not be interacted with from outside the core-framework, use Porm instead!
Table of Contents
Properties
- $application : PioniaApplication
- Pionia application instance
- $error : string|null
- Error Message.
- $errorInfo : array<string|int, mixed>|null
- The array of error information.
- $pdo : PDO
- The PDO object.
- $prettyQuery : string|null
- Current readable Query
- $queryString : string
- The last query string was generated in test mode.
- $returnId : string
- The returned id for the insert.
- $type : string
- The type of database.
- $debugLogging : bool
- Determine should save debug logging.
- $debugLogs : array<string|int, mixed>
- The array of logs for debugging.
- $debugMode : bool
- Determine is in debug mode.
- $dsn : string
- The DSN connection string.
- $guid : int
- The unique global id.
- $logging : bool
- Determine should log the query or not.
- $logs : array<string|int, mixed>
- The array of logs.
- $prefix : string
- Table prefix.
- $statement : PDOStatement|null
- The PDO statement object.
- $testMode : bool
- Determine is in test mode.
Methods
- __construct() : mixed
- DatabaseConnector the database.
- action() : void
- Start a transaction.
- avg() : string
- avg() : string
- avg() : string|null
- Calculate the average value of the column.
- beginDebug() : void
- Enable debug logging mode.
- columnQuote() : string
- Quote column name for use in a query.
- count() : int
- count() : int|null
- Count the number of rows from the table.
- create() : PDOStatement|null
- Create a table.
- debug() : Db
- Enable debug mode and output readable statement string.
- debugLog() : void
- Disable debug logging and return all readable statements.
- delete() : PDOStatement|null
- Delete data from the table.
- drop() : PDOStatement|null
- Drop a table.
- exec() : PDOStatement|null
- Execute the raw statement.
- get() : mixed
- get() : mixed
- Get only one record from the table.
- has() : bool
- has() : bool
- Determine whether the target data existed from the table.
- id() : string|null
- Return the ID for the last inserted row.
- info() : array<string|int, mixed>
- Get information about the database connection.
- insert() : PDOStatement|null
- Insert one or more records into the table.
- last() : string|null
- Return the last performed statement.
- log() : array<string|int, string>
- Return all executed statements.
- max() : string
- max() : string
- max() : string|null
- Get the maximum value of the column.
- min() : string
- min() : string
- min() : string|null
- Get the minimum value of the column.
- query() : PDOStatement|null
- Execute customized raw statement.
- quote() : string
- Quote a string for use in a query.
- rand() : mixed
- rand() : array<string|int, mixed>
- Randomly fetch data from the table.
- raw() : Db::raw
- Build a raw object.
- replace() : PDOStatement|null
- Replace old data with a new one.
- select() : array<string|int, mixed>
- select() : array<string|int, mixed>
- select() : array<string|int, mixed>
- select() : array<string|int, mixed>
- select() : array<string|int, mixed>|null
- Select data from the table.
- sum() : string
- sum() : string
- sum() : string|null
- Calculate the total value of the column.
- tableQuote() : string
- Quote table name for use in a query.
- update() : PDOStatement|null
- Modify data from the table.
- availableAt() : int
- Get the "available at" UNIX timestamp.
- buildJoin() : string
- Build the join statement.
- buildRaw() : string|null
- Generate the actual query from the raw object.
- columnMap() : array<string|int, mixed>
- Mapping columns for the stack.
- columnPush() : string
- Build the statement part for the column stack.
- currentTime() : int
- Get the current system time as a UNIX timestamp.
- dataImplode() : string
- Implode the Where conditions.
- dataMap() : void
- Mapping the data from the table.
- generate() : string
- Generate readable statement.
- isJoin() : bool
- Determine the array with join syntax.
- isRaw() : bool
- Finds whether the object is raw.
- mapKey() : string
- Generate a new map key for the placeholder.
- parseDateInterval() : DateInterval|DateTimeInterface|int
- If the given value is an interval, convert it to a DateTime instance.
- runTimeForHumans() : string
- Given a start time, format the total run time for human readability.
- secondsUntil() : int
- Get the number of seconds until the given DateTime.
- selectContext() : string
- Build statement for the select query.
- typeMap() : array<string|int, mixed>
- Mapping the type name as PDO data type.
- whereClause() : string
- Build the where clause.
- aggregate() : string|null
- Build for the aggregate function.
- returningQuery() : PDOStatement|null
- Build and execute returning query.
Properties
$application
Pionia application instance
public
PioniaApplication
$application
$error
Error Message.
public
string|null
$error
= null
$errorInfo
The array of error information.
public
array<string|int, mixed>|null
$errorInfo
= null
$pdo
The PDO object.
public
PDO
$pdo
$prettyQuery
Current readable Query
public
string|null
$prettyQuery
= null
$queryString
The last query string was generated in test mode.
public
string
$queryString
$returnId
The returned id for the insert.
public
string
$returnId
= ''
$type
The type of database.
public
string
$type
$debugLogging
Determine should save debug logging.
protected
bool
$debugLogging
= false
$debugLogs
The array of logs for debugging.
protected
array<string|int, mixed>
$debugLogs
= []
$debugMode
Determine is in debug mode.
protected
bool
$debugMode
= false
$dsn
The DSN connection string.
protected
string
$dsn
$guid
The unique global id.
protected
int
$guid
= 0
$logging
Determine should log the query or not.
protected
bool
$logging
= false
$logs
The array of logs.
protected
array<string|int, mixed>
$logs
= []
$prefix
Table prefix.
protected
string
$prefix
$statement
The PDO statement object.
protected
PDOStatement|null
$statement
$testMode
Determine is in test mode.
protected
bool
$testMode
= false
Methods
__construct()
DatabaseConnector the database.
public
__construct(Connection $connection) : mixed
$database = new Db($app, [
// required
'type' => 'mysql',
'database' => 'name',
'host' => 'localhost',
'username' => 'your_username',
'password' => 'your_password',
// [optional]
'charset' => 'utf8mb4',
'port' => 3306,
'prefix' => 'PREFIX_'
]);
Parameters
- $connection : Connection
Tags
action()
Start a transaction.
public
action(callable $actions) : void
Parameters
- $actions : callable
Tags
avg()
public
avg(string $table, string $column) : string
Parameters
- $table : string
- $column : string
Return values
stringavg()
public
avg(string $table, string $column, array<string|int, mixed> $where) : string
Parameters
- $table : string
- $column : string
- $where : array<string|int, mixed>
Return values
stringavg()
Calculate the average value of the column.
public
avg(string $table, array<string|int, mixed> $join[, string $column = null ][, array<string|int, mixed> $where = null ]) : string|null
Parameters
- $table : string
- $join : array<string|int, mixed>
- $column : string = null
- $where : array<string|int, mixed> = null
Return values
string|nullbeginDebug()
Enable debug logging mode.
public
beginDebug() : void
Tags
columnQuote()
Quote column name for use in a query.
public
columnQuote(string $column) : string
Parameters
- $column : string
Return values
stringcount()
public
count(string $table, array<string|int, mixed> $where) : int
Parameters
- $table : string
- $where : array<string|int, mixed>
Return values
intcount()
Count the number of rows from the table.
public
count(string $table[, array<string|int, mixed> $join = null ][, string $column = null ][, array<string|int, mixed> $where = null ]) : int|null
Parameters
- $table : string
- $join : array<string|int, mixed> = null
- $column : string = null
- $where : array<string|int, mixed> = null
Return values
int|nullcreate()
Create a table.
public
create(string $table, array<string|int, mixed> $columns[, array<string|int, mixed> $options = null ]) : PDOStatement|null
Parameters
- $table : string
- $columns : array<string|int, mixed>
-
Columns definition.
- $options : array<string|int, mixed> = null
-
Additional table options for creating a table.
Return values
PDOStatement|nulldebug()
Enable debug mode and output readable statement string.
public
debug() : Db
Tags
Return values
DbdebugLog()
Disable debug logging and return all readable statements.
public
debugLog() : void
Tags
delete()
Delete data from the table.
public
delete(string $table, array<string|int, mixed>|Raw $where) : PDOStatement|null
Parameters
- $table : string
- $where : array<string|int, mixed>|Raw
Return values
PDOStatement|nulldrop()
Drop a table.
public
drop(string $table) : PDOStatement|null
Parameters
- $table : string
Return values
PDOStatement|nullexec()
Execute the raw statement.
public
exec(string $statement[, array<string|int, mixed> $map = [] ][, callable $callback = null ]) : PDOStatement|null
Parameters
- $statement : string
-
The SQL statement.
- $map : array<string|int, mixed> = []
-
The array of input parameters value for prepared statement.
- $callback : callable = null
Tags
Return values
PDOStatement|nullget()
public
get(string $table, array<string|int, mixed>|string $columns, array<string|int, mixed> $where) : mixed
Parameters
- $table : string
- $columns : array<string|int, mixed>|string
- $where : array<string|int, mixed>
get()
Get only one record from the table.
public
get(string $table[, array<string|int, mixed> $join = null ][, array<string|int, mixed>|string $columns = null ][, array<string|int, mixed> $where = null ]) : mixed
Parameters
- $table : string
- $join : array<string|int, mixed> = null
- $columns : array<string|int, mixed>|string = null
- $where : array<string|int, mixed> = null
has()
public
has(string $table, array<string|int, mixed> $where) : bool
Parameters
- $table : string
- $where : array<string|int, mixed>
Return values
boolhas()
Determine whether the target data existed from the table.
public
has(string $table, array<string|int, mixed> $join[, array<string|int, mixed> $where = null ]) : bool
Parameters
- $table : string
- $join : array<string|int, mixed>
- $where : array<string|int, mixed> = null
Return values
boolid()
Return the ID for the last inserted row.
public
id([string $name = null ]) : string|null
Parameters
- $name : string = null
Tags
Return values
string|nullinfo()
Get information about the database connection.
public
info() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>insert()
Insert one or more records into the table.
public
insert(string $table, array<string|int, mixed> $values[, string $primaryKey = null ]) : PDOStatement|null
Parameters
- $table : string
- $values : array<string|int, mixed>
- $primaryKey : string = null
Return values
PDOStatement|nulllast()
Return the last performed statement.
public
last() : string|null
Tags
Return values
string|nulllog()
Return all executed statements.
public
log() : array<string|int, string>
Tags
Return values
array<string|int, string>max()
public
max(string $table, string $column) : string
Parameters
- $table : string
- $column : string
Return values
stringmax()
public
max(string $table, string $column, array<string|int, mixed> $where) : string
Parameters
- $table : string
- $column : string
- $where : array<string|int, mixed>
Return values
stringmax()
Get the maximum value of the column.
public
max(string $table, array<string|int, mixed> $join[, string $column = null ][, array<string|int, mixed> $where = null ]) : string|null
Parameters
- $table : string
- $join : array<string|int, mixed>
- $column : string = null
- $where : array<string|int, mixed> = null
Return values
string|nullmin()
public
min(string $table, string $column) : string
Parameters
- $table : string
- $column : string
Return values
stringmin()
public
min(string $table, string $column, array<string|int, mixed> $where) : string
Parameters
- $table : string
- $column : string
- $where : array<string|int, mixed>
Return values
stringmin()
Get the minimum value of the column.
public
min(string $table, array<string|int, mixed> $join[, string $column = null ][, array<string|int, mixed> $where = null ]) : string|null
Parameters
- $table : string
- $join : array<string|int, mixed>
- $column : string = null
- $where : array<string|int, mixed> = null
Return values
string|nullquery()
Execute customized raw statement.
public
query(string $statement[, array<string|int, mixed> $map = [] ]) : PDOStatement|null
Parameters
- $statement : string
-
The raw SQL statement.
- $map : array<string|int, mixed> = []
-
The array of input parameters value for prepared statement.
Return values
PDOStatement|nullquote()
Quote a string for use in a query.
public
quote(string $string) : string
Parameters
- $string : string
Return values
stringrand()
public
rand(string $table, array<string|int, mixed>|string $column, array<string|int, mixed> $where) : mixed
Parameters
- $table : string
- $column : array<string|int, mixed>|string
- $where : array<string|int, mixed>
rand()
Randomly fetch data from the table.
public
rand(string $table[, array<string|int, mixed> $join = null ][, array<string|int, mixed>|string $columns = null ][, array<string|int, mixed> $where = null ]) : array<string|int, mixed>
Parameters
- $table : string
- $join : array<string|int, mixed> = null
- $columns : array<string|int, mixed>|string = null
- $where : array<string|int, mixed> = null
Return values
array<string|int, mixed>raw()
Build a raw object.
public
static raw(string $string[, array<string|int, mixed> $map = [] ]) : Db::raw
Parameters
- $string : string
-
The raw string.
- $map : array<string|int, mixed> = []
-
The array of mapping data for the raw string.
Return values
Db::rawreplace()
Replace old data with a new one.
public
replace(string $table, array<string|int, mixed> $columns[, array<string|int, mixed> $where = null ]) : PDOStatement|null
Parameters
- $table : string
- $columns : array<string|int, mixed>
- $where : array<string|int, mixed> = null
Return values
PDOStatement|nullselect()
public
select(string $table, array<string|int, mixed> $columns, callable $callback) : array<string|int, mixed>
Parameters
- $table : string
- $columns : array<string|int, mixed>
- $callback : callable
Return values
array<string|int, mixed>select()
public
select(string $table, array<string|int, mixed> $columns, array<string|int, mixed> $where, callable $callback) : array<string|int, mixed>
Parameters
- $table : string
- $columns : array<string|int, mixed>
- $where : array<string|int, mixed>
- $callback : callable
Return values
array<string|int, mixed>select()
public
select(string $table, array<string|int, mixed> $columns, array<string|int, mixed> $where) : array<string|int, mixed>
Parameters
- $table : string
- $columns : array<string|int, mixed>
- $where : array<string|int, mixed>
Return values
array<string|int, mixed>select()
public
select(string $table, array<string|int, mixed> $join, array<string|int, mixed> $columns, array<string|int, mixed> $where, callable $callback) : array<string|int, mixed>
Parameters
- $table : string
- $join : array<string|int, mixed>
- $columns : array<string|int, mixed>
- $where : array<string|int, mixed>
- $callback : callable
Return values
array<string|int, mixed>select()
Select data from the table.
public
select(string $table, array<string|int, mixed> $join[, array<string|int, mixed>|string $columns = null ][, array<string|int, mixed> $where = null ]) : array<string|int, mixed>|null
Parameters
- $table : string
- $join : array<string|int, mixed>
- $columns : array<string|int, mixed>|string = null
- $where : array<string|int, mixed> = null
Return values
array<string|int, mixed>|nullsum()
public
sum(string $table, string $column) : string
Parameters
- $table : string
- $column : string
Return values
stringsum()
public
sum(string $table, string $column, array<string|int, mixed> $where) : string
Parameters
- $table : string
- $column : string
- $where : array<string|int, mixed>
Return values
stringsum()
Calculate the total value of the column.
public
sum(string $table, array<string|int, mixed> $join[, string $column = null ][, array<string|int, mixed> $where = null ]) : string|null
Parameters
- $table : string
- $join : array<string|int, mixed>
- $column : string = null
- $where : array<string|int, mixed> = null
Return values
string|nulltableQuote()
Quote table name for use in a query.
public
tableQuote(string $table) : string
Parameters
- $table : string
Return values
stringupdate()
Modify data from the table.
public
update(string $table, array<string|int, mixed> $data[, array<string|int, mixed> $where = null ]) : PDOStatement|null
Parameters
- $table : string
- $data : array<string|int, mixed>
- $where : array<string|int, mixed> = null
Return values
PDOStatement|nullavailableAt()
Get the "available at" UNIX timestamp.
protected
availableAt([DateInterval|DateTimeInterface|int $delay = 0 ]) : int
Parameters
- $delay : DateInterval|DateTimeInterface|int = 0
Return values
intbuildJoin()
Build the join statement.
protected
buildJoin(string $table, array<string|int, mixed> $join, array<string|int, mixed> &$map) : string
Parameters
- $table : string
- $join : array<string|int, mixed>
- $map : array<string|int, mixed>
Return values
stringbuildRaw()
Generate the actual query from the raw object.
protected
buildRaw(mixed $raw, array<string|int, mixed> &$map) : string|null
Parameters
- $raw : mixed
- $map : array<string|int, mixed>
Return values
string|nullcolumnMap()
Mapping columns for the stack.
protected
columnMap(array<string|int, mixed>|string $columns, array<string|int, mixed> &$stack, bool $root) : array<string|int, mixed>
Parameters
- $columns : array<string|int, mixed>|string
- $stack : array<string|int, mixed>
- $root : bool
Return values
array<string|int, mixed>columnPush()
Build the statement part for the column stack.
protected
columnPush(array<string|int, mixed>|string &$columns, array<string|int, mixed> &$map, bool $root[, bool $isJoin = false ]) : string
Parameters
- $columns : array<string|int, mixed>|string
- $map : array<string|int, mixed>
- $root : bool
- $isJoin : bool = false
Return values
stringcurrentTime()
Get the current system time as a UNIX timestamp.
protected
currentTime() : int
Return values
intdataImplode()
Implode the Where conditions.
protected
dataImplode(array<string|int, mixed> $data, array<string|int, mixed> &$map, string $conjunctor) : string
Parameters
- $data : array<string|int, mixed>
- $map : array<string|int, mixed>
- $conjunctor : string
Return values
stringdataMap()
Mapping the data from the table.
protected
dataMap(array<string|int, mixed> $data, array<string|int, mixed> $columns, array<string|int, mixed> $columnMap, array<string|int, mixed> &$stack, bool $root[, array<string|int, mixed> &$result = null ]) : void
Parameters
- $data : array<string|int, mixed>
- $columns : array<string|int, mixed>
- $columnMap : array<string|int, mixed>
- $stack : array<string|int, mixed>
- $root : bool
- $result : array<string|int, mixed> = null
Tags
generate()
Generate readable statement.
protected
generate(string $statement, array<string|int, mixed> $map) : string
Parameters
- $statement : string
- $map : array<string|int, mixed>
Tags
Return values
stringisJoin()
Determine the array with join syntax.
protected
isJoin(mixed $join) : bool
Parameters
- $join : mixed
Return values
boolisRaw()
Finds whether the object is raw.
protected
isRaw(object $object) : bool
Parameters
- $object : object
Return values
boolmapKey()
Generate a new map key for the placeholder.
protected
mapKey() : string
Return values
stringparseDateInterval()
If the given value is an interval, convert it to a DateTime instance.
protected
parseDateInterval(DateInterval|DateTimeInterface|int $delay) : DateInterval|DateTimeInterface|int
Parameters
- $delay : DateInterval|DateTimeInterface|int
Return values
DateInterval|DateTimeInterface|intrunTimeForHumans()
Given a start time, format the total run time for human readability.
protected
runTimeForHumans(float $startTime[, float|null $endTime = null ]) : string
Parameters
- $startTime : float
- $endTime : float|null = null
Tags
Return values
stringsecondsUntil()
Get the number of seconds until the given DateTime.
protected
secondsUntil(DateInterval|DateTimeInterface|int $delay) : int
Parameters
- $delay : DateInterval|DateTimeInterface|int
Return values
intselectContext()
Build statement for the select query.
protected
selectContext(string $table, array<string|int, mixed> &$map, array<string|int, mixed>|string $join[, array<string|int, mixed>|string &$columns = null ][, array<string|int, mixed> $where = null ][, string $columnFn = null ]) : string
Parameters
- $table : string
- $map : array<string|int, mixed>
- $join : array<string|int, mixed>|string
- $columns : array<string|int, mixed>|string = null
- $where : array<string|int, mixed> = null
- $columnFn : string = null
Return values
stringtypeMap()
Mapping the type name as PDO data type.
protected
typeMap(mixed $value, string $type) : array<string|int, mixed>
Parameters
- $value : mixed
- $type : string
Return values
array<string|int, mixed>whereClause()
Build the where clause.
protected
whereClause(array<string|int, mixed>|null $where, array<string|int, mixed> &$map) : string
Parameters
- $where : array<string|int, mixed>|null
- $map : array<string|int, mixed>
Return values
stringaggregate()
Build for the aggregate function.
private
aggregate(string $type, string $table[, array<string|int, mixed> $join = null ][, string $column = null ][, array<string|int, mixed> $where = null ]) : string|null
Parameters
- $type : string
- $table : string
- $join : array<string|int, mixed> = null
- $column : string = null
- $where : array<string|int, mixed> = null
Return values
string|nullreturningQuery()
Build and execute returning query.
private
returningQuery(string $query, array<string|int, mixed> &$map, array<string|int, mixed> &$data) : PDOStatement|null
Parameters
- $query : string
- $map : array<string|int, mixed>
- $data : array<string|int, mixed>