From b6c66d5a17ea8e89bb1f23b023cbd30c3d5bcc69 Mon Sep 17 00:00:00 2001 From: kphoen Date: Sat, 14 Apr 2012 15:00:52 +0200 Subject: [PATCH] Port PR #332 from Propel --- .../Runtime/Adapter/Pdo/PgsqlAdapter.php | 42 +++++++++++++++++++ .../Runtime/Adapter/Pdo/PgsqlAdapter.php | 30 +++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 tests/Propel/Tests/Runtime/Adapter/Pdo/PgsqlAdapter.php diff --git a/src/Propel/Runtime/Adapter/Pdo/PgsqlAdapter.php b/src/Propel/Runtime/Adapter/Pdo/PgsqlAdapter.php index 56a87b8d1e..4b90195171 100644 --- a/src/Propel/Runtime/Adapter/Pdo/PgsqlAdapter.php +++ b/src/Propel/Runtime/Adapter/Pdo/PgsqlAdapter.php @@ -13,7 +13,9 @@ use Propel\Runtime\Adapter\AdapterInterface; use Propel\Runtime\Connection\ConnectionInterface; use Propel\Runtime\Exception\InvalidArgumentException; +use Propel\Runtime\Propel; use Propel\Runtime\Query\Criteria; +use Propel\Runtime\Util\BasePeer; use \PDO; @@ -182,4 +184,44 @@ 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 ConnectionInterface $con propel connection + * @param Criteria|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(ConnectionInterface $con, $query) + { + if ($query instanceof Criteria) { + $params = array(); + $dbMap = Propel::getServiceContainer()->getDatabaseMap($query->getDbName()); + $sql = BasePeer::createSelectSql($query, $params); + } else { + $sql = $query; + } + + $stmt = $con->prepare($this->getExplainPlanQuery($sql)); + + if ($query instanceof Criteria) { + $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; + } } diff --git a/tests/Propel/Tests/Runtime/Adapter/Pdo/PgsqlAdapter.php b/tests/Propel/Tests/Runtime/Adapter/Pdo/PgsqlAdapter.php new file mode 100644 index 0000000000..08e674484d --- /dev/null +++ b/tests/Propel/Tests/Runtime/Adapter/Pdo/PgsqlAdapter.php @@ -0,0 +1,30 @@ + + */ +class PgsqlAdapterTest extends \PHPUnit_Framework_TestCase +{ + public function testGetExplainPlanQuery() + { + $db = new PgsqlAdapter(); + $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'; + $expected = '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'; + + $this->assertEquals($expected, $db->getExplainPlanQuery($query), 'getExplainPlanQuery() returns a SQL Explain query'); + } +}