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
91091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022protected
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
;
}