Exemplo n.º 1
0
 /**
  * query function.
  *
  * @access public
  * @todo implement cache
  *
  * @param string $query
  * @param array  $data (default: array())
  * @param string|callable $entity (default: null)
  *
  * @return DatabaseResult
  */
 public function query($query, $data = array(), $entity = null)
 {
     if ($this->cache->isCached($query)) {
         return $this->cache->getCachedResult($query);
     } else {
         try {
             $statement = $this->database->prepare((string) $query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
             $result = $statement->execute($data);
             $this->lastQuery = $statement->queryString;
             if (!$result) {
                 throw new DatabaseException(implode(': ', $statement->errorInfo()) . PHP_EOL . PHP_EOL . $query);
             }
         } catch (PDOException $e) {
             throw new DatabaseException($e->getMessage() . PHP_EOL . PHP_EOL . $query);
         }
         $databaseResult = $this->createResult($statement, $entity);
         if ($query instanceof DatabaseQuery && $databaseResult instanceof DatabaseResult) {
             $this->cache->cacheQuery($query, $databaseResult);
         }
         if ($query instanceof DatabaseQuery) {
             $this->cache->refreshCache($query);
         }
         if ($databaseResult instanceof DatabaseResult) {
             return $databaseResult;
         } else {
             if ($query instanceof DatabaseQuery && $query->getType() == DatabaseQuery::QUERY_TYPE_INSERT) {
                 return $this->insertId($query->getTable());
             } else {
                 return $databaseResult;
             }
         }
     }
 }
Exemplo n.º 2
0
 public function testCache()
 {
     $query = $this->connection->createQuery()->select('*')->from('children')->setCache(true);
     $result = $query->execute();
     $this->cache->cacheQuery($query, $result);
     $this->assertTrue($this->cache->isCached($query));
     $this->assertSame($result, $this->cache->getCachedResult($query));
 }