From fd5d527925e087196b1e788600c0a3697d6de0be Mon Sep 17 00:00:00 2001 From: stronk7 Date: Thu, 26 Jul 2007 17:45:08 +0000 Subject: [PATCH] Adding some sql_bitXXX() functions to be able to generate cross-db bitwise operations. Not fully tested yet but I need this in CVS to be able to test under Oracle. --- lib/dmllib.php | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/lib/dmllib.php b/lib/dmllib.php index 767b6927f6..c0a8633377 100644 --- a/lib/dmllib.php +++ b/lib/dmllib.php @@ -1809,6 +1809,86 @@ function sql_order_by_text($fieldname, $numchars=32) { } } +/** + * Returns the SQL text to be used in order to perform one bitwise AND operation + * between 2 integers. + * @param integer int1 first integer in the operation + * @param integer int2 second integer in the operation + * @return string the piece of SQL code to be used in your statement. + */ +function sql_bitand($int1, $int2) { + + global $CFG; + + switch ($CFG->dbfamily) { + case 'oracle': + return 'bitand(' . $int1 . ', ' . $int2 . ')'; + break; + default: + return $int1 . ' & ' . $int2; + } +} + +/** + * Returns the SQL text to be used in order to perform one bitwise OR operation + * between 2 integers. + * @param integer int1 first integer in the operation + * @param integer int2 second integer in the operation + * @return string the piece of SQL code to be used in your statement. + */ +function sql_bitor($int1, $int2) { + + global $CFG; + + switch ($CFG->dbfamily) { + case 'oracle': + return '(' . $int1 . ' + ' . $int2 . ' - ' . sql_bitand($int1, $int2) . ')'; + break; + default: + return $int1 . ' | ' . $int2; + } +} + +/** + * Returns the SQL text to be used in order to perform one bitwise XOR operation + * between 2 integers. + * @param integer int1 first integer in the operation + * @param integer int2 second integer in the operation + * @return string the piece of SQL code to be used in your statement. + */ +function sql_bitxor($int1, $int2) { + + global $CFG; + + switch ($CFG->dbfamily) { + case 'oracle': + return '(' . sql_bitor($int1, $int2) . ' - ' . sql_bitand($int1, $int2) . ')'; + break; + case 'postgres': + return $int1 . ' # ' . $int2; + default: + return $int1 . ' ^ ' . $int2; + } +} + +/** + * Returns the SQL text to be used in order to perform one bitwise NOT operation + * with 1 integer. + * @param integer int1 integer in the operation + * @return string the piece of SQL code to be used in your statement. + */ +function sql_bitnot($int1) { + + global $CFG; + + switch ($CFG->dbfamily) { + case 'oracle': + return '(' . '(0 - ' . $int1 . ') - 1' . ')'; + break; + default: + return ' ~' . $int1; + } +} /** * Returns SQL to be used as a subselect to find the primary role of users. -- 2.39.5