PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 344 lines | 145 code | 53 blank | 146 comment | 15 complexity | d2bab512a479fbef71ca5b2337a0a924 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Database\Query\Grammars;
  3. use Illuminate\Database\Query\Builder;
  4. class SqlServerGrammar extends Grammar
  5. {
  6. /**
  7. * All of the available clause operators.
  8. *
  9. * @var array
  10. */
  11. protected $operators = [
  12. '=', '<', '>', '<=', '>=', '!<', '!>', '<>', '!=',
  13. 'like', 'not like', 'between', 'ilike',
  14. '&', '&=', '|', '|=', '^', '^=',
  15. ];
  16. /**
  17. * Compile a select query into SQL.
  18. *
  19. * @param \Illuminate\Database\Query\Builder $query
  20. * @return string
  21. */
  22. public function compileSelect(Builder $query)
  23. {
  24. if (is_null($query->columns)) {
  25. $query->columns = ['*'];
  26. }
  27. $components = $this->compileComponents($query);
  28. // If an offset is present on the query, we will need to wrap the query in
  29. // a big "ANSI" offset syntax block. This is very nasty compared to the
  30. // other database systems but is necessary for implementing features.
  31. if ($query->offset > 0) {
  32. return $this->compileAnsiOffset($query, $components);
  33. }
  34. return $this->concatenate($components);
  35. }
  36. /**
  37. * Compile the "select *" portion of the query.
  38. *
  39. * @param \Illuminate\Database\Query\Builder $query
  40. * @param array $columns
  41. * @return string|null
  42. */
  43. protected function compileColumns(Builder $query, $columns)
  44. {
  45. if (! is_null($query->aggregate)) {
  46. return;
  47. }
  48. $select = $query->distinct ? 'select distinct ' : 'select ';
  49. // If there is a limit on the query, but not an offset, we will add the top
  50. // clause to the query, which serves as a "limit" type clause within the
  51. // SQL Server system similar to the limit keywords available in MySQL.
  52. if ($query->limit > 0 && $query->offset <= 0) {
  53. $select .= 'top '.$query->limit.' ';
  54. }
  55. return $select.$this->columnize($columns);
  56. }
  57. /**
  58. * Compile the "from" portion of the query.
  59. *
  60. * @param \Illuminate\Database\Query\Builder $query
  61. * @param string $table
  62. * @return string
  63. */
  64. protected function compileFrom(Builder $query, $table)
  65. {
  66. $from = parent::compileFrom($query, $table);
  67. if (is_string($query->lock)) {
  68. return $from.' '.$query->lock;
  69. }
  70. if (! is_null($query->lock)) {
  71. return $from.' with(rowlock,'.($query->lock ? 'updlock,' : '').'holdlock)';
  72. }
  73. return $from;
  74. }
  75. /**
  76. * Create a full ANSI offset clause for the query.
  77. *
  78. * @param \Illuminate\Database\Query\Builder $query
  79. * @param array $components
  80. * @return string
  81. */
  82. protected function compileAnsiOffset(Builder $query, $components)
  83. {
  84. // An ORDER BY clause is required to make this offset query work, so if one does
  85. // not exist we'll just create a dummy clause to trick the database and so it
  86. // does not complain about the queries for not having an "order by" clause.
  87. if (! isset($components['orders'])) {
  88. $components['orders'] = 'order by (select 0)';
  89. }
  90. // We need to add the row number to the query so we can compare it to the offset
  91. // and limit values given for the statements. So we will add an expression to
  92. // the "select" that will give back the row numbers on each of the records.
  93. $orderings = $components['orders'];
  94. $components['columns'] .= $this->compileOver($orderings);
  95. unset($components['orders']);
  96. // Next we need to calculate the constraints that should be placed on the query
  97. // to get the right offset and limit from our query but if there is no limit
  98. // set we will just handle the offset only since that is all that matters.
  99. $constraint = $this->compileRowConstraint($query);
  100. $sql = $this->concatenate($components);
  101. // We are now ready to build the final SQL query so we'll create a common table
  102. // expression from the query and get the records with row numbers within our
  103. // given limit and offset value that we just put on as a query constraint.
  104. return $this->compileTableExpression($sql, $constraint);
  105. }
  106. /**
  107. * Compile the over statement for a table expression.
  108. *
  109. * @param string $orderings
  110. * @return string
  111. */
  112. protected function compileOver($orderings)
  113. {
  114. return ", row_number() over ({$orderings}) as row_num";
  115. }
  116. /**
  117. * Compile the limit / offset row constraint for a query.
  118. *
  119. * @param \Illuminate\Database\Query\Builder $query
  120. * @return string
  121. */
  122. protected function compileRowConstraint($query)
  123. {
  124. $start = $query->offset + 1;
  125. if ($query->limit > 0) {
  126. $finish = $query->offset + $query->limit;
  127. return "between {$start} and {$finish}";
  128. }
  129. return ">= {$start}";
  130. }
  131. /**
  132. * Compile a common table expression for a query.
  133. *
  134. * @param string $sql
  135. * @param string $constraint
  136. * @return string
  137. */
  138. protected function compileTableExpression($sql, $constraint)
  139. {
  140. return "select * from ({$sql}) as temp_table where row_num {$constraint}";
  141. }
  142. /**
  143. * Compile the "limit" portions of the query.
  144. *
  145. * @param \Illuminate\Database\Query\Builder $query
  146. * @param int $limit
  147. * @return string
  148. */
  149. protected function compileLimit(Builder $query, $limit)
  150. {
  151. return '';
  152. }
  153. /**
  154. * Compile the "offset" portions of the query.
  155. *
  156. * @param \Illuminate\Database\Query\Builder $query
  157. * @param int $offset
  158. * @return string
  159. */
  160. protected function compileOffset(Builder $query, $offset)
  161. {
  162. return '';
  163. }
  164. /**
  165. * Compile a truncate table statement into SQL.
  166. *
  167. * @param \Illuminate\Database\Query\Builder $query
  168. * @return array
  169. */
  170. public function compileTruncate(Builder $query)
  171. {
  172. return ['truncate table '.$this->wrapTable($query->from) => []];
  173. }
  174. /**
  175. * Compile an exists statement into SQL.
  176. *
  177. * @param \Illuminate\Database\Query\Builder $query
  178. * @return string
  179. */
  180. public function compileExists(Builder $query)
  181. {
  182. $existsQuery = clone $query;
  183. $existsQuery->columns = [];
  184. return $this->compileSelect($existsQuery->selectRaw('1 [exists]')->limit(1));
  185. }
  186. /**
  187. * Compile a "where date" clause.
  188. *
  189. * @param \Illuminate\Database\Query\Builder $query
  190. * @param array $where
  191. * @return string
  192. */
  193. protected function whereDate(Builder $query, $where)
  194. {
  195. $value = $this->parameter($where['value']);
  196. return 'cast('.$this->wrap($where['column']).' as date) '.$where['operator'].' '.$value;
  197. }
  198. /**
  199. * Determine if the grammar supports savepoints.
  200. *
  201. * @return bool
  202. */
  203. public function supportsSavepoints()
  204. {
  205. return false;
  206. }
  207. /**
  208. * Get the format for database stored dates.
  209. *
  210. * @return string
  211. */
  212. public function getDateFormat()
  213. {
  214. return 'Y-m-d H:i:s.000';
  215. }
  216. /**
  217. * Wrap a single string in keyword identifiers.
  218. *
  219. * @param string $value
  220. * @return string
  221. */
  222. protected function wrapValue($value)
  223. {
  224. if ($value === '*') {
  225. return $value;
  226. }
  227. return '['.str_replace(']', ']]', $value).']';
  228. }
  229. /**
  230. * Compile an update statement into SQL.
  231. *
  232. * @param \Illuminate\Database\Query\Builder $query
  233. * @param array $values
  234. * @return string
  235. */
  236. public function compileUpdate(Builder $query, $values)
  237. {
  238. $table = $alias = $this->wrapTable($query->from);
  239. if (strpos(strtolower($table), '] as [') !== false) {
  240. $segments = explode('] as [', $table);
  241. $alias = '['.$segments[1];
  242. }
  243. // Each one of the columns in the update statements needs to be wrapped in the
  244. // keyword identifiers, also a place-holder needs to be created for each of
  245. // the values in the list of bindings so we can make the sets statements.
  246. $columns = [];
  247. foreach ($values as $key => $value) {
  248. $columns[] = $this->wrap($key).' = '.$this->parameter($value);
  249. }
  250. $columns = implode(', ', $columns);
  251. // If the query has any "join" clauses, we will setup the joins on the builder
  252. // and compile them so we can attach them to this update, as update queries
  253. // can get join statements to attach to other tables when they're needed.
  254. if (isset($query->joins)) {
  255. $joins = ' '.$this->compileJoins($query, $query->joins);
  256. } else {
  257. $joins = '';
  258. }
  259. // Of course, update queries may also be constrained by where clauses so we'll
  260. // need to compile the where clauses and attach it to the query so only the
  261. // intended records are updated by the SQL statements we generate to run.
  262. $where = $this->compileWheres($query);
  263. if (! empty($joins)) {
  264. return trim("update {$alias} set {$columns} from {$table}{$joins} {$where}");
  265. }
  266. return trim("update {$table}{$joins} set $columns $where");
  267. }
  268. /**
  269. * Wrap a table in keyword identifiers.
  270. *
  271. * @param \Illuminate\Database\Query\Expression|string $table
  272. * @return string
  273. */
  274. public function wrapTable($table)
  275. {
  276. return $this->wrapTableValuedFunction(parent::wrapTable($table));
  277. }
  278. /**
  279. * Wrap a table in keyword identifiers.
  280. *
  281. * @param string $table
  282. * @return string
  283. */
  284. protected function wrapTableValuedFunction($table)
  285. {
  286. if (preg_match('/^(.+?)(\(.*?\))]$/', $table, $matches) === 1) {
  287. $table = $matches[1].']'.$matches[2];
  288. }
  289. return $table;
  290. }
  291. }