Sqlsrv.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_ORACLE_NULLS => PDO::NULL_NATURAL,
  24. PDO::ATTR_STRINGIFY_FETCHES => false,
  25. ];
  26. protected $builder = '\\think\\db\\builder\\Sqlsrv';
  27. /**
  28. * 解析pdo连接的dsn信息
  29. * @access protected
  30. * @param array $config 连接信息
  31. * @return string
  32. */
  33. protected function parseDsn($config)
  34. {
  35. $dsn = 'sqlsrv:Database=' . $config['database'] . ';Server=' . $config['hostname'];
  36. if (!empty($config['hostport'])) {
  37. $dsn .= ',' . $config['hostport'];
  38. }
  39. return $dsn;
  40. }
  41. /**
  42. * 取得数据表的字段信息
  43. * @access public
  44. * @param string $tableName
  45. * @return array
  46. */
  47. public function getFields($tableName)
  48. {
  49. list($tableName) = explode(' ', $tableName);
  50. $sql = "SELECT column_name, data_type, column_default, is_nullable
  51. FROM information_schema.tables AS t
  52. JOIN information_schema.columns AS c
  53. ON t.table_catalog = c.table_catalog
  54. AND t.table_schema = c.table_schema
  55. AND t.table_name = c.table_name
  56. WHERE t.table_name = '$tableName'";
  57. $pdo = $this->query($sql, [], false, true);
  58. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  59. $info = [];
  60. if ($result) {
  61. foreach ($result as $key => $val) {
  62. $val = array_change_key_case($val);
  63. $info[$val['column_name']] = [
  64. 'name' => $val['column_name'],
  65. 'type' => $val['data_type'],
  66. 'notnull' => (bool) ('' === $val['is_nullable']), // not null is empty, null is yes
  67. 'default' => $val['column_default'],
  68. 'primary' => false,
  69. 'autoinc' => false,
  70. ];
  71. }
  72. }
  73. $sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'";
  74. // 调试开始
  75. $this->debug(true);
  76. $pdo = $this->linkID->query($sql);
  77. // 调试结束
  78. $this->debug(false, $sql);
  79. $result = $pdo->fetch(PDO::FETCH_ASSOC);
  80. if ($result) {
  81. $info[$result['column_name']]['primary'] = true;
  82. }
  83. return $this->fieldCase($info);
  84. }
  85. /**
  86. * 取得数据表的字段信息
  87. * @access public
  88. * @param string $dbName
  89. * @return array
  90. */
  91. public function getTables($dbName = '')
  92. {
  93. $sql = "SELECT TABLE_NAME
  94. FROM INFORMATION_SCHEMA.TABLES
  95. WHERE TABLE_TYPE = 'BASE TABLE'
  96. ";
  97. $pdo = $this->query($sql, [], false, true);
  98. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  99. $info = [];
  100. foreach ($result as $key => $val) {
  101. $info[$key] = current($val);
  102. }
  103. return $info;
  104. }
  105. /**
  106. * SQL性能分析
  107. * @access protected
  108. * @param string $sql
  109. * @return array
  110. */
  111. protected function getExplain($sql)
  112. {
  113. return [];
  114. }
  115. }