SqlserverCompiler.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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;
  16. /**
  17. * Responsible for compiling a Query object into its SQL representation
  18. * for SQL Server
  19. *
  20. * @internal
  21. */
  22. class SqlserverCompiler extends QueryCompiler
  23. {
  24. /**
  25. * SQLserver does not support ORDER BY in UNION queries.
  26. *
  27. * @var bool
  28. */
  29. protected $_orderedUnion = false;
  30. /**
  31. * {@inheritDoc}
  32. */
  33. protected $_templates = [
  34. 'delete' => 'DELETE',
  35. 'where' => ' WHERE %s',
  36. 'group' => ' GROUP BY %s ',
  37. 'having' => ' HAVING %s ',
  38. 'order' => ' %s',
  39. 'offset' => ' OFFSET %s ROWS',
  40. 'epilog' => ' %s'
  41. ];
  42. /**
  43. * {@inheritDoc}
  44. */
  45. protected $_selectParts = [
  46. 'select', 'from', 'join', 'where', 'group', 'having', 'order', 'offset',
  47. 'limit', 'union', 'epilog'
  48. ];
  49. /**
  50. * Generates the INSERT part of a SQL query
  51. *
  52. * To better handle concurrency and low transaction isolation levels,
  53. * we also include an OUTPUT clause so we can ensure we get the inserted
  54. * row's data back.
  55. *
  56. * @param array $parts The parts to build
  57. * @param \Cake\Database\Query $query The query that is being compiled
  58. * @param \Cake\Database\ValueBinder $generator the placeholder generator to be used in expressions
  59. * @return string
  60. */
  61. protected function _buildInsertPart($parts, $query, $generator)
  62. {
  63. $table = $parts[0];
  64. $columns = $this->_stringifyExpressions($parts[1], $generator);
  65. $modifiers = $this->_buildModifierPart($query->clause('modifier'), $query, $generator);
  66. return sprintf(
  67. 'INSERT%s INTO %s (%s) OUTPUT INSERTED.*',
  68. $modifiers,
  69. $table,
  70. implode(', ', $columns)
  71. );
  72. }
  73. /**
  74. * Generates the LIMIT part of a SQL query
  75. *
  76. * @param int $limit the limit clause
  77. * @param \Cake\Database\Query $query The query that is being compiled
  78. * @return string
  79. */
  80. protected function _buildLimitPart($limit, $query)
  81. {
  82. if ($limit === null || $query->clause('offset') === null) {
  83. return '';
  84. }
  85. return sprintf(' FETCH FIRST %d ROWS ONLY', $limit);
  86. }
  87. }