选择语言 :

 Driver_Database_Driver_Postgre::_compile_conditions

Compiles an array of conditions into an SQL partial. Used for WHERE and HAVING.

string Driver_Database_Driver_Postgre::_compile_conditions( object $conditions , array $parameters )

参数列表

参数 类型 描述 默认值
$conditions object Database instance
$parameters array Condition statements
返回值
  • string
File: ./drivers/database/postgre/postgre.class.php
protected function _compile_conditions(array $conditions, $parameters)
{
    $last_condition = null;

    $sql = '';
    foreach ($conditions as $group)
    {
        // Process groups of conditions
        foreach ($group as $logic => $condition)
        {
            if ($condition === '(')
            {
                if (!empty($sql) && $last_condition !== '(')
                {
                    // Include logic operator
                    $sql .= ' ' . $logic . ' ';
                }

                $sql .= '(';
            }
            elseif ($condition === ')')
            {
                $sql .= ')';
            }
            else
            {
                if (!empty($sql) && $last_condition !== '(')
                {
                    // Add the logic operator
                    $sql .= ' ' . $logic . ' ';
                }

                // Split the condition
                list ($column, $op, $value) = $condition;

                if ($value === null)
                {
                    if ($op === '=')
                    {
                        // Convert "val = NULL" to "val IS NULL"
                        $op = 'IS';
                    }
                    elseif ($op === '!=' || $op === '<>')
                    {
                        // Convert "val != NULL" to "valu IS NOT NULL"
                        $op = 'IS NOT';
                    }
                }

                // Database operators are always uppercase
                $op = strtoupper($op);

                if (is_array($value) && count($value)<=1)
                {
                    # 将in条件下只有1条数据的改为where方式
                    if ($op == 'IN')
                    {
                        $op = '=';
                        $value = current($value);
                    }
                    elseif ($op == 'NOT IN')
                    {
                        $op = '!=';
                        $value = current($value);
                    }
                }

                if ($op === 'BETWEEN' && is_array($value))
                {
                    // BETWEEN always has exactly two arguments
                    list ($min, $max) = $value;

                    if (is_string($min) && array_key_exists($min, $parameters))
                    {
                        // Set the parameter as the minimum
                        $min = $parameters[$min];
                    }

                    if (is_string($max) && array_key_exists($max, $parameters))
                    {
                        // Set the parameter as the maximum
                        $max = $parameters[$max];
                    }

                    // Quote the min and max value
                    $value = $this->quote($min) . ' AND ' . $this->quote($max);
                }
                elseif ($op == 'MOD')
                {
                    $value = $this->quote($value[0]) .' '.strtoupper($value[2]).' '. $this->quote($value[1]);
                }
                else
                {
                    if (is_string($value) && array_key_exists($value, $parameters))
                    {
                        // Set the parameter as the value
                        $value = $parameters[$value];
                    }

                    // Quote the entire value normally
                    $value = $this->quote($value);
                }

                // Append the statement to the query
                $sql .= $this->_quote_identifier($column) . ' ' . $op . ' ' . $value;
            }

            $last_condition = $condition;
        }
    }

    return $sql;
}