|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests\Support\Stubs; |
|
6
|
|
|
|
|
7
|
|
|
use PDO; |
|
8
|
|
|
use Yiisoft\Db\Command\CommandInterface; |
|
9
|
|
|
use Yiisoft\Db\Driver\PDO\ConnectionPDO; |
|
10
|
|
|
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface; |
|
11
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
|
12
|
|
|
use Yiisoft\Db\Schema\Quoter; |
|
13
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
|
14
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
|
15
|
|
|
use Yiisoft\Db\Tests\Support\Mock; |
|
16
|
|
|
use Yiisoft\Db\Transaction\TransactionInterface; |
|
17
|
|
|
|
|
18
|
|
|
final class Connection extends ConnectionPDO implements ConnectionPDOInterface |
|
19
|
|
|
{ |
|
20
|
|
|
protected QueryBuilderInterface|null $queryBuilder = null; |
|
21
|
|
|
protected SchemaInterface|null $schema = null; |
|
22
|
|
|
private Mock|null $mock = null; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(string $dsn) |
|
25
|
|
|
{ |
|
26
|
|
|
parent::__construct(new PDODriver($dsn), $this->getMock()->getQueryCache(), $this->getMock()->getSchemaCache()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function createCommand(string $sql = null, array $params = []): CommandInterface |
|
30
|
|
|
{ |
|
31
|
|
|
$command = new Command($this, $this->getMock()->getQueryCache()); |
|
32
|
|
|
|
|
33
|
|
|
if ($sql !== null) { |
|
34
|
|
|
$command->setSql($sql); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if ($this->logger !== null) { |
|
38
|
|
|
$command->setLogger($this->logger); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ($this->profiler !== null) { |
|
42
|
|
|
$command->setProfiler($this->profiler); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $command->bindValues($params); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function createTransaction(): TransactionInterface |
|
49
|
|
|
{ |
|
50
|
|
|
return new Transaction($this); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getMock(): Mock |
|
54
|
|
|
{ |
|
55
|
|
|
if ($this->mock === null) { |
|
56
|
|
|
$this->mock = new Mock(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this->mock; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getQueryBuilder(): QueryBuilderInterface |
|
63
|
|
|
{ |
|
64
|
|
|
if ($this->queryBuilder === null) { |
|
65
|
|
|
$this->queryBuilder = new QueryBuilder( |
|
66
|
|
|
$this->getQuoter(), |
|
67
|
|
|
$this->getSchema(), |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->queryBuilder; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getQuoter(): QuoterInterface |
|
75
|
|
|
{ |
|
76
|
|
|
if ($this->quoter === null) { |
|
77
|
|
|
$this->quoter = new Quoter('`', '`', $this->getTablePrefix()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->quoter; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getSchema(): SchemaInterface |
|
84
|
|
|
{ |
|
85
|
|
|
if ($this->schema === null) { |
|
86
|
|
|
$this->schema = new Schema($this, $this->getMock()->getSchemaCache()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->schema; |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function initConnection(): void |
|
93
|
|
|
{ |
|
94
|
|
|
if ($this->getEmulatePrepare() !== null) { |
|
95
|
|
|
$this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->pdo = $this->driver->createConnection(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|