SqlsrvTest.php 6.3 KB

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