/** * Returns the sql expression to determinate the distincts numbers of lines * * @return string */ protected function getDistinct() { if (isset($this->conf->group)) { return $this->conf->group; } return implode(', ', $this->queryBuilder->getQueryPart('select')); }
/** * @return bool|string */ public function getTotal() { if ($this->total) { return $this->total; } $queryBuilder = $this->repository->getEm()->createQueryBuilder(); $fromParts = $this->queryBuilder->getQueryPart('from'); if ($fromParts) { foreach ($fromParts as $from) { $queryBuilder->from($from['table'], $from['alias']); } } $queryBuilder->select('COUNT(*)'); $where = $this->queryBuilder->getQueryPart('where'); if ($where) { $queryBuilder->add('where', (string) $where); } $parameters = $this->queryBuilder->getParameters(); if ($parameters) { foreach ($parameters as $key => $value) { $queryBuilder->setParameter($key, $value); } } return $this->total = $queryBuilder->execute()->fetchColumn(); }
/** * {@inheritDoc} */ public function hasJoinAlias($joinAlias) { $joinParts = $this->queryBuilder->getQueryPart('join'); foreach ($joinParts as $rootAlias => $joins) { foreach ($joins as $join) { if ($join['joinAlias'] === $joinAlias) { return true; } } } return false; }
/** * @internal */ private function incorporateDbalQueryBuilder(QueryBuilder $qb, FilterInterface $filter) { $criteria = $this->getFilteringCriteria($filter); // extraindo os rootAliases, pois o DBAL\QueryBuilder não tem $fromPart = $qb->getQueryPart('from'); $rootAliases = array(); foreach ($fromPart as $part) { $rootAliases[] = $part['alias']; } $visitor = new DbalQueryExpressionVisitor($qb->getConnection(), $rootAliases, $this->fieldMap); if ($whereExpression = $criteria->getWhereExpression()) { $qb->andWhere($visitor->dispatch($whereExpression)); $qb->setParameters($visitor->getParameters()); } if ($criteria->getOrderings()) { foreach ($criteria->getOrderings() as $sort => $order) { $qb->addOrderBy($visitor->getFieldName($sort), $order); } } if (($firstResult = $criteria->getFirstResult()) !== null) { $qb->setFirstResult($firstResult); } if (($maxResults = $criteria->getMaxResults()) !== null) { $qb->setMaxResults($maxResults); } }
/** * For repeating fields, the load method adds extra joins and selects to the query that * fetches the related records from the field and field value tables in the same query as the content fetch. * * @param QueryBuilder $query * @param ClassMetadata $metadata * * @return void */ public function load(QueryBuilder $query, ClassMetadata $metadata) { $field = $this->mapping['fieldname']; $boltname = $metadata->getBoltName(); $from = $query->getQueryPart('from'); if (isset($from[0]['alias'])) { $alias = $from[0]['alias']; } else { $alias = $from[0]['table']; } $query->addSelect($this->getPlatformGroupConcat('fields', $query))->leftJoin($alias, $this->mapping['tables']['field_value'], 'f', "f.content_id = {$alias}.id AND f.contenttype='{$boltname}' AND f.name='{$field}'"); }
/** * For relations, the load method adds an extra ->addSelect() and ->leftJoin() to the query that * fetches the related records from the join table in the same query as the content fetch. * * IDs are returned comma-separated which the ->hydrate() method can then turn into pointers * to the related entities. * * @param QueryBuilder $query * @param ClassMetadata $metadata */ public function load(QueryBuilder $query, ClassMetadata $metadata) { $field = $this->mapping['fieldname']; $target = $this->mapping['target']; $boltname = $metadata->getBoltName(); $from = $query->getQueryPart('from'); if (isset($from[0]['alias'])) { $alias = $from[0]['alias']; } else { $alias = $from[0]['table']; } $query->addSelect($this->getPlatformGroupConcat("{$field}.to_id", $field, $query))->leftJoin($alias, $target, $field, "{$alias}.id = {$field}.from_id AND {$field}.from_contenttype='{$boltname}' AND {$field}.to_contenttype='{$field}'")->addGroupBy("{$alias}.id"); }
/** * Add the additional query conditions returned by the QueryRestrictionBuilder * to the current query and return the original set of conditions so that they * can be restored after the query has been built/executed. * * @return \Doctrine\DBAL\Query\Expression\CompositeExpression|mixed */ protected function addAdditonalWhereConditions() { $queryRestrictionBuilder = GeneralUtility::makeInstance(QueryRestrictionBuilder::class, $this->getQueriedTables(), $this->expr(), $this->getQueryContext()); $originalWhereConditions = $this->concreteQueryBuilder->getQueryPart('where'); if ($originalWhereConditions instanceof CompositeExpression) { $originalWhereConditions = clone $originalWhereConditions; } $additionalQueryRestrictions = $queryRestrictionBuilder->getVisibilityConstraints(); if ($additionalQueryRestrictions->count() !== 0) { // save the original query conditions so we can restore // them after the query has been built. $this->concreteQueryBuilder->andWhere($additionalQueryRestrictions); } return $originalWhereConditions; }
/** * @inheritdoc */ public function load(QueryBuilder $query, ClassMetadata $metadata) { $field = $this->mapping['fieldname']; $boltname = $metadata->getBoltName(); if ($this->mapping['data']['has_sortorder']) { $order = "{$field}.sortorder"; } else { $order = "{$field}.id"; } $from = $query->getQueryPart('from'); if (isset($from[0]['alias'])) { $alias = $from[0]['alias']; } else { $alias = $from[0]['table']; } $query->addSelect($this->getPlatformGroupConcat("{$field}.slug", $order, $field, $query))->leftJoin($alias, 'bolt_taxonomy', $field, "{$alias}.id = {$field}.content_id AND {$field}.contenttype='{$boltname}' AND {$field}.taxonomytype='{$field}'")->addGroupBy("{$alias}.id"); }
/** * Add the additional query conditions returned by the QueryRestrictionBuilder * to the current query and return the original set of conditions so that they * can be restored after the query has been built/executed. * * @return \Doctrine\DBAL\Query\Expression\CompositeExpression|mixed */ protected function addAdditionalWhereConditions() { $originalWhereConditions = $this->concreteQueryBuilder->getQueryPart('where'); $expression = $this->restrictionContainer->buildExpression($this->getQueriedTables(), $this->expr()); // This check would be obsolete, as the composite expression would not add empty expressions anyway // But we keep it here to only clone the previous state, in case we really will change it. // Once we remove this state preserving functionality, we can remove the count check here // and just add the expression to the query builder. if ($expression->count() > 0) { if ($originalWhereConditions instanceof CompositeExpression) { // Save the original query conditions so we can restore // them after the query has been built. $originalWhereConditions = clone $originalWhereConditions; } $this->concreteQueryBuilder->andWhere($expression); } // @todo add hook to be able to add additional restrictions return $originalWhereConditions; }
/** * Preprocess and execute SELECT query. * * @param \Doctrine\DBAL\Query\QueryBuilder $qb * * @throws \Lokhman\Silex\ARM\Exception\RepositoryException * @return \Doctrine\DBAL\Driver\Statement */ private function preprocess(QueryBuilder $qb) { // for each FROM part in QueryBuilder foreach ($qb->getQueryPart('from') as $part) { $table = $table = $this->prefix . $part['table']; if (!isset($this->app['arm'][$table])) { self::raise('No repository registered for "' . $table . '".'); } $tables[$part['alias']] = $this->app['arm'][$table]; } // for each JOIN part in QueryBuilder foreach ($qb->getQueryPart('join') as $part) { $table = $this->prefix . $part[0]['joinTable']; if (!isset($this->app['arm'][$table])) { self::raise('No repository registered for "' . $table . '".'); } $tables[$part[0]['joinAlias']] = $this->app['arm'][$table]; } // re-define select columns $qb->select($this->formatSelect($qb, $tables)); // execute pre-formatted SELECT query ( $qb->execute() ) return $this->db->executeQuery($this->formatQuery($qb, $tables), $qb->getParameters(), $qb->getParameterTypes()); }
/** * Modifies the passed DBAL query builder object to calculate * the total count. * * @param QueryBuilder $builder * @return $this */ private function addTotalCountSelect(QueryBuilder $builder) { $select = $builder->getQueryPart('select'); $select[0] = ' SQL_CALC_FOUND_ROWS ' . $select[0]; $builder->select($select); return $this; }
/** * @test */ public function getQueryPartDelegatesToConcreteQueryBuilder() { $this->concreteQueryBuilder->getQueryPart('from')->shouldBeCalled()->willReturn('aTable'); $this->subject->getQueryPart('from'); }
/** * @param QueryBuilder $q * @param array $tables $tables[0] should be primary table * @param bool $innerJoinTables * @param null $whereExpression * @param null $having */ protected function applySearchQueryRelationship(QueryBuilder $q, array $tables, $innerJoinTables, $whereExpression = null, $having = null) { $primaryTable = $tables[0]; unset($tables[0]); $joinType = $innerJoinTables ? 'join' : 'leftJoin'; $this->useDistinctCount = true; $joins = $q->getQueryPart('join'); if (!array_key_exists($primaryTable['alias'], $joins)) { $q->{$joinType}($primaryTable['from_alias'], MAUTIC_TABLE_PREFIX . $primaryTable['table'], $primaryTable['alias'], $primaryTable['condition']); foreach ($tables as $table) { $q->{$joinType}($table['from_alias'], MAUTIC_TABLE_PREFIX . $table['table'], $table['alias'], $table['condition']); } if ($whereExpression) { $q->andWhere($whereExpression); } if ($having) { $q->andHaving($having); } $q->groupBy('l.id'); } }
/** * Gets a query part by its name. * * @param string $queryPartName * * @return mixed */ public function getQueryPart($queryPartName) { return $this->queryBuilder->getQueryPart($queryPartName); }
private function hasQueryBuilderJoins(QueryBuilder $queryBuilder) { $joins = $queryBuilder->getQueryPart('join'); return !empty($joins); }
/** * @TODO Temporary workaround for PostgreSQL databases that don't use a sequence. * * @param QueryBuilder $query */ private function setLastInsertId(QueryBuilder $query) { $seq = null; if ($query->getConnection()->getDatabasePlatform()->getName() === 'postgresql') { $sequences = $query->getConnection()->getSchemaManager()->listSequences(); $desiredSeq = $query->getQueryPart('from')['table'] . '_id_seq'; foreach ($sequences as $sequence) { if ($desiredSeq === $sequence->getName()) { $seq = $desiredSeq; break; } } } $this->lastInsertId = $query->getConnection()->lastInsertId($seq); }
/** * Get root alias. * * @return string */ public function getAlias() { $from = $this->queryBuilder->getQueryPart('from'); return $from[0]['alias']; }
/** * @param QueryBuilder $qb * @param $wherePart * @param $value * @return string */ protected function processWhereField(QueryBuilder $qb, $wherePart, $value) { $paramName = $this->getParameterName($wherePart); $column = $this->getColumnName($wherePart); $columnName = $this->db->quoteIdentifier($column); $operator = $this->getOperator($wherePart); if (strpos($operator, 'BETWEEN') !== false) { if (!is_array($value) || !isset($value[0]) || !isset($value[1])) { throw new InvalidArgumentException('BETWEEN value must be a 2 item array with numeric keys'); } $gtParam = $paramName . 'GreaterThan'; $ltParam = $paramName . 'LessThan'; $clause = "{$columnName} {$operator} {$gtParam} AND {$ltParam}"; $qb->setParameter($gtParam, $value[0])->setParameter($ltParam, $value[1]); return $clause; } else { if (is_array($value)) { if ($operator == '<>' || $operator == '!=') { $clause = "{$columnName} NOT IN({$paramName})"; $qb->setParameter($paramName, $value, Connection::PARAM_STR_ARRAY); } else { $clause = "{$columnName} IN({$paramName})"; $qb->setParameter($paramName, $value, Connection::PARAM_STR_ARRAY); } return $clause; } else { if ($value === null && $this->acceptsNull($qb->getQueryPart('from'), $column)) { if ($operator == '<>' || $operator == '!=') { $clause = $this->db->getDatabasePlatform()->getIsNotNullExpression($columnName); return $clause; } else { $clause = $this->db->getDatabasePlatform()->getIsNullExpression($columnName); return $clause; } } elseif ($value !== null) { $clause = "{$columnName} {$operator} {$paramName}"; $qb->setParameter($paramName, $value); return $clause; } else { throw new InvalidArgumentException("Value for {$column} is null, but null is not allowed on this column"); } } } }