/**
  * Implodes an array of WHERE clause configuration into a WHERE clause.
  *
  * DBAL-specific: The only(!) handled "calc" operators supported by parseWhereClause() are:
  * - the bitwise logical and (&)
  * - the addition (+)
  * - the substraction (-)
  * - the multiplication (*)
  * - the division (/)
  * - the modulo (%)
  *
  * @param array WHERE clause configuration
  * @return string WHERE clause as string.
  * @see	t3lib_sqlparser::parseWhereClause()
  */
 public function compileWhereClause($clauseArray, $functionMapping = TRUE)
 {
     switch ((string) $GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['type']) {
         case 'native':
             $output = parent::compileWhereClause($clauseArray);
             break;
         case 'adodb':
             // Prepare buffer variable:
             $output = '';
             // Traverse clause array:
             if (is_array($clauseArray)) {
                 foreach ($clauseArray as $k => $v) {
                     // Set operator:
                     $output .= $v['operator'] ? ' ' . $v['operator'] : '';
                     // Look for sublevel:
                     if (is_array($v['sub'])) {
                         $output .= ' (' . trim($this->compileWhereClause($v['sub'], $functionMapping)) . ')';
                     } elseif (isset($v['func']) && $v['func']['type'] === 'EXISTS') {
                         $output .= ' ' . trim($v['modifier']) . ' EXISTS (' . $this->compileSELECT($v['func']['subquery']) . ')';
                     } else {
                         if (isset($v['func']) && $v['func']['type'] === 'LOCATE') {
                             $output .= ' ' . trim($v['modifier']);
                             switch (TRUE) {
                                 case $GLOBALS['TYPO3_DB']->runningADOdbDriver('mssql') && $functionMapping:
                                     $output .= ' CHARINDEX(';
                                     $output .= $v['func']['substr'][1] . $v['func']['substr'][0] . $v['func']['substr'][1];
                                     $output .= ', ' . ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                                     $output .= isset($v['func']['pos']) ? ', ' . $v['func']['pos'][0] : '';
                                     $output .= ')';
                                     break;
                                 case $GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8') && $functionMapping:
                                     $output .= ' INSTR(';
                                     $output .= ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                                     $output .= ', ' . $v['func']['substr'][1] . $v['func']['substr'][0] . $v['func']['substr'][1];
                                     $output .= isset($v['func']['pos']) ? ', ' . $v['func']['pos'][0] : '';
                                     $output .= ')';
                                     break;
                                 default:
                                     $output .= ' LOCATE(';
                                     $output .= $v['func']['substr'][1] . $v['func']['substr'][0] . $v['func']['substr'][1];
                                     $output .= ', ' . ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                                     $output .= isset($v['func']['pos']) ? ', ' . $v['func']['pos'][0] : '';
                                     $output .= ')';
                                     break;
                             }
                         } elseif (isset($v['func']) && $v['func']['type'] === 'IFNULL') {
                             $output .= ' ' . trim($v['modifier']) . ' ';
                             switch (TRUE) {
                                 case $GLOBALS['TYPO3_DB']->runningADOdbDriver('mssql') && $functionMapping:
                                     $output .= 'ISNULL';
                                     break;
                                 case $GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8') && $functionMapping:
                                     $output .= 'NVL';
                                     break;
                                 default:
                                     $output .= 'IFNULL';
                                     break;
                             }
                             $output .= '(';
                             $output .= ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                             $output .= ', ' . $v['func']['default'][1] . $this->compileAddslashes($v['func']['default'][0]) . $v['func']['default'][1];
                             $output .= ')';
                         } else {
                             // Set field/table with modifying prefix if any:
                             $output .= ' ' . trim($v['modifier']) . ' ';
                             // DBAL-specific: Set calculation, if any:
                             if ($v['calc'] === '&' && $functionMapping) {
                                 switch (TRUE) {
                                     case $GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8'):
                                         // Oracle only knows BITAND(x,y) - sigh
                                         $output .= 'BITAND(' . trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']) . ',' . $v['calc_value'][1] . $this->compileAddslashes($v['calc_value'][0]) . $v['calc_value'][1] . ')';
                                         break;
                                     default:
                                         // MySQL, MS SQL Server, PostgreSQL support the &-syntax
                                         $output .= trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']) . $v['calc'] . $v['calc_value'][1] . $this->compileAddslashes($v['calc_value'][0]) . $v['calc_value'][1];
                                         break;
                                 }
                             } elseif ($v['calc']) {
                                 $output .= trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']) . $v['calc'];
                                 if (isset($v['calc_table'])) {
                                     $output .= trim(($v['calc_table'] ? $v['calc_table'] . '.' : '') . $v['calc_field']);
                                 } else {
                                     $output .= $v['calc_value'][1] . $this->compileAddslashes($v['calc_value'][0]) . $v['calc_value'][1];
                                 }
                             } elseif (!($GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8') && $v['comparator'] === 'LIKE' && $functionMapping)) {
                                 $output .= trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']);
                             }
                         }
                         // Set comparator:
                         if ($v['comparator']) {
                             switch (TRUE) {
                                 case $GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8') && $v['comparator'] === 'LIKE' && $functionMapping:
                                     // Oracle cannot handle LIKE on CLOB fields - sigh
                                     if (isset($v['value']['operator'])) {
                                         $values = array();
                                         foreach ($v['value']['args'] as $fieldDef) {
                                             $values[] = ($fieldDef['table'] ? $fieldDef['table'] . '.' : '') . $fieldDef['field'];
                                         }
                                         $compareValue = ' ' . $v['value']['operator'] . '(' . implode(',', $values) . ')';
                                     } else {
                                         $compareValue = $v['value'][1] . $this->compileAddslashes(trim($v['value'][0], '%')) . $v['value'][1];
                                     }
                                     // To be on the safe side
                                     $isLob = TRUE;
                                     if ($v['table']) {
                                         // Table and field names are quoted:
                                         $tableName = substr($v['table'], 1, strlen($v['table']) - 2);
                                         $fieldName = substr($v['field'], 1, strlen($v['field']) - 2);
                                         $fieldType = $GLOBALS['TYPO3_DB']->sql_field_metatype($tableName, $fieldName);
                                         $isLob = $fieldType === 'B' || $fieldType === 'XL';
                                     }
                                     if ($isLob) {
                                         $output .= '(dbms_lob.instr(' . trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']) . ', ' . $compareValue . ',1,1) > 0)';
                                     } else {
                                         $output .= '(instr(' . trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']) . ', ' . $compareValue . ',1,1) > 0)';
                                     }
                                     break;
                                 default:
                                     $output .= ' ' . $v['comparator'];
                                     // Detecting value type; list or plain:
                                     if (t3lib_div::inList('NOTIN,IN', strtoupper(str_replace(array(' ', "\t", "\r", "\n"), '', $v['comparator'])))) {
                                         if (isset($v['subquery'])) {
                                             $output .= ' (' . $this->compileSELECT($v['subquery']) . ')';
                                         } else {
                                             $valueBuffer = array();
                                             foreach ($v['value'] as $realValue) {
                                                 $valueBuffer[] = $realValue[1] . $this->compileAddslashes($realValue[0]) . $realValue[1];
                                             }
                                             $output .= ' (' . trim(implode(',', $valueBuffer)) . ')';
                                         }
                                     } else {
                                         if (isset($v['value']['operator'])) {
                                             $values = array();
                                             foreach ($v['value']['args'] as $fieldDef) {
                                                 $values[] = ($fieldDef['table'] ? $fieldDef['table'] . '.' : '') . $fieldDef['field'];
                                             }
                                             $output .= ' ' . $v['value']['operator'] . '(' . implode(',', $values) . ')';
                                         } else {
                                             $output .= ' ' . $v['value'][1] . $this->compileAddslashes($v['value'][0]) . $v['value'][1];
                                         }
                                     }
                                     break;
                             }
                         }
                     }
                 }
             }
             break;
     }
     return $output;
 }
 /**
  * Implodes an array of WHERE clause configuration into a WHERE clause.
  *
  * DBAL-specific: The only(!) handled "calc" operators supported by parseWhereClause() are:
  * - the bitwise logical and (&)
  * - the addition (+)
  * - the substraction (-)
  * - the multiplication (*)
  * - the division (/)
  * - the modulo (%)
  *
  * @param array WHERE clause configuration
  * @return string WHERE clause as string.
  * @see	t3lib_sqlparser::parseWhereClause()
  */
 public function compileWhereClause($clauseArray, $functionMapping = TRUE)
 {
     switch ((string) $GLOBALS['TYPO3_DB']->handlerCfg[$GLOBALS['TYPO3_DB']->lastHandlerKey]['type']) {
         case 'native':
             $output = parent::compileWhereClause($clauseArray);
             break;
         case 'adodb':
             // Prepare buffer variable:
             $output = '';
             // Traverse clause array:
             if (is_array($clauseArray)) {
                 foreach ($clauseArray as $k => $v) {
                     // Set operator:
                     $output .= $v['operator'] ? ' ' . $v['operator'] : '';
                     // Look for sublevel:
                     if (is_array($v['sub'])) {
                         $output .= ' (' . trim($this->compileWhereClause($v['sub'], $functionMapping)) . ')';
                     } elseif (isset($v['func']) && $v['func']['type'] === 'EXISTS') {
                         $output .= ' ' . trim($v['modifier']) . ' EXISTS (' . $this->compileSELECT($v['func']['subquery']) . ')';
                     } else {
                         if (isset($v['func']) && $v['func']['type'] === 'LOCATE') {
                             $output .= ' ' . trim($v['modifier']);
                             switch (TRUE) {
                                 case $GLOBALS['TYPO3_DB']->runningADOdbDriver('mssql') && $functionMapping:
                                     $output .= ' CHARINDEX(';
                                     $output .= $v['func']['substr'][1] . $v['func']['substr'][0] . $v['func']['substr'][1];
                                     $output .= ', ' . ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                                     $output .= isset($v['func']['pos']) ? ', ' . $v['func']['pos'][0] : '';
                                     $output .= ')';
                                     break;
                                 case $GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8') && $functionMapping:
                                     $output .= ' INSTR(';
                                     $output .= ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                                     $output .= ', ' . $v['func']['substr'][1] . $v['func']['substr'][0] . $v['func']['substr'][1];
                                     $output .= isset($v['func']['pos']) ? ', ' . $v['func']['pos'][0] : '';
                                     $output .= ')';
                                     break;
                                 default:
                                     $output .= ' LOCATE(';
                                     $output .= $v['func']['substr'][1] . $v['func']['substr'][0] . $v['func']['substr'][1];
                                     $output .= ', ' . ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                                     $output .= isset($v['func']['pos']) ? ', ' . $v['func']['pos'][0] : '';
                                     $output .= ')';
                                     break;
                             }
                         } elseif (isset($v['func']) && $v['func']['type'] === 'IFNULL') {
                             $output .= ' ' . trim($v['modifier']) . ' ';
                             switch (TRUE) {
                                 case $GLOBALS['TYPO3_DB']->runningADOdbDriver('mssql') && $functionMapping:
                                     $output .= 'ISNULL';
                                     break;
                                 case $GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8') && $functionMapping:
                                     $output .= 'NVL';
                                     break;
                                 default:
                                     $output .= 'IFNULL';
                                     break;
                             }
                             $output .= '(';
                             $output .= ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                             $output .= ', ' . $v['func']['default'][1] . $this->compileAddslashes($v['func']['default'][0]) . $v['func']['default'][1];
                             $output .= ')';
                         } elseif (isset($v['func']) && $v['func']['type'] === 'FIND_IN_SET') {
                             $output .= ' ' . trim($v['modifier']) . ' ';
                             if ($functionMapping) {
                                 switch (TRUE) {
                                     case $GLOBALS['TYPO3_DB']->runningADOdbDriver('mssql'):
                                         $field = ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                                         if (!isset($v['func']['str_like'])) {
                                             $v['func']['str_like'] = $v['func']['str'][0];
                                         }
                                         $output .= '\',\'+' . $field . '+\',\' LIKE \'%,' . $v['func']['str_like'] . ',%\'';
                                         break;
                                     case $GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8'):
                                         $field = ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                                         if (!isset($v['func']['str_like'])) {
                                             $v['func']['str_like'] = $v['func']['str'][0];
                                         }
                                         $output .= '\',\'||' . $field . '||\',\' LIKE \'%,' . $v['func']['str_like'] . ',%\'';
                                         break;
                                     case $GLOBALS['TYPO3_DB']->runningADOdbDriver('postgres'):
                                         $output .= ' FIND_IN_SET(';
                                         $output .= $v['func']['str'][1] . $v['func']['str'][0] . $v['func']['str'][1];
                                         $output .= ', ' . ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                                         $output .= ') != 0';
                                         break;
                                     default:
                                         $field = ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                                         if (!isset($v['func']['str_like'])) {
                                             $v['func']['str_like'] = $v['func']['str'][0];
                                         }
                                         $output .= '(' . $field . ' LIKE \'%,' . $v['func']['str_like'] . ',%\'' . ' OR ' . $field . ' LIKE \'' . $v['func']['str_like'] . ',%\'' . ' OR ' . $field . ' LIKE \'%,' . $v['func']['str_like'] . '\'' . ' OR ' . $field . '= ' . $v['func']['str'][1] . $v['func']['str'][0] . $v['func']['str'][1] . ')';
                                         break;
                                 }
                             } else {
                                 switch (TRUE) {
                                     case $GLOBALS['TYPO3_DB']->runningADOdbDriver('mssql'):
                                     case $GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8'):
                                     case $GLOBALS['TYPO3_DB']->runningADOdbDriver('postgres'):
                                         $output .= ' FIND_IN_SET(';
                                         $output .= $v['func']['str'][1] . $v['func']['str'][0] . $v['func']['str'][1];
                                         $output .= ', ' . ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                                         $output .= ')';
                                         break;
                                     default:
                                         $field = ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
                                         if (!isset($v['func']['str_like'])) {
                                             $v['func']['str_like'] = $v['func']['str'][0];
                                         }
                                         $output .= '(' . $field . ' LIKE \'%,' . $v['func']['str_like'] . ',%\'' . ' OR ' . $field . ' LIKE \'' . $v['func']['str_like'] . ',%\'' . ' OR ' . $field . ' LIKE \'%,' . $v['func']['str_like'] . '\'' . ' OR ' . $field . '= ' . $v['func']['str'][1] . $v['func']['str'][0] . $v['func']['str'][1] . ')';
                                         break;
                                 }
                             }
                         } else {
                             // Set field/table with modifying prefix if any:
                             $output .= ' ' . trim($v['modifier']) . ' ';
                             // DBAL-specific: Set calculation, if any:
                             if ($v['calc'] === '&' && $functionMapping) {
                                 switch (TRUE) {
                                     case $GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8'):
                                         // Oracle only knows BITAND(x,y) - sigh
                                         $output .= 'BITAND(' . trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']) . ',' . $v['calc_value'][1] . $this->compileAddslashes($v['calc_value'][0]) . $v['calc_value'][1] . ')';
                                         break;
                                     default:
                                         // MySQL, MS SQL Server, PostgreSQL support the &-syntax
                                         $output .= trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']) . $v['calc'] . $v['calc_value'][1] . $this->compileAddslashes($v['calc_value'][0]) . $v['calc_value'][1];
                                         break;
                                 }
                             } elseif ($v['calc']) {
                                 $output .= trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']) . $v['calc'];
                                 if (isset($v['calc_table'])) {
                                     $output .= trim(($v['calc_table'] ? $v['calc_table'] . '.' : '') . $v['calc_field']);
                                 } else {
                                     $output .= $v['calc_value'][1] . $this->compileAddslashes($v['calc_value'][0]) . $v['calc_value'][1];
                                 }
                             } elseif (!($GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8') && preg_match('/(NOT )?LIKE( BINARY)?/', $v['comparator']) && $functionMapping)) {
                                 $output .= trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']);
                             }
                         }
                         // Set comparator:
                         if ($v['comparator']) {
                             $isLikeOperator = preg_match('/(NOT )?LIKE( BINARY)?/', $v['comparator']);
                             switch (TRUE) {
                                 case $GLOBALS['TYPO3_DB']->runningADOdbDriver('oci8') && $isLikeOperator && $functionMapping:
                                     // Oracle cannot handle LIKE on CLOB fields - sigh
                                     if (isset($v['value']['operator'])) {
                                         $values = array();
                                         foreach ($v['value']['args'] as $fieldDef) {
                                             $values[] = ($fieldDef['table'] ? $fieldDef['table'] . '.' : '') . $fieldDef['field'];
                                         }
                                         $compareValue = ' ' . $v['value']['operator'] . '(' . implode(',', $values) . ')';
                                     } else {
                                         $compareValue = $v['value'][1] . $this->compileAddslashes(trim($v['value'][0], '%')) . $v['value'][1];
                                     }
                                     if (t3lib_div::isFirstPartOfStr($v['comparator'], 'NOT')) {
                                         $output .= 'NOT ';
                                     }
                                     // To be on the safe side
                                     $isLob = TRUE;
                                     if ($v['table']) {
                                         // Table and field names are quoted:
                                         $tableName = substr($v['table'], 1, strlen($v['table']) - 2);
                                         $fieldName = substr($v['field'], 1, strlen($v['field']) - 2);
                                         $fieldType = $GLOBALS['TYPO3_DB']->sql_field_metatype($tableName, $fieldName);
                                         $isLob = $fieldType === 'B' || $fieldType === 'XL';
                                     }
                                     if (strtoupper(substr($v['comparator'], -6)) === 'BINARY') {
                                         if ($isLob) {
                                             $output .= '(dbms_lob.instr(' . trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']) . ', ' . $compareValue . ',1,1) > 0)';
                                         } else {
                                             $output .= '(instr(' . trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']) . ', ' . $compareValue . ',1,1) > 0)';
                                         }
                                     } else {
                                         if ($isLob) {
                                             $output .= '(dbms_lob.instr(LOWER(' . trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']) . '), ' . t3lib_div::strtolower($compareValue) . ',1,1) > 0)';
                                         } else {
                                             $output .= '(instr(LOWER(' . trim(($v['table'] ? $v['table'] . '.' : '') . $v['field']) . '), ' . t3lib_div::strtolower($compareValue) . ',1,1) > 0)';
                                         }
                                     }
                                     break;
                                 default:
                                     if ($isLikeOperator && $functionMapping) {
                                         if ($GLOBALS['TYPO3_DB']->runningADOdbDriver('postgres') || $GLOBALS['TYPO3_DB']->runningADOdbDriver('postgres64') || $GLOBALS['TYPO3_DB']->runningADOdbDriver('postgres7') || $GLOBALS['TYPO3_DB']->runningADOdbDriver('postgres8')) {
                                             // Remap (NOT)? LIKE to (NOT)? ILIKE
                                             // and (NOT)? LIKE BINARY to (NOT)? LIKE
                                             switch ($v['comparator']) {
                                                 // Remap (NOT)? LIKE to (NOT)? ILIKE
                                                 case 'LIKE':
                                                     $v['comparator'] = 'ILIKE';
                                                     break;
                                                 case 'NOT LIKE':
                                                     $v['comparator'] = 'NOT ILIKE';
                                                     break;
                                                 default:
                                                     $v['comparator'] = str_replace(' BINARY', '', $v['comparator']);
                                                     break;
                                             }
                                         } else {
                                             // No more BINARY operator
                                             $v['comparator'] = str_replace(' BINARY', '', $v['comparator']);
                                         }
                                     }
                                     $output .= ' ' . $v['comparator'];
                                     // Detecting value type; list or plain:
                                     if (t3lib_div::inList('NOTIN,IN', strtoupper(str_replace(array(' ', TAB, CR, LF), '', $v['comparator'])))) {
                                         if (isset($v['subquery'])) {
                                             $output .= ' (' . $this->compileSELECT($v['subquery']) . ')';
                                         } else {
                                             $valueBuffer = array();
                                             foreach ($v['value'] as $realValue) {
                                                 $valueBuffer[] = $realValue[1] . $this->compileAddslashes($realValue[0]) . $realValue[1];
                                             }
                                             $output .= ' (' . trim(implode(',', $valueBuffer)) . ')';
                                         }
                                     } else {
                                         if (t3lib_div::inList('BETWEEN,NOT BETWEEN', $v['comparator'])) {
                                             $lbound = $v['values'][0];
                                             $ubound = $v['values'][1];
                                             $output .= ' ' . $lbound[1] . $this->compileAddslashes($lbound[0]) . $lbound[1];
                                             $output .= ' AND ';
                                             $output .= $ubound[1] . $this->compileAddslashes($ubound[0]) . $ubound[1];
                                         } else {
                                             if (isset($v['value']['operator'])) {
                                                 $values = array();
                                                 foreach ($v['value']['args'] as $fieldDef) {
                                                     $values[] = ($fieldDef['table'] ? $fieldDef['table'] . '.' : '') . $fieldDef['field'];
                                                 }
                                                 $output .= ' ' . $v['value']['operator'] . '(' . implode(',', $values) . ')';
                                             } else {
                                                 $output .= ' ' . $v['value'][1] . $this->compileAddslashes($v['value'][0]) . $v['value'][1];
                                             }
                                         }
                                     }
                                     break;
                             }
                         }
                     }
                 }
             }
             break;
     }
     return $output;
 }