SqlsrvTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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-2012 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/Statement/TestCommon.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Db
  26. * @subpackage UnitTests
  27. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @group Zend_Db
  30. * @group Zend_Db_Statement
  31. */
  32. class Zend_Db_Statement_SqlsrvTest extends Zend_Db_Statement_TestCommon
  33. {
  34. // http://msdn.microsoft.com/en-us/library/cc296197(SQL.90).aspx
  35. protected $_getColumnMetaKeys = array(
  36. 'Name' , 'Type', 'Size', 'Precision', 'Scale', 'Nullable'
  37. );
  38. public function testStatementExecuteWithParams()
  39. {
  40. $products = $this->_db->quoteIdentifier('zfproducts');
  41. // Make IDENTITY column accept explicit value.
  42. // This can be done in only one table in a given session.
  43. sqlsrv_query($this->_db->getConnection(), "SET IDENTITY_INSERT $products ON");
  44. parent::testStatementExecuteWithParams();
  45. sqlsrv_query($this->_db->getConnection(), "SET IDENTITY_INSERT $products OFF");
  46. }
  47. public function testStatementBindParamByName()
  48. {
  49. $this->markTestSkipped($this->getDriver() . ' does not support bind by name.');
  50. }
  51. public function testStatementBindValueByName()
  52. {
  53. $this->markTestSkipped($this->getDriver() . ' does not support bind by name.');
  54. }
  55. public function testStatementBindParamByPosition()
  56. {
  57. $this->markTestSkipped($this->getDriver() . ' does not support bind by position.');
  58. }
  59. public function testStatementBindValueByPosition()
  60. {
  61. $this->markTestSkipped($this->getDriver() . ' does not support bind by position.');
  62. }
  63. public function testStatementNextRowset()
  64. {
  65. $products = $this->_db->quoteIdentifier('zfproducts');
  66. $product_id = $this->_db->quoteIdentifier('product_id');
  67. $query = "SELECT * FROM $products WHERE $product_id > 1 ORDER BY $product_id ASC";
  68. $stmt = $this->_db->query($query . ';' . $query);
  69. $result1 = $stmt->fetchAll();
  70. $stmt->nextRowset();
  71. $result2 = $stmt->fetchAll();
  72. $this->assertEquals(count($result1), count($result2));
  73. $this->assertEquals($result1, $result2);
  74. $stmt->closeCursor();
  75. }
  76. /*
  77. * @group ZF-8138
  78. */
  79. public function testStatementNextRowsetWithProcedure()
  80. {
  81. $products = $this->_db->quoteIdentifier('zfproducts');
  82. $product_id = $this->_db->quoteIdentifier('product_id');
  83. $product_name = $this->_db->quoteIdentifier('product_name');
  84. $products_procedure = $this->_db->quoteIdentifier('#InsertIntoProducts');
  85. $prodecure = "CREATE PROCEDURE $products_procedure
  86. @ProductName varchar(100)
  87. AS
  88. BEGIN
  89. -- insert row (result set 1)
  90. INSERT INTO $products
  91. ($product_name)
  92. VALUES
  93. (@ProductName);
  94. -- Get results (result set 2)
  95. SELECT * FROM $products;
  96. END";
  97. // create procedure
  98. $this->_db->query($prodecure);
  99. $stmt = $this->_db->query('{call ' . $products_procedure .'(?)}', array('Product'));
  100. $result1 = $stmt->rowCount();
  101. $this->assertEquals(1, $result1, 'Expected 1 row to be inserted');
  102. $stmt->nextRowset();
  103. $result2 = $stmt->fetchAll();
  104. $this->assertEquals(4, count($result2), 'Expected 3 results from original data and one 1 row');
  105. $this->assertEquals('Product', $result2[3]['product_name']);
  106. $stmt->closeCursor();
  107. }
  108. /*
  109. * @group ZF-7559
  110. */
  111. public function testStatementWithProcedure()
  112. {
  113. $products = $this->_db->quoteIdentifier('zfproducts');
  114. $products_procedure = $this->_db->quoteIdentifier('#GetProducts');
  115. $prodecure = "CREATE PROCEDURE $products_procedure
  116. AS
  117. BEGIN
  118. SELECT * FROM $products;
  119. END";
  120. // create procedure
  121. $this->_db->query($prodecure);
  122. $stmt = $this->_db->query('EXECUTE ' . $products_procedure);
  123. $result1 = $stmt->fetchAll();
  124. $this->assertEquals(3, count($result1), 'Expected 3 results from original data');
  125. $stmt->closeCursor();
  126. }
  127. public function testStatementErrorInfo()
  128. {
  129. $products = $this->_db->quoteIdentifier('zfproducts');
  130. $product_id = $this->_db->quoteIdentifier('product_id');
  131. $query = "INVALID SELECT * FROM INVALID TABLE WHERE $product_id > 1 ORDER BY $product_id ASC";
  132. $stmt = new Zend_Db_Statement_Sqlsrv($this->_db, $query);
  133. try {
  134. $stmt->fetchAll();
  135. $this->fail("Invalid query should have throw an error");
  136. } catch (Zend_Db_Statement_Sqlsrv_Exception $e) {
  137. // Exception is thrown, nothing to worry about
  138. $this->assertEquals(-11, $e->getCode());
  139. }
  140. $this->assertNotSame(false, $stmt->errorCode());
  141. $this->assertEquals(-11, $stmt->errorCode());
  142. $errors = $stmt->errorInfo();
  143. $this->assertEquals(2, count($errors));
  144. $this->assertEquals($stmt->errorCode(), $errors[0]);
  145. $this->assertTrue(is_string($errors[1]));
  146. }
  147. public function getDriver()
  148. {
  149. return 'Sqlsrv';
  150. }
  151. }