Compiles an array of conditions into an SQL partial. Used for WHERE and HAVING.
string Driver_Database_Driver_MySQL::_compile_conditions( object $conditions , array $parameters )
参数列表
参数 类型 描述 默认值 $conditions
object
Database instance $parameters
array
Condition statements
string
11131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225protected
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
;
}