Commit c566341d authored by Chris Graham's avatar Chris Graham
Browse files

Fixed MANTIS-4227 (Add support for database-abstracted IFF function)

parent a01499d5
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -18,6 +18,17 @@
 */
class database_misc_test_set extends cms_test_case
{
    public function testIFF()
    {
        $sql = 'SELECT ' . db_function('IFF', array('1=1', '2', '3'));
        $result = $GLOBALS['SITE_DB']->query_value_if_there($sql);
        $this->assertTrue($result === 2);

        $sql = 'SELECT ' . db_function('IFF', array('1=2', '2', '3'));
        $result = $GLOBALS['SITE_DB']->query_value_if_there($sql);
        $this->assertTrue($result === 3);
    }

    public function testCONCAT()
    {
        $sql = 'SELECT ' . db_function('CONCAT', array('\'a\'', '\'b\''));
+28 −1
Original line number Diff line number Diff line
@@ -465,7 +465,7 @@ function db_escape_string($string)
 * Basic arithmetic and inequality operators are assumed supported without needing a function.
 *
 * @param string $function Function name
 * @set CONCAT REPLACE SUBSTR LENGTH RAND COALESCE LEAST GREATEST MOD GROUP_CONCAT X_ORDER_BY_BOOLEAN
 * @set IFF CONCAT REPLACE SUBSTR LENGTH RAND COALESCE LEAST GREATEST MOD GROUP_CONCAT X_ORDER_BY_BOOLEAN
 * @param ?array $args List of string arguments, assumed already quoted/escaped correctly for the particular database (null: none)
 * @return string SQL fragment
 */
@@ -482,6 +482,33 @@ function db_function($function, $args = null)
    }

    switch ($function) {
        case 'IFF':
            /*
                         CASE construct   IF function   IIF function   IF construct

            mysql        Yes              Yes           No             IF...THEN...ELSEIF...ELSE...END IF
            postgresql   Yes              No            No             IF...THEN...ELSIF...ELSE...END IF
            sqlite       Yes              No            No             No
            oracle       Yes              No            Yes            IF...THEN...ELSIF...ELSE...END IF
            db2          Yes              No            No             IF...THEN...ELSEIF...ELSE...END IF
            access       No               No            Yes            No
            SQL server   Yes              No            Yes            IF...ELSE...

            Anything supporting CASE supports both simple and complex forms, as both are standardised.
            */
            switch (get_db_type()) {
                case 'mysql':
                case 'mysqli':
                case 'mysql_pdo':
                case 'mysql_dbx':
                    return 'IF(' . implode(',', $args) . ')';

                case 'postgresql':
                case 'sqlite':
                case 'db2':
                    return 'CASE WHEN ' . $args[0] . ' THEN ' . $args[1] . ' ELSE ' . $args[2] . ' END';
            }

        case 'CONCAT':
            switch (get_db_type()) {
                // Supported on most