Commit 216d0e42 authored by Steve Müller's avatar Steve Müller

allow to get bound parameter types from query builder

parent d6ee8aa7
...@@ -307,9 +307,9 @@ class QueryBuilder ...@@ -307,9 +307,9 @@ class QueryBuilder
} }
/** /**
* Gets all defined query parameters for the query being constructed. * Gets all defined query parameters for the query being constructed indexed by parameter index or name.
* *
* @return array The currently defined query parameters. * @return array The currently defined query parameters indexed by parameter index or name.
*/ */
public function getParameters() public function getParameters()
{ {
...@@ -328,6 +328,28 @@ class QueryBuilder ...@@ -328,6 +328,28 @@ class QueryBuilder
return isset($this->params[$key]) ? $this->params[$key] : null; return isset($this->params[$key]) ? $this->params[$key] : null;
} }
/**
* Gets all defined query parameter types for the query being constructed indexed by parameter index or name.
*
* @return array The currently defined query parameter types indexed by parameter index or name.
*/
public function getParameterTypes()
{
return $this->paramTypes;
}
/**
* Gets a (previously set) query parameter type of the query being constructed.
*
* @param mixed $key The key (index or name) of the bound parameter type.
*
* @return mixed The value of the bound parameter type.
*/
public function getParameterType($key)
{
return isset($this->paramTypes[$key]) ? $this->paramTypes[$key] : null;
}
/** /**
* Sets the position of the first result to retrieve (the "offset"). * Sets the position of the first result to retrieve (the "offset").
* *
......
...@@ -595,6 +595,7 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase ...@@ -595,6 +595,7 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals('SELECT u.* FROM users u WHERE u.name = :dcValue1', (string)$qb); $this->assertEquals('SELECT u.* FROM users u WHERE u.name = :dcValue1', (string)$qb);
$this->assertEquals(10, $qb->getParameter('dcValue1')); $this->assertEquals(10, $qb->getParameter('dcValue1'));
$this->assertEquals(\PDO::PARAM_INT, $qb->getParameterType('dcValue1'));
} }
public function testCreateNamedParameterCustomPlaceholder() public function testCreateNamedParameterCustomPlaceholder()
...@@ -607,6 +608,7 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase ...@@ -607,6 +608,7 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals('SELECT u.* FROM users u WHERE u.name = :test', (string)$qb); $this->assertEquals('SELECT u.* FROM users u WHERE u.name = :test', (string)$qb);
$this->assertEquals(10, $qb->getParameter('test')); $this->assertEquals(10, $qb->getParameter('test'));
$this->assertEquals(\PDO::PARAM_INT, $qb->getParameterType('test'));
} }
public function testCreatePositionalParameter() public function testCreatePositionalParameter()
...@@ -619,6 +621,7 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase ...@@ -619,6 +621,7 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals('SELECT u.* FROM users u WHERE u.name = ?', (string)$qb); $this->assertEquals('SELECT u.* FROM users u WHERE u.name = ?', (string)$qb);
$this->assertEquals(10, $qb->getParameter(1)); $this->assertEquals(10, $qb->getParameter(1));
$this->assertEquals(\PDO::PARAM_INT, $qb->getParameterType(1));
} }
/** /**
...@@ -763,4 +766,49 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase ...@@ -763,4 +766,49 @@ class QueryBuilderTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals("SELECT * FROM users", (string) $qb); $this->assertEquals("SELECT * FROM users", (string) $qb);
} }
/**
* @group DBAL-959
*/
public function testGetParameterType()
{
$qb = new QueryBuilder($this->conn);
$qb->select('*')->from('users');
$this->assertNull($qb->getParameterType('name'));
$qb->where('name = :name');
$qb->setParameter('name', 'foo');
$this->assertNull($qb->getParameterType('name'));
$qb->setParameter('name', 'foo', \PDO::PARAM_STR);
$this->assertSame(\PDO::PARAM_STR, $qb->getParameterType('name'));
}
/**
* @group DBAL-959
*/
public function testGetParameterTypes()
{
$qb = new QueryBuilder($this->conn);
$qb->select('*')->from('users');
$this->assertSame(array(), $qb->getParameterTypes());
$qb->where('name = :name');
$qb->setParameter('name', 'foo');
$this->assertSame(array(), $qb->getParameterTypes());
$qb->setParameter('name', 'foo', \PDO::PARAM_STR);
$qb->where('is_active = :isActive');
$qb->setParameter('isActive', true, \PDO::PARAM_BOOL);
$this->assertSame(array('name' => \PDO::PARAM_STR, 'isActive' => \PDO::PARAM_BOOL), $qb->getParameterTypes());
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment