Sqlsrv.php 4.3 KB

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