Sqlsrv.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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\Driver;
  12. use Think\Db\Driver;
  13. use PDO;
  14. /**
  15. * Sqlsrv数据库驱动
  16. */
  17. class Sqlsrv extends Driver{
  18. protected $selectSql = 'SELECT T1.* FROM (SELECT thinkphp.*, ROW_NUMBER() OVER (%ORDER%) AS ROW_NUMBER FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%) AS thinkphp) AS T1 %LIMIT%%COMMENT%';
  19. // PDO连接参数
  20. protected $options = array(
  21. PDO::ATTR_CASE => PDO::CASE_LOWER,
  22. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  23. PDO::ATTR_STRINGIFY_FETCHES => false,
  24. PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_UTF8,
  25. );
  26. /**
  27. * 解析pdo连接的dsn信息
  28. * @access public
  29. * @param array $config 连接信息
  30. * @return string
  31. */
  32. protected function parseDsn($config){
  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. * @return array
  43. */
  44. public function getFields($tableName) {
  45. list($tableName) = explode(' ', $tableName);
  46. $result = $this->query("SELECT column_name, data_type, column_default, is_nullable
  47. FROM information_schema.tables AS t
  48. JOIN information_schema.columns AS c
  49. ON t.table_catalog = c.table_catalog
  50. AND t.table_schema = c.table_schema
  51. AND t.table_name = c.table_name
  52. WHERE t.table_name = '$tableName'");
  53. $info = array();
  54. if($result) {
  55. foreach ($result as $key => $val) {
  56. $info[$val['column_name']] = array(
  57. 'name' => $val['column_name'],
  58. 'type' => $val['data_type'],
  59. 'notnull' => (bool) ($val['is_nullable'] === ''), // not null is empty, null is yes
  60. 'default' => $val['column_default'],
  61. 'primary' => false,
  62. 'autoinc' => false,
  63. );
  64. }
  65. }
  66. return $info;
  67. }
  68. /**
  69. * 取得数据表的字段信息
  70. * @access public
  71. * @return array
  72. */
  73. public function getTables($dbName='') {
  74. $result = $this->query("SELECT TABLE_NAME
  75. FROM INFORMATION_SCHEMA.TABLES
  76. WHERE TABLE_TYPE = 'BASE TABLE'
  77. ");
  78. $info = array();
  79. foreach ($result as $key => $val) {
  80. $info[$key] = current($val);
  81. }
  82. return $info;
  83. }
  84. /**
  85. * order分析
  86. * @access protected
  87. * @param mixed $order
  88. * @return string
  89. */
  90. protected function parseOrder($order) {
  91. return !empty($order)? ' ORDER BY '.$order:' ORDER BY rand()';
  92. }
  93. /**
  94. * 字段名分析
  95. * @access protected
  96. * @param string $key
  97. * @return string
  98. */
  99. protected function parseKey(&$key) {
  100. $key = trim($key);
  101. if(!is_numeric($key) && !preg_match('/[,\'\"\*\(\)\[.\s]/',$key)) {
  102. $key = '['.$key.']';
  103. }
  104. return $key;
  105. }
  106. /**
  107. * limit
  108. * @access public
  109. * @param mixed $limit
  110. * @return string
  111. */
  112. public function parseLimit($limit) {
  113. if(empty($limit)) return '';
  114. $limit = explode(',',$limit);
  115. if(count($limit)>1)
  116. $limitStr = '(T1.ROW_NUMBER BETWEEN '.$limit[0].' + 1 AND '.$limit[0].' + '.$limit[1].')';
  117. else
  118. $limitStr = '(T1.ROW_NUMBER BETWEEN 1 AND '.$limit[0].")";
  119. return 'WHERE '.$limitStr;
  120. }
  121. /**
  122. * 更新记录
  123. * @access public
  124. * @param mixed $data 数据
  125. * @param array $options 表达式
  126. * @return false | integer
  127. */
  128. public function update($data,$options) {
  129. $this->model = $options['model'];
  130. $this->parseBind(!empty($options['bind'])?$options['bind']:array());
  131. $sql = 'UPDATE '
  132. .$this->parseTable($options['table'])
  133. .$this->parseSet($data)
  134. .$this->parseWhere(!empty($options['where'])?$options['where']:'')
  135. .$this->parseLock(isset($options['lock'])?$options['lock']:false)
  136. .$this->parseComment(!empty($options['comment'])?$options['comment']:'');
  137. return $this->execute($sql,!empty($options['fetch_sql']) ? true : false);
  138. }
  139. /**
  140. * 删除记录
  141. * @access public
  142. * @param array $options 表达式
  143. * @return false | integer
  144. */
  145. public function delete($options=array()) {
  146. $this->model = $options['model'];
  147. $this->parseBind(!empty($options['bind'])?$options['bind']:array());
  148. $sql = 'DELETE FROM '
  149. .$this->parseTable($options['table'])
  150. .$this->parseWhere(!empty($options['where'])?$options['where']:'')
  151. .$this->parseLock(isset($options['lock'])?$options['lock']:false)
  152. .$this->parseComment(!empty($options['comment'])?$options['comment']:'');
  153. return $this->execute($sql,!empty($options['fetch_sql']) ? true : false);
  154. }
  155. }