1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mssql; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Driver\PDO\CommandPDOInterface; |
8
|
|
|
use Yiisoft\Db\Driver\PDO\ConnectionPDO as AbstractConnectionPDO; |
9
|
|
|
use Yiisoft\Db\Exception\Exception; |
10
|
|
|
use Yiisoft\Db\Exception\InvalidCallException; |
11
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
12
|
|
|
use Yiisoft\Db\Query\BatchQueryResultInterface; |
13
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
14
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
15
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
16
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
17
|
|
|
use Yiisoft\Db\Transaction\TransactionInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Database connection class prefilled for Microsoft SQL Server. |
21
|
|
|
* The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php). |
22
|
|
|
*/ |
23
|
|
|
final class ConnectionPDO extends AbstractConnectionPDO |
24
|
|
|
{ |
25
|
4 |
|
public function createBatchQueryResult(QueryInterface $query, bool $each = false): BatchQueryResultInterface |
26
|
|
|
{ |
27
|
4 |
|
return new BatchQueryResult($query, $each); |
28
|
|
|
} |
29
|
|
|
|
30
|
243 |
|
public function createCommand(string $sql = null, array $params = []): CommandPDOInterface |
31
|
|
|
{ |
32
|
243 |
|
$command = new CommandPDO($this, $this->queryCache); |
33
|
|
|
|
34
|
243 |
|
if ($sql !== null) { |
35
|
199 |
|
$command->setSql($sql); |
36
|
|
|
} |
37
|
|
|
|
38
|
243 |
|
if ($this->logger !== null) { |
39
|
3 |
|
$command->setLogger($this->logger); |
40
|
|
|
} |
41
|
|
|
|
42
|
243 |
|
if ($this->profiler !== null) { |
43
|
3 |
|
$command->setProfiler($this->profiler); |
44
|
|
|
} |
45
|
|
|
|
46
|
243 |
|
return $command->bindValues($params); |
47
|
|
|
} |
48
|
|
|
|
49
|
9 |
|
public function createTransaction(): TransactionInterface |
50
|
|
|
{ |
51
|
9 |
|
return new TransactionPDO($this); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns value of the last inserted ID. |
56
|
|
|
* |
57
|
|
|
* SQLSERVER driver implements {@see PDO::lastInsertId()} method but with a single peculiarity: |
58
|
|
|
* |
59
|
|
|
* When `$sequence` value is a null or an empty string it returns the last inserted ID for table with an identity |
60
|
|
|
* column. |
61
|
|
|
* But when parameter is not specified it works as expected and returns actual last inserted ID (like the other PDO |
62
|
|
|
* drivers). |
63
|
|
|
* if `$sequenceName` is the table name return empty string. |
64
|
|
|
* |
65
|
|
|
* @param string|null $sequenceNAme the sequence name. Defaults to null. |
66
|
|
|
* |
67
|
|
|
* @return string last inserted ID value. |
68
|
|
|
*/ |
69
|
5 |
|
public function getLastInsertID(string $sequenceName = null): string |
70
|
|
|
{ |
71
|
5 |
|
if ($this->isActive() && $this->pdo) { |
72
|
4 |
|
return $this->pdo->lastInsertID($sequenceName); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
throw new InvalidCallException('DB Connection is not active.'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @throws Exception|InvalidConfigException |
80
|
|
|
*/ |
81
|
466 |
|
public function getQueryBuilder(): QueryBuilderInterface |
82
|
|
|
{ |
83
|
466 |
|
if ($this->queryBuilder === null) { |
84
|
466 |
|
$this->queryBuilder = new QueryBuilder( |
85
|
466 |
|
$this->getQuoter(), |
86
|
466 |
|
$this->getSchema(), |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
466 |
|
return $this->queryBuilder; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
522 |
|
public function getQuoter(): QuoterInterface |
94
|
|
|
{ |
95
|
522 |
|
if ($this->quoter === null) { |
96
|
522 |
|
$this->quoter = new Quoter(['[', ']'], ['[', ']'], $this->getTablePrefix()); |
97
|
|
|
} |
98
|
|
|
|
99
|
522 |
|
return $this->quoter; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
489 |
|
public function getSchema(): SchemaInterface |
103
|
|
|
{ |
104
|
489 |
|
if ($this->schema === null) { |
105
|
489 |
|
$this->schema = new Schema($this, $this->schemaCache); |
106
|
|
|
} |
107
|
|
|
|
108
|
489 |
|
return $this->schema; |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Initializes the DB connection. |
113
|
|
|
* |
114
|
|
|
* This method is invoked right after the DB connection is established. |
115
|
|
|
* |
116
|
|
|
* The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`. |
117
|
|
|
* |
118
|
|
|
* if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty. |
119
|
|
|
* |
120
|
|
|
* It then triggers an {@see EVENT_AFTER_OPEN} event. |
121
|
|
|
*/ |
122
|
326 |
|
protected function initConnection(): void |
123
|
|
|
{ |
124
|
326 |
|
$this->pdo = $this->driver->createConnection(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|