Gdn_SQLDriver::conditionExpr PHP Method

conditionExpr() public method

Returns a single Condition Expression for use in a 'where' or an 'on' clause.
public conditionExpr ( string $Field, mixed $Value, $EscapeFieldSql = true, $EscapeValueSql = true ) : string
$Field string The name of the field on the left hand side of the expression. If $Field ends with an operator, then it used for the comparison. Otherwise '=' will be used.
$Value mixed The value on the right side of the expression. If $EscapeValueSql is true then it will end up in a parameter. Syntax The $Field and Value expressions can begin with special characters to do certain things.
  • =: This means that the argument is a function call. If you want to pass field reference arguments into the function then enclose them in square brackets. ex. '=LEFT([u.Name], 4)' will call the LEFT database function on the u.Name column.
  • @: This means that the argument is a literal. This is useful for passing in literal numbers.
  • no prefix>: This will treat the argument differently depending on the argument. - $Field - The argument is a column reference. - $Value - The argument will become a named parameter.
return string The single expression.
    public function conditionExpr($Field, $Value, $EscapeFieldSql = true, $EscapeValueSql = true)
    {
        // Change some variables from the old parameter style to the new one.
        // THIS PART OF THE FUNCTION SHOULD EVENTUALLY BE REMOVED.
        if ($EscapeFieldSql === false) {
            $Field = '@' . $Field;
        }
        if (is_array($Value)) {
            throw new Exception('Gdn_SQL->ConditionExpr(VALUE, ARRAY) is not supported.', 500);
        } elseif (!$EscapeValueSql && !is_null($Value)) {
            $Value = '@' . $Value;
        }
        // Check for a straight literal field expression.
        if (!$EscapeFieldSql && !$EscapeValueSql && is_null($Value)) {
            return substr($Field, 1);
            // warning: might not be portable across different drivers
        }
        $Expr = '';
        // final expression which is built up
        $Op = '';
        // logical operator
        // Try and split an operator out of $Field.
        $FieldOpRegex = "/(?:\\s*(=|<>|>|<|>=|<=)\\s*\$)|\\s+(like|not\\s+like)\\s*\$|\\s+(?:(is)\\s+(null)|(is\\s+not)\\s+(null))\\s*\$/i";
        $Split = preg_split($FieldOpRegex, $Field, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
        if (count($Split) > 1) {
            $Field = $Split[0];
            $Op = strtolower($Split[1]);
            if (count($Split) > 2) {
                $Value = null;
            }
        } else {
            $Op = '=';
        }
        if ($Op == '=' && is_null($Value)) {
            // This is a special case where the value SQL is checking for an is null operation.
            $Op = 'is';
            $Value = '@null';
            $EscapeValueSql = false;
        }
        // Add the left hand side of the expression.
        $Expr .= $this->_parseExpr($Field, null, $EscapeFieldSql);
        // Add the expression operator.
        $Expr .= ' ' . $Op . ' ';
        if ($Op == 'is' || $Op == 'is not' && is_null($Value)) {
            $Expr .= 'null';
        } else {
            // Add the right side of the expression.
            $Expr .= $this->_parseExpr($Value, $Field, $EscapeValueSql);
        }
        return $Expr;
    }