Skip to content

Commit 466e537

Browse files
committed
allow to get bound parameter types from query builder
1 parent c8b58d9 commit 466e537

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

lib/Doctrine/DBAL/Query/QueryBuilder.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ public function setParameters(array $params, array $types = array())
307307
}
308308

309309
/**
310-
* Gets all defined query parameters for the query being constructed.
310+
* Gets all defined query parameters for the query being constructed indexed by parameter index or name.
311311
*
312-
* @return array The currently defined query parameters.
312+
* @return array The currently defined query parameters indexed by parameter index or name.
313313
*/
314314
public function getParameters()
315315
{
@@ -328,6 +328,28 @@ public function getParameter($key)
328328
return isset($this->params[$key]) ? $this->params[$key] : null;
329329
}
330330

331+
/**
332+
* Gets all defined query parameter types for the query being constructed indexed by parameter index or name.
333+
*
334+
* @return array The currently defined query parameter types indexed by parameter index or name.
335+
*/
336+
public function getParameterTypes()
337+
{
338+
return $this->paramTypes;
339+
}
340+
341+
/**
342+
* Gets a (previously set) query parameter type of the query being constructed.
343+
*
344+
* @param mixed $key The key (index or name) of the bound parameter type.
345+
*
346+
* @return mixed The value of the bound parameter type.
347+
*/
348+
public function getParameterType($key)
349+
{
350+
return isset($this->paramTypes[$key]) ? $this->paramTypes[$key] : null;
351+
}
352+
331353
/**
332354
* Sets the position of the first result to retrieve (the "offset").
333355
*

tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ public function testCreateNamedParameter()
595595

596596
$this->assertEquals('SELECT u.* FROM users u WHERE u.name = :dcValue1', (string)$qb);
597597
$this->assertEquals(10, $qb->getParameter('dcValue1'));
598+
$this->assertEquals(\PDO::PARAM_INT, $qb->getParameterType('dcValue1'));
598599
}
599600

600601
public function testCreateNamedParameterCustomPlaceholder()
@@ -607,6 +608,7 @@ public function testCreateNamedParameterCustomPlaceholder()
607608

608609
$this->assertEquals('SELECT u.* FROM users u WHERE u.name = :test', (string)$qb);
609610
$this->assertEquals(10, $qb->getParameter('test'));
611+
$this->assertEquals(\PDO::PARAM_INT, $qb->getParameterType('test'));
610612
}
611613

612614
public function testCreatePositionalParameter()
@@ -619,6 +621,7 @@ public function testCreatePositionalParameter()
619621

620622
$this->assertEquals('SELECT u.* FROM users u WHERE u.name = ?', (string)$qb);
621623
$this->assertEquals(10, $qb->getParameter(1));
624+
$this->assertEquals(\PDO::PARAM_INT, $qb->getParameterType(1));
622625
}
623626

624627
/**
@@ -763,4 +766,49 @@ public function testSelectAllWithoutTableAlias()
763766

764767
$this->assertEquals("SELECT * FROM users", (string) $qb);
765768
}
769+
770+
/**
771+
* @group DBAL-959
772+
*/
773+
public function testGetParameterType()
774+
{
775+
$qb = new QueryBuilder($this->conn);
776+
777+
$qb->select('*')->from('users');
778+
779+
$this->assertNull($qb->getParameterType('name'));
780+
781+
$qb->where('name = :name');
782+
$qb->setParameter('name', 'foo');
783+
784+
$this->assertNull($qb->getParameterType('name'));
785+
786+
$qb->setParameter('name', 'foo', \PDO::PARAM_STR);
787+
788+
$this->assertSame(\PDO::PARAM_STR, $qb->getParameterType('name'));
789+
}
790+
791+
/**
792+
* @group DBAL-959
793+
*/
794+
public function testGetParameterTypes()
795+
{
796+
$qb = new QueryBuilder($this->conn);
797+
798+
$qb->select('*')->from('users');
799+
800+
$this->assertSame(array(), $qb->getParameterTypes());
801+
802+
$qb->where('name = :name');
803+
$qb->setParameter('name', 'foo');
804+
805+
$this->assertSame(array(), $qb->getParameterTypes());
806+
807+
$qb->setParameter('name', 'foo', \PDO::PARAM_STR);
808+
809+
$qb->where('is_active = :isActive');
810+
$qb->setParameter('isActive', true, \PDO::PARAM_BOOL);
811+
812+
$this->assertSame(array('name' => \PDO::PARAM_STR, 'isActive' => \PDO::PARAM_BOOL), $qb->getParameterTypes());
813+
}
766814
}

0 commit comments

Comments
 (0)