PgsqlTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /**
  23. * @see Zend_Db_Select_TestCommon
  24. */
  25. require_once 'Zend/Db/Select/TestCommon.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Db
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Db
  33. * @group Zend_Db_Select
  34. */
  35. class Zend_Db_Select_Pdo_PgsqlTest extends Zend_Db_Select_TestCommon
  36. {
  37. public function getDriver()
  38. {
  39. return 'Pdo_Pgsql';
  40. }
  41. /**
  42. * This test must be done on string field
  43. */
  44. protected function _selectColumnWithColonQuotedParameter ()
  45. {
  46. $product_name = $this->_db->quoteIdentifier('product_name');
  47. $select = $this->_db->select()
  48. ->from('zfproducts')
  49. ->where($product_name . ' = ?', "as'as:x");
  50. return $select;
  51. }
  52. public function testSelectGroupByExpr()
  53. {
  54. $this->markTestSkipped($this->getDriver() . ' does not support expressions in GROUP BY');
  55. }
  56. public function testSelectGroupByAutoExpr()
  57. {
  58. $this->markTestSkipped($this->getDriver() . ' does not support expressions in GROUP BY');
  59. }
  60. /**
  61. * Ensures that from() provides expected behavior using schema specification
  62. *
  63. * @return void
  64. */
  65. public function testSelectFromSchemaSpecified()
  66. {
  67. $schema = 'public';
  68. $table = 'zfbugs';
  69. $sql = $this->_db->select()->from($table, '*', $schema);
  70. $this->assertRegExp("/FROM \"$schema\".\"$table\"/", $sql->__toString());
  71. $rowset = $this->_db->fetchAll($sql);
  72. $this->assertEquals(4, count($rowset));
  73. }
  74. /**
  75. * Ensures that from() provides expected behavior using schema in the table name
  76. *
  77. * @return void
  78. */
  79. public function testSelectFromSchemaInName()
  80. {
  81. $schema = 'public';
  82. $table = 'zfbugs';
  83. $name = "$schema.$table";
  84. $sql = $this->_db->select()->from($name);
  85. $this->assertRegExp("/FROM \"$schema\".\"$table\"/", $sql->__toString());
  86. $rowset = $this->_db->fetchAll($sql);
  87. $this->assertEquals(4, count($rowset));
  88. }
  89. /**
  90. * Ensures that from() overrides schema specification with schema in the table name
  91. *
  92. * @return void
  93. */
  94. public function testSelectFromSchemaInNameOverridesSchemaArgument()
  95. {
  96. $schema = 'public';
  97. $table = 'zfbugs';
  98. $name = "$schema.$table";
  99. $sql = $this->_db->select()->from($name, '*', 'ignored');
  100. $this->assertRegExp("/FROM \"$schema\".\"$table\"/", $sql->__toString());
  101. $rowset = $this->_db->fetchAll($sql);
  102. $this->assertEquals(4, count($rowset));
  103. }
  104. public function testSqlInjectionWithOrder()
  105. {
  106. $select = $this->_db->select();
  107. $select->from(array('p' => 'products'))->order('MD5(1);select');
  108. $this->assertEquals('SELECT "p".* FROM "products" AS "p" ORDER BY "MD5(1);select" ASC', $select->assemble());
  109. $select = $this->_db->select();
  110. $select->from(array('p' => 'products'))->order('name;select;MD5(1)');
  111. $this->assertEquals('SELECT "p".* FROM "products" AS "p" ORDER BY "name;select;MD5(1)" ASC', $select->assemble());
  112. }
  113. /**
  114. * @group ZF-378
  115. */
  116. public function testOrderOfSingleFieldWithDirection()
  117. {
  118. $select = $this->_db->select();
  119. $select->from(array ('p' => 'product'))
  120. ->order('productId DESC');
  121. $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC';
  122. $this->assertEquals($expected, $select->assemble(),
  123. 'Order direction of field failed');
  124. }
  125. /**
  126. * @group ZF-378
  127. */
  128. public function testOrderOfMultiFieldWithDirection()
  129. {
  130. $select = $this->_db->select();
  131. $select->from(array ('p' => 'product'))
  132. ->order(array ('productId DESC', 'userId ASC'));
  133. $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC, "userId" ASC';
  134. $this->assertEquals($expected, $select->assemble(),
  135. 'Order direction of field failed');
  136. }
  137. /**
  138. * @group ZF-378
  139. */
  140. public function testOrderOfMultiFieldButOnlyOneWithDirection()
  141. {
  142. $select = $this->_db->select();
  143. $select->from(array ('p' => 'product'))
  144. ->order(array ('productId', 'userId DESC'));
  145. $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" ASC, "userId" DESC';
  146. $this->assertEquals($expected, $select->assemble(),
  147. 'Order direction of field failed');
  148. }
  149. /**
  150. * @group ZF-378
  151. * @group ZF-381
  152. */
  153. public function testOrderOfConditionalFieldWithDirection($adapter)
  154. {
  155. $select = $this->_db->select();
  156. $select->from(array ('p' => 'product'))
  157. ->order('IF("productId" > 5,1,0) ASC');
  158. $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY IF("productId" > 5,1,0) ASC';
  159. $this->assertEquals($expected, $select->assemble(),
  160. 'Order direction of field failed');
  161. }
  162. }