From 16e14076213d1b4cf5e9a0d6c31a0ac0f6b80967 Mon Sep 17 00:00:00 2001 From: Christian Boltz Date: Tue, 1 Nov 2016 15:45:45 +0000 Subject: [PATCH] db_where_clause(): allow NULL and NOTNULL searchmodes git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1878 a1433add-5e2c-0410-b055-b7f2511e0802 --- functions.inc.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/functions.inc.php b/functions.inc.php index 5ff4ab32..e60a853a 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -1688,11 +1688,13 @@ function db_in_clause($field, $values) { * db_where_clause * Action: builds and returns a WHERE clause for database queries. All given conditions will be AND'ed. * Call: db_where_clause (array $conditions, array $struct) - * param array $conditios: array('field' => 'value', 'field2' => 'value2, ...) + * param array $condition: array('field' => 'value', 'field2' => 'value2, ...) * param array $struct - field structure, used for automatic bool conversion * param string $additional_raw_where - raw sniplet to include in the WHERE part - typically needs to start with AND * param array $searchmode - operators to use (=, <, > etc.) - defaults to = if not specified for a field (see * $allowed_operators for available operators) + * Note: the $searchmode operator will only be used if a $condition for that field is set. + * This also means you'll need to set a (dummy) condition for NULL and NOTNULL. */ function db_where_clause($condition, $struct, $additional_raw_where = '', $searchmode = array()) { if (!is_array($condition)) { @@ -1705,7 +1707,7 @@ function db_where_clause($condition, $struct, $additional_raw_where = '', $searc die('db_where_cond: parameter $struct is not an array!'); } - $allowed_operators = explode(' ', '< > >= <= = != <> CONT LIKE'); + $allowed_operators = explode(' ', '< > >= <= = != <> CONT LIKE NULL NOTNULL'); $where_parts = array(); $having_parts = array(); @@ -1726,7 +1728,15 @@ function db_where_clause($condition, $struct, $additional_raw_where = '', $searc die('db_where_clause: Invalid searchmode for ' . $field); } } - $querypart = $field . $operator . "'" . escape_string($value) . "'"; + + if ($operator == "NULL") { + $querypart = $field . ' IS NULL'; + } elseif ($operator == "NOTNULL") { + $querypart = $field . ' IS NOT NULL'; + } else { + $querypart = $field . $operator . "'" . escape_string($value) . "'"; + } + if($struct[$field]['select'] != '') { $having_parts[$field] = $querypart; } else {