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