SqlServerConnection.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Illuminate\Database;
  3. use Closure;
  4. use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as DoctrineDriver;
  5. use Doctrine\DBAL\Version;
  6. use Illuminate\Database\PDO\SqlServerDriver;
  7. use Illuminate\Database\Query\Grammars\SqlServerGrammar as QueryGrammar;
  8. use Illuminate\Database\Query\Processors\SqlServerProcessor;
  9. use Illuminate\Database\Schema\Grammars\SqlServerGrammar as SchemaGrammar;
  10. use Illuminate\Database\Schema\SqlServerBuilder;
  11. use Illuminate\Filesystem\Filesystem;
  12. use RuntimeException;
  13. use Throwable;
  14. class SqlServerConnection extends Connection
  15. {
  16. /**
  17. * Execute a Closure within a transaction.
  18. *
  19. * @param \Closure $callback
  20. * @param int $attempts
  21. * @return mixed
  22. *
  23. * @throws \Throwable
  24. */
  25. public function transaction(Closure $callback, $attempts = 1)
  26. {
  27. for ($a = 1; $a <= $attempts; $a++) {
  28. if ($this->getDriverName() === 'sqlsrv') {
  29. return parent::transaction($callback, $attempts);
  30. }
  31. $this->getPdo()->exec('BEGIN TRAN');
  32. // We'll simply execute the given callback within a try / catch block
  33. // and if we catch any exception we can rollback the transaction
  34. // so that none of the changes are persisted to the database.
  35. try {
  36. $result = $callback($this);
  37. $this->getPdo()->exec('COMMIT TRAN');
  38. }
  39. // If we catch an exception, we will rollback so nothing gets messed
  40. // up in the database. Then we'll re-throw the exception so it can
  41. // be handled how the developer sees fit for their applications.
  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 schema state for the connection.
  81. *
  82. * @param \Illuminate\Filesystem\Filesystem|null $files
  83. * @param callable|null $processFactory
  84. *
  85. * @throws \RuntimeException
  86. */
  87. public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
  88. {
  89. throw new RuntimeException('Schema dumping is not supported when using SQL Server.');
  90. }
  91. /**
  92. * Get the default post processor instance.
  93. *
  94. * @return \Illuminate\Database\Query\Processors\SqlServerProcessor
  95. */
  96. protected function getDefaultPostProcessor()
  97. {
  98. return new SqlServerProcessor;
  99. }
  100. /**
  101. * Get the Doctrine DBAL driver.
  102. *
  103. * @return \Doctrine\DBAL\Driver\PDOSqlsrv\Driver|\Illuminate\Database\PDO\SqlServerDriver
  104. */
  105. protected function getDoctrineDriver()
  106. {
  107. return class_exists(Version::class) ? new DoctrineDriver : new SqlServerDriver;
  108. }
  109. }