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