From 51adead13f87e221cc3ae37e5fd450b7d0c2a1be Mon Sep 17 00:00:00 2001 From: stronk7 Date: Mon, 3 Dec 2007 20:30:41 +0000 Subject: [PATCH] Adding two new functions: sql_isempty() and sql_isnotempty() to perform SQL comparisons to empties in a cross-db way. General solution for problems like MDL-12084 Merged from MOODLE_19_STABLE --- lib/dmllib.php | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/lib/dmllib.php b/lib/dmllib.php index ccb5c7a607..b9d5c3310d 100644 --- a/lib/dmllib.php +++ b/lib/dmllib.php @@ -1812,6 +1812,94 @@ function sql_concat_join($separator="' '", $elements=array()) { return call_user_func_array(array($db, 'Concat'), $elem); } +/** + * Returns the proper SQL to know if one field is empty. + * + * Note that the function behavior strongly relies on the + * parameters passed describing the field so, please, be accurate + * when speciffying them. + * + * Also, note that this function is not suitable to look for + * fields having NULL contents at all. It's all for empty values! + * + * This function should be applied in all the places where conditins of + * the type: + * + * ... AND fieldname = ''; + * + * are being used. Final result should be: + * + * ... AND ' . sql_isempty('tablename', 'fieldname', true/false, true/false); + * + * (see parameters description below) + * + * @param string $tablename name of the table (without prefix). Not used for now but can be + * necessary in the future if we want to use some introspection using + * meta information against the DB. /// TODO /// + * @param string $fieldname name of the field we are going to check + * @param boolean $nullablefield to specify if the field us nullable (true) or no (false) in the DB + * @param boolean $textfield to specify if it is a text (also called clob) field (true) or a varchar one (false) + * @return string the sql code to be added to check for empty values + */ +function sql_isempty($tablename, $fieldname, $nullablefield, $textfield) { + + global $CFG; + + $sql = $fieldname . " = '' "; + + switch ($CFG->dbfamily) { + case 'mssql': + if ($textfield) { + $sql = sql_compare_text($fieldname) . " = '' "; + } + break; + case 'oracle': + if ($nullablefield) { + $sql = $fieldname . " IS NULL "; /// empties in nullable fields are stored as + } else { /// NULLs + if ($textfield) { + $sql = sql_compare_text($fieldname) . " = ' ' "; /// oracle_dirty_hack inserts 1-whitespace + } else { /// in NOT NULL varchar and text columns so + $sql = $fieldname . " = ' ' "; /// we need to look for that in any situation + } + } + break; + } + + return $sql; +} + +/** + * Returns the proper SQL to know if one field is not empty. + * + * Note that the function behavior strongly relies on the + * parameters passed describing the field so, please, be accurate + * when speciffying them. + * + * This function should be applied in all the places where conditions of + * the type: + * + * ... AND fieldname != ''; + * + * are being used. Final result should be: + * + * ... AND ' . sql_isnotempty('tablename', 'fieldname', true/false, true/false); + * + * (see parameters description below) + * + * @param string $tablename name of the table (without prefix). Not used for now but can be + * necessary in the future if we want to use some introspection using + * meta information against the DB. /// TODO /// + * @param string $fieldname name of the field we are going to check + * @param boolean $nullablefield to specify if the field us nullable (true) or no (false) in the DB + * @param boolean $textfield to specify if it is a text (also called clob) field (true) or a varchar one (false) + * @return string the sql code to be added to check for non empty values + */ +function sql_isnotempty($tablename, $fieldname, $nullablefield, $textfield) { + + return '( NOT ' . sql_isempty($tablename, $fieldname, $nullablefield, $textfield) . ')'; +} + /** * Returns the proper SQL to do IS NULL * @uses $CFG -- 2.39.5