SqlsrvTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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_TestCommon
  24. */
  25. require_once 'Zend/Db/Adapter/TestCommon.php';
  26. /**
  27. * @see Zend_Db_Adapter_Sqlsrv
  28. */
  29. require_once 'Zend/Db/Adapter/Sqlsrv.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. */
  38. class Zend_Db_Adapter_SqlsrvTest extends Zend_Db_Adapter_TestCommon
  39. {
  40. protected $_numericDataTypes = array(
  41. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  42. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  43. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  44. 'INT' => Zend_Db::INT_TYPE,
  45. 'SMALLINT' => Zend_Db::INT_TYPE,
  46. 'TINYINT' => Zend_Db::INT_TYPE,
  47. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  48. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  49. 'FLOAT' => Zend_Db::FLOAT_TYPE,
  50. 'MONEY' => Zend_Db::FLOAT_TYPE,
  51. 'NUMERIC' => Zend_Db::FLOAT_TYPE,
  52. 'REAL' => Zend_Db::FLOAT_TYPE,
  53. 'SMALLMONEY' => Zend_Db::FLOAT_TYPE
  54. );
  55. /**
  56. * Test AUTO_QUOTE_IDENTIFIERS option
  57. * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = true
  58. */
  59. public function testAdapterAutoQuoteIdentifiersTrue()
  60. {
  61. $params = $this->_util->getParams();
  62. $params['options'] = array(
  63. Zend_Db::AUTO_QUOTE_IDENTIFIERS => true
  64. );
  65. $db = Zend_Db::factory($this->getDriver(), $params);
  66. $db->getConnection();
  67. $select = $this->_db->select();
  68. $select->from('zfproducts');
  69. $stmt = $this->_db->query($select);
  70. $result = $stmt->fetchAll();
  71. $this->assertEquals(3, count($result), 'Expected 3 rows in first query result');
  72. $this->assertEquals(1, $result[0]['product_id']);
  73. }
  74. /**
  75. * Test the Adapter's insert() method.
  76. * This requires providing an associative array of column=>value pairs.
  77. */
  78. public function testAdapterInsert()
  79. {
  80. $row = array (
  81. 'bug_description' => 'New bug',
  82. 'bug_status' => 'NEW',
  83. 'created_on' => '2007-04-02',
  84. 'updated_on' => '2007-04-02',
  85. 'reported_by' => 'micky',
  86. 'assigned_to' => 'goofy',
  87. 'verified_by' => 'dduck'
  88. );
  89. $rowsAffected = $this->_db->insert('zfbugs', $row);
  90. $this->assertEquals(1, $rowsAffected);
  91. $lastInsertId = $this->_db->lastInsertId();
  92. $this->assertType('string', $lastInsertId);
  93. $this->assertEquals('5', (string) $lastInsertId,
  94. 'Expected new id to be 5');
  95. $lastInsertId = $this->_db->lastInsertId('zfbugs');
  96. $this->assertEquals('5', (string) $lastInsertId,
  97. 'Expected new id to be 5, selecting by table');
  98. }
  99. /**
  100. * Test the Adapter's insert() method.
  101. * This requires providing an associative array of column=>value pairs.
  102. * Multiple rows are insert in one query
  103. */
  104. public function testAdapterMultipleInsert()
  105. {
  106. $row = array (
  107. 'bug_description' => 'New bug',
  108. 'bug_status' => 'NEW',
  109. 'created_on' => '2007-04-02',
  110. 'updated_on' => '2007-04-02',
  111. 'reported_by' => 'micky',
  112. 'assigned_to' => 'goofy',
  113. 'verified_by' => 'dduck'
  114. );
  115. $bugs = $this->_db->quoteIdentifier('zfbugs');
  116. $values = '(?, ?, ?, ?, ?, ?, ?)';
  117. $query = 'INSERT INTO ' . $bugs . ' VALUES ' . implode(',', array($values, $values, $values));
  118. $data = array();
  119. for ($i = 0; $i < 3; $i++) {
  120. foreach ($row as $value) {
  121. $data[] = $value;
  122. }
  123. }
  124. $stmt = $this->_db->query($query, $data);
  125. $rowsAffected = $stmt->rowCount();
  126. $this->assertEquals(3, $rowsAffected);
  127. }
  128. public function testAdapterDescribeTableAttributeColumn()
  129. {
  130. $desc = $this->_db->describeTable('zfproducts');
  131. $this->assertEquals('zfproducts', $desc['product_name']['TABLE_NAME']);
  132. $this->assertEquals('product_name', $desc['product_name']['COLUMN_NAME']);
  133. $this->assertEquals(2, $desc['product_name']['COLUMN_POSITION']);
  134. $this->assertRegExp('/varchar/i', $desc['product_name']['DATA_TYPE']);
  135. $this->assertEquals('', $desc['product_name']['DEFAULT']);
  136. $this->assertTrue($desc['product_name']['NULLABLE'], 'Expected product_name to be nullable');
  137. $this->assertNull($desc['product_name']['SCALE'], 'scale is not 0');
  138. // MS SQL Server reports varchar length in the PRECISION field. Whaaa?!?
  139. $this->assertEquals(100, $desc['product_name']['PRECISION'], 'precision is not 100');
  140. $this->assertFalse($desc['product_name']['PRIMARY'], 'Expected product_name not to be a primary key');
  141. $this->assertNull($desc['product_name']['PRIMARY_POSITION'], 'Expected product_name to return null for PRIMARY_POSITION');
  142. $this->assertFalse($desc['product_name']['IDENTITY'], 'Expected product_name to return false for IDENTITY');
  143. }
  144. /**
  145. * test that describeTable() returns empty array on not existing table
  146. * @group ZF-9079
  147. */
  148. public function testAdapterDescribeTableNotExistingTable()
  149. {
  150. $desc = $this->_db->describeTable('not_existing_table');
  151. $this->assertEquals(0, count($desc), 'Expected to have empty result');
  152. }
  153. public function testAdapterDescribeTablePrimaryKeyColumn()
  154. {
  155. $desc = $this->_db->describeTable('zfproducts');
  156. $this->assertEquals('zfproducts', $desc['product_id']['TABLE_NAME']);
  157. $this->assertEquals('product_id', $desc['product_id']['COLUMN_NAME']);
  158. $this->assertEquals(1, $desc['product_id']['COLUMN_POSITION']);
  159. $this->assertEquals('', $desc['product_id']['DEFAULT']);
  160. $this->assertFalse($desc['product_id']['NULLABLE'], 'Expected product_id not to be nullable');
  161. $this->assertEquals(0, $desc['product_id']['SCALE'], 'scale is not 0');
  162. $this->assertEquals(10, $desc['product_id']['PRECISION'], 'precision is not 10');
  163. $this->assertTrue($desc['product_id']['PRIMARY'], 'Expected product_id to be a primary key');
  164. $this->assertEquals(1, $desc['product_id']['PRIMARY_POSITION']);
  165. }
  166. /**
  167. * Test that quote() takes an array and returns
  168. * an imploded string of comma-separated, quoted elements.
  169. */
  170. public function testAdapterQuoteArray()
  171. {
  172. $array = array("it's", 'all', 'right!');
  173. $value = $this->_db->quote($array);
  174. $this->assertEquals("'it''s', 'all', 'right!'", $value);
  175. }
  176. /**
  177. * test that quote() escapes a double-quote
  178. * character in a string.
  179. */
  180. public function testAdapterQuoteDoubleQuote()
  181. {
  182. $string = 'St John"s Wort';
  183. $value = $this->_db->quote($string);
  184. $this->assertEquals("'St John\"s Wort'", $value);
  185. }
  186. /**
  187. * test that quote() escapes a single-quote
  188. * character in a string.
  189. */
  190. public function testAdapterQuoteSingleQuote()
  191. {
  192. $string = "St John's Wort";
  193. $value = $this->_db->quote($string);
  194. $this->assertEquals("'St John''s Wort'", $value);
  195. }
  196. /**
  197. * test that quoteInto() escapes a double-quote
  198. * character in a string.
  199. */
  200. public function testAdapterQuoteIntoDoubleQuote()
  201. {
  202. $string = 'id=?';
  203. $param = 'St John"s Wort';
  204. $value = $this->_db->quoteInto($string, $param);
  205. $this->assertEquals("id='St John\"s Wort'", $value);
  206. }
  207. /**
  208. * test that quoteInto() escapes a single-quote
  209. * character in a string.
  210. */
  211. public function testAdapterQuoteIntoSingleQuote()
  212. {
  213. $string = 'id = ?';
  214. $param = 'St John\'s Wort';
  215. $value = $this->_db->quoteInto($string, $param);
  216. $this->assertEquals("id = 'St John''s Wort'", $value);
  217. }
  218. public function testAdapterInsertSequence()
  219. {
  220. $this->markTestSkipped($this->getDriver() . ' does not support sequences.');
  221. }
  222. public function testAdapterInsertDbExpr()
  223. {
  224. $bugs = $this->_db->quoteIdentifier('zfbugs');
  225. $bug_id = $this->_db->quoteIdentifier('bug_id');
  226. $expr = new Zend_Db_Expr('2+3');
  227. $row = array (
  228. 'bug_id' => $expr,
  229. 'bug_description' => 'New bug',
  230. 'bug_status' => 'NEW',
  231. 'created_on' => '2007-04-02',
  232. 'updated_on' => '2007-04-02',
  233. 'reported_by' => 'micky',
  234. 'assigned_to' => 'goofy',
  235. 'verified_by' => 'dduck'
  236. );
  237. $this->_db->query("SET IDENTITY_INSERT $bugs ON");
  238. $rowsAffected = $this->_db->insert('zfbugs', $row);
  239. $this->assertEquals(1, $rowsAffected);
  240. $this->_db->query("SET IDENTITY_INSERT $bugs OFF");
  241. $value = $this->_db->fetchOne("SELECT $bug_id FROM $bugs WHERE $bug_id = 5");
  242. $this->assertEquals(5, $value);
  243. }
  244. /**
  245. * @group ZF-1541
  246. */
  247. public function testCharacterSetUtf8()
  248. {
  249. // Create a new adapter
  250. $params = $this->_util->getParams();
  251. $params['charset'] = 'utf8';
  252. $db = Zend_Db::factory($this->getDriver(), $params);
  253. // create a new util object, with the new db adapter
  254. $driver = $this->getDriver();
  255. $utilClass = "Zend_Db_TestUtil_{$driver}";
  256. $util = new $utilClass();
  257. $util->setAdapter($db);
  258. // create test table using no identifier quoting
  259. $util->createTable('charsetutf8', array(
  260. 'id' => 'IDENTITY',
  261. 'stuff' => 'VARCHAR(32)'
  262. ));
  263. $tableName = $this->_util->getTableName('charsetutf8');
  264. $table = $db->quoteIdentifier('charsetutf8');
  265. $db->query("SET IDENTITY_INSERT $table ON");
  266. // insert into the table
  267. $numRows = $db->insert($tableName, array(
  268. 'id' => 1,
  269. 'stuff' => 'äöüß'
  270. ));
  271. // check if the row was inserted as expected
  272. $select = $db->select()->from($tableName, array('id', 'stuff'));
  273. $stmt = $db->query($select);
  274. $fetched = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  275. $a = array(
  276. 0 => array(0 => 1, 1 => 'äöüß')
  277. );
  278. $this->assertEquals($a, $fetched,
  279. 'result of query not as expected');
  280. $db->query("SET IDENTITY_INSERT $table OFF");
  281. // clean up
  282. unset($stmt);
  283. $util->dropTable($tableName);
  284. }
  285. public function testAdapterTransactionCommit()
  286. {
  287. $bugs = $this->_db->quoteIdentifier('zfbugs');
  288. $bug_id = $this->_db->quoteIdentifier('bug_id');
  289. // notice the number of rows in connection 2
  290. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  291. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  292. // start an explicit transaction in connection 1
  293. $this->_db->beginTransaction();
  294. // delete a row in connection 1
  295. $rowsAffected = $this->_db->delete(
  296. 'zfbugs',
  297. "$bug_id = 1"
  298. );
  299. $this->assertEquals(1, $rowsAffected);
  300. // we should still see all rows in connection 2
  301. // because the DELETE has not been committed yet
  302. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  303. $this->assertEquals(3, $count, 'Expecting to still see 4 rows in bugs table (step 2); perhaps Adapter is still in autocommit mode?');
  304. // commit the DELETE
  305. $this->_db->commit();
  306. // now we should see one fewer rows in connection 2
  307. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  308. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 3)');
  309. // delete another row in connection 1
  310. $rowsAffected = $this->_db->delete(
  311. 'zfbugs',
  312. "$bug_id = 2"
  313. );
  314. $this->assertEquals(1, $rowsAffected);
  315. // we should see results immediately, because
  316. // the db connection returns to auto-commit mode
  317. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  318. $this->assertEquals(2, $count);
  319. }
  320. public function testAdapterTransactionRollback()
  321. {
  322. $bugs = $this->_db->quoteIdentifier('zfbugs');
  323. $bug_id = $this->_db->quoteIdentifier('bug_id');
  324. // notice the number of rows in connection 2
  325. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  326. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  327. // start an explicit transaction in connection 1
  328. $this->_db->beginTransaction();
  329. // delete a row in connection 1
  330. $rowsAffected = $this->_db->delete(
  331. 'zfbugs',
  332. "$bug_id = 1"
  333. );
  334. $this->assertEquals(1, $rowsAffected);
  335. // we should still see all rows in connection 2
  336. // because the DELETE has not been committed yet
  337. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  338. $this->assertEquals(3, $count, 'Expecting to still see 4 rows in bugs table (step 2); perhaps Adapter is still in autocommit mode?');
  339. // rollback the DELETE
  340. $this->_db->rollback();
  341. // now we should see the same number of rows
  342. // because the DELETE was rolled back
  343. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  344. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table after DELETE is rolled back (step 3)');
  345. // delete another row in connection 1
  346. $rowsAffected = $this->_db->delete(
  347. 'zfbugs',
  348. "$bug_id = 2"
  349. );
  350. $this->assertEquals(1, $rowsAffected);
  351. // we should see results immediately, because
  352. // the db connection returns to auto-commit mode
  353. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  354. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 4)');
  355. }
  356. public function testCanChangeIsolationLevel()
  357. {
  358. $db = $this->_db;
  359. // All of these should work
  360. $this->assertTrue($db->setTransactionIsolationLevel(SQLSRV_TXN_READ_UNCOMMITTED));
  361. $this->assertTrue($db->setTransactionIsolationLevel(SQLSRV_TXN_READ_COMMITTED));
  362. $this->assertTrue($db->setTransactionIsolationLevel(SQLSRV_TXN_REPEATABLE_READ));
  363. $this->assertTrue($db->setTransactionIsolationLevel(SQLSRV_TXN_SNAPSHOT));
  364. $this->assertTrue($db->setTransactionIsolationLevel(SQLSRV_TXN_SERIALIZABLE));
  365. try {
  366. $db->setTransactionIsolationLevel('not existing isolation level');
  367. $this->fail("Not existing isolation types are allowed to set");
  368. } catch (Zend_Db_Adapter_Sqlsrv_Exception $e) {
  369. }
  370. $this->assertTrue($db->setTransactionIsolationLevel(), "Setting to default should work by passsing null or nothing");
  371. }
  372. /**
  373. * @group ZF-9252
  374. * @see zf-trunk/tests/Zend/Db/Adapter/Zend_Db_Adapter_TestCommon#testAdapterLimit()
  375. */
  376. public function testAdapterLimit()
  377. {
  378. parent::testAdapterLimit();
  379. $products = $this->_db->quoteIdentifier('zfproducts');
  380. $product_id = $this->_db->quoteIdentifier('product_id');
  381. $sql = $this->_db->limit("SELECT * FROM $products ORDER BY $products.$product_id", 1);
  382. $stmt = $this->_db->query($sql);
  383. $result = $stmt->fetchAll();
  384. $this->assertEquals(1, count($result),
  385. 'Expecting row count to be 1');
  386. $this->assertEquals(1, $result[0]['product_id'],
  387. 'Expecting to get product_id 1');
  388. }
  389. /**
  390. * @group ZF-9252
  391. * @see zf-trunk/tests/Zend/Db/Adapter/Zend_Db_Adapter_TestCommon#testAdapterLimitOffset()
  392. */
  393. public function testAdapterLimitOffset()
  394. {
  395. parent::testAdapterLimitOffset();
  396. $products = $this->_db->quoteIdentifier('zfproducts');
  397. $product_id = $this->_db->quoteIdentifier('product_id');
  398. $sql = $this->_db->limit("SELECT * FROM $products ORDER BY $products.$product_id", 1, 1);
  399. $stmt = $this->_db->query($sql);
  400. $result = $stmt->fetchAll();
  401. $this->assertEquals(1, count($result),
  402. 'Expecting row count to be 1');
  403. $this->assertEquals(2, $result[0]['product_id'],
  404. 'Expecting to get product_id 2');
  405. }
  406. /**
  407. * @group ZF-8148
  408. */
  409. public function testAdapterLimitOffsetWithOffsetExceedingRowCount()
  410. {
  411. $products = $this->_db->quoteIdentifier('zfproducts');
  412. $product_id = $this->_db->quoteIdentifier('product_id');
  413. $sql = $this->_db->limit("SELECT * FROM $products ORDER BY $products.$product_id", 2, 2);
  414. $stmt = $this->_db->query($sql);
  415. $result = $stmt->fetchAll();
  416. $this->assertEquals(1, count($result),
  417. 'Expecting row count to be 1');
  418. }
  419. /**
  420. * @group ZF-8148
  421. */
  422. public function testAdapterLimitWithoutOrder()
  423. {
  424. $products = $this->_db->quoteIdentifier('zfproducts');
  425. $product_id = $this->_db->quoteIdentifier('product_id');
  426. $sql = $this->_db->limit("SELECT * FROM $products", 1);
  427. $stmt = $this->_db->query($sql);
  428. $result = $stmt->fetchAll();
  429. $this->assertEquals(1, count($result),
  430. 'Expecting row count to be 1');
  431. $this->assertEquals(1, $result[0]['product_id'],
  432. 'Expecting to get product_id 1');
  433. }
  434. /**
  435. * @group ZF-8148
  436. */
  437. public function testAdapterLimitOffsetWithoutOrder()
  438. {
  439. $products = $this->_db->quoteIdentifier('zfproducts');
  440. $product_id = $this->_db->quoteIdentifier('product_id');
  441. $sql = $this->_db->limit("SELECT * FROM $products", 1, 1);
  442. $stmt = $this->_db->query($sql);
  443. $result = $stmt->fetchAll();
  444. $this->assertEquals(1, count($result),
  445. 'Expecting row count to be 1');
  446. $this->assertEquals(2, $result[0]['product_id'],
  447. 'Expecting to get product_id 2');
  448. }
  449. /**
  450. * @group ZF-8901
  451. */
  452. public function testAdapterLimitOffsetWithMultipleOrderColumns()
  453. {
  454. $products = $this->_db->quoteIdentifier('zfproducts');
  455. $product_id = $this->_db->quoteIdentifier('product_id');
  456. $product_name = $this->_db->quoteIdentifier('product_name');
  457. $sql = $this->_db->limit("SELECT * FROM $products ORDER BY $products.$product_id ASC, $products.$product_name DESC", 1, 1);
  458. $stmt = $this->_db->query($sql);
  459. $result = $stmt->fetchAll();
  460. $this->assertEquals(1, count($result),
  461. 'Expecting row count to be 1');
  462. $this->assertEquals(2, $result[0]['product_id'],
  463. 'Expecting to get product_id 2');
  464. }
  465. public function getDriver()
  466. {
  467. return 'Sqlsrv';
  468. }
  469. }