Sqlsrv.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\db\connector;
  12. use PDO;
  13. use think\db\Connection;
  14. /**
  15. * Sqlsrv数据库驱动
  16. */
  17. class Sqlsrv extends Connection
  18. {
  19. // PDO连接参数
  20. protected $params = [
  21. PDO::ATTR_CASE => PDO::CASE_NATURAL,
  22. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  23. PDO::ATTR_STRINGIFY_FETCHES => false,
  24. ];
  25. /**
  26. * 解析pdo连接的dsn信息
  27. * @access protected
  28. * @param array $config 连接信息
  29. * @return string
  30. */
  31. protected function parseDsn($config)
  32. {
  33. $dsn = 'sqlsrv:Database=' . $config['database'] . ';Server=' . $config['hostname'];
  34. if (!empty($config['hostport'])) {
  35. $dsn .= ',' . $config['hostport'];
  36. }
  37. return $dsn;
  38. }
  39. /**
  40. * 取得数据表的字段信息
  41. * @access public
  42. * @param string $tableName
  43. * @return array
  44. */
  45. public function getFields($tableName)
  46. {
  47. $this->initConnect(true);
  48. list($tableName) = explode(' ', $tableName);
  49. $sql = "SELECT column_name, data_type, column_default, is_nullable
  50. FROM information_schema.tables AS t
  51. JOIN information_schema.columns AS c
  52. ON t.table_catalog = c.table_catalog
  53. AND t.table_schema = c.table_schema
  54. AND t.table_name = c.table_name
  55. WHERE t.table_name = '$tableName'";
  56. // 调试开始
  57. $this->debug(true);
  58. $pdo = $this->linkID->query($sql);
  59. // 调试结束
  60. $this->debug(false, $sql);
  61. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  62. $info = [];
  63. if ($result) {
  64. foreach ($result as $key => $val) {
  65. $val = array_change_key_case($val);
  66. $info[$val['column_name']] = [
  67. 'name' => $val['column_name'],
  68. 'type' => $val['data_type'],
  69. 'notnull' => (bool) ('' === $val['is_nullable']), // not null is empty, null is yes
  70. 'default' => $val['column_default'],
  71. 'primary' => false,
  72. 'autoinc' => false,
  73. ];
  74. }
  75. }
  76. $sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'";
  77. // 调试开始
  78. $this->debug(true);
  79. $pdo = $this->linkID->query($sql);
  80. // 调试结束
  81. $this->debug(false, $sql);
  82. $result = $pdo->fetch(PDO::FETCH_ASSOC);
  83. if ($result) {
  84. $info[$result['column_name']]['primary'] = true;
  85. }
  86. return $this->fieldCase($info);
  87. }
  88. /**
  89. * 取得数据表的字段信息
  90. * @access public
  91. * @param string $dbName
  92. * @return array
  93. */
  94. public function getTables($dbName = '')
  95. {
  96. $this->initConnect(true);
  97. $sql = "SELECT TABLE_NAME
  98. FROM INFORMATION_SCHEMA.TABLES
  99. WHERE TABLE_TYPE = 'BASE TABLE'
  100. ";
  101. // 调试开始
  102. $this->debug(true);
  103. $pdo = $this->linkID->query($sql);
  104. // 调试结束
  105. $this->debug(false, $sql);
  106. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  107. $info = [];
  108. foreach ($result as $key => $val) {
  109. $info[$key] = current($val);
  110. }
  111. return $info;
  112. }
  113. /**
  114. * SQL性能分析
  115. * @access protected
  116. * @param string $sql
  117. * @return array
  118. */
  119. protected function getExplain($sql)
  120. {
  121. return [];
  122. }
  123. }