Query
in package
implements
QueryInterface, Stringable
Query builder
Table of Contents
Interfaces
- QueryInterface
- Represents a single statement that can be executed against the database.
- Stringable
Properties
- $db : ConnectionInterface
- Pointer to database connection.
- $bindMarker : string
- Bind marker
- $binds : array<string|int, array{0: mixed, 1: bool}>
- The binds and their values used for binding.
- $endTime : float
- The end time in seconds with microseconds for when this query was executed.
- $errorCode : int
- The error code, if any.
- $errorString : string
- The error message, if any.
- $finalQueryString : string|null
- The final query string after binding, etc.
- $originalQueryString : string
- The query string, as provided by the user.
- $startTime : float|string
- The start time in seconds with microseconds for when this query was executed.
- $swappedQueryString : string|null
- The query string if table prefix has been swapped.
Methods
- __construct() : mixed
- __toString() : string
- Return text representation of the query
- debugToolbarDisplay() : string
- Returns string to display in debug toolbar
- getDuration() : string
- Returns the duration of this query during execution, or null if the query has not been executed yet.
- getErrorCode() : int
- Returns the error code created while executing this statement.
- getErrorMessage() : string
- Returns the error message created while executing this statement.
- getOriginalQuery() : string
- Returns the original SQL that was passed into the system.
- getQuery() : string
- Returns the final, processed query string after binding, etal has been performed.
- getStartTime() : float|string
- Returns the start time in seconds with microseconds.
- hasError() : bool
- Reports whether this statement created an error not.
- isWriteType() : bool
- Determines if the statement is a write-type query or not.
- setBinds() : $this
- Will store the variables to bind into the query later.
- setDuration() : self
- Records the execution time of the statement using microtime(true) for it's start and end values. If no end value is present, will use the current time to determine total duration.
- setError() : self
- Stores the error description that happened for this query.
- setQuery() : self
- Sets the raw query string to use for this statement.
- swapPrefix() : self
- Swaps out one table prefix for a new one.
- compileBinds() : void
- Escapes and inserts any binds into the finalQueryString property.
- matchNamedBinds() : string
- matchSimpleBinds() : string
- findCommentEnd() : int
- Returns the position just after the end of the block comment that starts at the given position.
- findPlaceholderAt() : string|null
- Returns the longest placeholder that starts at the given position, matching the strtr() behavior.
- findQuotedEnd() : int
- Returns the position just after the closing quote of the quoted string or identifier that starts at the given position.
- getCharLength() : int
- Returns the length in bytes of the character at the given position.
- getMultibyteCharPattern() : string|null
- Returns a pattern matching one multibyte character for connection charsets in which a trailing byte may be an ASCII byte, such as a backslash, or null when the charset does not need it.
- replaceNamedPlaceholders() : string
- Works like strtr(), but leaves quoted strings, quoted identifiers and comments untouched, so that values already rendered into the SQL (escaped values, compiled subqueries) are never treated as placeholders.
Properties
$db
Pointer to database connection.
public
ConnectionInterface
$db
Mainly for escaping features.
$bindMarker
Bind marker
protected
string
$bindMarker
= '?'
Character used to identify values in a prepared statement.
$binds
The binds and their values used for binding.
protected
array<string|int, array{0: mixed, 1: bool}>
$binds
= []
$endTime
The end time in seconds with microseconds for when this query was executed.
protected
float
$endTime
$errorCode
The error code, if any.
protected
int
$errorCode
$errorString
The error message, if any.
protected
string
$errorString
= ''
$finalQueryString
The final query string after binding, etc.
protected
string|null
$finalQueryString
$originalQueryString
The query string, as provided by the user.
protected
string
$originalQueryString
$startTime
The start time in seconds with microseconds for when this query was executed.
protected
float|string
$startTime
$swappedQueryString
The query string if table prefix has been swapped.
protected
string|null
$swappedQueryString
Methods
__construct()
public
__construct(ConnectionInterface $db) : mixed
Parameters
- $db : ConnectionInterface
__toString()
Return text representation of the query
public
__toString() : string
Return values
stringdebugToolbarDisplay()
Returns string to display in debug toolbar
public
debugToolbarDisplay() : string
Return values
stringgetDuration()
Returns the duration of this query during execution, or null if the query has not been executed yet.
public
getDuration([int $decimals = 6 ]) : string
Parameters
- $decimals : int = 6
-
The accuracy of the returned time.
Return values
stringgetErrorCode()
Returns the error code created while executing this statement.
public
getErrorCode() : int
Return values
intgetErrorMessage()
Returns the error message created while executing this statement.
public
getErrorMessage() : string
Return values
stringgetOriginalQuery()
Returns the original SQL that was passed into the system.
public
getOriginalQuery() : string
Return values
stringgetQuery()
Returns the final, processed query string after binding, etal has been performed.
public
getQuery() : string
Return values
stringgetStartTime()
Returns the start time in seconds with microseconds.
public
getStartTime([bool $returnRaw = false ][, int $decimals = 6 ]) : float|string
Parameters
- $returnRaw : bool = false
- $decimals : int = 6
Return values
float|stringhasError()
Reports whether this statement created an error not.
public
hasError() : bool
Return values
boolisWriteType()
Determines if the statement is a write-type query or not.
public
isWriteType() : bool
Return values
boolsetBinds()
Will store the variables to bind into the query later.
public
setBinds(array<string|int, mixed> $binds[, bool $setEscape = true ]) : $this
Parameters
- $binds : array<string|int, mixed>
- $setEscape : bool = true
Return values
$thissetDuration()
Records the execution time of the statement using microtime(true) for it's start and end values. If no end value is present, will use the current time to determine total duration.
public
setDuration(float $start[, float|null $end = null ]) : self
Parameters
- $start : float
- $end : float|null = null
Return values
selfsetError()
Stores the error description that happened for this query.
public
setError(int $code, string $error) : self
Parameters
- $code : int
- $error : string
Return values
selfsetQuery()
Sets the raw query string to use for this statement.
public
setQuery(string $sql[, mixed $binds = null ][, bool $setEscape = true ]) : self
Parameters
- $sql : string
- $binds : mixed = null
- $setEscape : bool = true
Return values
selfswapPrefix()
Swaps out one table prefix for a new one.
public
swapPrefix(string $orig, string $swap) : self
Parameters
- $orig : string
- $swap : string
Return values
selfcompileBinds()
Escapes and inserts any binds into the finalQueryString property.
protected
compileBinds() : void
Tags
matchNamedBinds()
protected
matchNamedBinds(string $sql, array<string|int, array{0: mixed, 1: bool}> $binds) : string
Parameters
- $sql : string
- $binds : array<string|int, array{0: mixed, 1: bool}>
Return values
stringmatchSimpleBinds()
protected
matchSimpleBinds(string $sql, array<string|int, array{0: mixed, 1: bool}> $binds, int $bindCount, int $ml) : string
Parameters
- $sql : string
- $binds : array<string|int, array{0: mixed, 1: bool}>
- $bindCount : int
- $ml : int
Return values
stringfindCommentEnd()
Returns the position just after the end of the block comment that starts at the given position.
private
findCommentEnd(string $sql, int $position, bool $nested) : int
Parameters
- $sql : string
- $position : int
- $nested : bool
Return values
intfindPlaceholderAt()
Returns the longest placeholder that starts at the given position, matching the strtr() behavior.
private
findPlaceholderAt(string $sql, int $position, array<string, string> $replacers, int $maxLength) : string|null
Parameters
- $sql : string
- $position : int
- $replacers : array<string, string>
- $maxLength : int
Return values
string|nullfindQuotedEnd()
Returns the position just after the closing quote of the quoted string or identifier that starts at the given position.
private
findQuotedEnd(string $sql, int $position, string $quote, bool $backslashEscapes[, string|null $multibyte = null ]) : int
Parameters
- $sql : string
- $position : int
- $quote : string
- $backslashEscapes : bool
- $multibyte : string|null = null
Return values
intgetCharLength()
Returns the length in bytes of the character at the given position.
private
getCharLength(string $sql, int $position, string $multibyte) : int
Parameters
- $sql : string
- $position : int
- $multibyte : string
Return values
intgetMultibyteCharPattern()
Returns a pattern matching one multibyte character for connection charsets in which a trailing byte may be an ASCII byte, such as a backslash, or null when the charset does not need it.
private
getMultibyteCharPattern(bool $isMySQL, bool $isPostgre) : string|null
Parameters
- $isMySQL : bool
- $isPostgre : bool
Return values
string|nullreplaceNamedPlaceholders()
Works like strtr(), but leaves quoted strings, quoted identifiers and comments untouched, so that values already rendered into the SQL (escaped values, compiled subqueries) are never treated as placeholders.
private
replaceNamedPlaceholders(string $sql, array<string, string> $replacers) : string
Parameters
- $sql : string
- $replacers : array<string, string>