Permalink
Browse files

Merge pull request #332 from K-Phoen/feature-explain-postgres

Add: support for EXPLAIN queries in postgres
  • Loading branch information...
willdurand committed Apr 6, 2012
2 parents 8280f6e + 678676a commit e0afe0246b5240c7c34b89cdafe0ff4a100ab71b
Showing with 69 additions and 0 deletions.
  1. +39 −0 runtime/lib/adapter/DBPostgres.php
  2. +30 −0 test/testsuite/runtime/adapter/DBPostgresTest.php
@@ -199,4 +199,43 @@ public function quoteIdentifierTable($table)
// e.g. 'database.table alias' should be escaped as '"database"."table" "alias"'
return '"' . strtr($table, array('.' => '"."', ' ' => '" "')) . '"';
}
+
+ /**
+ * 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);
+ } else {
+ $sql = $query;
+ }
+
+ $stmt = $con->prepare($this->getExplainPlanQuery($sql));
+
+ if ($query instanceof ModelCriteria) {
+ $this->bindValues($stmt, $params, $dbMap);
+ }
+
+ $stmt->execute();
+
+ return $stmt;
+ }
+
+ /**
+ * Explain Plan compute query getter
+ *
+ * @param string $query query to explain
+ */
+ public function getExplainPlanQuery($query)
+ {
+ return 'EXPLAIN ' . $query;
+ }
}
@@ -0,0 +1,30 @@
+<?php
+
+/**
+ * This file is part of the Propel package.
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @license MIT License
+ */
+
+require_once dirname(__FILE__) . '/../../../../runtime/lib/adapter/DBAdapter.php';
+require_once dirname(__FILE__) . '/../../../../runtime/lib/adapter/DBPostgres.php';
+
+/**
+ * Tests the DbPostgres adapter
+ *
+ * @see BookstoreDataPopulator
+ * @author Kévin Gomez <contact@kevingomez.fr<
+ * @package runtime.adapter
+ */
+class DBPostgresTest extends PHPUnit_Framework_TestCase
+{
+ public function testGetExplainPlanQuery()
+ {
+ $db = new DBPostgres();
+ $query = 'SELECT B.* FROM (SELECT A.*, rownum AS PROPEL_ROWNUM FROM (SELECT book.ID AS ORA_COL_ALIAS_0, book.TITLE AS ORA_COL_ALIAS_1, book.ISBN AS ORA_COL_ALIAS_2, book.PRICE AS ORA_COL_ALIAS_3, book.PUBLISHER_ID AS ORA_COL_ALIAS_4, book.AUTHOR_ID AS ORA_COL_ALIAS_5, author.ID AS ORA_COL_ALIAS_6, author.FIRST_NAME AS ORA_COL_ALIAS_7, author.LAST_NAME AS ORA_COL_ALIAS_8, author.EMAIL AS ORA_COL_ALIAS_9, author.AGE AS ORA_COL_ALIAS_10, book.PRICE AS BOOK_PRICE FROM book, author) A ) B WHERE B.PROPEL_ROWNUM <= 1';
+
+ $this->assertEquals('EXPLAIN SELECT B.* FROM (SELECT A.*, rownum AS PROPEL_ROWNUM FROM (SELECT book.ID AS ORA_COL_ALIAS_0, book.TITLE AS ORA_COL_ALIAS_1, book.ISBN AS ORA_COL_ALIAS_2, book.PRICE AS ORA_COL_ALIAS_3, book.PUBLISHER_ID AS ORA_COL_ALIAS_4, book.AUTHOR_ID AS ORA_COL_ALIAS_5, author.ID AS ORA_COL_ALIAS_6, author.FIRST_NAME AS ORA_COL_ALIAS_7, author.LAST_NAME AS ORA_COL_ALIAS_8, author.EMAIL AS ORA_COL_ALIAS_9, author.AGE AS ORA_COL_ALIAS_10, book.PRICE AS BOOK_PRICE FROM book, author) A ) B WHERE B.PROPEL_ROWNUM <= 1', $db->getExplainPlanQuery($query), 'getExplainPlanQuery() returns a SQL Explain query');
+ }
+}

0 comments on commit e0afe02

Please sign in to comment.