예제 #1
0
 /**
  * Processa a query de consulta
  * @param Expression $qry
  * @return array No formato queryresult
  */
 protected function proccessQuery(Expression $qry)
 {
     $params = array();
     $sql = '';
     if ($qry->hasWhere()) {
         $sql = $qry->getWhere()->getSQL() . ' ';
         $params = $qry->getWhere()->getParams();
     }
     if ($qry->hasGroupBy()) {
         $sql .= 'GROUP BY ' . implode(',', $qry->getGroupBy()) . ' ';
     }
     if ($qry->hasHaving()) {
         $count = 1;
         $sql .= str_replace('WHERE', 'HAVING', $qry->getHaving()->getSQL(), $count) . ' ';
         $params = array_merge($params, $qry->getHaving()->getParams());
     }
     if ($qry->hasOrderBy()) {
         $order = array();
         foreach ($qry->getOrderBy() as $cp => $ord) {
             $order[] = $cp . ' ' . $ord;
         }
         $sql .= 'ORDER BY ' . implode(',', $order) . ' ';
     }
     if ($qry->hasLimit()) {
         $sql .= 'LIMIT ? ';
         $params[] = $qry->getLimit();
     }
     if ($qry->hasOffset() && $qry->getOffset() > 0) {
         $sql .= 'OFFSET ? ';
         $params[] = $qry->getOffset();
     }
     return $this->getQueryResult($sql, $params);
 }
예제 #2
0
 /**
  * @covers Expression::addOrderBy
  * @covers Expression::clearOrderBy
  * @covers Expression::hasOrderBy
  * @covers Expression::getOrderBy
  */
 public function testOrderBy()
 {
     $this->assertFalse($this->object->hasOrderBy(), 'OrderBy deveria ser vazio');
     $this->assertEquals($this->object, $this->object->addOrderby('id'), 'Não retornou o objeto');
     $this->assertTrue($this->object->hasOrderBy(), 'OrderBy não deveria ser vazio');
     $this->assertEquals(array('id' => 'ASC'), $this->object->getOrderBy(), 'Retorno inválido');
     $this->object->addOrderby('cp', 'DESC');
     $this->assertEquals(array('id' => 'ASC', 'cp' => 'DESC'), $this->object->getOrderBy(), 'Retorno inválido');
     $this->object->addOrderby('id', 'DESC');
     $this->assertEquals(array('id' => 'DESC', 'cp' => 'DESC'), $this->object->getOrderBy(), 'Retorno inválido');
     $this->assertEquals($this->object, $this->object->clearOrderBy(), 'Não retornou o objeto');
     $this->assertFalse($this->object->hasOrderBy(), 'OrderBy deveria ser vazio novamente');
     $this->object->addOrderBy('teste', 'xyz');
     $this->assertFalse($this->object->hasGroupBy(), 'GroupBy deveria continuar vazio');
 }