sqlserver.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php namespace Laravel\Database\Query\Grammars;
  2. use Laravel\Database\Query;
  3. class SQLServer extends Grammar {
  4. /**
  5. * The keyword identifier for the database system.
  6. *
  7. * @var string
  8. */
  9. protected $wrapper = '[%s]';
  10. /**
  11. * The format for properly saving a DateTime.
  12. *
  13. * @var string
  14. */
  15. public $datetime = 'Y-m-d H:i:s.000';
  16. /**
  17. * Compile a SQL SELECT statement from a Query instance.
  18. *
  19. * @param Query $query
  20. * @return string
  21. */
  22. public function select(Query $query)
  23. {
  24. $sql = parent::components($query);
  25. // SQL Server does not currently implement an "OFFSET" type keyword, so we
  26. // actually have to generate the ANSI standard SQL for doing offset like
  27. // functionality. OFFSET is in SQL Server 2012, however.
  28. if ($query->offset > 0)
  29. {
  30. return $this->ansi_offset($query, $sql);
  31. }
  32. // Once all of the clauses have been compiled, we can join them all as
  33. // one statement. Any segments that are null or an empty string will
  34. // be removed from the array before imploding.
  35. return $this->concatenate($sql);
  36. }
  37. /**
  38. * Compile the SELECT clause for a query.
  39. *
  40. * @param Query $query
  41. * @return string
  42. */
  43. protected function selects(Query $query)
  44. {
  45. if ( ! is_null($query->aggregate)) return;
  46. $select = ($query->distinct) ? 'SELECT DISTINCT ' : 'SELECT ';
  47. // Instead of using a "LIMIT" keyword, SQL Server uses the TOP keyword
  48. // within the SELECT statement. So, if we have a limit, we will add
  49. // it to the query here if there is not an OFFSET present.
  50. if ($query->limit > 0 and $query->offset <= 0)
  51. {
  52. $select .= 'TOP '.$query->limit.' ';
  53. }
  54. return $select.$this->columnize($query->selects);
  55. }
  56. /**
  57. * Generate the ANSI standard SQL for an offset clause.
  58. *
  59. * @param Query $query
  60. * @param array $components
  61. * @return array
  62. */
  63. protected function ansi_offset(Query $query, $components)
  64. {
  65. // An ORDER BY clause is required to make this offset query work, so if
  66. // one doesn't exist, we'll just create a dummy clause to trick the
  67. // database and pacify it so it doesn't complain about the query.
  68. if ( ! isset($components['orderings']))
  69. {
  70. $components['orderings'] = 'ORDER BY (SELECT 0)';
  71. }
  72. // We need to add the row number to the query so we can compare it to
  73. // the offset and limit values given for the statement. So we'll add
  74. // an expression to the select for the row number.
  75. $orderings = $components['orderings'];
  76. $components['selects'] .= ", ROW_NUMBER() OVER ({$orderings}) AS RowNum";
  77. unset($components['orderings']);
  78. $start = $query->offset + 1;
  79. // Next we need to calculate the constraint that should be placed on
  80. // the row number to get the correct offset and limit on the query.
  81. // If there is not a limit, we'll just handle the offset.
  82. if ($query->limit > 0)
  83. {
  84. $finish = $query->offset + $query->limit;
  85. $constraint = "BETWEEN {$start} AND {$finish}";
  86. }
  87. else
  88. {
  89. $constraint = ">= {$start}";
  90. }
  91. // We're finally ready to build the final SQL query so we'll create
  92. // a common table expression with the query and select all of the
  93. // results with row numbers between the limit and offset.
  94. $sql = $this->concatenate($components);
  95. return "SELECT * FROM ($sql) AS TempTable WHERE RowNum {$constraint}";
  96. }
  97. /**
  98. * Compile the LIMIT clause for a query.
  99. *
  100. * @param Query $query
  101. * @return string
  102. */
  103. protected function limit(Query $query)
  104. {
  105. return '';
  106. }
  107. /**
  108. * Compile the OFFSET clause for a query.
  109. *
  110. * @param Query $query
  111. * @return string
  112. */
  113. protected function offset(Query $query)
  114. {
  115. return '';
  116. }
  117. }