|
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
|
3 |
|
{ |
|
25
|
|
|
public function createBatchQueryResult(QueryInterface $query, bool $each = false): BatchQueryResultInterface |
|
26
|
3 |
|
{ |
|
27
|
|
|
return new BatchQueryResult($query, $each); |
|
28
|
|
|
} |
|
29
|
233 |
|
|
|
30
|
|
|
public function createCommand(string $sql = null, array $params = []): CommandPDOInterface |
|
31
|
233 |
|
{ |
|
32
|
|
|
$command = new CommandPDO($this, $this->queryCache); |
|
33
|
233 |
|
|
|
34
|
232 |
|
if ($sql !== null) { |
|
35
|
|
|
$command->setSql($sql); |
|
36
|
|
|
} |
|
37
|
233 |
|
|
|
38
|
233 |
|
if ($this->logger !== null) { |
|
39
|
|
|
$command->setLogger($this->logger); |
|
40
|
|
|
} |
|
41
|
233 |
|
|
|
42
|
233 |
|
if ($this->profiler !== null) { |
|
43
|
|
|
$command->setProfiler($this->profiler); |
|
44
|
|
|
} |
|
45
|
233 |
|
|
|
46
|
|
|
return $command->bindValues($params); |
|
47
|
|
|
} |
|
48
|
12 |
|
|
|
49
|
|
|
public function createTransaction(): TransactionInterface |
|
50
|
12 |
|
{ |
|
51
|
|
|
return new TransactionPDO($this); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns value of the last inserted ID. |
|
56
|
394 |
|
* |
|
57
|
|
|
* SQLSERVER driver implements {@see PDO::lastInsertId()} method but with a single peculiarity: |
|
58
|
394 |
|
* |
|
59
|
394 |
|
* When `$sequence` value is a null or an empty string it returns the last inserted ID for table with an identity |
|
60
|
394 |
|
* column. |
|
61
|
394 |
|
* 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
|
394 |
|
* @param string|null $sequenceNAme the sequence name. Defaults to null. |
|
66
|
|
|
* |
|
67
|
|
|
* @return string last inserted ID value. |
|
68
|
449 |
|
*/ |
|
69
|
|
|
public function getLastInsertID(string $sequenceName = null): string |
|
70
|
449 |
|
{ |
|
71
|
449 |
|
if ($this->isActive() && $this->pdo) { |
|
72
|
|
|
return $this->pdo->lastInsertID($sequenceName); |
|
73
|
|
|
} |
|
74
|
449 |
|
|
|
75
|
|
|
throw new InvalidCallException('DB Connection is not active.'); |
|
76
|
|
|
} |
|
77
|
397 |
|
|
|
78
|
|
|
/** |
|
79
|
397 |
|
* @throws Exception|InvalidConfigException |
|
80
|
397 |
|
*/ |
|
81
|
|
|
public function getQueryBuilder(): QueryBuilderInterface |
|
82
|
|
|
{ |
|
83
|
397 |
|
if ($this->queryBuilder === null) { |
|
84
|
|
|
$this->queryBuilder = new QueryBuilder( |
|
85
|
|
|
$this->getQuoter(), |
|
86
|
|
|
$this->getSchema(), |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->queryBuilder; |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function getQuoter(): QuoterInterface |
|
94
|
|
|
{ |
|
95
|
|
|
if ($this->quoter === null) { |
|
96
|
|
|
$this->quoter = new Quoter(['[', ']'], ['[', ']'], $this->getTablePrefix()); |
|
97
|
228 |
|
} |
|
98
|
|
|
|
|
99
|
228 |
|
return $this->quoter; |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function getSchema(): SchemaInterface |
|
103
|
|
|
{ |
|
104
|
|
|
if ($this->schema === null) { |
|
105
|
|
|
$this->schema = new Schema($this, $this->schemaCache); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
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
|
|
|
protected function initConnection(): void |
|
123
|
|
|
{ |
|
124
|
|
|
$this->pdo = $this->driver->createConnection(); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|