SQLParserUtilsTest.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. array('SELECT :foo_id', false, array(':foo_id' => array(7))), // Ticket DBAL-231
  31. );
  32. }
  33. /**
  34. * @dataProvider dataGetPlaceholderPositions
  35. * @param type $query
  36. * @param type $isPositional
  37. * @param type $expectedParamPos
  38. */
  39. public function testGetPlaceholderPositions($query, $isPositional, $expectedParamPos)
  40. {
  41. $actualParamPos = SQLParserUtils::getPlaceholderPositions($query, $isPositional);
  42. $this->assertEquals($expectedParamPos, $actualParamPos);
  43. }
  44. static public function dataExpandListParameters()
  45. {
  46. return array(
  47. // Positional: Very simple with one needle
  48. array(
  49. "SELECT * FROM Foo WHERE foo IN (?)",
  50. array(array(1, 2, 3)),
  51. array(Connection::PARAM_INT_ARRAY),
  52. 'SELECT * FROM Foo WHERE foo IN (?, ?, ?)',
  53. array(1, 2, 3),
  54. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  55. ),
  56. // Positional: One non-list before d one after list-needle
  57. array(
  58. "SELECT * FROM Foo WHERE foo = ? AND bar IN (?)",
  59. array("string", array(1, 2, 3)),
  60. array(\PDO::PARAM_STR, Connection::PARAM_INT_ARRAY),
  61. 'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?)',
  62. array("string", 1, 2, 3),
  63. array(\PDO::PARAM_STR, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  64. ),
  65. // Positional: One non-list after list-needle
  66. array(
  67. "SELECT * FROM Foo WHERE bar IN (?) AND baz = ?",
  68. array(array(1, 2, 3), "foo"),
  69. array(Connection::PARAM_INT_ARRAY, \PDO::PARAM_STR),
  70. 'SELECT * FROM Foo WHERE bar IN (?, ?, ?) AND baz = ?',
  71. array(1, 2, 3, "foo"),
  72. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR)
  73. ),
  74. // Positional: One non-list before and one after list-needle
  75. array(
  76. "SELECT * FROM Foo WHERE foo = ? AND bar IN (?) AND baz = ?",
  77. array(1, array(1, 2, 3), 4),
  78. array(\PDO::PARAM_INT, Connection::PARAM_INT_ARRAY, \PDO::PARAM_INT),
  79. 'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?) AND baz = ?',
  80. array(1, 1, 2, 3, 4),
  81. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  82. ),
  83. // Positional: Two lists
  84. array(
  85. "SELECT * FROM Foo WHERE foo IN (?, ?)",
  86. array(array(1, 2, 3), array(4, 5)),
  87. array(Connection::PARAM_INT_ARRAY, Connection::PARAM_INT_ARRAY),
  88. 'SELECT * FROM Foo WHERE foo IN (?, ?, ?, ?, ?)',
  89. array(1, 2, 3, 4, 5),
  90. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  91. ),
  92. );
  93. }
  94. /**
  95. * @dataProvider dataExpandListParameters
  96. * @param type $q
  97. * @param type $p
  98. * @param type $t
  99. * @param type $expectedQuery
  100. * @param type $expectedParams
  101. * @param type $expectedTypes
  102. */
  103. public function testExpandListParameters($q, $p, $t, $expectedQuery, $expectedParams, $expectedTypes)
  104. {
  105. list($query, $params, $types) = SQLParserUtils::expandListParameters($q, $p, $t);
  106. $this->assertEquals($expectedQuery, $query, "Query was not rewritten correctly.");
  107. $this->assertEquals($expectedParams, $params, "Params dont match");
  108. $this->assertEquals($expectedTypes, $types, "Types dont match");
  109. }
  110. }