Sqlsrv.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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-2014 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_TestUtil_Common
  24. */
  25. require_once 'Zend/Db/TestUtil/Common.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Db
  29. * @subpackage Table
  30. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Db_TestUtil_Sqlsrv extends Zend_Db_TestUtil_Common
  34. {
  35. public function getParams(array $constants = array())
  36. {
  37. $constants = array(
  38. 'host' => 'TESTS_ZEND_DB_ADAPTER_SQLSRV_HOSTNAME',
  39. 'username' => 'TESTS_ZEND_DB_ADAPTER_SQLSRV_USERNAME',
  40. 'password' => 'TESTS_ZEND_DB_ADAPTER_SQLSRV_PASSWORD',
  41. 'dbname' => 'TESTS_ZEND_DB_ADAPTER_SQLSRV_DATABASE',
  42. );
  43. $constants = parent::getParams($constants);
  44. return $constants;
  45. }
  46. public function getSqlType($type)
  47. {
  48. if ($type == 'IDENTITY') {
  49. return 'INT NOT NULL IDENTITY PRIMARY KEY';
  50. }
  51. return $type;
  52. }
  53. protected function _getColumnsDocuments()
  54. {
  55. return array(
  56. 'doc_id' => 'INTEGER NOT NULL',
  57. 'doc_clob' => 'VARCHAR(8000)',
  58. 'doc_blob' => 'VARCHAR(8000)',
  59. 'PRIMARY KEY' => 'doc_id',
  60. );
  61. }
  62. protected function _getColumnsBugs()
  63. {
  64. return array(
  65. 'bug_id' => 'IDENTITY',
  66. 'bug_description' => 'VARCHAR(100) NULL',
  67. 'bug_status' => 'VARCHAR(20) NULL',
  68. 'created_on' => 'DATETIME NULL',
  69. 'updated_on' => 'DATETIME NULL',
  70. 'reported_by' => 'VARCHAR(100) NULL',
  71. 'assigned_to' => 'VARCHAR(100) NULL',
  72. 'verified_by' => 'VARCHAR(100) NULL',
  73. );
  74. }
  75. protected function _getSqlCreateTable($tableName)
  76. {
  77. $sql = "exec sp_tables @table_name = " . $this->_db->quoteIdentifier($tableName, true);
  78. $stmt = $this->_db->query($sql);
  79. $tableList = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
  80. if (count($tableList) > 0 && $tableName == $tableList[0]['TABLE_NAME']) {
  81. return null;
  82. }
  83. return 'CREATE TABLE ' . $this->_db->quoteIdentifier($tableName);
  84. }
  85. protected function _getSqlDropElement($elementName, $typeElement = 'TABLE')
  86. {
  87. $sql = "exec sp_tables @table_name = " . $this->_db->quoteIdentifier($elementName, true);
  88. $stmt = $this->_db->query($sql);
  89. $elementList = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
  90. if (count($elementList) > 0 && $elementName == $elementList[0]['TABLE_NAME']) {
  91. return "DROP $typeElement " . $this->_db->quoteIdentifier($elementName);
  92. }
  93. return null;
  94. }
  95. protected function _getSqlDropTable($tableName)
  96. {
  97. return $this->_getSqlDropElement($tableName);
  98. }
  99. protected function _getSqlDropView($viewName)
  100. {
  101. return $this->_getSqlDropElement($viewName, 'VIEW');
  102. }
  103. public function getSchema()
  104. {
  105. $desc = $this->_db->describeTable('zfproducts');
  106. return $desc['product_id']['SCHEMA_NAME'];
  107. }
  108. public function createView()
  109. {
  110. parent::dropView();
  111. parent::createView();
  112. }
  113. protected function _rawQuery($sql)
  114. {
  115. $sqlsrv = $this->_db->getConnection();
  116. $retval = sqlsrv_query($sqlsrv, $sql);
  117. if (!$retval) {
  118. $e = sqlsrv_errors();
  119. $e = $e[0]['message'];
  120. require_once 'Zend/Db/Exception.php';
  121. throw new Zend_Db_Exception("SQL error for \"$sql\": $e");
  122. }
  123. }
  124. }