SQLParserUtilsTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. * @group DDC-1372
  9. */
  10. class SQLParserUtilsTest extends \Doctrine\Tests\DbalTestCase
  11. {
  12. static public function dataGetPlaceholderPositions()
  13. {
  14. return array(
  15. // none
  16. array('SELECT * FROM Foo', true, array()),
  17. array('SELECT * FROM Foo', false, array()),
  18. // Positionals
  19. array('SELECT ?', true, array(7)),
  20. array('SELECT * FROM Foo WHERE bar IN (?, ?, ?)', true, array(32, 35, 38)),
  21. array('SELECT ? FROM ?', true, array(7, 14)),
  22. array('SELECT "?" FROM foo', true, array()),
  23. array("SELECT '?' FROM foo", true, array()),
  24. array('SELECT "?" FROM foo WHERE bar = ?', true, array(32)),
  25. array("SELECT '?' FROM foo WHERE bar = ?", true, array(32)),
  26. array(
  27. <<<'SQLDATA'
  28. SELECT * FROM foo WHERE bar = 'it\'s a trap? \\' OR bar = ?
  29. AND baz = "\"quote\" me on it? \\" OR baz = ?
  30. SQLDATA
  31. , true, array(58, 104)
  32. ),
  33. // named
  34. array('SELECT :foo FROM :bar', false, array(7 => 'foo', 17 => 'bar')),
  35. array('SELECT * FROM Foo WHERE bar IN (:name1, :name2)', false, array(32 => 'name1', 40 => 'name2')),
  36. array('SELECT ":foo" FROM Foo WHERE bar IN (:name1, :name2)', false, array(37 => 'name1', 45 => 'name2')),
  37. array("SELECT ':foo' FROM Foo WHERE bar IN (:name1, :name2)", false, array(37 => 'name1', 45 => 'name2')),
  38. array('SELECT :foo_id', false, array(7 => 'foo_id')), // Ticket DBAL-231
  39. array('SELECT @rank := 1', false, array()), // Ticket DBAL-398
  40. array('SELECT @rank := 1 AS rank, :foo AS foo FROM :bar', false, array(27 => 'foo', 44 => 'bar')), // Ticket DBAL-398
  41. array('SELECT * FROM Foo WHERE bar > :start_date AND baz > :start_date', false, array(30 => 'start_date', 52 => 'start_date')), // Ticket GH-113
  42. array('SELECT foo::date as date FROM Foo WHERE bar > :start_date AND baz > :start_date', false, array(46 => 'start_date', 68 => 'start_date')) // Ticket GH-259
  43. );
  44. }
  45. /**
  46. * @dataProvider dataGetPlaceholderPositions
  47. * @param type $query
  48. * @param type $isPositional
  49. * @param type $expectedParamPos
  50. */
  51. public function testGetPlaceholderPositions($query, $isPositional, $expectedParamPos)
  52. {
  53. $actualParamPos = SQLParserUtils::getPlaceholderPositions($query, $isPositional);
  54. $this->assertEquals($expectedParamPos, $actualParamPos);
  55. }
  56. static public function dataExpandListParameters()
  57. {
  58. return array(
  59. // Positional: Very simple with one needle
  60. array(
  61. "SELECT * FROM Foo WHERE foo IN (?)",
  62. array(array(1, 2, 3)),
  63. array(Connection::PARAM_INT_ARRAY),
  64. 'SELECT * FROM Foo WHERE foo IN (?, ?, ?)',
  65. array(1, 2, 3),
  66. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  67. ),
  68. // Positional: One non-list before d one after list-needle
  69. array(
  70. "SELECT * FROM Foo WHERE foo = ? AND bar IN (?)",
  71. array("string", array(1, 2, 3)),
  72. array(\PDO::PARAM_STR, Connection::PARAM_INT_ARRAY),
  73. 'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?)',
  74. array("string", 1, 2, 3),
  75. array(\PDO::PARAM_STR, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  76. ),
  77. // Positional: One non-list after list-needle
  78. array(
  79. "SELECT * FROM Foo WHERE bar IN (?) AND baz = ?",
  80. array(array(1, 2, 3), "foo"),
  81. array(Connection::PARAM_INT_ARRAY, \PDO::PARAM_STR),
  82. 'SELECT * FROM Foo WHERE bar IN (?, ?, ?) AND baz = ?',
  83. array(1, 2, 3, "foo"),
  84. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR)
  85. ),
  86. // Positional: One non-list before and one after list-needle
  87. array(
  88. "SELECT * FROM Foo WHERE foo = ? AND bar IN (?) AND baz = ?",
  89. array(1, array(1, 2, 3), 4),
  90. array(\PDO::PARAM_INT, Connection::PARAM_INT_ARRAY, \PDO::PARAM_INT),
  91. 'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?) AND baz = ?',
  92. array(1, 1, 2, 3, 4),
  93. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  94. ),
  95. // Positional: Two lists
  96. array(
  97. "SELECT * FROM Foo WHERE foo IN (?, ?)",
  98. array(array(1, 2, 3), array(4, 5)),
  99. array(Connection::PARAM_INT_ARRAY, Connection::PARAM_INT_ARRAY),
  100. 'SELECT * FROM Foo WHERE foo IN (?, ?, ?, ?, ?)',
  101. array(1, 2, 3, 4, 5),
  102. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  103. ),
  104. // Positional : Empty "integer" array DDC-1978
  105. array(
  106. "SELECT * FROM Foo WHERE foo IN (?)",
  107. array('foo'=>array()),
  108. array('foo'=>Connection::PARAM_INT_ARRAY),
  109. 'SELECT * FROM Foo WHERE foo IN (?)',
  110. array(),
  111. array()
  112. ),
  113. // Positional : Empty "str" array DDC-1978
  114. array(
  115. "SELECT * FROM Foo WHERE foo IN (?)",
  116. array('foo'=>array()),
  117. array('foo'=>Connection::PARAM_STR_ARRAY),
  118. 'SELECT * FROM Foo WHERE foo IN (?)',
  119. array(),
  120. array()
  121. ),
  122. // Named parameters : Very simple with param int
  123. array(
  124. "SELECT * FROM Foo WHERE foo = :foo",
  125. array('foo'=>1),
  126. array('foo'=>\PDO::PARAM_INT),
  127. 'SELECT * FROM Foo WHERE foo = ?',
  128. array(1),
  129. array(\PDO::PARAM_INT)
  130. ),
  131. // Named parameters : Very simple with param int and string
  132. array(
  133. "SELECT * FROM Foo WHERE foo = :foo AND bar = :bar",
  134. array('bar'=>'Some String','foo'=>1),
  135. array('foo'=>\PDO::PARAM_INT,'bar'=>\PDO::PARAM_STR),
  136. 'SELECT * FROM Foo WHERE foo = ? AND bar = ?',
  137. array(1,'Some String'),
  138. array(\PDO::PARAM_INT, \PDO::PARAM_STR)
  139. ),
  140. // Named parameters : Very simple with one needle
  141. array(
  142. "SELECT * FROM Foo WHERE foo IN (:foo)",
  143. array('foo'=>array(1, 2, 3)),
  144. array('foo'=>Connection::PARAM_INT_ARRAY),
  145. 'SELECT * FROM Foo WHERE foo IN (?, ?, ?)',
  146. array(1, 2, 3),
  147. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  148. ),
  149. // Named parameters: One non-list before d one after list-needle
  150. array(
  151. "SELECT * FROM Foo WHERE foo = :foo AND bar IN (:bar)",
  152. array('foo'=>"string", 'bar'=>array(1, 2, 3)),
  153. array('foo'=>\PDO::PARAM_STR, 'bar'=>Connection::PARAM_INT_ARRAY),
  154. 'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?)',
  155. array("string", 1, 2, 3),
  156. array(\PDO::PARAM_STR, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  157. ),
  158. // Named parameters: One non-list after list-needle
  159. array(
  160. "SELECT * FROM Foo WHERE bar IN (:bar) AND baz = :baz",
  161. array('bar'=>array(1, 2, 3), 'baz'=>"foo"),
  162. array('bar'=>Connection::PARAM_INT_ARRAY, 'baz'=>\PDO::PARAM_STR),
  163. 'SELECT * FROM Foo WHERE bar IN (?, ?, ?) AND baz = ?',
  164. array(1, 2, 3, "foo"),
  165. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR)
  166. ),
  167. // Named parameters: One non-list before and one after list-needle
  168. array(
  169. "SELECT * FROM Foo WHERE foo = :foo AND bar IN (:bar) AND baz = :baz",
  170. array('bar'=>array(1, 2, 3),'foo'=>1, 'baz'=>4),
  171. array('bar'=>Connection::PARAM_INT_ARRAY, 'foo'=>\PDO::PARAM_INT, 'baz'=>\PDO::PARAM_INT),
  172. 'SELECT * FROM Foo WHERE foo = ? AND bar IN (?, ?, ?) AND baz = ?',
  173. array(1, 1, 2, 3, 4),
  174. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  175. ),
  176. // Named parameters: Two lists
  177. array(
  178. "SELECT * FROM Foo WHERE foo IN (:a, :b)",
  179. array('b'=>array(4, 5),'a'=>array(1, 2, 3)),
  180. array('a'=>Connection::PARAM_INT_ARRAY, 'b'=>Connection::PARAM_INT_ARRAY),
  181. 'SELECT * FROM Foo WHERE foo IN (?, ?, ?, ?, ?)',
  182. array(1, 2, 3, 4, 5),
  183. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  184. ),
  185. // Named parameters : With the same name arg type string
  186. array(
  187. "SELECT * FROM Foo WHERE foo <> :arg AND bar = :arg",
  188. array('arg'=>"Some String"),
  189. array('arg'=>\PDO::PARAM_STR),
  190. 'SELECT * FROM Foo WHERE foo <> ? AND bar = ?',
  191. array("Some String","Some String"),
  192. array(\PDO::PARAM_STR,\PDO::PARAM_STR,)
  193. ),
  194. // Named parameters : With the same name arg
  195. array(
  196. "SELECT * FROM Foo WHERE foo IN (:arg) AND NOT bar IN (:arg)",
  197. array('arg'=>array(1, 2, 3)),
  198. array('arg'=>Connection::PARAM_INT_ARRAY),
  199. 'SELECT * FROM Foo WHERE foo IN (?, ?, ?) AND NOT bar IN (?, ?, ?)',
  200. array(1, 2, 3, 1, 2, 3),
  201. array(\PDO::PARAM_INT,\PDO::PARAM_INT, \PDO::PARAM_INT,\PDO::PARAM_INT,\PDO::PARAM_INT, \PDO::PARAM_INT)
  202. ),
  203. // Named parameters : Same name, other name in between DBAL-299
  204. array(
  205. "SELECT * FROM Foo WHERE (:foo = 2) AND (:bar = 3) AND (:foo = 2)",
  206. array('foo'=>2,'bar'=>3),
  207. array('foo'=>\PDO::PARAM_INT,'bar'=>\PDO::PARAM_INT),
  208. 'SELECT * FROM Foo WHERE (? = 2) AND (? = 3) AND (? = 2)',
  209. array(2, 3, 2),
  210. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT)
  211. ),
  212. // Named parameters : Empty "integer" array DDC-1978
  213. array(
  214. "SELECT * FROM Foo WHERE foo IN (:foo)",
  215. array('foo'=>array()),
  216. array('foo'=>Connection::PARAM_INT_ARRAY),
  217. 'SELECT * FROM Foo WHERE foo IN (?)',
  218. array(),
  219. array()
  220. ),
  221. // Named parameters : Two empty "str" array DDC-1978
  222. array(
  223. "SELECT * FROM Foo WHERE foo IN (:foo) OR bar IN (:bar)",
  224. array('foo'=>array(), 'bar'=>array()),
  225. array('foo'=>Connection::PARAM_STR_ARRAY, 'bar'=>Connection::PARAM_STR_ARRAY),
  226. 'SELECT * FROM Foo WHERE foo IN (?) OR bar IN (?)',
  227. array(),
  228. array()
  229. ),
  230. array(
  231. "SELECT * FROM Foo WHERE foo IN (:foo) OR bar = :bar OR baz = :baz",
  232. array('foo' => array(1, 2), 'bar' => 'bar', 'baz' => 'baz'),
  233. array('foo' => Connection::PARAM_INT_ARRAY, 'baz' => 'string'),
  234. 'SELECT * FROM Foo WHERE foo IN (?, ?) OR bar = ? OR baz = ?',
  235. array(1, 2, 'bar', 'baz'),
  236. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR, 'string')
  237. ),
  238. array(
  239. "SELECT * FROM Foo WHERE foo IN (:foo) OR bar = :bar",
  240. array('foo' => array(1, 2), 'bar' => 'bar'),
  241. array('foo' => Connection::PARAM_INT_ARRAY),
  242. 'SELECT * FROM Foo WHERE foo IN (?, ?) OR bar = ?',
  243. array(1, 2, 'bar'),
  244. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR)
  245. ),
  246. // Params/types with colons
  247. array(
  248. "SELECT * FROM Foo WHERE foo = :foo OR bar = :bar",
  249. array(':foo' => 'foo', ':bar' => 'bar'),
  250. array(':foo' => \PDO::PARAM_INT),
  251. 'SELECT * FROM Foo WHERE foo = ? OR bar = ?',
  252. array('foo', 'bar'),
  253. array(\PDO::PARAM_INT, \PDO::PARAM_STR)
  254. ),
  255. array(
  256. "SELECT * FROM Foo WHERE foo = :foo OR bar = :bar",
  257. array(':foo' => 'foo', ':bar' => 'bar'),
  258. array(':foo' => \PDO::PARAM_INT, 'bar' => \PDO::PARAM_INT),
  259. 'SELECT * FROM Foo WHERE foo = ? OR bar = ?',
  260. array('foo', 'bar'),
  261. array(\PDO::PARAM_INT, \PDO::PARAM_INT)
  262. ),
  263. array(
  264. "SELECT * FROM Foo WHERE foo IN (:foo) OR bar = :bar",
  265. array(':foo' => array(1, 2), ':bar' => 'bar'),
  266. array('foo' => Connection::PARAM_INT_ARRAY),
  267. 'SELECT * FROM Foo WHERE foo IN (?, ?) OR bar = ?',
  268. array(1, 2, 'bar'),
  269. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR)
  270. ),
  271. array(
  272. "SELECT * FROM Foo WHERE foo IN (:foo) OR bar = :bar",
  273. array('foo' => array(1, 2), 'bar' => 'bar'),
  274. array(':foo' => Connection::PARAM_INT_ARRAY),
  275. 'SELECT * FROM Foo WHERE foo IN (?, ?) OR bar = ?',
  276. array(1, 2, 'bar'),
  277. array(\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_STR)
  278. ),
  279. );
  280. }
  281. /**
  282. * @dataProvider dataExpandListParameters
  283. * @param type $q
  284. * @param type $p
  285. * @param type $t
  286. * @param type $expectedQuery
  287. * @param type $expectedParams
  288. * @param type $expectedTypes
  289. */
  290. public function testExpandListParameters($q, $p, $t, $expectedQuery, $expectedParams, $expectedTypes)
  291. {
  292. list($query, $params, $types) = SQLParserUtils::expandListParameters($q, $p, $t);
  293. $this->assertEquals($expectedQuery, $query, "Query was not rewritten correctly.");
  294. $this->assertEquals($expectedParams, $params, "Params dont match");
  295. $this->assertEquals($expectedTypes, $types, "Types dont match");
  296. }
  297. public static function dataQueryWithMissingParameters()
  298. {
  299. return array(
  300. array(
  301. "SELECT * FROM foo WHERE bar = :param",
  302. array('other' => 'val'),
  303. array(),
  304. ),
  305. array(
  306. "SELECT * FROM foo WHERE bar = :param",
  307. array(),
  308. array(),
  309. ),
  310. array(
  311. "SELECT * FROM foo WHERE bar = :param",
  312. array(),
  313. array('param' => Connection::PARAM_INT_ARRAY),
  314. ),
  315. array(
  316. "SELECT * FROM foo WHERE bar = :param",
  317. array(),
  318. array(':param' => Connection::PARAM_INT_ARRAY),
  319. ),
  320. array(
  321. "SELECT * FROM foo WHERE bar = :param",
  322. array(),
  323. array('bar' => Connection::PARAM_INT_ARRAY),
  324. ),
  325. array(
  326. "SELECT * FROM foo WHERE bar = :param",
  327. array('bar' => 'value'),
  328. array('bar' => Connection::PARAM_INT_ARRAY),
  329. ),
  330. );
  331. }
  332. /**
  333. * @dataProvider dataQueryWithMissingParameters
  334. */
  335. public function testExceptionIsThrownForMissingParam($query, $params, $types = array())
  336. {
  337. $this->setExpectedException(
  338. 'Doctrine\DBAL\SQLParserUtilsException',
  339. 'Value for :param not found in params array. Params array key should be "param"'
  340. );
  341. SQLParserUtils::expandListParameters($query, $params, $types);
  342. }
  343. }