Sqlsrv.php 4.1 KB

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