Sqlsrv.php 4.5 KB

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