/** * Executes a COUNT query using either a simple SQL rewrite or, for more complex queries, a * sub-select of the SQL created by createSelectSql() and returns the statement. * * @param Criteria $criteria A Criteria. * @param PropelPDO $con A PropelPDO connection to use. * @return PDOStatement The resultset statement. * @throws PropelException * @see createSelectSql() */ public static function doCount(Criteria $criteria, PropelPDO $con = null) { $dbMap = Propel::getDatabaseMap($criteria->getDbName()); $db = Propel::getDB($criteria->getDbName()); if ($con === null) { $con = Propel::getConnection($criteria->getDbName(), Propel::CONNECTION_READ); } $stmt = null; if ($criteria->isUseTransaction()) { $con->beginTransaction(); } $needsComplexCount = $criteria->getGroupByColumns() || $criteria->getOffset() || $criteria->getLimit() || $criteria->getHaving() || in_array(Criteria::DISTINCT, $criteria->getSelectModifiers()); try { $params = array(); if ($needsComplexCount) { if (self::needsSelectAliases($criteria)) { if ($criteria->getHaving()) { throw new PropelException('Propel cannot create a COUNT query when using HAVING and duplicate column names in the SELECT part'); } self::turnSelectColumnsToAliases($criteria); } $selectSql = self::createSelectSql($criteria, $params); $sql = 'SELECT COUNT(*) FROM (' . $selectSql . ') propelmatch4cnt'; } else { // Replace SELECT columns with COUNT(*) $criteria->clearSelectColumns()->addSelectColumn('COUNT(*)'); $sql = self::createSelectSql($criteria, $params); } $stmt = $con->prepare($sql); self::populateStmtValues($stmt, $params, $dbMap, $db); $stmt->execute(); if ($criteria->isUseTransaction()) { $con->commit(); } } catch (Exception $e) { if ($stmt !== null) { $stmt = null; } if ($criteria->isUseTransaction()) { $con->rollBack(); } Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException(sprintf('Unable to execute COUNT statement [%s]', $sql), $e); } return $stmt; }
/** * Executes query build by createSelectSql() and returns ResultSet. * * @param Criteria $criteria A Criteria. * @param Connection $con A connection to use. * @return ResultSet The resultset. * @throws PropelException * @see createSelectSql() */ public static function doSelect(Criteria $criteria, $con = null) { $arrTables = array_keys($criteria->getTablesColumns()); if (sizeof($arrTables) > 1) { $arrJoins = array(); foreach ($criteria->getJoins() as $objJoin) { if (false) { $objJoin = new Join(); } $arrJoins[] = $objJoin->getLeftTableName(); $arrJoins[] = $objJoin->getRightTableName(); } array_unique($arrJoins); $arrMissedJoinsWhatever = array_diff($arrTables, $arrJoins); } // endif $dbMap = Propel::getDatabaseMap($criteria->getDbName()); if ($con === null) { $con = Propel::getConnection($criteria->getDbName()); } $stmt = null; try { // Transaction support exists for (only?) Postgres, which must // have SELECT statements that include bytea columns wrapped w/ // transactions. if ($criteria->isUseTransaction()) { $con->begin(); } $params = array(); $sql = self::createSelectSql($criteria, $params); $stmt = $con->prepareStatement($sql); $stmt->setLimit($criteria->getLimit()); $stmt->setOffset($criteria->getOffset()); self::populateStmtValues($stmt, $params, $dbMap); $rs = $stmt->executeQuery(ResultSet::FETCHMODE_NUM); if ($criteria->isUseTransaction()) { $con->commit(); } } catch (Exception $e) { if ($stmt) { $stmt->close(); } if ($criteria->isUseTransaction()) { $con->rollback(); } Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException($e); } if (isset($arrMissedJoinsWhatever) && sizeof($arrMissedJoinsWhatever)) { $strBody = "The probable error, a lack of joins tables " . var_export($arrMissedJoinsWhatever, true) . " in query: \n\n" . $sql; // $strBody .= "\n\n".sfContext::getInstance()->getRequest()->getUri(); throw new Exception($strBody); } // endif return $rs; }
/** * Executes query build by createSelectSql() and returns ResultSet. * * @param Criteria $criteria A Criteria. * @param Connection $con A connection to use. * @return ResultSet The resultset. * @throws PropelException * @see createSelectSql() */ public static function doSelect(Criteria $criteria, $con = null) { $dbMap = Propel::getDatabaseMap($criteria->getDbName()); if ($con === null) { $con = Propel::getConnection($criteria->getDbName()); } $stmt = null; try { // Transaction support exists for (only?) Postgres, which must // have SELECT statements that include bytea columns wrapped w/ // transactions. if ($criteria->isUseTransaction()) { $con->begin(); } $params = array(); $sql = self::createSelectSql($criteria, $params); $stmt = $con->prepareStatement($sql); $stmt->setLimit($criteria->getLimit()); $stmt->setOffset($criteria->getOffset()); self::populateStmtValues($stmt, $params, $dbMap); $rs = $stmt->executeQuery(ResultSet::FETCHMODE_NUM); if ($criteria->isUseTransaction()) { $con->commit(); } } catch (Exception $e) { if ($stmt) { $stmt->close(); } if ($criteria->isUseTransaction()) { $con->rollback(); } Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException($e); } return $rs; }
/** * Executes a COUNT query using either a simple SQL rewrite or, for more complex queries, a * sub-select of the SQL created by createSelectSql() and returns the statement. * * @param Criteria $criteria A Criteria. * @param PropelPDO $con A PropelPDO connection to use. * @return PDOStatement The resultset statement. * @throws PropelException * @see createSelectSql() */ public static function doCount(Criteria $criteria, PropelPDO $con = null) { $dbMap = Propel::getDatabaseMap($criteria->getDbName()); $db = Propel::getDB($criteria->getDbName()); if ($con === null) { $con = Propel::getConnection($criteria->getDbName(), Propel::CONNECTION_READ); } $stmt = null; if ($criteria->isUseTransaction()) { $con->beginTransaction(); } $needsComplexCount = $criteria->getGroupByColumns() || $criteria->getOffset() || $criteria->getLimit() || $criteria->getHaving() || in_array(Criteria::DISTINCT, $criteria->getSelectModifiers()); try { $params = array(); if ($needsComplexCount) { $selectSql = self::createSelectSql($criteria, $params); $sql = 'SELECT COUNT(*) FROM (' . $selectSql . ') AS propelmatch4cnt'; } else { // Replace SELECT columns with COUNT(*) $criteria->clearSelectColumns()->addSelectColumn('COUNT(*)'); $sql = self::createSelectSql($criteria, $params); } $stmt = $con->prepare($sql); self::populateStmtValues($stmt, $params, $dbMap, $db); $stmt->execute(); if ($criteria->isUseTransaction()) { $con->commit(); } } catch (Exception $e) { if ($stmt) { $stmt = null; } // close if ($criteria->isUseTransaction()) { $con->rollBack(); } Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException($e); } return $stmt; }