SqlserverDialectTrait.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Dialect;
  16. use Cake\Database\ExpressionInterface;
  17. use Cake\Database\Expression\FunctionExpression;
  18. use Cake\Database\Expression\OrderByExpression;
  19. use Cake\Database\Expression\UnaryExpression;
  20. use Cake\Database\Query;
  21. use Cake\Database\Schema\SqlserverSchema;
  22. use Cake\Database\SqlDialectTrait;
  23. use Cake\Database\SqlserverCompiler;
  24. use Cake\Database\ValueBinder;
  25. use PDO;
  26. /**
  27. * Contains functions that encapsulates the SQL dialect used by SQLServer,
  28. * including query translators and schema introspection.
  29. *
  30. * @internal
  31. */
  32. trait SqlserverDialectTrait
  33. {
  34. use SqlDialectTrait;
  35. use TupleComparisonTranslatorTrait;
  36. /**
  37. * String used to start a database identifier quoting to make it safe
  38. *
  39. * @var string
  40. */
  41. protected $_startQuote = '[';
  42. /**
  43. * String used to end a database identifier quoting to make it safe
  44. *
  45. * @var string
  46. */
  47. protected $_endQuote = ']';
  48. /**
  49. * Modify the limit/offset to TSQL
  50. *
  51. * @param \Cake\Database\Query $query The query to translate
  52. * @return \Cake\Database\Query The modified query
  53. */
  54. protected function _selectQueryTranslator($query)
  55. {
  56. $limit = $query->clause('limit');
  57. $offset = $query->clause('offset');
  58. if ($limit && $offset === null) {
  59. $query->modifier(['_auto_top_' => sprintf('TOP %d', $limit)]);
  60. }
  61. if ($offset !== null && !$query->clause('order')) {
  62. $query->order($query->newExpr()->add('(SELECT NULL)'));
  63. }
  64. if ($this->_version() < 11 && $offset !== null) {
  65. return $this->_pagingSubquery($query, $limit, $offset);
  66. }
  67. return $this->_transformDistinct($query);
  68. }
  69. /**
  70. * Get the version of SQLserver we are connected to.
  71. *
  72. * @return int
  73. */
  74. // @codingStandardsIgnoreLine
  75. public function _version()
  76. {
  77. $this->connect();
  78. return $this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
  79. }
  80. /**
  81. * Generate a paging subquery for older versions of SQLserver.
  82. *
  83. * Prior to SQLServer 2012 there was no equivalent to LIMIT OFFSET, so a subquery must
  84. * be used.
  85. *
  86. * @param \Cake\Database\Query $original The query to wrap in a subquery.
  87. * @param int $limit The number of rows to fetch.
  88. * @param int $offset The number of rows to offset.
  89. * @return \Cake\Database\Query Modified query object.
  90. */
  91. protected function _pagingSubquery($original, $limit, $offset)
  92. {
  93. $field = '_cake_paging_._cake_page_rownum_';
  94. if ($original->clause('order')) {
  95. // SQL server does not support column aliases in OVER clauses. But
  96. // the only practical way to specify the use of calculated columns
  97. // is with their alias. So substitute the select SQL in place of
  98. // any column aliases for those entries in the order clause.
  99. $select = $original->clause('select');
  100. $order = new OrderByExpression();
  101. $original
  102. ->clause('order')
  103. ->iterateParts(function ($direction, $orderBy) use ($select, $order) {
  104. $key = $orderBy;
  105. if (
  106. isset($select[$orderBy]) &&
  107. $select[$orderBy] instanceof ExpressionInterface
  108. ) {
  109. $key = $select[$orderBy]->sql(new ValueBinder());
  110. if ($select[$orderBy] instanceof Query) {
  111. $key = "($key)";
  112. }
  113. }
  114. $order->add([$key => $direction]);
  115. // Leave original order clause unchanged.
  116. return $orderBy;
  117. });
  118. } else {
  119. $order = new OrderByExpression('(SELECT NULL)');
  120. }
  121. $query = clone $original;
  122. $query->select([
  123. '_cake_page_rownum_' => new UnaryExpression('ROW_NUMBER() OVER', $order),
  124. ])->limit(null)
  125. ->offset(null)
  126. ->order([], true);
  127. $outer = new Query($query->getConnection());
  128. $outer->select('*')
  129. ->from(['_cake_paging_' => $query]);
  130. if ($offset) {
  131. $outer->where(["$field > " . (int)$offset]);
  132. }
  133. if ($limit) {
  134. $value = (int)$offset + (int)$limit;
  135. $outer->where(["$field <= $value"]);
  136. }
  137. // Decorate the original query as that is what the
  138. // end developer will be calling execute() on originally.
  139. $original->decorateResults(function ($row) {
  140. if (isset($row['_cake_page_rownum_'])) {
  141. unset($row['_cake_page_rownum_']);
  142. }
  143. return $row;
  144. });
  145. return $outer;
  146. }
  147. /**
  148. * Returns the passed query after rewriting the DISTINCT clause, so that drivers
  149. * that do not support the "ON" part can provide the actual way it should be done
  150. *
  151. * @param \Cake\Database\Query $original The query to be transformed
  152. * @return \Cake\Database\Query
  153. */
  154. protected function _transformDistinct($original)
  155. {
  156. if (!is_array($original->clause('distinct'))) {
  157. return $original;
  158. }
  159. $query = clone $original;
  160. $distinct = $query->clause('distinct');
  161. $query->distinct(false);
  162. $order = new OrderByExpression($distinct);
  163. $query
  164. ->select(function ($q) use ($distinct, $order) {
  165. $over = $q->newExpr('ROW_NUMBER() OVER')
  166. ->add('(PARTITION BY')
  167. ->add($q->newExpr()->add($distinct)->setConjunction(','))
  168. ->add($order)
  169. ->add(')')
  170. ->setConjunction(' ');
  171. return [
  172. '_cake_distinct_pivot_' => $over,
  173. ];
  174. })
  175. ->limit(null)
  176. ->offset(null)
  177. ->order([], true);
  178. $outer = new Query($query->getConnection());
  179. $outer->select('*')
  180. ->from(['_cake_distinct_' => $query])
  181. ->where(['_cake_distinct_pivot_' => 1]);
  182. // Decorate the original query as that is what the
  183. // end developer will be calling execute() on originally.
  184. $original->decorateResults(function ($row) {
  185. if (isset($row['_cake_distinct_pivot_'])) {
  186. unset($row['_cake_distinct_pivot_']);
  187. }
  188. return $row;
  189. });
  190. return $outer;
  191. }
  192. /**
  193. * Returns a dictionary of expressions to be transformed when compiling a Query
  194. * to SQL. Array keys are method names to be called in this class
  195. *
  196. * @return array
  197. */
  198. protected function _expressionTranslators()
  199. {
  200. $namespace = 'Cake\Database\Expression';
  201. return [
  202. $namespace . '\FunctionExpression' => '_transformFunctionExpression',
  203. $namespace . '\TupleComparison' => '_transformTupleComparison',
  204. ];
  205. }
  206. /**
  207. * Receives a FunctionExpression and changes it so that it conforms to this
  208. * SQL dialect.
  209. *
  210. * @param \Cake\Database\Expression\FunctionExpression $expression The function expression to convert to TSQL.
  211. * @return void
  212. */
  213. protected function _transformFunctionExpression(FunctionExpression $expression)
  214. {
  215. switch ($expression->getName()) {
  216. case 'CONCAT':
  217. // CONCAT function is expressed as exp1 + exp2
  218. $expression->setName('')->setConjunction(' +');
  219. break;
  220. case 'DATEDIFF':
  221. $hasDay = false;
  222. $visitor = function ($value) use (&$hasDay) {
  223. if ($value === 'day') {
  224. $hasDay = true;
  225. }
  226. return $value;
  227. };
  228. $expression->iterateParts($visitor);
  229. if (!$hasDay) {
  230. $expression->add(['day' => 'literal'], [], true);
  231. }
  232. break;
  233. case 'CURRENT_DATE':
  234. $time = new FunctionExpression('GETUTCDATE');
  235. $expression->setName('CONVERT')->add(['date' => 'literal', $time]);
  236. break;
  237. case 'CURRENT_TIME':
  238. $time = new FunctionExpression('GETUTCDATE');
  239. $expression->setName('CONVERT')->add(['time' => 'literal', $time]);
  240. break;
  241. case 'NOW':
  242. $expression->setName('GETUTCDATE');
  243. break;
  244. case 'EXTRACT':
  245. $expression->setName('DATEPART')->setConjunction(' ,');
  246. break;
  247. case 'DATE_ADD':
  248. $params = [];
  249. $visitor = function ($p, $key) use (&$params) {
  250. if ($key === 0) {
  251. $params[2] = $p;
  252. } else {
  253. $valueUnit = explode(' ', $p);
  254. $params[0] = rtrim($valueUnit[1], 's');
  255. $params[1] = $valueUnit[0];
  256. }
  257. return $p;
  258. };
  259. $manipulator = function ($p, $key) use (&$params) {
  260. return $params[$key];
  261. };
  262. $expression
  263. ->setName('DATEADD')
  264. ->setConjunction(',')
  265. ->iterateParts($visitor)
  266. ->iterateParts($manipulator)
  267. ->add([$params[2] => 'literal']);
  268. break;
  269. case 'DAYOFWEEK':
  270. $expression
  271. ->setName('DATEPART')
  272. ->setConjunction(' ')
  273. ->add(['weekday, ' => 'literal'], [], true);
  274. break;
  275. case 'SUBSTR':
  276. $expression->setName('SUBSTRING');
  277. if (count($expression) < 4) {
  278. $params = [];
  279. $expression
  280. ->iterateParts(function ($p) use (&$params) {
  281. return $params[] = $p;
  282. })
  283. ->add([new FunctionExpression('LEN', [$params[0]]), ['string']]);
  284. }
  285. break;
  286. }
  287. }
  288. /**
  289. * Get the schema dialect.
  290. *
  291. * Used by Cake\Schema package to reflect schema and
  292. * generate schema.
  293. *
  294. * @return \Cake\Database\Schema\SqlserverSchema
  295. */
  296. public function schemaDialect()
  297. {
  298. return new SqlserverSchema($this);
  299. }
  300. /**
  301. * Returns a SQL snippet for creating a new transaction savepoint
  302. *
  303. * @param string $name save point name
  304. * @return string
  305. */
  306. public function savePointSQL($name)
  307. {
  308. return 'SAVE TRANSACTION t' . $name;
  309. }
  310. /**
  311. * Returns a SQL snippet for releasing a previously created save point
  312. *
  313. * @param string $name save point name
  314. * @return string
  315. */
  316. public function releaseSavePointSQL($name)
  317. {
  318. return 'COMMIT TRANSACTION t' . $name;
  319. }
  320. /**
  321. * Returns a SQL snippet for rollbacking a previously created save point
  322. *
  323. * @param string $name save point name
  324. * @return string
  325. */
  326. public function rollbackSavePointSQL($name)
  327. {
  328. return 'ROLLBACK TRANSACTION t' . $name;
  329. }
  330. /**
  331. * {@inheritDoc}
  332. *
  333. * @return \Cake\Database\SqlserverCompiler
  334. */
  335. public function newCompiler()
  336. {
  337. return new SqlserverCompiler();
  338. }
  339. /**
  340. * {@inheritDoc}
  341. */
  342. public function disableForeignKeySQL()
  343. {
  344. return 'EXEC sp_MSforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"';
  345. }
  346. /**
  347. * {@inheritDoc}
  348. */
  349. public function enableForeignKeySQL()
  350. {
  351. return 'EXEC sp_MSforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"';
  352. }
  353. }