Example #1
0
 /**
  * {@inheritdoc}
  *
  * {@inheritdoc}
  */
 public function query(Query $query)
 {
     $limits = $query->getLimit();
     $limit = !$limits ? self::LIMIT_INFINITY : $query->getLimit()->getLimit();
     $offset = !$limits ? 0 : $query->getLimit()->getOffset();
     $sort = $query->getSort();
     $sortFilds = !$sort ? [$this->getIdentifier() => SortNode::SORT_ASC] : $sort->getFields();
     $select = $query->getSelect();
     //What filds will return
     $selectFilds = !$select ? [] : $select->getFields();
     $selectSQL = $this->dbTable->getSql()->select();
     // ***********************   where   ***********************
     $conditionBuilder = $this->conditionBuilder;
     $where = $conditionBuilder($query->getQuery());
     $selectSQL->where($where);
     // ***********************   order   ***********************
     foreach ($sortFilds as $ordKey => $ordVal) {
         if ((int) $ordVal === SortNode::SORT_DESC) {
             $selectSQL->order($ordKey . ' ' . Select::ORDER_DESCENDING);
         } else {
             $selectSQL->order($ordKey . ' ' . Select::ORDER_ASCENDING);
         }
     }
     // *********************  limit, offset   ***********************
     if ($limit != self::LIMIT_INFINITY) {
         $selectSQL->limit($limit);
     }
     if ($offset != 0) {
         $selectSQL->offset($offset);
     }
     // *********************  filds  ***********************
     if (!empty($selectFilds)) {
         $selectSQL->columns($selectFilds);
     }
     // ***********************   return   ***********************
     $rowset = $this->dbTable->selectWith($selectSQL);
     return $rowset->toArray();
 }
Example #2
0
 protected function querySelect($data, Query $query)
 {
     $selectNode = $query->getSelect();
     if (empty($selectNode)) {
         return $data;
     } else {
         $resultArray = array();
         $compareArray = array();
         foreach ($selectNode->getFields() as $field) {
             if ($field instanceof AggregateFunctionNode) {
                 switch ($field->getFunction()) {
                     case 'count':
                         $arr = [];
                         foreach ($data as $item) {
                             if (isset($item[$field->getField()])) {
                                 $arr[] = $item[$field->getField()];
                             }
                         }
                         $compareArray[$field->getField() . '->' . $field->getFunction()] = [count($arr)];
                         break;
                     case 'max':
                         $max = 0;
                         foreach ($data as $item) {
                             if ($max < $item[$field->getField()]) {
                                 $max = $item[$field->getField()];
                             }
                         }
                         $compareArray[$field->getField() . '->' . $field->getFunction()] = [$max];
                         break;
                     case 'min':
                         $min = null;
                         foreach ($data as $item) {
                             if (!isset($min)) {
                                 $min = $item[$field->getField()];
                             }
                             if ($min > $item[$field->getField()]) {
                                 $min = $item[$field->getField()];
                             }
                         }
                         $compareArray[$field->getField() . '->' . $field->getFunction()] = [$min];
                         break;
                 }
             } else {
                 $dataLine = [];
                 foreach ($data as $item) {
                     $dataLine[] = $item[$field];
                 }
                 $compareArray[$field] = $dataLine;
             }
         }
         $min = null;
         foreach ($compareArray as $column) {
             if (!isset($min)) {
                 $min = count($column);
             } elseif (count($column) < $min) {
                 $min = count($column);
             }
         }
         for ($i = 0; $i < $min; ++$i) {
             $item = [];
             foreach ($compareArray as $fieldName => $column) {
                 $item[$fieldName] = $column[$i];
             }
             $resultArray[] = $item;
         }
         return $resultArray;
     }
 }
