SqlServerProcessor.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Illuminate\Database\Query\Processors;
  3. use Exception;
  4. use Illuminate\Database\Connection;
  5. use Illuminate\Database\Query\Builder;
  6. class SqlServerProcessor extends Processor
  7. {
  8. /**
  9. * Process an "insert get ID" query.
  10. *
  11. * @param \Illuminate\Database\Query\Builder $query
  12. * @param string $sql
  13. * @param array $values
  14. * @param string|null $sequence
  15. * @return int
  16. */
  17. public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
  18. {
  19. $connection = $query->getConnection();
  20. $connection->insert($sql, $values);
  21. if ($connection->getConfig('odbc') === true) {
  22. $id = $this->processInsertGetIdForOdbc($connection);
  23. } else {
  24. $id = $connection->getPdo()->lastInsertId();
  25. }
  26. return is_numeric($id) ? (int) $id : $id;
  27. }
  28. /**
  29. * Process an "insert get ID" query for ODBC.
  30. *
  31. * @param \Illuminate\Database\Connection $connection
  32. * @return int
  33. *
  34. * @throws \Exception
  35. */
  36. protected function processInsertGetIdForOdbc(Connection $connection)
  37. {
  38. $result = $connection->selectFromWriteConnection(
  39. 'SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS int) AS insertid'
  40. );
  41. if (! $result) {
  42. throw new Exception('Unable to retrieve lastInsertID for ODBC.');
  43. }
  44. $row = $result[0];
  45. return is_object($row) ? $row->insertid : $row['insertid'];
  46. }
  47. /**
  48. * Process the results of a columns query.
  49. *
  50. * @param array $results
  51. * @return array
  52. */
  53. public function processColumns($results)
  54. {
  55. return array_map(function ($result) {
  56. $result = (object) $result;
  57. $type = match ($typeName = $result->type_name) {
  58. 'binary', 'varbinary', 'char', 'varchar', 'nchar', 'nvarchar' => $result->length == -1 ? $typeName.'(max)' : $typeName."($result->length)",
  59. 'decimal', 'numeric' => $typeName."($result->precision,$result->places)",
  60. 'float', 'datetime2', 'datetimeoffset', 'time' => $typeName."($result->precision)",
  61. default => $typeName,
  62. };
  63. return [
  64. 'name' => $result->name,
  65. 'type_name' => $result->type_name,
  66. 'type' => $type,
  67. 'collation' => $result->collation,
  68. 'nullable' => (bool) $result->nullable,
  69. 'default' => $result->default,
  70. 'auto_increment' => (bool) $result->autoincrement,
  71. 'comment' => $result->comment,
  72. 'generation' => $result->expression ? [
  73. 'type' => $result->persisted ? 'stored' : 'virtual',
  74. 'expression' => $result->expression,
  75. ] : null,
  76. ];
  77. }, $results);
  78. }
  79. /**
  80. * Process the results of an indexes query.
  81. *
  82. * @param array $results
  83. * @return array
  84. */
  85. public function processIndexes($results)
  86. {
  87. return array_map(function ($result) {
  88. $result = (object) $result;
  89. return [
  90. 'name' => strtolower($result->name),
  91. 'columns' => explode(',', $result->columns),
  92. 'type' => strtolower($result->type),
  93. 'unique' => (bool) $result->unique,
  94. 'primary' => (bool) $result->primary,
  95. ];
  96. }, $results);
  97. }
  98. /**
  99. * Process the results of a foreign keys query.
  100. *
  101. * @param array $results
  102. * @return array
  103. */
  104. public function processForeignKeys($results)
  105. {
  106. return array_map(function ($result) {
  107. $result = (object) $result;
  108. return [
  109. 'name' => $result->name,
  110. 'columns' => explode(',', $result->columns),
  111. 'foreign_schema' => $result->foreign_schema,
  112. 'foreign_table' => $result->foreign_table,
  113. 'foreign_columns' => explode(',', $result->foreign_columns),
  114. 'on_update' => strtolower(str_replace('_', ' ', $result->on_update)),
  115. 'on_delete' => strtolower(str_replace('_', ' ', $result->on_delete)),
  116. ];
  117. }, $results);
  118. }
  119. }