PgsqlTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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-2010 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/Adapter/Pdo/TestCommon.php';
  23. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  24. /**
  25. * @category Zend
  26. * @package Zend_Db
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @group Zend_Db
  31. * @group Zend_Db_Adapter
  32. */
  33. class Zend_Db_Adapter_Pdo_PgsqlTest extends Zend_Db_Adapter_Pdo_TestCommon
  34. {
  35. protected $_numericDataTypes = array(
  36. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  37. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  38. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  39. 'INTEGER' => Zend_Db::INT_TYPE,
  40. 'SERIAL' => Zend_Db::INT_TYPE,
  41. 'SMALLINT' => Zend_Db::INT_TYPE,
  42. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  43. 'BIGSERIAL' => Zend_Db::BIGINT_TYPE,
  44. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  45. 'DOUBLE PRECISION' => Zend_Db::FLOAT_TYPE,
  46. 'NUMERIC' => Zend_Db::FLOAT_TYPE,
  47. 'REAL' => Zend_Db::FLOAT_TYPE
  48. );
  49. public function testAdapterDescribeTablePrimaryAuto()
  50. {
  51. $desc = $this->_db->describeTable('zfbugs');
  52. $this->assertTrue($desc['bug_id']['PRIMARY']);
  53. $this->assertEquals(1, $desc['bug_id']['PRIMARY_POSITION']);
  54. $this->assertTrue($desc['bug_id']['IDENTITY']);
  55. }
  56. /**
  57. * Test the Adapter's insert() method.
  58. * This requires providing an associative array of column=>value pairs.
  59. */
  60. public function testAdapterInsert()
  61. {
  62. $row = array (
  63. 'bug_description' => 'New bug',
  64. 'bug_status' => 'NEW',
  65. 'created_on' => '2007-04-02',
  66. 'updated_on' => '2007-04-02',
  67. 'reported_by' => 'micky',
  68. 'assigned_to' => 'goofy'
  69. );
  70. $rowsAffected = $this->_db->insert('zfbugs', $row);
  71. $this->assertEquals(1, $rowsAffected);
  72. $lastInsertId = $this->_db->lastInsertId('zfbugs', 'bug_id');
  73. $lastSequenceId = $this->_db->lastSequenceId('zfbugs_bug_id_seq');
  74. $this->assertEquals((string) $lastInsertId, (string) $lastSequenceId,
  75. 'Expected last insert id to be equal to last sequence id');
  76. $this->assertEquals('5', (string) $lastInsertId,
  77. 'Expected new id to be 5');
  78. }
  79. public function testAdapterInsertSequence()
  80. {
  81. $row = array (
  82. 'product_id' => $this->_db->nextSequenceId('zfproducts_seq'),
  83. 'product_name' => 'Solaris',
  84. );
  85. $rowsAffected = $this->_db->insert('zfproducts', $row);
  86. $this->assertEquals(1, $rowsAffected);
  87. $lastInsertId = $this->_db->lastInsertId('zfproducts');
  88. $lastSequenceId = $this->_db->lastSequenceId('zfproducts_seq');
  89. $this->assertEquals((string) $lastInsertId, (string) $lastSequenceId,
  90. 'Expected last insert id to be equal to last sequence id');
  91. $this->assertEquals('4', (string) $lastInsertId,
  92. 'Expected new id to be 4');
  93. }
  94. public function testAdapterInsertDbExpr()
  95. {
  96. $bugs = $this->_db->quoteIdentifier('zfbugs');
  97. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  98. $bug_description = $this->_db->quoteIdentifier('bug_description', true);
  99. $expr = new Zend_Db_Expr('2+3');
  100. $row = array (
  101. 'bug_id' => $expr,
  102. 'bug_description' => 'New bug 5',
  103. 'bug_status' => 'NEW',
  104. 'created_on' => '2007-04-02',
  105. 'updated_on' => '2007-04-02',
  106. 'reported_by' => 'micky',
  107. 'assigned_to' => 'goofy',
  108. 'verified_by' => 'dduck'
  109. );
  110. $rowsAffected = $this->_db->insert('zfbugs', $row);
  111. $this->assertEquals(1, $rowsAffected);
  112. $value = $this->_db->fetchOne("SELECT $bug_description FROM $bugs WHERE $bug_id = 5");
  113. $this->assertEquals('New bug 5', $value);
  114. }
  115. /**
  116. * Test that quote() takes an array and returns
  117. * an imploded string of comma-separated, quoted elements.
  118. */
  119. public function testAdapterQuoteArray()
  120. {
  121. $array = array("it's", 'all', 'right!');
  122. $value = $this->_db->quote($array);
  123. $this->assertEquals("'it''s', 'all', 'right!'", $value);
  124. }
  125. /**
  126. * test that quote() escapes a double-quote
  127. * character in a string.
  128. */
  129. public function testAdapterQuoteDoubleQuote()
  130. {
  131. $value = $this->_db->quote('St John"s Wort');
  132. $this->assertEquals("'St John\"s Wort'", $value);
  133. }
  134. /**
  135. * test that quote() escapes a single-quote
  136. * character in a string.
  137. */
  138. public function testAdapterQuoteSingleQuote()
  139. {
  140. $string = "St John's Wort";
  141. $value = $this->_db->quote($string);
  142. $this->assertEquals("'St John''s Wort'", $value);
  143. }
  144. /**
  145. * test that quoteInto() escapes a double-quote
  146. * character in a string.
  147. */
  148. public function testAdapterQuoteIntoDoubleQuote()
  149. {
  150. $value = $this->_db->quoteInto('id=?', 'St John"s Wort');
  151. $this->assertEquals("id='St John\"s Wort'", $value);
  152. }
  153. /**
  154. * test that quoteInto() escapes a single-quote
  155. * character in a string.
  156. */
  157. public function testAdapterQuoteIntoSingleQuote()
  158. {
  159. $value = $this->_db->quoteInto('id = ?', 'St John\'s Wort');
  160. $this->assertEquals("id = 'St John''s Wort'", $value);
  161. }
  162. function getDriver()
  163. {
  164. return 'Pdo_Pgsql';
  165. }
  166. /**
  167. * @group ZF-3972
  168. */
  169. public function testAdapterCharacterVarying()
  170. {
  171. $this->_util->createTable('zf_pgsql_charvary',
  172. array('pg_id' => 'character varying(4) NOT NULL',
  173. 'pg_info' => "character varying(1) NOT NULL DEFAULT 'A'::character varying"));
  174. $description = $this->_db->describeTable('zf_pgsql_charvary');
  175. $this->_util->dropTable('zf_pgsql_charvary');
  176. $this->assertEquals(null , $description['pg_id']['DEFAULT']);
  177. $this->assertEquals('A', $description['pg_info']['DEFAULT']);
  178. }
  179. /**
  180. * @group ZF-7640
  181. */
  182. public function testAdapterBpchar()
  183. {
  184. $this->_util->createTable('zf_pgsql_bpchar',
  185. array('pg_name' => "character(100) DEFAULT 'Default'::bpchar"));
  186. $description = $this->_db->describeTable('zf_pgsql_bpchar');
  187. $this->_util->dropTable('zf_pgsql_bpchar');
  188. $this->assertEquals('Default', $description['pg_name']['DEFAULT']);
  189. }
  190. /**
  191. * @group ZF-10160
  192. * @group ZF-10257
  193. */
  194. public function testQuoteIdentifiersInSequence()
  195. {
  196. $this->_util->createSequence('camelCase_id_seq');
  197. $this->_util->createSequence("single'quotes");
  198. $this->_db->nextSequenceId('camelCase_id_seq');
  199. $this->_db->nextSequenceId($this->_db->quoteIdentifier('camelCase_id_seq', true));
  200. $this->_db->lastSequenceId('camelCase_id_seq');
  201. $this->_db->lastSequenceId($this->_db->quoteIdentifier('camelCase_id_seq', true));
  202. require_once 'Zend/Db/Expr.php';
  203. $this->_db->lastSequenceId(new Zend_Db_Expr('camelCase_id_seq'));
  204. $lastId = $this->_db->lastSequenceId(new Zend_Db_Expr('camelCase_id_seq'));
  205. $this->assertEquals(2, $lastId);
  206. $this->_db->nextSequenceId('"public"."camelCase_id_seq"');
  207. $lastId = $this->_db->lastSequenceId('"public"."camelCase_id_seq"');
  208. $this->assertEquals(3, $lastId);
  209. $this->_db->nextSequenceId("single'quotes");
  210. $lastId = $this->_db->lastSequenceId("single'quotes");
  211. $this->assertEquals(1, $lastId);
  212. $this->_util->dropSequence("single'quotes");
  213. $this->_util->dropSequence('camelCase_id_seq');
  214. }
  215. }