Example #1
0
 /**
  * @param QueryBuilder|NativeQueryBuilder $query
  * @param array $args
  * @return array
  */
 public static function separateParameters($query, array $args)
 {
     for ($i = 0; array_key_exists($i, $args) && array_key_exists($i + 1, $args) && ($arg = $args[$i]); $i++) {
         if (!preg_match_all('~((\\:|\\?)(?P<name>[a-z0-9_]+))(?=(?:\\z|\\s|\\)))~i', $arg, $m)) {
             continue;
         }
         $repeatedArgs = [];
         foreach ($m['name'] as $l => $name) {
             if (isset($repeatedArgs[$name])) {
                 continue;
             }
             $value = $args[++$i];
             $type = NULL;
             if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) {
                 $type = Type::DATETIME;
             } elseif (is_array($value)) {
                 $type = Connection::PARAM_STR_ARRAY;
             }
             $query->setParameter($name, $value, $type);
             $repeatedArgs[$name] = TRUE;
             unset($args[$i]);
         }
     }
     return $args;
 }
Example #2
0
 /**
  * @param \Kdyby\Doctrine\QueryBuilder|\Kdyby\Doctrine\NativeQueryBuilder $qb
  * @param \Carrooi\NoGrid\Condition $condition
  * @throws \Carrooi\NoGrid\NotImplementedException
  */
 public static function makeWhere($qb, Condition $condition)
 {
     if (!$qb instanceof QueryBuilder && !$qb instanceof NativeQueryBuilder) {
         throw new InvalidArgumentException();
     }
     $column = $condition->getColumn();
     $value = $condition->getValue();
     if (!Strings::contains($column, '.')) {
         $column = current($qb->getRootAliases()) . '.' . $column;
     }
     $parameter = ':grid' . self::$parametersCount;
     $options = $condition->getOptions();
     $lower = isset($options[Condition::CASE_INSENSITIVE]) && $options[Condition::CASE_INSENSITIVE];
     if ($lower) {
         $column = 'lower(' . $column . ')';
         $parameter = 'lower(' . $parameter . ')';
     }
     if ($condition->getType() === Condition::SAME) {
         $qb->andWhere($column . ' = ' . $parameter);
     } elseif ($condition->getType() === Condition::NOT_SAME) {
         $qb->andWhere($column . ' != ' . $parameter);
     } elseif ($condition->getType() === Condition::IS_NULL) {
         $qb->andWhere($column . ' IS NULL');
     } elseif ($condition->getType() === Condition::IS_NOT_NULL) {
         $qb->andWhere($column . ' IS NOT NULL');
     } elseif ($condition->getType() === Condition::LIKE) {
         $qb->andWhere($column . ' LIKE ' . $parameter);
     } else {
         throw new NotImplementedException('Filtering condition is not implemented.');
     }
     if (!in_array($condition->getType(), [Condition::IS_NULL, Condition::IS_NOT_NULL])) {
         $qb->setParameter('grid' . self::$parametersCount, $value);
     }
     self::$parametersCount++;
 }