Example #1
0
 /**
  * Turns Criterion into IN Sql Clause
  *
  * @param   Criterion   $criterion      Criterion to process
  * @param   string      $placeHolder    Placeholder for parameter name
  * @param   array       $fieldMap       Array representing field map
  */
 public static function criterionToInClause(Criterion $criterion, $placeHolder = 'filter_0', $fieldMap = array())
 {
     // Create local operand and collate
     $op = false;
     $collate = false;
     $clause = new Clause();
     switch ($criterion->op()) {
         case 'in':
             $op = 'IN';
             $collate = 'utf8_bin';
             break;
         case 'nin':
             $op = 'NOT IN';
             $collate = 'utf8_bin';
             break;
         case 'ini':
             $op = 'IN';
             $collate = 'utf8_general_ci';
             break;
         case 'nini':
             $op = 'NOT IN';
             $collate = 'utf8_general_ci';
             break;
     }
     if ($op && $collate) {
         if ($criterion->type() == Criterion::CRITERION_TYPE_VALUE) {
             // Values should be in a comma separated list.
             $values = explode(',', $criterion->value());
             // We must add a parameter and a placeholder for each value.
             $placeHolders = array();
             foreach ($values as $key => $value) {
                 $clause->setParameter($placeHolder . '_' . $key, $value);
                 $placeHolders[] = ':' . $placeHolder . '_' . $key . ' COLLATE ' . $collate;
             }
             // The comparand for this operation is the list of values.
             $comparand = '(' . implode(', ', $placeHolders) . ')';
         } elseif ($criterion->type() == Criterion::CRITERION_TYPE_FIELD) {
             // Field names should be in a comma separated list.
             $fList = explode(',', $criterion->value());
             foreach (array_keys($fList) as $key) {
                 $fList[$key] = self::getSafeFieldName($fList[$key], $fieldMap) . ' COLLATE ' . $collate;
             }
             // The comparand for this operation is the list of field names.
             $comparand = '(' . implode(', ', $fList) . ')';
         }
         // Escape, quote and qualify the field name for security.
         $field = self::getSafeFieldName($criterion->key(), $fieldMap);
         // Build the final clause.
         // Use end wild-card character for "begins with".
         $clause->setStatement(self::addLogic($criterion) . ' ' . $field . ' ' . $op . ' ' . $comparand);
     }
     return $clause;
 }