Sqlsrv.php 3.8 KB

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