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
|
|
|
private QueryCache $queryCache; |
21
|
|
|
private SchemaCache $schemaCache; |
22
|
|
|
|
23
|
414 |
|
public function __construct(string $dsn, QueryCache $queryCache, SchemaCache $schemaCache) |
24
|
|
|
{ |
25
|
414 |
|
$this->queryCache = $queryCache; |
26
|
414 |
|
$this->schemaCache = $schemaCache; |
27
|
|
|
|
28
|
414 |
|
parent::__construct($dsn, $queryCache); |
29
|
414 |
|
} |
30
|
|
|
|
31
|
201 |
|
public function createCommand(?string $sql = null, array $params = []): Command |
32
|
|
|
{ |
33
|
201 |
|
if ($sql !== null) { |
34
|
201 |
|
$sql = $this->quoteSql($sql); |
35
|
|
|
} |
36
|
|
|
|
37
|
201 |
|
$command = new Command($this, $this->queryCache, $sql); |
38
|
|
|
|
39
|
201 |
|
if ($this->logger !== null) { |
40
|
201 |
|
$command->setLogger($this->logger); |
41
|
|
|
} |
42
|
|
|
|
43
|
201 |
|
if ($this->profiler !== null) { |
44
|
201 |
|
$command->setProfiler($this->profiler); |
45
|
|
|
} |
46
|
|
|
|
47
|
201 |
|
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
|
|
|
*/ |
55
|
335 |
|
public function getSchema(): Schema |
56
|
|
|
{ |
57
|
335 |
|
return new Schema($this, $this->schemaCache); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function isSybase(): bool |
61
|
|
|
{ |
62
|
|
|
return $this->isSybase; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param bool $value set the database connected via pdo_dblib is SyBase, for default it's false. |
67
|
|
|
*/ |
68
|
|
|
public function sybase(bool $value): void |
69
|
|
|
{ |
70
|
|
|
$this->isSybase = $value; |
71
|
|
|
} |
72
|
|
|
|
73
|
204 |
|
protected function createPdoInstance(): \PDO |
74
|
|
|
{ |
75
|
204 |
|
if ($this->getDriverName() === 'sqlsrv') { |
76
|
204 |
|
$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
|
|
|
} |
80
|
|
|
|
81
|
204 |
|
return $pdo; |
82
|
|
|
} |
83
|
|
|
|
84
|
204 |
|
protected function initConnection(): void |
85
|
|
|
{ |
86
|
204 |
|
$pdo = $this->getPDO(); |
87
|
|
|
|
88
|
204 |
|
if ($pdo !== null) { |
89
|
204 |
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
90
|
|
|
|
91
|
204 |
|
if (!$this->isSybase && in_array($this->getDriverName(), ['mssql', 'dblib'], true)) { |
92
|
|
|
$pdo->exec('SET ANSI_NULL_DFLT_ON ON'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
204 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the name of the DB driver. |
99
|
|
|
* |
100
|
|
|
* @return string name of the DB driver |
101
|
|
|
*/ |
102
|
205 |
|
public function getDriverName(): string |
103
|
|
|
{ |
104
|
205 |
|
return 'sqlsrv'; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|