SqliteTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id $
  21. */
  22. require_once 'Zend/Db/Table/Select/TestCommon.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Db
  26. * @subpackage UnitTests
  27. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @group Zend_Db
  30. * @group Zend_Db_Table
  31. * @group Zend_Db_Table_Select
  32. */
  33. class Zend_Db_Table_Select_Pdo_SqliteTest extends Zend_Db_Table_Select_TestCommon
  34. {
  35. public function testSelectFromQualified()
  36. {
  37. $this->markTestSkipped($this->getDriver() . ' does not support qualified table names');
  38. }
  39. public function testSelectJoinQualified()
  40. {
  41. $this->markTestSkipped($this->getDriver() . ' does not support qualified table names');
  42. }
  43. public function testSelectFromForUpdate()
  44. {
  45. $this->markTestSkipped($this->getDriver() . ' does not support FOR UPDATE');
  46. }
  47. public function testSelectJoinRight()
  48. {
  49. $this->markTestSkipped($this->getDriver() . ' does not support RIGHT OUTER JOIN');
  50. }
  51. public function testSelectGroupBy()
  52. {
  53. //SQLite doesn't need different test from 5.3
  54. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  55. return parent::testSelectGroupBy();
  56. }
  57. $select = $this->_selectGroupBy();
  58. $stmt = $this->_db->query($select);
  59. $result = $stmt->fetchAll();
  60. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  61. $bug_id = $this->_db->quoteIdentifier('bug_id');
  62. $key = "$bugs_products.$bug_id";
  63. $this->assertEquals(3, count($result),
  64. 'Expected count of first result set to be 2');
  65. $this->assertEquals(1, $result[0][$key]);
  66. $this->assertEquals(3, $result[0]['thecount'],
  67. 'Expected count(*) of first result set to be 2');
  68. $this->assertEquals(2, $result[1][$key]);
  69. $this->assertEquals(1, $result[1]['thecount']);
  70. }
  71. public function testSelectGroupByQualified()
  72. {
  73. //SQLite doesn't need different test from 5.3
  74. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  75. return parent::testSelectGroupByQualified();
  76. }
  77. $select = $this->_selectGroupByQualified();
  78. $stmt = $this->_db->query($select);
  79. $result = $stmt->fetchAll();
  80. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  81. $bug_id = $this->_db->quoteIdentifier('bug_id');
  82. $key = "$bugs_products.$bug_id";
  83. $this->assertEquals(3, count($result),
  84. 'Expected count of first result set to be 2');
  85. $this->assertEquals(1, $result[0][$key]);
  86. $this->assertEquals(3, $result[0]['thecount'],
  87. 'Expected count(*) of first result set to be 2');
  88. $this->assertEquals(2, $result[1][$key]);
  89. $this->assertEquals(1, $result[1]['thecount']);
  90. }
  91. public function testSelectHaving()
  92. {
  93. //SQLite doesn't need different test from 5.3
  94. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  95. return parent::testSelectHaving();
  96. }
  97. $select = $this->_selectHaving();
  98. $stmt = $this->_db->query($select);
  99. $result = $stmt->fetchAll();
  100. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  101. $bug_id = $this->_db->quoteIdentifier('bug_id');
  102. $key = "$bugs_products.$bug_id";
  103. $this->assertEquals(2, count($result));
  104. $this->assertEquals(1, $result[0][$key]);
  105. $this->assertEquals(3, $result[0]['thecount']);
  106. }
  107. public function testSelectHavingWithParameter()
  108. {
  109. //SQLite doesn't need different test from 5.3
  110. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  111. return parent::testSelectHavingWithParameter();
  112. }
  113. $select = $this->_selectHavingWithParameter();
  114. $stmt = $this->_db->query($select);
  115. $result = $stmt->fetchAll();
  116. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  117. $bug_id = $this->_db->quoteIdentifier('bug_id');
  118. $key = "$bugs_products.$bug_id";
  119. $this->assertEquals(2, count($result));
  120. $this->assertEquals(1, $result[0][$key]);
  121. $this->assertEquals(3, $result[0]['thecount']);
  122. }
  123. public function testSelectHavingOr()
  124. {
  125. //SQLite doesn't need different test from 5.3
  126. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  127. return parent::testSelectHavingOr();
  128. }
  129. $select = $this->_selectHavingOr();
  130. $stmt = $this->_db->query($select);
  131. $result = $stmt->fetchAll();
  132. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  133. $bug_id = $this->_db->quoteIdentifier('bug_id');
  134. $key = "$bugs_products.$bug_id";
  135. $this->assertEquals(3, count($result));
  136. $this->assertEquals(1, $result[0][$key]);
  137. $this->assertEquals(3, $result[0]['thecount']);
  138. $this->assertEquals(2, $result[1][$key]);
  139. $this->assertEquals(1, $result[1]['thecount']);
  140. }
  141. public function testSelectHavingOrWithParameter()
  142. {
  143. //SQLite doesn't need different test from 5.3
  144. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  145. return parent::testSelectHavingOrWithParameter();
  146. }
  147. $select = $this->_selectHavingOrWithParameter();
  148. $stmt = $this->_db->query($select);
  149. $result = $stmt->fetchAll();
  150. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  151. $bug_id = $this->_db->quoteIdentifier('bug_id');
  152. $key = "$bugs_products.$bug_id";
  153. $this->assertEquals(3, count($result));
  154. $this->assertEquals(1, $result[0][$key]);
  155. $this->assertEquals(3, $result[0]['thecount']);
  156. $this->assertEquals(2, $result[1][$key]);
  157. $this->assertEquals(1, $result[1]['thecount']);
  158. }
  159. public function getDriver()
  160. {
  161. return 'Pdo_Sqlite';
  162. }
  163. public function testSqlInjectionWithOrder()
  164. {
  165. $select = $this->_db->select();
  166. $select->from(array('p' => 'products'))->order('MD5(1);select');
  167. $this->assertEquals('SELECT "p".* FROM "products" AS "p" ORDER BY "MD5(1);select" ASC', $select->assemble());
  168. $select = $this->_db->select();
  169. $select->from(array('p' => 'products'))->order('name;select;MD5(1)');
  170. $this->assertEquals('SELECT "p".* FROM "products" AS "p" ORDER BY "name;select;MD5(1)" ASC', $select->assemble());
  171. }
  172. /**
  173. * @group ZF-378
  174. */
  175. public function testOrderOfSingleFieldWithDirection()
  176. {
  177. $select = $this->_db->select();
  178. $select->from(array ('p' => 'product'))
  179. ->order('productId DESC');
  180. $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC';
  181. $this->assertEquals($expected, $select->assemble(),
  182. 'Order direction of field failed');
  183. }
  184. /**
  185. * @group ZF-378
  186. */
  187. public function testOrderOfMultiFieldWithDirection()
  188. {
  189. $select = $this->_db->select();
  190. $select->from(array ('p' => 'product'))
  191. ->order(array ('productId DESC', 'userId ASC'));
  192. $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC, "userId" ASC';
  193. $this->assertEquals($expected, $select->assemble(),
  194. 'Order direction of field failed');
  195. }
  196. /**
  197. * @group ZF-378
  198. */
  199. public function testOrderOfMultiFieldButOnlyOneWithDirection()
  200. {
  201. $select = $this->_db->select();
  202. $select->from(array ('p' => 'product'))
  203. ->order(array ('productId', 'userId DESC'));
  204. $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" ASC, "userId" DESC';
  205. $this->assertEquals($expected, $select->assemble(),
  206. 'Order direction of field failed');
  207. }
  208. /**
  209. * @group ZF-378
  210. * @group ZF-381
  211. */
  212. public function testOrderOfConditionalFieldWithDirection($adapter)
  213. {
  214. $select = $this->_db->select();
  215. $select->from(array ('p' => 'product'))
  216. ->order('IF("productId" > 5,1,0) ASC');
  217. $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY IF("productId" > 5,1,0) ASC';
  218. $this->assertEquals($expected, $select->assemble(),
  219. 'Order direction of field failed');
  220. }
  221. }