Sqlsrv.php 3.9 KB

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