选择语言 :

 Driver_Database_Driver_SQLite::_compile_conditions

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

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

参数列表

参数 类型 描述 默认值
$conditions object Database instance
$parameters array Condition statements
返回值
  • string
File: ./drivers/database/sqlite/sqlite.class.php
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
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;
}