Example #3
0
 protected function setSelectJoin(Select $selectSQL, Query $query)
 {
     $select = $query->getSelect();
     $selectField = !$select ? [] : $select->getFields();
     /** @var Adapter $adapter */
     $adapter = $this->dbTable->getAdapter();
     $metadata = Factory::createSourceFromAdapter($adapter);
     $sysEntityTable = $metadata->getTable(SysEntities::TABLE_NAME);
     $identifier = $this->getIdentifier();
     //todo: agregate function
     $joinedEntityfileds = [];
     if (!empty($selectField)) {
         foreach ($selectField as $field) {
             if ($field instanceof AggregateFunctionNode and !in_array($field->getField(), $sysEntityTable->getColumns())) {
                 $joinedEntityfileds[$field->getField() . "->" . $field->getFunction()] = new Expression($field->__toString());
             } else {
                 if (!in_array($field, $sysEntityTable->getColumns())) {
                     $joinedEntityfileds[] = $field;
                 }
             }
         }
     }
     $prew = $this;
     /** @var DbTable $entity */
     foreach ($this->joinedEntities as $entity) {
         if (is_object($entity)) {
             $entityField = [];
             $entityTable = $metadata->getTable($entity->dbTable->table);
             /** @var Column $column */
             foreach ($entityTable->getColumns() as $column) {
                 $colName = $column->getName();
                 if (in_array($colName, $joinedEntityfileds)) {
                     $entityField[] = $colName;
                 }
             }
             $selectSQL->join($entity->dbTable->table, $entity->dbTable->table . '.' . $identifier . '=' . $prew->dbTable->table . '.' . $identifier, empty($entityField) ? Select::SQL_STAR : $entityField, Select::JOIN_INNER);
             $prew = $entity;
         }
     }
     return $selectSQL;
 }
Example #4
0
 protected function querySelect($data, Query $query)
 {
     $selectNode = $query->getSelect();
     if (empty($selectNode)) {
         return $data;
     } else {
         $resultArray = array();
         foreach ($data as $item) {
             $resultArray[] = array_intersect_key($item, array_flip($selectNode->getFields()));
         }
         return $resultArray;
     }
 }
Example #5
0
 protected function makeSelect(Query $query)
 {
     $selectNode = $query->getSelect();
     //What fields will be return
     $selectFields = !$selectNode ? [] : $selectNode->getFields();
     if (empty($selectFields)) {
         return '';
     } else {
         $selectString = '&select(';
         foreach ($selectFields as $field) {
             $selectString = $selectString . $field . ',';
         }
         return rtrim($selectString, ',') . ')';
     }
 }
 public function visit(Query $query, SqlBuilder $sqlBuilder)
 {
     if ($query->getSelect() !== null) {
         $this->visitSelectNode($query->getSelect(), $sqlBuilder);
     }
     if ($query->getQuery() !== null) {
         $this->visitQueryNode($query->getQuery(), $sqlBuilder);
     }
     if ($query->getSort() !== null) {
         $this->visitSortNode($query->getSort(), $sqlBuilder);
     }
     if ($query->getLimit() !== null) {
         $this->visitLimitNode($query->getLimit(), $sqlBuilder);
     }
 }
Example #7
0
 protected function setSelectColumns(Select $selectSQL, Query $query)
 {
     $select = $query->getSelect();
     //What fields will return
     $selectFields = !$select ? [] : $select->getFields();
     if (!empty($selectFields)) {
         $fields = [];
         foreach ($selectFields as $field) {
             if ($field instanceof AggregateFunctionNode) {
                 $fields[$field->getField() . "->" . $field->getFunction()] = new Expression($field->__toString());
             } else {
                 $fields[] = $field;
             }
         }
         $selectSQL->columns($fields);
     }
     return $selectSQL;
 }
