SqlsrvTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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-2009 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-2009 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. public function testAdapterDescribeTablePrimaryKeyColumn()
  145. {
  146. $desc = $this->_db->describeTable('zfproducts');
  147. $this->assertEquals('zfproducts', $desc['product_id']['TABLE_NAME']);
  148. $this->assertEquals('product_id', $desc['product_id']['COLUMN_NAME']);
  149. $this->assertEquals(1, $desc['product_id']['COLUMN_POSITION']);
  150. $this->assertEquals('', $desc['product_id']['DEFAULT']);
  151. $this->assertFalse($desc['product_id']['NULLABLE'], 'Expected product_id not to be nullable');
  152. $this->assertEquals(0, $desc['product_id']['SCALE'], 'scale is not 0');
  153. $this->assertEquals(10, $desc['product_id']['PRECISION'], 'precision is not 10');
  154. $this->assertTrue($desc['product_id']['PRIMARY'], 'Expected product_id to be a primary key');
  155. $this->assertEquals(1, $desc['product_id']['PRIMARY_POSITION']);
  156. }
  157. /**
  158. * Test that quote() takes an array and returns
  159. * an imploded string of comma-separated, quoted elements.
  160. */
  161. public function testAdapterQuoteArray()
  162. {
  163. $array = array("it's", 'all', 'right!');
  164. $value = $this->_db->quote($array);
  165. $this->assertEquals("'it''s', 'all', 'right!'", $value);
  166. }
  167. /**
  168. * test that quote() escapes a double-quote
  169. * character in a string.
  170. */
  171. public function testAdapterQuoteDoubleQuote()
  172. {
  173. $string = 'St John"s Wort';
  174. $value = $this->_db->quote($string);
  175. $this->assertEquals("'St John\"s Wort'", $value);
  176. }
  177. /**
  178. * test that quote() escapes a single-quote
  179. * character in a string.
  180. */
  181. public function testAdapterQuoteSingleQuote()
  182. {
  183. $string = "St John's Wort";
  184. $value = $this->_db->quote($string);
  185. $this->assertEquals("'St John''s Wort'", $value);
  186. }
  187. /**
  188. * test that quoteInto() escapes a double-quote
  189. * character in a string.
  190. */
  191. public function testAdapterQuoteIntoDoubleQuote()
  192. {
  193. $string = 'id=?';
  194. $param = 'St John"s Wort';
  195. $value = $this->_db->quoteInto($string, $param);
  196. $this->assertEquals("id='St John\"s Wort'", $value);
  197. }
  198. /**
  199. * test that quoteInto() escapes a single-quote
  200. * character in a string.
  201. */
  202. public function testAdapterQuoteIntoSingleQuote()
  203. {
  204. $string = 'id = ?';
  205. $param = 'St John\'s Wort';
  206. $value = $this->_db->quoteInto($string, $param);
  207. $this->assertEquals("id = 'St John''s Wort'", $value);
  208. }
  209. public function testAdapterInsertSequence()
  210. {
  211. $this->markTestSkipped($this->getDriver() . ' does not support sequences.');
  212. }
  213. public function testAdapterInsertDbExpr()
  214. {
  215. $bugs = $this->_db->quoteIdentifier('zfbugs');
  216. $bug_id = $this->_db->quoteIdentifier('bug_id');
  217. $expr = new Zend_Db_Expr('2+3');
  218. $row = array (
  219. 'bug_id' => $expr,
  220. 'bug_description' => 'New bug',
  221. 'bug_status' => 'NEW',
  222. 'created_on' => '2007-04-02',
  223. 'updated_on' => '2007-04-02',
  224. 'reported_by' => 'micky',
  225. 'assigned_to' => 'goofy',
  226. 'verified_by' => 'dduck'
  227. );
  228. $this->_db->query("SET IDENTITY_INSERT $bugs ON");
  229. $rowsAffected = $this->_db->insert('zfbugs', $row);
  230. $this->assertEquals(1, $rowsAffected);
  231. $this->_db->query("SET IDENTITY_INSERT $bugs OFF");
  232. $value = $this->_db->fetchOne("SELECT $bug_id FROM $bugs WHERE $bug_id = 5");
  233. $this->assertEquals(5, $value);
  234. }
  235. /**
  236. * @group ZF-1541
  237. */
  238. public function testCharacterSetUtf8()
  239. {
  240. // Create a new adapter
  241. $params = $this->_util->getParams();
  242. $params['charset'] = 'utf8';
  243. $db = Zend_Db::factory($this->getDriver(), $params);
  244. // create a new util object, with the new db adapter
  245. $driver = $this->getDriver();
  246. $utilClass = "Zend_Db_TestUtil_{$driver}";
  247. $util = new $utilClass();
  248. $util->setAdapter($db);
  249. // create test table using no identifier quoting
  250. $util->createTable('charsetutf8', array(
  251. 'id' => 'IDENTITY',
  252. 'stuff' => 'VARCHAR(32)'
  253. ));
  254. $tableName = $this->_util->getTableName('charsetutf8');
  255. $table = $db->quoteIdentifier('charsetutf8');
  256. $db->query("SET IDENTITY_INSERT $table ON");
  257. // insert into the table
  258. $numRows = $db->insert($tableName, array(
  259. 'id' => 1,
  260. 'stuff' => 'äöüß'
  261. ));
  262. // check if the row was inserted as expected
  263. $select = $db->select()->from($tableName, array('id', 'stuff'));
  264. $stmt = $db->query($select);
  265. $fetched = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  266. $a = array(
  267. 0 => array(0 => 1, 1 => 'äöüß')
  268. );
  269. $this->assertEquals($a, $fetched,
  270. 'result of query not as expected');
  271. $db->query("SET IDENTITY_INSERT $table OFF");
  272. // clean up
  273. unset($stmt);
  274. $util->dropTable($tableName);
  275. }
  276. public function testAdapterTransactionCommit()
  277. {
  278. $bugs = $this->_db->quoteIdentifier('zfbugs');
  279. $bug_id = $this->_db->quoteIdentifier('bug_id');
  280. // notice the number of rows in connection 2
  281. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  282. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  283. // start an explicit transaction in connection 1
  284. $this->_db->beginTransaction();
  285. // delete a row in connection 1
  286. $rowsAffected = $this->_db->delete(
  287. 'zfbugs',
  288. "$bug_id = 1"
  289. );
  290. $this->assertEquals(1, $rowsAffected);
  291. // we should still see all rows in connection 2
  292. // because the DELETE has not been committed yet
  293. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  294. $this->assertEquals(3, $count, 'Expecting to still see 4 rows in bugs table (step 2); perhaps Adapter is still in autocommit mode?');
  295. // commit the DELETE
  296. $this->_db->commit();
  297. // now we should see one fewer rows in connection 2
  298. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  299. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 3)');
  300. // delete another row in connection 1
  301. $rowsAffected = $this->_db->delete(
  302. 'zfbugs',
  303. "$bug_id = 2"
  304. );
  305. $this->assertEquals(1, $rowsAffected);
  306. // we should see results immediately, because
  307. // the db connection returns to auto-commit mode
  308. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  309. $this->assertEquals(2, $count);
  310. }
  311. public function testAdapterTransactionRollback()
  312. {
  313. $bugs = $this->_db->quoteIdentifier('zfbugs');
  314. $bug_id = $this->_db->quoteIdentifier('bug_id');
  315. // notice the number of rows in connection 2
  316. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  317. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  318. // start an explicit transaction in connection 1
  319. $this->_db->beginTransaction();
  320. // delete a row in connection 1
  321. $rowsAffected = $this->_db->delete(
  322. 'zfbugs',
  323. "$bug_id = 1"
  324. );
  325. $this->assertEquals(1, $rowsAffected);
  326. // we should still see all rows in connection 2
  327. // because the DELETE has not been committed yet
  328. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  329. $this->assertEquals(3, $count, 'Expecting to still see 4 rows in bugs table (step 2); perhaps Adapter is still in autocommit mode?');
  330. // rollback the DELETE
  331. $this->_db->rollback();
  332. // now we should see the same number of rows
  333. // because the DELETE was rolled back
  334. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  335. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table after DELETE is rolled back (step 3)');
  336. // delete another row in connection 1
  337. $rowsAffected = $this->_db->delete(
  338. 'zfbugs',
  339. "$bug_id = 2"
  340. );
  341. $this->assertEquals(1, $rowsAffected);
  342. // we should see results immediately, because
  343. // the db connection returns to auto-commit mode
  344. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  345. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 4)');
  346. }
  347. public function testCanChangeIsolationLevel()
  348. {
  349. $db = $this->_db;
  350. // All of these should work
  351. $this->assertTrue($db->setTransactionIsolationLevel(SQLSRV_TXN_READ_UNCOMMITTED));
  352. $this->assertTrue($db->setTransactionIsolationLevel(SQLSRV_TXN_READ_COMMITTED));
  353. $this->assertTrue($db->setTransactionIsolationLevel(SQLSRV_TXN_REPEATABLE_READ));
  354. $this->assertTrue($db->setTransactionIsolationLevel(SQLSRV_TXN_SNAPSHOT));
  355. $this->assertTrue($db->setTransactionIsolationLevel(SQLSRV_TXN_SERIALIZABLE));
  356. try {
  357. $db->setTransactionIsolationLevel('not existing isolation level');
  358. $this->fail("Not existing isolation types are allowed to set");
  359. } catch (Zend_Db_Adapter_Sqlsrv_Exception $e) {
  360. }
  361. $this->assertTrue($db->setTransactionIsolationLevel(), "Setting to default should work by passsing null or nothing");
  362. }
  363. public function getDriver()
  364. {
  365. return 'Sqlsrv';
  366. }
  367. }