Esempio n. 1
0
 public function grammarDelete(Collection $collection, array $criteria = array())
 {
     if (func_num_args() === 1) {
         return "DELETE FROM {$collection->getName()}";
     } else {
         throw new Exception(__METHOD__ . ' unimplemented yet!');
     }
 }
Esempio n. 2
0
 /**
  * Constructor
  *
  * @param \Norm\Collection $collection
  *
  * @param array $criteria
  */
 public function __construct(Collection $collection, $criteria = array())
 {
     $this->collection = $collection;
     $this->connection = $collection->getConnection();
     if (is_null($this->connection)) {
         throw new \Exception('[Norm/Cursor] Collection does not have connection, check your configuration!');
     }
     if ($criteria === null) {
         $criteria = array();
     }
     $this->criteria = $this->translateCriteria($criteria);
 }
Esempio n. 3
0
 /**
  * Get schema of collection
  *
  * @param string|null $schema
  *
  * @return mixed
  */
 public function schema($schema = null)
 {
     if (func_num_args() === 0) {
         return $this->collection->schema();
     }
     return $this->collection->schema($schema);
 }
Esempio n. 4
0
 /**
  * Set query to match on every field exists in schema. Beware this will override criteria
  *
  * @param string $q String to query
  *
  * @return \Norm\Cursor Chainable object
  */
 public function match($q)
 {
     if (is_null($q)) {
         return $this;
     }
     $orCriteria = array();
     $schema = $this->collection->schema();
     if (empty($schema)) {
         throw new \Exception('[Norm\\Cursor] Cannot use match for schemaless collection');
     }
     foreach ($schema as $key => $value) {
         $orCriteria[] = array($key . '!like' => $q);
     }
     $this->criteria = $this->translateCriteria(array('!or' => $orCriteria));
     return $this;
 }
Esempio n. 5
0
 /**
  * Execute a query.
  *
  * @return int
  */
 public function execute()
 {
     $data = array();
     $wheres = array();
     $matchOrs = array();
     if (is_null($this->match)) {
         $criteria = $this->prepareCriteria($this->criteria);
         if ($criteria) {
             foreach ($criteria as $key => $value) {
                 $wheres[] = $this->dialect->grammarExpression($key, $value, $data);
             }
         }
     } else {
         $schema = $this->collection->schema();
         $i = 0;
         foreach ($schema as $key => $value) {
             if ($value instanceof \Norm\Schema\Reference) {
                 $foreign = $value['foreign'];
                 $foreignLabel = $value['foreignLabel'];
                 $foreignKey = $value['foreignKey'];
                 $matchOrs[] = $this->getQueryReference($key, $foreign, $foreignLabel, $foreignKey, $i);
             } else {
                 $matchOrs[] = $key . ' LIKE :f' . $i;
                 $i++;
             }
         }
         $wheres[] = '(' . implode(' OR ', $matchOrs) . ')';
     }
     $select = '';
     if ($this->skip > 0 or $this->limit > 0) {
         $select .= 'rownum r, ';
     }
     $select .= $this->collection->name . '.*';
     $query = 'SELECT ' . $select . ' FROM ' . $this->collection->name;
     $order = '';
     if ($this->sortBy) {
         foreach ($this->sortBy as $key => $value) {
             if ($value == 1) {
                 $op = ' ASC';
             } else {
                 $op = ' DESC';
             }
             $order[] = $key . $op;
         }
         if (!empty($order)) {
             $order = ' ORDER BY ' . implode(',', $order);
         }
     }
     if (!empty($wheres)) {
         $query .= ' WHERE ' . implode(' AND ', $wheres);
     }
     $limit = '';
     if ($this->skip > 0) {
         $limit = 'r > ' . $this->skip . ' AND ROWNUM <= (SELECT COUNT(ROWNUM) FROM (' . $query . '))';
         if ($this->limit > 0) {
             $limit = 'r > ' . $this->skip . ' AND ROWNUM <= ' . $this->limit;
         }
     } elseif ($this->limit > 0) {
         $limit = 'ROWNUM <= ' . $this->limit;
     }
     $query .= $order;
     if ($limit !== '') {
         $query = 'SELECT * FROM (' . $query . ') WHERE ' . $limit;
     }
     $statement = oci_parse($this->raw, $query);
     foreach ($data as $key => $value) {
         oci_bind_by_name($statement, ':' . $key, $data[$key]);
     }
     if ($matchOrs) {
         $match = '%' . $this->match . '%';
         foreach ($matchOrs as $key => $value) {
             oci_bind_by_name($statement, ':f' . $key, $match);
         }
     }
     oci_execute($statement);
     $result = array();
     while ($row = oci_fetch_array($statement, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS)) {
         $result[] = $row;
     }
     $this->rows = $result;
     oci_free_statement($statement);
     $this->index = -1;
 }