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