MssqlTest.php 14 KB

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