Skip to content

Commit

Permalink
Merge pull request propelorm#173 from K-Phoen/port-pr-332
Browse files Browse the repository at this point in the history
Port PR propelorm#332 from Propel
  • Loading branch information
willdurand committed Apr 14, 2012
2 parents 02eecda + b6c66d5 commit 7d77665
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Propel/Runtime/Adapter/Pdo/PgsqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
30 changes: 30 additions & 0 deletions tests/Propel/Tests/Runtime/Adapter/Pdo/PgsqlAdapter.php
Original file line number Diff line number Diff line change
@@ -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
*/

namespace Propel\Tests\Runtime\Adapter\Pdo;

use Propel\Runtime\Adapter\Pdo\PgsqlAdapter;

/**
* Tests the Pgsql adapter
*
* @author Kévin Gomez <contact@kevingomez.fr>
*/
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');
}
}

0 comments on commit 7d77665

Please sign in to comment.