Exemple #1
0
 public function testNeedsSelectAliases()
 {
     $c = new Criteria();
     $this->assertFalse(BasePeer::needsSelectAliases($c), 'Empty Criterias dont need aliases');
     $c = new Criteria();
     $c->addSelectColumn(BookPeer::ID);
     $c->addSelectColumn(BookPeer::TITLE);
     $this->assertFalse(BasePeer::needsSelectAliases($c), 'Criterias with distinct column names dont need aliases');
     $c = new Criteria();
     BookPeer::addSelectColumns($c);
     $this->assertFalse(BasePeer::needsSelectAliases($c), 'Criterias with only the columns of a model dont need aliases');
     $c = new Criteria();
     $c->addSelectColumn(BookPeer::ID);
     $c->addSelectColumn(AuthorPeer::ID);
     $this->assertTrue(BasePeer::needsSelectAliases($c), 'Criterias with common column names do need aliases');
 }
 protected function doCount($con)
 {
     $dbMap = Propel::getDatabaseMap($this->getDbName());
     $db = Propel::getDB($this->getDbName());
     // check that the columns of the main class are already added (if this is the primary ModelCriteria)
     if (!$this->hasSelectClause() && !$this->getPrimaryCriteria()) {
         $this->addSelfSelectColumns();
     }
     $this->configureSelectColumns();
     $needsComplexCount = $this->getGroupByColumns() || $this->getOffset() || $this->getLimit() || $this->getHaving() || in_array(Criteria::DISTINCT, $this->getSelectModifiers()) || count($this->selectQueries) > 0;
     $params = array();
     if ($needsComplexCount) {
         if (BasePeer::needsSelectAliases($this)) {
             if ($this->getHaving()) {
                 throw new PropelException('Propel cannot create a COUNT query when using HAVING and  duplicate column names in the SELECT part');
             }
             $db->turnSelectColumnsToAliases($this);
         }
         $selectSql = BasePeer::createSelectSql($this, $params);
         $sql = 'SELECT COUNT(*) FROM (' . $selectSql . ') propelmatch4cnt';
     } else {
         // Replace SELECT columns with COUNT(*)
         $this->clearSelectColumns()->addSelectColumn('COUNT(*)');
         $sql = BasePeer::createSelectSql($this, $params);
     }
     try {
         $stmt = $con->prepare($sql);
         $db->bindValues($stmt, $params, $dbMap);
         $stmt->execute();
     } catch (Exception $e) {
         Propel::log($e->getMessage(), Propel::LOG_ERR);
         throw new PropelException(sprintf('Unable to execute COUNT statement [%s]', $sql), $e);
     }
     return $stmt;
 }
Exemple #3
0
 /**
  * @see       DBAdapter::applyLimit()
  *
  * @param     string   $sql
  * @param     integer  $offset
  * @param     integer  $limit
  * @param     null|Criteria  $criteria
  */
 public function applyLimit(&$sql, $offset, $limit, $criteria = null)
 {
     if (BasePeer::needsSelectAliases($criteria)) {
         $crit = clone $criteria;
         $selectSql = $this->createSelectSqlPart($crit, $params, true);
         $sql = $selectSql . substr($sql, strpos($sql, 'FROM') - 1);
     }
     $sql = 'SELECT B.* FROM (' . 'SELECT A.*, rownum AS PROPEL_ROWNUM FROM (' . $sql . ') A ' . ') B WHERE ';
     if ($offset > 0) {
         $sql .= ' B.PROPEL_ROWNUM > ' . $offset;
         if ($limit > 0) {
             $sql .= ' AND B.PROPEL_ROWNUM <= ' . ($offset + $limit);
         }
     } else {
         $sql .= ' B.PROPEL_ROWNUM <= ' . $limit;
     }
 }
Exemple #4
0
 protected function getCountStatement($con = null)
 {
     $dbMap = Propel::getDatabaseMap($this->getDbName());
     $db = Propel::getDB($this->getDbName());
     if ($con === null) {
         $con = Propel::getConnection($this->getDbName(), Propel::CONNECTION_READ);
     }
     // check that the columns of the main class are already added (if this is the primary ModelCriteria)
     if (!$this->hasSelectClause() && !$this->getPrimaryCriteria()) {
         $this->addSelfSelectColumns();
     }
     $needsComplexCount = $this->getGroupByColumns() || $this->getOffset() || $this->getLimit() || $this->getHaving() || in_array(Criteria::DISTINCT, $this->getSelectModifiers());
     $con->beginTransaction();
     try {
         $this->basePreSelect($con);
         $params = array();
         if ($needsComplexCount) {
             if (BasePeer::needsSelectAliases($this)) {
                 if ($this->getHaving()) {
                     throw new PropelException('Propel cannot create a COUNT query when using HAVING and  duplicate column names in the SELECT part');
                 }
                 BasePeer::turnSelectColumnsToAliases($this);
             }
             $selectSql = BasePeer::createSelectSql($this, $params);
             $sql = 'SELECT COUNT(*) FROM (' . $selectSql . ') propelmatch4cnt';
         } else {
             // Replace SELECT columns with COUNT(*)
             $this->clearSelectColumns()->addSelectColumn('COUNT(*)');
             $sql = BasePeer::createSelectSql($this, $params);
         }
         $stmt = $con->prepare($sql);
         BasePeer::populateStmtValues($stmt, $params, $dbMap, $db);
         $stmt->execute();
         $con->commit();
     } catch (PropelException $e) {
         $con->rollback();
         throw $e;
     }
     return $stmt;
 }