The ezcQueryExpression class is used to create database independent SQL expression.
The QueryExpression class is usually used through the 'expr' variable in one of the Select, Insert, Update or Delete classes.
Note that the methods for logical or and and are named lOr and lAnd respectively. This is because and and or are reserved names in PHP and can not be used in method names.
Source for this file: /Database/src/sqlabstraction/expression.php
Version: | //autogentag// |
Child Class | Description |
---|---|
ezcQueryExpressionOracle | The ezcQueryExpressionOracle class is used to create SQL expression for Oracle. |
ezcQueryExpressionPgsql | The ezcQueryExpressionPgsql class is used to create SQL expression for PostgreSQL. |
ezcQueryExpressionSqlite | The ezcQueryExpressionSqlite class is used to create SQL expression for SQLite. |
ezcQueryExpressionMssql | The ezcQueryExpressionMssql class is used to create SQL expression for SQL Server. |
protected PDO |
$db
A pointer to the database handler to use for this query. |
protected array(string=>string) |
$intervalMap
= array(
Contains an interval map from generic intervals to MySQL native intervals. |
protected boolean |
$quoteValues
= true
The flag that switch quoting mode for values provided by user in miscelaneous SQL functions. |
public ezcQueryExpression |
__construct(
$db
, [ $aliases
= array()] )
Constructs an empty ezcQueryExpression |
public string |
add(
$...
)
Returns the SQL to add values or expressions together. |
public string |
avg(
$column
)
Returns the average value of a column |
public string |
between(
$expression
, $value1
, $value2
)
Returns SQL that checks if an expression evaluates to a value between two values. |
public string |
bitAnd(
$value1
, $value2
)
Returns the SQL that performs the bitwise AND on two values. |
public string |
bitOr(
$value1
, $value2
)
Returns the SQL that performs the bitwise OR on two values. |
public string |
bitXor(
$value1
, $value2
)
Returns the SQL that performs the bitwise XOR on two values. |
public string |
ceil(
$number
)
Returns the SQL to calculate the next highest integer value from the number. |
public void |
concat(
$...
)
Returns a series of strings concatinated |
public string |
count(
$column
)
Returns the number of rows (without a NULL value) of a column |
public string |
dateAdd(
$column
, $expr
, $type
)
Returns the SQL that adds an interval to a timestamp value. |
public string |
dateExtract(
$column
, $type
)
Returns the SQL that extracts parts from a timestamp value. |
public string |
dateSub(
$column
, $expr
, $type
)
Returns the SQL that subtracts an interval from a timestamp value. |
public string |
div(
$...
)
Returns the SQL to divide values or expressions by eachother. |
public string |
eq(
$value1
, $value2
)
Returns the SQL to check if two values are equal. |
public string |
floor(
$number
)
Returns the SQL to calculate the next lowest integer value from the number. |
protected string |
getIdentifier(
$alias
)
Returns the correct identifier for the alias $alias. |
protected array(string) |
getIdentifiers(
$aliasList
)
Returns the correct identifiers for the aliases found in $aliases. |
public string |
gt(
$value1
, $value2
)
Returns the SQL to check if one value is greater than another value. |
public string |
gte(
$value1
, $value2
)
Returns the SQL to check if one value is greater than or equal to another value. |
public bool |
hasAliases(
)
Returns true if this object has aliases. |
public string |
in(
$column
, $...
)
Returns the SQL to check if a value is one in a set of given values.. |
public string |
isNull(
$expression
)
Returns SQL that checks if a expression is null. |
public string |
lAnd(
)
Returns the SQL to bind logical expressions together using a logical and. |
public string |
length(
$column
)
Returns the length of text field $column |
public void |
like(
$expression
, $pattern
)
Match a partial string in a column. |
public string |
lOr(
)
Returns the SQL to bind logical expressions together using a logical or. |
public string |
lower(
$value
)
Returns the SQL to change all characters to lowercase |
public string |
lt(
$value1
, $value2
)
Returns the SQL to check if one value is less than another value. |
public string |
lte(
$value1
, $value2
)
Returns the SQL to check if one value is less than or equal to another value. |
public string |
max(
$column
)
Returns the highest value of a column |
public string |
md5(
$column
)
Returns the md5 sum of $column. |
public string |
min(
$column
)
Returns the lowest value of a column |
public string |
mod(
$expression1
, $expression2
)
Returns the remainder of the division operation $expression1 / $expression2. |
public string |
mul(
$...
)
Returns the SQL to multiply values or expressions by eachother. |
public string |
neq(
$value1
, $value2
)
Returns the SQL to check if two values are unequal. |
public string |
not(
$expression
)
Returns the SQL for a logical not, negating the $expression. |
public string |
now(
)
Returns the current system date and time in the database internal format. |
public string |
position(
$substr
, $value
)
Returns the SQL to locate the position of the first occurrence of a substring |
public string |
round(
$column
, $decimals
)
Rounds a numeric field to the number of decimals specified. |
public string |
searchedCase(
)
Returns a searched CASE statement. |
public void |
setAliases(
$aliases
)
Sets the aliases $aliases for this object. |
public void |
setValuesQuoting(
$doQuoting
)
Sets the mode of quoting for parameters passed to SQL functions and operators. |
public string |
sub(
$...
)
Returns the SQL to subtract values or expressions from eachother. |
public string |
subString(
$value
, $from
, [ $len
= null] )
Returns part of a string. |
public string |
sum(
$column
)
Returns the total sum of a column |
public string |
unixTimestamp(
$column
)
Returns the SQL that converts a timestamp value to a unix timestamp. |
public string |
upper(
$value
)
Returns the SQL to change all characters to uppercase |
Constructs an empty ezcQueryExpression
Name | Type | Description |
---|---|---|
$db |
PDO | |
$aliases |
array(string=>string) |
Method | Description |
---|---|
ezcQueryExpressionOracle::__construct() |
Constructs an empty ezcQueryExpression |
ezcQueryExpressionPgsql::__construct() |
Constructs an pgsql expression object using the db $db. |
Returns the SQL to add values or expressions together.
add() accepts an arbitrary number of parameters. Each parameter must contain a value or an expression or an array with values or expressions.
Example:
Name | Type | Description |
---|---|---|
$... |
string|array(string) |
Type | Description |
---|---|
ezcDbAbstractionException |
if called with no parameters. |
Returns the average value of a column
Name | Type | Description |
---|---|---|
$column |
string | the column to use |
Returns SQL that checks if an expression evaluates to a value between two values.
The parameter $expression is checked if it is between $value1 and $value2.
Note: There is a slight difference in the way BETWEEN works on some databases. http://www.w3schools.com/sql/sql_between.asp. If you want complete database independence you should avoid using between().
Example:
Name | Type | Description |
---|---|---|
$expression |
string | the value to compare to |
$value1 |
string | the lower value to compare with |
$value2 |
string | the higher value to compare with |
Returns the SQL that performs the bitwise AND on two values.
Name | Type | Description |
---|---|---|
$value1 |
string | |
$value2 |
string |
Method | Description |
---|---|
ezcQueryExpressionOracle::bitAnd() |
Returns the SQL that performs the bitwise AND on two values. |
Returns the SQL that performs the bitwise OR on two values.
Name | Type | Description |
---|---|---|
$value1 |
string | |
$value2 |
string |
Method | Description |
---|---|
ezcQueryExpressionOracle::bitOr() |
Returns the SQL that performs the bitwise OR on two values. |
Returns the SQL that performs the bitwise XOR on two values.
Name | Type | Description |
---|---|---|
$value1 |
string | |
$value2 |
string |
Method | Description |
---|---|
ezcQueryExpressionOracle::bitXor() |
Returns the SQL that performs the bitwise XOR on two values. |
ezcQueryExpressionPgsql::bitXor() |
Returns the SQL that performs the bitwise XOR on two values. |
ezcQueryExpressionSqlite::bitXor() |
Returns the SQL that performs the bitwise XOR on two values. |
Returns the SQL to calculate the next highest integer value from the number.
Name | Type | Description |
---|---|---|
$number |
string |
Method | Description |
---|---|
ezcQueryExpressionMssql::ceil() |
Returns the SQL to calculate the next highest integer value from the number. |
Returns a series of strings concatinated
concat() accepts an arbitrary number of parameters. Each parameter must contain an expression or an array with expressions.
Name | Type | Description |
---|---|---|
$... |
string|array(string) | strings that will be concatinated. |
Method | Description |
---|---|
ezcQueryExpressionOracle::concat() |
Returns a series of strings concatinated |
ezcQueryExpressionPgsql::concat() |
Returns a series of strings concatinated |
ezcQueryExpressionMssql::concat() |
Returns a series of strings concatinated |
Returns the number of rows (without a NULL value) of a column
If a '*' is used instead of a column the number of selected rows is returned.
Name | Type | Description |
---|---|---|
$column |
string | the column to use |
Returns the SQL that adds an interval to a timestamp value.
Name | Type | Description |
---|---|---|
$column |
string | |
$expr |
numeric | |
$type |
string | one of SECOND, MINUTE, HOUR, DAY, MONTH, or YEAR |
Method | Description |
---|---|
ezcQueryExpressionOracle::dateAdd() |
Returns the SQL that adds an interval to a timestamp value. |
ezcQueryExpressionPgsql::dateAdd() |
Returns the SQL that adds an interval to a timestamp value. |
ezcQueryExpressionSqlite::dateAdd() |
Returns the SQL that adds an interval to a timestamp value. |
ezcQueryExpressionMssql::dateAdd() |
Returns the SQL that adds an interval to a timestamp value. |
Returns the SQL that extracts parts from a timestamp value.
Name | Type | Description |
---|---|---|
$column |
string | The column to operate on |
$type |
string | one of SECOND, MINUTE, HOUR, DAY, MONTH, or YEAR |
Method | Description |
---|---|
ezcQueryExpressionOracle::dateExtract() |
Returns the SQL that extracts parts from a timestamp value. |
ezcQueryExpressionPgsql::dateExtract() |
Returns the SQL that extracts parts from a timestamp value. |
ezcQueryExpressionSqlite::dateExtract() |
Returns the SQL that extracts parts from a timestamp value. |
ezcQueryExpressionMssql::dateExtract() |
Returns the SQL that extracts parts from a timestamp value from a column. |
Returns the SQL that subtracts an interval from a timestamp value.
Name | Type | Description |
---|---|---|
$column |
string | |
$expr |
numeric | |
$type |
string | one of SECOND, MINUTE, HOUR, DAY, MONTH, or YEAR |
Method | Description |
---|---|
ezcQueryExpressionOracle::dateSub() |
Returns the SQL that subtracts an interval from a timestamp value. |
ezcQueryExpressionPgsql::dateSub() |
Returns the SQL that subtracts an interval from a timestamp value. |
ezcQueryExpressionSqlite::dateSub() |
Returns the SQL that subtracts an interval from a timestamp value. |
ezcQueryExpressionMssql::dateSub() |
Returns the SQL that subtracts an interval from a timestamp value. |
Returns the SQL to divide values or expressions by eachother.
divide() accepts an arbitrary number of parameters. Each parameter must contain a value or an expression or an array with values or expressions.
Example:
Name | Type | Description |
---|---|---|
$... |
string|array(string) |
Type | Description |
---|---|
ezcDbAbstractionException |
if called with no parameters. |
Returns the SQL to check if two values are equal.
Example:
Name | Type | Description |
---|---|---|
$value1 |
string | logical expression to compare |
$value2 |
string | logical expression to compare with |
Returns the SQL to calculate the next lowest integer value from the number.
Name | Type | Description |
---|---|---|
$number |
string |
Returns the correct identifier for the alias $alias.
If the alias does not exists in the list of aliases it is returned unchanged.
Name | Type | Description |
---|---|---|
$alias |
string |
Returns the correct identifiers for the aliases found in $aliases.
This method is similar to getIdentifier except that it works on an array.
Name | Type | Description |
---|---|---|
$aliasList |
array(string) |
Returns the SQL to check if one value is greater than another value.
Example:
Name | Type | Description |
---|---|---|
$value1 |
string | logical expression to compare |
$value2 |
string | logical expression to compare with |
Returns the SQL to check if one value is greater than or equal to another value.
Example:
Name | Type | Description |
---|---|---|
$value1 |
string | logical expression to compare |
$value2 |
string | logical expression to compare with |
Returns true if this object has aliases.
Returns the SQL to check if a value is one in a set of given values..
in() accepts an arbitrary number of parameters. The first parameter must always specify the value that should be matched against. Successive parameters must contain a logical expression or an array with logical expressions. These expressions will be matched against the first parameter.
Example:
Optimization note: Call setQuotingValues( false ) before using in() with big lists of numeric parameters. This avoid redundant quoting of numbers in resulting SQL query and saves time of converting strings to numbers inside RDBMS.
Name | Type | Description |
---|---|---|
$column |
string | the value that should be matched against |
$... |
string|array(string) | values that will be matched against $column |
Type | Description |
---|---|
ezcQueryInvalidParameterException |
if the 2nd parameter is an empty array. |
ezcQueryVariableParameterException |
if called with less than two parameters. |
Method | Description |
---|---|
ezcQueryExpressionOracle::in() |
Returns the SQL to check if a value is one in a set of given values. |
Returns SQL that checks if a expression is null.
Example:
Name | Type | Description |
---|---|---|
$expression |
string | the expression that should be compared to null |
Returns the SQL to bind logical expressions together using a logical and.
lAnd() accepts an arbitrary number of parameters. Each parameter must contain a logical expression or an array with logical expressions.
Example:
Type | Description |
---|---|
ezcDbAbstractionException |
if called with no parameters. |
Returns the length of text field $column
Name | Type | Description |
---|---|---|
$column |
string |
Method | Description |
---|---|
ezcQueryExpressionMssql::length() |
Returns the length of a text field. |
Match a partial string in a column.
Like will look for the pattern in the column given. Like accepts the wildcards '_' matching a single character and '%' matching any number of characters.
Name | Type | Description |
---|---|---|
$expression |
string | the name of the expression to match on |
$pattern |
string | the pattern to match with. |
Returns the SQL to bind logical expressions together using a logical or.
lOr() accepts an arbitrary number of parameters. Each parameter must contain a logical expression or an array with logical expressions.
Example:
Type | Description |
---|---|
ezcDbAbstractionException |
if called with no parameters. |
Returns the SQL to change all characters to lowercase
Name | Type | Description |
---|---|---|
$value |
string |
Returns the SQL to check if one value is less than another value.
Example:
Name | Type | Description |
---|---|---|
$value1 |
string | logical expression to compare |
$value2 |
string | logical expression to compare with |
Returns the SQL to check if one value is less than or equal to another value.
Example:
Name | Type | Description |
---|---|---|
$value1 |
string | logical expression to compare |
$value2 |
string | logical expression to compare with |
Returns the highest value of a column
Name | Type | Description |
---|---|---|
$column |
string | the column to use |
Returns the md5 sum of $column.
Note: Not SQL92, but common functionality
Name | Type | Description |
---|---|---|
$column |
string |
Method | Description |
---|---|
ezcQueryExpressionPgsql::md5() |
Returns the md5 sum of the field $column. |
ezcQueryExpressionMssql::md5() |
Returns the md5 sum of a field. |
Returns the lowest value of a column
Name | Type | Description |
---|---|---|
$column |
string | the column to use |
Returns the remainder of the division operation $expression1 / $expression2.
Name | Type | Description |
---|---|---|
$expression1 |
string | |
$expression2 |
string |
Method | Description |
---|---|
ezcQueryExpressionMssql::mod() |
Returns the remainder of the division operation $expression1 / $expression2. |
Returns the SQL to multiply values or expressions by eachother.
multiply() accepts an arbitrary number of parameters. Each parameter must contain a value or an expression or an array with values or expressions.
Example:
Name | Type | Description |
---|---|---|
$... |
string|array(string) |
Type | Description |
---|---|
ezcDbAbstractionException |
if called with no parameters. |
Returns the SQL to check if two values are unequal.
Example:
Name | Type | Description |
---|---|---|
$value1 |
string | logical expression to compare |
$value2 |
string | logical expression to compare with |
Returns the SQL for a logical not, negating the $expression.
Example:
Name | Type | Description |
---|---|---|
$expression |
string |
Returns the current system date and time in the database internal format.
Method | Description |
---|---|
ezcQueryExpressionOracle::now() |
Returns the current system date and time in the database internal format. |
ezcQueryExpressionPgsql::now() |
Returns the current system date and time in the database internal format. |
ezcQueryExpressionSqlite::now() |
Returns the current system date and time in the database internal format. |
ezcQueryExpressionMssql::now() |
Returns the current system date and time in the database internal format. |
Returns the SQL to locate the position of the first occurrence of a substring
Name | Type | Description |
---|---|---|
$substr |
string | |
$value |
string |
Method | Description |
---|---|
ezcQueryExpressionOracle::position() |
Returns the SQL to locate the position of the first occurrence of a substring |
ezcQueryExpressionPgsql::position() |
Returns the SQL to locate the position of the first occurrence of a substring |
ezcQueryExpressionMssql::position() |
Returns the SQL to locate the position of the first occurrence of a substring |
Rounds a numeric field to the number of decimals specified.
Name | Type | Description |
---|---|---|
$column |
string | |
$decimals |
int |
Returns a searched CASE statement.
Accepts an arbitrary number of parameters. The first parameter (array) must always be specified, the last parameter (string) specifies the ELSE result.
Example:
Type | Description |
---|---|
ezcQueryVariableParameterException |
Sets the aliases $aliases for this object.
The aliases can be used to substitute the column and table names with more friendly names. E.g PersistentObject uses it to allow using property and class names instead of column and table names.
Name | Type | Description |
---|---|---|
$aliases |
array(string=>string) |
Sets the mode of quoting for parameters passed to SQL functions and operators.
Quoting mode is set to ON by default. $q->expr->in( 'column1', 'Hello', 'world' ) will produce SQL "column1 IN ( 'Hello', 'world' )" ( note quotes in SQL ).
User must execute setValuesQuoting( false ) before call to function where quoting of parameters is not desirable. Example:
Quoting mode will remain unchanged until next call to setValuesQuoting().
Name | Type | Description |
---|---|---|
$doQuoting |
boolean | - flag that switch quoting. |
Returns the SQL to subtract values or expressions from eachother.
subtract() accepts an arbitrary number of parameters. Each parameter must contain a value or an expression or an array with values or expressions.
Example:
Name | Type | Description |
---|---|---|
$... |
string|array(string) |
Type | Description |
---|---|
ezcDbAbstractionException |
if called with no parameters. |
Returns part of a string.
Note: Not SQL92, but common functionality.
Name | Type | Description |
---|---|---|
$value |
string | the target $value the string or the string column. |
$from |
int | extract from this characeter. |
$len |
int | extract this amount of characters. |
Method | Description |
---|---|
ezcQueryExpressionOracle::subString() |
Returns part of a string. |
ezcQueryExpressionPgsql::subString() |
Returns part of a string. |
ezcQueryExpressionSqlite::subString() |
Returns part of a string. |
ezcQueryExpressionMssql::subString() |
Returns part of a string. |
Returns the total sum of a column
Name | Type | Description |
---|---|---|
$column |
string | the column to use |
Returns the SQL that converts a timestamp value to a unix timestamp.
Name | Type | Description |
---|---|---|
$column |
string |
Method | Description |
---|---|
ezcQueryExpressionOracle::unixTimestamp() |
Returns the SQL that converts a timestamp value to a unix timestamp. |
ezcQueryExpressionPgsql::unixTimestamp() |
Returns the SQL that converts a timestamp value to a unix timestamp. |
ezcQueryExpressionSqlite::unixTimestamp() |
Returns the SQL that converts a timestamp value to a unix timestamp. |
ezcQueryExpressionMssql::unixTimestamp() |
Returns the SQL that converts a timestamp value to number of seconds since 1970-01-01 00:00:00-00. |
Returns the SQL to change all characters to uppercase
Name | Type | Description |
---|---|---|
$value |
string |