MssqlTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. /**
  23. * @see Zend_Db_Adapter_Pdo_TestCommon
  24. */
  25. require_once 'Zend/Db/Adapter/Pdo/TestCommon.php';
  26. /**
  27. * @see Zend_Db_Adapter_Pdo_Mysql
  28. */
  29. require_once 'Zend/Db/Adapter/Pdo/Mysql.php';
  30. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  31. /**
  32. * @category Zend
  33. * @package Zend_Db
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Db
  38. * @group Zend_Db_Adapter
  39. */
  40. class Zend_Db_Adapter_Pdo_MssqlTest extends Zend_Db_Adapter_Pdo_TestCommon
  41. {
  42. protected $_numericDataTypes = array(
  43. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  44. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  45. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  46. 'INT' => Zend_Db::INT_TYPE,
  47. 'SMALLINT' => Zend_Db::INT_TYPE,
  48. 'TINYINT' => Zend_Db::INT_TYPE,
  49. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  50. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  51. 'FLOAT' => Zend_Db::FLOAT_TYPE,
  52. 'MONEY' => Zend_Db::FLOAT_TYPE,
  53. 'NUMERIC' => Zend_Db::FLOAT_TYPE,
  54. 'REAL' => Zend_Db::FLOAT_TYPE,
  55. 'SMALLMONEY' => Zend_Db::FLOAT_TYPE
  56. );
  57. /**
  58. * Test AUTO_QUOTE_IDENTIFIERS option
  59. * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = true
  60. */
  61. public function testAdapterAutoQuoteIdentifiersTrue()
  62. {
  63. $params = $this->_util->getParams();
  64. $params['options'] = array(
  65. Zend_Db::AUTO_QUOTE_IDENTIFIERS => true
  66. );
  67. $db = Zend_Db::factory($this->getDriver(), $params);
  68. $db->getConnection();
  69. $select = $this->_db->select();
  70. $select->from('zfproducts');
  71. $stmt = $this->_db->query($select);
  72. $result = $stmt->fetchAll();
  73. $this->assertEquals(3, count($result), 'Expected 3 rows in first query result');
  74. $this->assertEquals(1, $result[0]['product_id']);
  75. }
  76. public function testAdapterDescribeTableAttributeColumn()
  77. {
  78. $desc = $this->_db->describeTable('zfproducts');
  79. $this->assertEquals('zfproducts', $desc['product_name']['TABLE_NAME']);
  80. $this->assertEquals('product_name', $desc['product_name']['COLUMN_NAME']);
  81. $this->assertEquals(2, $desc['product_name']['COLUMN_POSITION']);
  82. $this->assertRegExp('/varchar/i', $desc['product_name']['DATA_TYPE']);
  83. $this->assertEquals('', $desc['product_name']['DEFAULT']);
  84. $this->assertFalse( $desc['product_name']['NULLABLE'], 'Expected product_name not to be nullable');
  85. $this->assertNull( $desc['product_name']['SCALE'], 'scale is not 0');
  86. // MS SQL Server reports varchar length in the PRECISION field. Whaaa?!?
  87. $this->assertEquals(100, $desc['product_name']['PRECISION'], 'precision is not 100');
  88. $this->assertFalse( $desc['product_name']['PRIMARY'], 'Expected product_name not to be a primary key');
  89. $this->assertNull( $desc['product_name']['PRIMARY_POSITION'], 'Expected product_name to return null for PRIMARY_POSITION');
  90. $this->assertFalse( $desc['product_name']['IDENTITY'], 'Expected product_name to return false for IDENTITY');
  91. }
  92. public function testAdapterDescribeTablePrimaryKeyColumn()
  93. {
  94. $desc = $this->_db->describeTable('zfproducts');
  95. $this->assertEquals('zfproducts', $desc['product_id']['TABLE_NAME']);
  96. $this->assertEquals('product_id', $desc['product_id']['COLUMN_NAME']);
  97. $this->assertEquals(1, $desc['product_id']['COLUMN_POSITION']);
  98. $this->assertEquals('', $desc['product_id']['DEFAULT']);
  99. $this->assertFalse( $desc['product_id']['NULLABLE'], 'Expected product_id not to be nullable');
  100. $this->assertEquals(0, $desc['product_id']['SCALE'], 'scale is not 0');
  101. $this->assertEquals(10, $desc['product_id']['PRECISION'], 'precision is not 10');
  102. $this->assertTrue( $desc['product_id']['PRIMARY'], 'Expected product_id to be a primary key');
  103. $this->assertEquals(1, $desc['product_id']['PRIMARY_POSITION']);
  104. }
  105. /**
  106. * Test that quote() takes an array and returns
  107. * an imploded string of comma-separated, quoted elements.
  108. */
  109. public function testAdapterQuoteArray()
  110. {
  111. $array = array("it's", 'all', 'right!');
  112. $value = $this->_db->quote($array);
  113. $this->assertEquals("'it''s', 'all', 'right!'", $value);
  114. }
  115. /**
  116. * test that quote() escapes a double-quote
  117. * character in a string.
  118. */
  119. public function testAdapterQuoteDoubleQuote()
  120. {
  121. $string = 'St John"s Wort';
  122. $value = $this->_db->quote($string);
  123. $this->assertEquals("'St John\"s Wort'", $value);
  124. }
  125. /**
  126. * test that quote() escapes a single-quote
  127. * character in a string.
  128. */
  129. public function testAdapterQuoteSingleQuote()
  130. {
  131. $string = "St John's Wort";
  132. $value = $this->_db->quote($string);
  133. $this->assertEquals("'St John''s Wort'", $value);
  134. }
  135. /**
  136. * test that quoteInto() escapes a double-quote
  137. * character in a string.
  138. */
  139. public function testAdapterQuoteIntoDoubleQuote()
  140. {
  141. $string = 'id=?';
  142. $param = 'St John"s Wort';
  143. $value = $this->_db->quoteInto($string, $param);
  144. $this->assertEquals("id='St John\"s Wort'", $value);
  145. }
  146. /**
  147. * test that quoteInto() escapes a single-quote
  148. * character in a string.
  149. */
  150. public function testAdapterQuoteIntoSingleQuote()
  151. {
  152. $string = 'id = ?';
  153. $param = 'St John\'s Wort';
  154. $value = $this->_db->quoteInto($string, $param);
  155. $this->assertEquals("id = 'St John''s Wort'", $value);
  156. }
  157. public function testAdapterInsertSequence()
  158. {
  159. $this->markTestSkipped($this->getDriver() . ' does not support sequences.');
  160. }
  161. public function testAdapterInsertDbExpr()
  162. {
  163. $bugs = $this->_db->quoteIdentifier('zfbugs');
  164. $bug_id = $this->_db->quoteIdentifier('bug_id');
  165. $expr = new Zend_Db_Expr('2+3');
  166. $row = array (
  167. 'bug_id' => $expr,
  168. 'bug_description' => 'New bug',
  169. 'bug_status' => 'NEW',
  170. 'created_on' => '2007-04-02',
  171. 'updated_on' => '2007-04-02',
  172. 'reported_by' => 'micky',
  173. 'assigned_to' => 'goofy',
  174. 'verified_by' => 'dduck'
  175. );
  176. $this->_db->query("SET IDENTITY_INSERT $bugs ON");
  177. $rowsAffected = $this->_db->insert('zfbugs', $row);
  178. $this->assertEquals(1, $rowsAffected);
  179. $this->_db->query("SET IDENTITY_INSERT $bugs OFF");
  180. $value = $this->_db->fetchOne("SELECT $bug_id FROM $bugs WHERE $bug_id = 5");
  181. $this->assertEquals(5, $value);
  182. }
  183. public function testAdapterTransactionCommit()
  184. {
  185. $bugs = $this->_db->quoteIdentifier('zfbugs');
  186. $bug_id = $this->_db->quoteIdentifier('bug_id');
  187. // notice the number of rows in connection 2
  188. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  189. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  190. // start an explicit transaction in connection 1
  191. $this->_db->beginTransaction();
  192. // delete a row in connection 1
  193. $rowsAffected = $this->_db->delete(
  194. 'zfbugs',
  195. "$bug_id = 1"
  196. );
  197. $this->assertEquals(1, $rowsAffected);
  198. // we should still see all rows in connection 2
  199. // because the DELETE has not been committed yet
  200. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  201. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table (step 2); perhaps Adapter is still in autocommit mode?');
  202. // commit the DELETE
  203. $this->_db->commit();
  204. // now we should see one fewer rows in connection 2
  205. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  206. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 3)');
  207. // delete another row in connection 1
  208. $rowsAffected = $this->_db->delete(
  209. 'zfbugs',
  210. "$bug_id = 2"
  211. );
  212. $this->assertEquals(1, $rowsAffected);
  213. // we should see results immediately, because
  214. // the db connection returns to auto-commit mode
  215. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  216. $this->assertEquals(2, $count);
  217. }
  218. public function testAdapterTransactionRollback()
  219. {
  220. $bugs = $this->_db->quoteIdentifier('zfbugs');
  221. $bug_id = $this->_db->quoteIdentifier('bug_id');
  222. // notice the number of rows in connection 2
  223. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  224. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  225. // start an explicit transaction in connection 1
  226. $this->_db->beginTransaction();
  227. // delete a row in connection 1
  228. $rowsAffected = $this->_db->delete(
  229. 'zfbugs',
  230. "$bug_id = 1"
  231. );
  232. $this->assertEquals(1, $rowsAffected);
  233. // we should still see all rows in connection 2
  234. // because the DELETE has not been committed yet
  235. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  236. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table (step 2); perhaps Adapter is still in autocommit mode?');
  237. // rollback the DELETE
  238. $this->_db->rollback();
  239. // now we should see the same number of rows
  240. // because the DELETE was rolled back
  241. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  242. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table after DELETE is rolled back (step 3)');
  243. // delete another row in connection 1
  244. $rowsAffected = $this->_db->delete(
  245. 'zfbugs',
  246. "$bug_id = 2"
  247. );
  248. $this->assertEquals(1, $rowsAffected);
  249. // we should see results immediately, because
  250. // the db connection returns to auto-commit mode
  251. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  252. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 4)');
  253. }
  254. /**
  255. * Test the Adapter's insert() method.
  256. * This requires providing an associative array of column=>value pairs.
  257. */
  258. public function testAdapterInsert()
  259. {
  260. $row = array (
  261. 'bug_description' => 'New bug',
  262. 'bug_status' => 'NEW',
  263. 'created_on' => '2007-04-02',
  264. 'updated_on' => '2007-04-02',
  265. 'reported_by' => 'micky',
  266. 'assigned_to' => 'goofy',
  267. 'verified_by' => 'dduck'
  268. );
  269. $rowsAffected = $this->_db->insert('zfbugs', $row);
  270. $this->assertEquals(1, $rowsAffected);
  271. $lastInsertId = $this->_db->lastInsertId();
  272. $this->assertType('integer', $lastInsertId);
  273. $this->assertEquals('5', (string) $lastInsertId,
  274. 'Expected new id to be 5');
  275. }
  276. /**
  277. * @group ZF-4099
  278. */
  279. public function testAdapterLimitWorksWithOrderByClause()
  280. {
  281. // more values
  282. $this->_db->insert('zfproducts', array('product_name' => 'Unix'));
  283. $this->_db->insert('zfproducts', array('product_name' => 'Windows'));
  284. $this->_db->insert('zfproducts', array('product_name' => 'AIX'));
  285. $this->_db->insert('zfproducts', array('product_name' => 'I5'));
  286. $this->_db->insert('zfproducts', array('product_name' => 'Linux'));
  287. $select = $this->_db->select();
  288. $select->from('zfproducts')
  289. ->order(array('product_name ASC', 'product_id DESC'))
  290. ->limit(4, 4);
  291. $products = $this->_db->fetchAll($select);
  292. $expectedProducts = array(
  293. 0 => array('product_id' => '3', 'product_name' => 'OS X'),
  294. 1 => array('product_id' => '4', 'product_name' => 'Unix'),
  295. 2 => array('product_id' => '5', 'product_name' => 'Windows'),
  296. 3 => array ('product_id' => '1', 'product_name' => 'Windows')
  297. );
  298. $this->assertEquals($expectedProducts, $products);
  299. }
  300. /**
  301. * @group ZF-4251
  302. */
  303. public function testAdapterLimitWorksWithDistinctClause()
  304. {
  305. $this->_db->insert('zfproducts', array('product_name' => 'Unix'));
  306. $this->_db->insert('zfproducts', array('product_name' => 'Windows'));
  307. $this->_db->insert('zfproducts', array('product_name' => 'AIX'));
  308. $this->_db->insert('zfproducts', array('product_name' => 'I5'));
  309. $this->_db->insert('zfproducts', array('product_name' => 'Linux'));
  310. $sql = 'SELECT DISTINCT product_name FROM zfproducts ORDER BY product_name DESC';
  311. $sql = $this->_db->limit($sql, 3, 3);
  312. $products = $this->_db->fetchAll($sql);
  313. $expectedProducts = array(
  314. 0 => array('product_name' => 'Linux'),
  315. 1 => array('product_name' => 'I5'),
  316. 2 => array('product_name' => 'AIX')
  317. );
  318. $this->assertEquals($expectedProducts, $products);
  319. }
  320. /**
  321. * @group ZF-5823
  322. */
  323. public function testAdapterLimitWithoutOffsetProducesConciseSql()
  324. {
  325. $sql = 'SELECT * FROM foo ORDER BY bar DESC';
  326. $this->assertEquals('SELECT TOP 3 * FROM foo ORDER BY bar DESC', $this->_db->limit($sql, 3));
  327. $sql = 'SELECT DISTINCT * FROM foo ORDER BY bar DESC';
  328. $this->assertEquals('SELECT DISTINCT TOP 3 * FROM foo ORDER BY bar DESC', $this->_db->limit($sql, 3));
  329. }
  330. /**
  331. * @group ZF-7629
  332. */
  333. public function testAdapterDescribeTableWithSchemaName()
  334. {
  335. $productsTableInfo = $this->_db->describeTable('zfproducts', 'dbo');
  336. $this->assertArrayHasKey('product_id', $productsTableInfo);
  337. $this->assertArrayHasKey('product_name', $productsTableInfo);
  338. }
  339. public function getDriver()
  340. {
  341. return 'Pdo_Mssql';
  342. }
  343. }