Sqlsrv.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. $tableNames = explode('.', $tableName);
  50. $tableName = isset($tableNames[1]) ? $tableNames[1] : $tableNames[0];
  51. $sql = "SELECT column_name, data_type, column_default, is_nullable
  52. FROM information_schema.tables AS t
  53. JOIN information_schema.columns AS c
  54. ON t.table_catalog = c.table_catalog
  55. AND t.table_schema = c.table_schema
  56. AND t.table_name = c.table_name
  57. WHERE t.table_name = '$tableName'";
  58. $pdo = $this->query($sql, [], false, true);
  59. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  60. $info = [];
  61. if ($result) {
  62. foreach ($result as $key => $val) {
  63. $val = array_change_key_case($val);
  64. $info[$val['column_name']] = [
  65. 'name' => $val['column_name'],
  66. 'type' => $val['data_type'],
  67. 'notnull' => (bool) ('' === $val['is_nullable']), // not null is empty, null is yes
  68. 'default' => $val['column_default'],
  69. 'primary' => false,
  70. 'autoinc' => false,
  71. ];
  72. }
  73. }
  74. $sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'";
  75. // 调试开始
  76. $this->debug(true);
  77. $pdo = $this->linkID->query($sql);
  78. // 调试结束
  79. $this->debug(false, $sql);
  80. $result = $pdo->fetch(PDO::FETCH_ASSOC);
  81. if ($result) {
  82. $info[$result['column_name']]['primary'] = true;
  83. }
  84. return $this->fieldCase($info);
  85. }
  86. /**
  87. * 取得数据表的字段信息
  88. * @access public
  89. * @param string $dbName
  90. * @return array
  91. */
  92. public function getTables($dbName = '')
  93. {
  94. $sql = "SELECT TABLE_NAME
  95. FROM INFORMATION_SCHEMA.TABLES
  96. WHERE TABLE_TYPE = 'BASE TABLE'
  97. ";
  98. $pdo = $this->query($sql, [], false, true);
  99. $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
  100. $info = [];
  101. foreach ($result as $key => $val) {
  102. $info[$key] = current($val);
  103. }
  104. return $info;
  105. }
  106. /**
  107. * SQL性能分析
  108. * @access protected
  109. * @param string $sql
  110. * @return array
  111. */
  112. protected function getExplain($sql)
  113. {
  114. return [];
  115. }
  116. }