SqlServerProcessor.php 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 $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. * @throws \Exception
  34. */
  35. protected function processInsertGetIdForOdbc(Connection $connection)
  36. {
  37. $result = $connection->selectFromWriteConnection(
  38. 'SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS int) AS insertid'
  39. );
  40. if (! $result) {
  41. throw new Exception('Unable to retrieve lastInsertID for ODBC.');
  42. }
  43. $row = $result[0];
  44. return is_object($row) ? $row->insertid : $row['insertid'];
  45. }
  46. /**
  47. * Process the results of a column listing query.
  48. *
  49. * @param array $results
  50. * @return array
  51. */
  52. public function processColumnListing($results)
  53. {
  54. return array_map(function ($result) {
  55. return ((object) $result)->name;
  56. }, $results);
  57. }
  58. }