/** * Produces a union-query from two queries. * * @todo Implement support for ORDER, LIMIT etc. * * @param ModelCriteria $mc1 * @param ModelCriteria $mc2 * @return mixed */ public static function union(ModelCriteria $mc1, ModelCriteria $mc2) { $dbMap = Propel::getDatabaseMap($mc1->getDbName()); $db = Propel::getDB($mc1->getDbName()); $con = Propel::getConnection($mc1->getDbName(), Propel::CONNECTION_READ); // we may modify criteria, so copy it first $c1 = clone $mc1; $c2 = clone $mc2; // check that the columns of the main class are already added (if this is the primary ModelCriteria) if (!$c1->hasSelectClause() && !$c1->getPrimaryCriteria()) { $c1->addSelfSelectColumns(); } if (!$c2->hasSelectClause() && !$c2->getPrimaryCriteria()) { $c2->addSelfSelectColumns(); } $con->beginTransaction(); try { $params = array(); $sql1 = BasePeer::createSelectSql($c1, $params); $sql2 = BasePeer::createSelectSql($c2, $params); $stmt = $con->prepare("({$sql1}) UNION ALL ({$sql2})"); $db->bindValues($stmt, $params, $dbMap); $stmt->execute(); $con->commit(); } catch (PropelException $e) { $con->rollback(); throw $e; } return $c1->getFormatter()->init($c1)->format($stmt); }
/** * Define the hydration schema based on a query object. * Fills the Formatter's properties using a Criteria as source * * @param ModelCriteria $criteria * * @return PropelFormatter The current formatter object */ public function init(ModelCriteria $criteria) { $this->dbName = $criteria->getDbName(); $this->setClass($criteria->getModelName()); $this->setWith($criteria->getWith()); $this->asColumns = $criteria->getAsColumns(); $this->hasLimit = $criteria->getLimit() != 0; return $this; }
/** * Do Explain Plan for query object or query string * * @param PropelPDO $con propel connection * @param ModelCriteria|string $query query the criteria or the query string * @throws PropelException * @return PDOStatement A PDO statement executed using the connection, ready to be fetched */ public function doExplainPlan(PropelPDO $con, $query) { $con->beginTransaction(); if ($query instanceof ModelCriteria) { $params = array(); $dbMap = Propel::getDatabaseMap($query->getDbName()); $sql = BasePeer::createSelectSql($query, $params); } else { $sql = $query; } // unique id for the query string $uniqueId = uniqid('Propel', true); $stmt = $con->prepare($this->getExplainPlanQuery($sql, $uniqueId)); if ($query instanceof ModelCriteria) { $this->bindValues($stmt, $params, $dbMap); } $stmt->execute(); // explain plan is save in a table, data must be commit $con->commit(); $stmt = $con->prepare($this->getExplainPlanReadQuery($uniqueId)); $stmt->execute(); return $stmt; }
/** * Do Explain Plan for query object or query string * * @param PropelPDO $con propel connection * @param ModelCriteria|string $query query the criteria or the query string * @throws PropelException * @return PDOStatement A PDO statement executed using the connection, ready to be fetched */ public function doExplainPlan(PropelPDO $con, $query) { if ($query instanceof ModelCriteria) { $params = array(); $dbMap = Propel::getDatabaseMap($query->getDbName()); $sql = BasePeer::createSelectSql($query, $params); $sql = 'EXPLAIN ' . $sql; } else { $sql = 'EXPLAIN ' . $query; } $stmt = $con->prepare($sql); if ($query instanceof ModelCriteria) { $this->bindValues($stmt, $params, $dbMap); } $stmt->execute(); return $stmt; }