SqlServerProcessor.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 column listing query.
  49. *
  50. * @param array $results
  51. * @return array
  52. */
  53. public function processColumnListing($results)
  54. {
  55. return array_map(function ($result) {
  56. return ((object) $result)->name;
  57. }, $results);
  58. }
  59. }