SQLParserUtilsTest.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Doctrine\Tests\DBAL;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\SQLParserUtils;
  5. require_once __DIR__ . '/../TestInit.php';
  6. /**
  7. * @group DBAL-78
  8. */
  9. class SQLParserUtilsTest extends \Doctrine\Tests\DbalTestCase
  10. {
  11. static public function dataGetPlaceholderPositions()
  12. {
  13. return array(
  14. // none
  15. array('SELECT * FROM Foo', true, array()),
  16. array('SELECT * FROM Foo', false, array()),
  17. // Positionals
  18. array('SELECT ?', true, array(7)),
  19. array('SELECT * FROM Foo WHERE bar IN (?, ?, ?)', true, array(32, 35, 38)),
  20. array('SELECT ? FROM ?', true, array(7, 14)),
  21. array('SELECT "?" FROM foo', true, array()),
  22. array("SELECT '?' FROM foo", true, array()),
  23. array('SELECT "?" FROM foo WHERE bar = ?', true, array(32)),
  24. array("SELECT '?' FROM foo WHERE bar = ?", true, array(32)),
  25. // named
  26. array('SELECT :foo FROM :bar', false, array(':foo' => array(7), ':bar' => array(17))),
  27. array('SELECT * FROM Foo WHERE bar IN (:name1, :name2)', false, array(':name1' => array(32), ':name2' => array(40))),
  28. array('SELECT ":foo" FROM Foo WHERE bar IN (:name1, :name2)', false, array(':name1' => array(37), ':name2' => array(45))),
  29. array("SELECT ':foo' FROM Foo WHERE bar IN (:name1, :name2)", false, array(':name1' => array(37), ':name2' => array(45))),
  30. );
  31. }
  32. /**
  33. * @dataProvider dataGetPlaceholderPositions
  34. * @param type $query
  35. * @param type $isPositional
  36. * @param type $expectedParamPos
  37. */
  38. public function testGetPlaceholderPositions($query, $isPositional, $expectedParamPos)
  39. {
  40. $actualParamPos = SQLParserUtils::getPlaceholderPositions($query, $isPositional);
  41. $this->assertEquals($expectedParamPos, $actualParamPos);
  42. }
  43. static public function dataExpandListParameters()
  44. {
  45. return array(
  46. // Positional: Very simple with one needle
  47. array(
  48. "SELECT * FROM Foo WHERE foo IN (?)",
  49. array(array(1, 2, 3)),
  50. array(Connection::PARAM_INT_ARRAY),
  51. 'SELECT * FROM Foo WHERE foo IN (?, ?, ?)',
  52. array(1, 2, 3),
  53. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  54. ),
  55. // Positional: One non-list before d one after list-needle
  56. array(
  57. "SELECT * FROM Foo WHERE foo = ? AND bar IN (?)",
  58. array("string", array(1, 2, 3)),
  59. array(\PDO::PARAM_STR, Connection::PARAM_INT_ARRAY),
  60. 'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?)',
  61. array("string", 1, 2, 3),
  62. array(\PDO::PARAM_STR, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  63. ),
  64. // Positional: One non-list after list-needle
  65. array(
  66. "SELECT * FROM Foo WHERE bar IN (?) AND baz = ?",
  67. array(array(1, 2, 3), "foo"),
  68. array(Connection::PARAM_INT_ARRAY, \PDO::PARAM_STR),
  69. 'SELECT * FROM Foo WHERE bar IN (?, ?, ?) AND baz = ?',
  70. array(1, 2, 3, "foo"),
  71. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR)
  72. ),
  73. // Positional: One non-list before and one after list-needle
  74. array(
  75. "SELECT * FROM Foo WHERE foo = ? AND bar IN (?) AND baz = ?",
  76. array(1, array(1, 2, 3), 4),
  77. array(\PDO::PARAM_INT, Connection::PARAM_INT_ARRAY, \PDO::PARAM_INT),
  78. 'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?) AND baz = ?',
  79. array(1, 1, 2, 3, 4),
  80. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  81. ),
  82. // Positional: Two lists
  83. array(
  84. "SELECT * FROM Foo WHERE foo IN (?, ?)",
  85. array(array(1, 2, 3), array(4, 5)),
  86. array(Connection::PARAM_INT_ARRAY, Connection::PARAM_INT_ARRAY),
  87. 'SELECT * FROM Foo WHERE foo IN (?, ?, ?, ?, ?)',
  88. array(1, 2, 3, 4, 5),
  89. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  90. ),
  91. );
  92. }
  93. /**
  94. * @dataProvider dataExpandListParameters
  95. * @param type $q
  96. * @param type $p
  97. * @param type $t
  98. * @param type $expectedQuery
  99. * @param type $expectedParams
  100. * @param type $expectedTypes
  101. */
  102. public function testExpandListParameters($q, $p, $t, $expectedQuery, $expectedParams, $expectedTypes)
  103. {
  104. list($query, $params, $types) = SQLParserUtils::expandListParameters($q, $p, $t);
  105. $this->assertEquals($expectedQuery, $query, "Query was not rewritten correctly.");
  106. $this->assertEquals($expectedParams, $params, "Params dont match");
  107. $this->assertEquals($expectedTypes, $types, "Types dont match");
  108. }
  109. }