SqlServerConnection.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Illuminate\Database;
  3. use Closure;
  4. use Exception;
  5. use Illuminate\Database\Query\Grammars\SqlServerGrammar as QueryGrammar;
  6. use Illuminate\Database\Query\Processors\SqlServerProcessor;
  7. use Illuminate\Database\Schema\Grammars\SqlServerGrammar as SchemaGrammar;
  8. use Illuminate\Database\Schema\SqlServerBuilder;
  9. use Illuminate\Filesystem\Filesystem;
  10. use RuntimeException;
  11. use Throwable;
  12. class SqlServerConnection extends Connection
  13. {
  14. /**
  15. * Execute a Closure within a transaction.
  16. *
  17. * @param \Closure $callback
  18. * @param int $attempts
  19. * @return mixed
  20. *
  21. * @throws \Throwable
  22. */
  23. public function transaction(Closure $callback, $attempts = 1)
  24. {
  25. for ($a = 1; $a <= $attempts; $a++) {
  26. if ($this->getDriverName() === 'sqlsrv') {
  27. return parent::transaction($callback, $attempts);
  28. }
  29. $this->getPdo()->exec('BEGIN TRAN');
  30. // We'll simply execute the given callback within a try / catch block
  31. // and if we catch any exception we can rollback the transaction
  32. // so that none of the changes are persisted to the database.
  33. try {
  34. $result = $callback($this);
  35. $this->getPdo()->exec('COMMIT TRAN');
  36. }
  37. // If we catch an exception, we will rollback so nothing gets messed
  38. // up in the database. Then we'll re-throw the exception so it can
  39. // be handled how the developer sees fit for their applications.
  40. catch (Throwable $e) {
  41. $this->getPdo()->exec('ROLLBACK TRAN');
  42. throw $e;
  43. }
  44. return $result;
  45. }
  46. }
  47. /**
  48. * Escape a binary value for safe SQL embedding.
  49. *
  50. * @param string $value
  51. * @return string
  52. */
  53. protected function escapeBinary($value)
  54. {
  55. $hex = bin2hex($value);
  56. return "0x{$hex}";
  57. }
  58. /**
  59. * Determine if the given database exception was caused by a unique constraint violation.
  60. *
  61. * @param \Exception $exception
  62. * @return bool
  63. */
  64. protected function isUniqueConstraintError(Exception $exception)
  65. {
  66. return boolval(preg_match('#Cannot insert duplicate key row in object#i', $exception->getMessage()));
  67. }
  68. /**
  69. * Get the default query grammar instance.
  70. *
  71. * @return \Illuminate\Database\Query\Grammars\SqlServerGrammar
  72. */
  73. protected function getDefaultQueryGrammar()
  74. {
  75. ($grammar = new QueryGrammar)->setConnection($this);
  76. return $this->withTablePrefix($grammar);
  77. }
  78. /**
  79. * Get a schema builder instance for the connection.
  80. *
  81. * @return \Illuminate\Database\Schema\SqlServerBuilder
  82. */
  83. public function getSchemaBuilder()
  84. {
  85. if (is_null($this->schemaGrammar)) {
  86. $this->useDefaultSchemaGrammar();
  87. }
  88. return new SqlServerBuilder($this);
  89. }
  90. /**
  91. * Get the default schema grammar instance.
  92. *
  93. * @return \Illuminate\Database\Schema\Grammars\SqlServerGrammar
  94. */
  95. protected function getDefaultSchemaGrammar()
  96. {
  97. ($grammar = new SchemaGrammar)->setConnection($this);
  98. return $this->withTablePrefix($grammar);
  99. }
  100. /**
  101. * Get the schema state for the connection.
  102. *
  103. * @param \Illuminate\Filesystem\Filesystem|null $files
  104. * @param callable|null $processFactory
  105. *
  106. * @throws \RuntimeException
  107. */
  108. public function getSchemaState(?Filesystem $files = null, ?callable $processFactory = null)
  109. {
  110. throw new RuntimeException('Schema dumping is not supported when using SQL Server.');
  111. }
  112. /**
  113. * Get the default post processor instance.
  114. *
  115. * @return \Illuminate\Database\Query\Processors\SqlServerProcessor
  116. */
  117. protected function getDefaultPostProcessor()
  118. {
  119. return new SqlServerProcessor;
  120. }
  121. }