예제 #1
0
 /**
  * {@inheritdoc}
  */
 protected function visitComparison(ComparisonNode $node)
 {
     if (!$this->model->hasField($node->left)) {
         return new ConditionBuilder('false');
     }
     $type = $this->model->getType($node->left);
     $right = $type->convert($node->right);
     switch ($node->comparison) {
         case '=':
         case '!=':
         case '<=':
         case '>=':
         case '>':
         case '<':
             if ($type->isDate() or $type->isDateTime()) {
                 $interval = I18n::stringToInterval($node->right);
                 if (isset($interval)) {
                     list($start, $end) = $interval;
                     switch ($node->comparison) {
                         case '=':
                             $cond = new ConditionBuilder('%m.%c >= %_', $this->model, $node->left, $type, $start);
                             $cond->and('%m.%c <= %_', $this->model, $node->left, $type, $end);
                             return $cond;
                         case '!=':
                             $cond = new ConditionBuilder('%m.%c < %_', $this->model, $node->left, $type, $start);
                             $cond->or('%m.%c > %_', $this->model, $node->left, $type, $end);
                             return $cond;
                         case '<':
                         case '>=':
                             $right = $start;
                             break;
                         default:
                             $right = $end;
                             break;
                     }
                 }
             }
             return new ConditionBuilder('%m.%c ' . $node->comparison . ' %_', $this->model, $node->left, $type, $right);
         case 'contains':
             return new ConditionBuilder('%m.%c LIKE %s', $this->model, $node->left, '%' . ConditionBuilder::escapeLike($right) . '%');
     }
     return new ConditionBuilder('false');
 }
예제 #2
0
파일: ModelBase.php 프로젝트: jivoo/jivoo
 /**
  * Find row number of a record in the result set of a selection. The selection
  * must be ordered.
  * @param ReadSelectionBuilder $selection A read selection.
  * @param Record $record A record.
  * @throws InvalidSelectionException If the selection is not ordered.
  * @return int Row number.
  */
 public function rowNumberSelection(ReadSelectionBuilder $selection, Record $record)
 {
     if (empty($selection->orderBy)) {
         throw new InvalidSelectionException(tr('Can\'t find row number in selection without ordering'));
     }
     $condition = new ConditionBuilder();
     foreach ($selection->orderBy as $orderBy) {
         $column = $orderBy['column'];
         $type = $this->getType($column)->placeholder;
         if ($orderBy['descending']) {
             $condition->and($column . ' > ' . $type, $record->{$column});
         } else {
             $condition->and($column . ' < ' . $type, $record->{$column});
         }
     }
     return $selection->and($condition)->count() + 1;
 }