Sqlsrv.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\builder;
  12. use think\db\Builder;
  13. use think\db\exception\DbException as Exception;
  14. use think\db\Query;
  15. use think\db\Raw;
  16. /**
  17. * Sqlsrv数据库驱动
  18. */
  19. class Sqlsrv extends Builder
  20. {
  21. /**
  22. * SELECT SQL表达式
  23. * @var string
  24. */
  25. 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%';
  26. /**
  27. * SELECT INSERT SQL表达式
  28. * @var string
  29. */
  30. protected $selectInsertSql = 'SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%';
  31. /**
  32. * UPDATE SQL表达式
  33. * @var string
  34. */
  35. protected $updateSql = 'UPDATE %TABLE% SET %SET% FROM %TABLE% %JOIN% %WHERE% %LIMIT% %LOCK%%COMMENT%';
  36. /**
  37. * DELETE SQL表达式
  38. * @var string
  39. */
  40. protected $deleteSql = 'DELETE FROM %TABLE% %USING% FROM %TABLE% %JOIN% %WHERE% %LIMIT% %LOCK%%COMMENT%';
  41. /**
  42. * INSERT SQL表达式
  43. * @var string
  44. */
  45. protected $insertSql = 'INSERT INTO %TABLE% (%FIELD%) VALUES (%DATA%) %COMMENT%';
  46. /**
  47. * INSERT ALL SQL表达式
  48. * @var string
  49. */
  50. protected $insertAllSql = 'INSERT INTO %TABLE% (%FIELD%) %DATA% %COMMENT%';
  51. /**
  52. * order分析
  53. * @access protected
  54. * @param Query $query 查询对象
  55. * @param mixed $order
  56. * @return string
  57. */
  58. protected function parseOrder(Query $query, array $order): string
  59. {
  60. if (empty($order)) {
  61. return ' ORDER BY rand()';
  62. }
  63. $array = [];
  64. foreach ($order as $key => $val) {
  65. if ($val instanceof Raw) {
  66. $array[] = $this->parseRaw($query, $val);
  67. } elseif ('[rand]' == $val) {
  68. $array[] = $this->parseRand($query);
  69. } else {
  70. if (is_numeric($key)) {
  71. [$key, $sort] = explode(' ', strpos($val, ' ') ? $val : $val . ' ');
  72. } else {
  73. $sort = $val;
  74. }
  75. $sort = in_array(strtolower($sort), ['asc', 'desc'], true) ? ' ' . $sort : '';
  76. $array[] = $this->parseKey($query, $key, true) . $sort;
  77. }
  78. }
  79. return ' ORDER BY ' . implode(',', $array);
  80. }
  81. /**
  82. * 随机排序
  83. * @access protected
  84. * @param Query $query 查询对象
  85. * @return string
  86. */
  87. protected function parseRand(Query $query): string
  88. {
  89. return 'rand()';
  90. }
  91. /**
  92. * 字段和表名处理
  93. * @access public
  94. * @param Query $query 查询对象
  95. * @param mixed $key 字段名
  96. * @param bool $strict 严格检测
  97. * @return string
  98. */
  99. public function parseKey(Query $query, $key, bool $strict = false): string
  100. {
  101. if (is_int($key)) {
  102. return (string) $key;
  103. } elseif ($key instanceof Raw) {
  104. return $this->parseRaw($query, $key);
  105. }
  106. $key = trim($key);
  107. if (strpos($key, '.') && !preg_match('/[,\'\"\(\)\[\s]/', $key)) {
  108. [$table, $key] = explode('.', $key, 2);
  109. $alias = $query->getOptions('alias');
  110. if ('__TABLE__' == $table) {
  111. $table = $query->getOptions('table');
  112. $table = is_array($table) ? array_shift($table) : $table;
  113. }
  114. if (isset($alias[$table])) {
  115. $table = $alias[$table];
  116. }
  117. }
  118. if ($strict && !preg_match('/^[\w\.\*]+$/', $key)) {
  119. throw new Exception('not support data:' . $key);
  120. }
  121. if ('*' != $key && !preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) {
  122. $key = '[' . $key . ']';
  123. }
  124. if (isset($table)) {
  125. $key = '[' . $table . '].' . $key;
  126. }
  127. return $key;
  128. }
  129. /**
  130. * limit
  131. * @access protected
  132. * @param Query $query 查询对象
  133. * @param mixed $limit
  134. * @return string
  135. */
  136. protected function parseLimit(Query $query, string $limit): string
  137. {
  138. if (empty($limit)) {
  139. return '';
  140. }
  141. $limit = explode(',', $limit);
  142. if (count($limit) > 1) {
  143. $limitStr = '(T1.ROW_NUMBER BETWEEN ' . $limit[0] . ' + 1 AND ' . $limit[0] . ' + ' . $limit[1] . ')';
  144. } else {
  145. $limitStr = '(T1.ROW_NUMBER BETWEEN 1 AND ' . $limit[0] . ")";
  146. }
  147. return 'WHERE ' . $limitStr;
  148. }
  149. public function selectInsert(Query $query, array $fields, string $table): string
  150. {
  151. $this->selectSql = $this->selectInsertSql;
  152. return parent::selectInsert($query, $fields, $table);
  153. }
  154. }