Example #8
0
 protected function setSelectColumns(Select $selectSQL, Query $query)
 {
     $select = $query->getSelect();
     $selectField = !$select ? [] : $select->getFields();
     $fields = [];
     if (!empty($selectField)) {
         $bounds = [];
         $hawAggregate = false;
         $hawBound = false;
         foreach ($selectField as $field) {
             $match = [];
             if ($field instanceof AggregateFunctionNode) {
                 $hawAggregate = true;
                 //todo: create aggregate
             } else {
                 if (preg_match('/([\\w]+)\\./', $field, $match)) {
                     $subMatch = [];
                     $name = $match[1];
                     $boundQuery = new Query();
                     if (preg_match('/([\\w]+)\\.\\#([\\w]+)?/', $field, $subMatch)) {
                         $withOut = $this->dbTable->table;
                         if (isset($subMatch[2])) {
                             $withOut = $subMatch[2];
                         }
                         $boundQuery->setSelect(new SelectNode(['#' . $withOut]));
                     } else {
                         if (preg_match('/[\\w]+\\.([\\w\\.\\#]+)/', $field, $subMatch)) {
                             $boundQuery->setSelect(new SelectNode([$subMatch[1]]));
                         } else {
                             $boundQuery->setSelect(new SelectNode());
                         }
                     }
                     $bounds[] = [$name => $boundQuery];
                 } else {
                     if (preg_match('/^#([\\w]+)?$/', $field, $match)) {
                         $withOut = '';
                         if ($match[1]) {
                             $withOut = $match[1];
                         }
                         foreach ($this->getBoundsTableName() as $bound) {
                             if ($bound != $withOut) {
                                 $boundQuery = new Query();
                                 $boundQuery->setSelect(new SelectNode());
                                 $bounds[] = [$bound => $boundQuery];
                             }
                         }
                     } else {
                         $fields[] = $field;
                     }
                 }
             }
             if ($hawAggregate && $hawBound) {
                 throw new DataStoreException('Cannot use aggregate function with bounds');
             }
         }
         if (!empty($bounds)) {
             $fields['.bounds.'] = $bounds;
         }
     }
     $selectSQL->columns(empty($fields) ? [Select::SQL_STAR] : $fields);
     return $selectSQL;
 }
Example #9
0
 protected function setSelectColumns(Select $selectSQL, Query $query)
 {
     $select = $query->getSelect();
     //What fields will return
     $selectFields = !$select ? [] : $select->getFields();
     $props = [];
     if (!empty($selectFields)) {
         $fields = [];
         $hawAggregate = false;
         $hawProps = false;
         foreach ($selectFields as $field) {
             if ($field instanceof AggregateFunctionNode) {
                 $fildName = $field->__toString();
                 $fullFildName = $fildName == "count(id)" ? 'count(' . $this->dbTable->table . '.id)' : $fildName;
                 $fields[$field->getField() . "->" . $field->getFunction()] = new Expression($fullFildName);
                 $hawAggregate = true;
             } else {
                 if (strpos($field, SysEntities::PROP_PREFIX) === 0) {
                     $propTableName = explode('.', $field)[0];
                     $props[$field] = new Prop(new TableGateway($propTableName, $this->dbTable->getAdapter()));
                     $hawProps = true;
                 } else {
                     $fields[] = $field;
                 }
             }
             if ($hawAggregate && $hawProps) {
                 throw new DataStoreException('Cannot use aggregate function with props');
             }
         }
         if (!empty($props)) {
             $fields['props'] = $props;
         }
         $selectSQL->columns($fields);
     }
     return $selectSQL;
 }
 /**
  * @param \Xiag\Rql\Parser\Query $query
  *
  * @throws \AndreasGlaser\DoctrineRql\Visitor\VisitorException
  * @author Andreas Glaser
  */
 protected function visitQuery(RqlQuery $query)
 {
     if ($selectNode = $query->getSelect()) {
         // todo: Implement this
     }
     if ($abstractQueryNode = $query->getQuery()) {
         $this->qb->andWhere($this->walkNodes($abstractQueryNode));
     }
     if ($query->getSort()) {
         $this->visitSort($query->getSort());
     }
     if ($query->getLimit()) {
         $this->visitLimit($query->getLimit());
     }
 }