SqlServerConnection.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Illuminate\Database;
  3. use Closure;
  4. use Exception;
  5. use Throwable;
  6. use Illuminate\Database\Schema\SqlServerBuilder;
  7. use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as DoctrineDriver;
  8. use Illuminate\Database\Query\Processors\SqlServerProcessor;
  9. use Illuminate\Database\Query\Grammars\SqlServerGrammar as QueryGrammar;
  10. use Illuminate\Database\Schema\Grammars\SqlServerGrammar as SchemaGrammar;
  11. class SqlServerConnection extends Connection
  12. {
  13. /**
  14. * Execute a Closure within a transaction.
  15. *
  16. * @param \Closure $callback
  17. * @param int $attempts
  18. * @return mixed
  19. *
  20. * @throws \Exception|\Throwable
  21. */
  22. public function transaction(Closure $callback, $attempts = 1)
  23. {
  24. for ($a = 1; $a <= $attempts; $a++) {
  25. if ($this->getDriverName() == 'sqlsrv') {
  26. return parent::transaction($callback);
  27. }
  28. $this->getPdo()->exec('BEGIN TRAN');
  29. // We'll simply execute the given callback within a try / catch block
  30. // and if we catch any exception we can rollback the transaction
  31. // so that none of the changes are persisted to the database.
  32. try {
  33. $result = $callback($this);
  34. $this->getPdo()->exec('COMMIT TRAN');
  35. }
  36. // If we catch an exception, we will roll back so nothing gets messed
  37. // up in the database. Then we'll re-throw the exception so it can
  38. // be handled how the developer sees fit for their applications.
  39. catch (Exception $e) {
  40. $this->getPdo()->exec('ROLLBACK TRAN');
  41. throw $e;
  42. } catch (Throwable $e) {
  43. $this->getPdo()->exec('ROLLBACK TRAN');
  44. throw $e;
  45. }
  46. return $result;
  47. }
  48. }
  49. /**
  50. * Get the default query grammar instance.
  51. *
  52. * @return \Illuminate\Database\Query\Grammars\SqlServerGrammar
  53. */
  54. protected function getDefaultQueryGrammar()
  55. {
  56. return $this->withTablePrefix(new QueryGrammar);
  57. }
  58. /**
  59. * Get a schema builder instance for the connection.
  60. *
  61. * @return \Illuminate\Database\Schema\SqlServerBuilder
  62. */
  63. public function getSchemaBuilder()
  64. {
  65. if (is_null($this->schemaGrammar)) {
  66. $this->useDefaultSchemaGrammar();
  67. }
  68. return new SqlServerBuilder($this);
  69. }
  70. /**
  71. * Get the default schema grammar instance.
  72. *
  73. * @return \Illuminate\Database\Schema\Grammars\SqlServerGrammar
  74. */
  75. protected function getDefaultSchemaGrammar()
  76. {
  77. return $this->withTablePrefix(new SchemaGrammar);
  78. }
  79. /**
  80. * Get the default post processor instance.
  81. *
  82. * @return \Illuminate\Database\Query\Processors\SqlServerProcessor
  83. */
  84. protected function getDefaultPostProcessor()
  85. {
  86. return new SqlServerProcessor;
  87. }
  88. /**
  89. * Get the Doctrine DBAL driver.
  90. *
  91. * @return \Doctrine\DBAL\Driver\PDOSqlsrv\Driver
  92. */
  93. protected function getDoctrineDriver()
  94. {
  95. return new DoctrineDriver;
  96. }
  97. }