|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mssql; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Db\Cache\QueryCache; |
|
8
|
|
|
use Yiisoft\Db\Cache\SchemaCache; |
|
9
|
|
|
use Yiisoft\Db\Command\Command; |
|
10
|
|
|
use Yiisoft\Db\Connection\Connection as AbstractConnection; |
|
11
|
|
|
|
|
12
|
|
|
use function in_array; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Database connection class prefilled for MSSQL Server. |
|
16
|
|
|
*/ |
|
17
|
|
|
final class Connection extends AbstractConnection |
|
18
|
|
|
{ |
|
19
|
|
|
private bool $isSybase = false; |
|
20
|
195 |
|
private QueryCache $queryCache; |
|
21
|
|
|
private SchemaCache $schemaCache; |
|
22
|
195 |
|
|
|
23
|
195 |
|
public function __construct(string $dsn, QueryCache $queryCache, SchemaCache $schemaCache) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->queryCache = $queryCache; |
|
26
|
195 |
|
$this->schemaCache = $schemaCache; |
|
27
|
|
|
|
|
28
|
195 |
|
parent::__construct($dsn, $queryCache); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function createCommand(?string $sql = null, array $params = []): Command |
|
32
|
|
|
{ |
|
33
|
|
|
if ($sql !== null) { |
|
34
|
|
|
$sql = $this->quoteSql($sql); |
|
35
|
|
|
} |
|
36
|
326 |
|
|
|
37
|
|
|
$command = new Command($this, $this->queryCache, $sql); |
|
38
|
326 |
|
|
|
39
|
|
|
if ($this->logger !== null) { |
|
40
|
|
|
$command->setLogger($this->logger); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ($this->profiler !== null) { |
|
44
|
|
|
$command->setProfiler($this->profiler); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $command->bindValues($params); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns the schema information for the database opened by this connection. |
|
52
|
|
|
* |
|
53
|
|
|
* @return Schema the schema information for the database opened by this connection. |
|
54
|
198 |
|
*/ |
|
55
|
|
|
public function getSchema(): Schema |
|
56
|
198 |
|
{ |
|
57
|
198 |
|
return new Schema($this, $this->schemaCache); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function isSybase(): bool |
|
61
|
|
|
{ |
|
62
|
198 |
|
return $this->isSybase; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
198 |
|
/** |
|
66
|
|
|
* @param bool $value set the database connected via pdo_dblib is SyBase, for default it's false. |
|
67
|
198 |
|
*/ |
|
68
|
|
|
public function sybase(bool $value): void |
|
69
|
198 |
|
{ |
|
70
|
|
|
$this->isSybase = $value; |
|
71
|
|
|
} |
|
72
|
198 |
|
|
|
73
|
|
|
protected function createPdoInstance(): \PDO |
|
74
|
|
|
{ |
|
75
|
|
|
if ($this->getDriverName() === 'sqlsrv') { |
|
76
|
|
|
$pdo = new SqlsrvPDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes()); |
|
77
|
|
|
} else { |
|
78
|
|
|
$pdo = new PDO($this->getDsn(), $this->getUsername(), $this->getPassword(), $this->getAttributes()); |
|
79
|
199 |
|
} |
|
80
|
|
|
|
|
81
|
199 |
|
return $pdo; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function initConnection(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$pdo = $this->getPDO(); |
|
87
|
|
|
|
|
88
|
|
|
if ($pdo !== null) { |
|
89
|
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
90
|
|
|
|
|
91
|
|
|
if (!$this->isSybase && in_array($this->getDriverName(), ['mssql', 'dblib'], true)) { |
|
92
|
|
|
$pdo->exec('SET ANSI_NULL_DFLT_ON ON'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns the name of the DB driver. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string name of the DB driver |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getDriverName(): string |
|
103
|
|
|
{ |
|
104
|
|
|
return 'sqlsrv'; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|