|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests\Common; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Db\Tests\AbstractConnectionPDOTest; |
|
8
|
|
|
use Yiisoft\Db\Tests\Support\TestTrait; |
|
9
|
|
|
|
|
10
|
|
|
use function chr; |
|
11
|
|
|
use function mb_chr; |
|
12
|
|
|
use function str_replace; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @group mssql |
|
16
|
|
|
* @group mysql |
|
17
|
|
|
* @group pgsql |
|
18
|
|
|
* @group oracle |
|
19
|
|
|
* @group sqlite |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class CommonConnectionPDOTest extends AbstractConnectionPDOTest |
|
22
|
|
|
{ |
|
23
|
|
|
use TestTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Ensure database connection is reset on when a connection is cloned. |
|
27
|
|
|
* |
|
28
|
|
|
* Make sure each connection element has its own PDO instance i.e. own connection to the DB. |
|
29
|
|
|
* Also, transaction elements should not be shared between two connections. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function testClone(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$db = $this->getConnection(); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertNull($db->getTransaction()); |
|
36
|
|
|
$this->assertNull($db->getPDO()); |
|
37
|
|
|
|
|
38
|
|
|
$db->open(); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertNull($db->getTransaction()); |
|
41
|
|
|
$this->assertNotNull($db->getPDO()); |
|
42
|
|
|
|
|
43
|
|
|
$conn2 = clone $db; |
|
44
|
|
|
|
|
45
|
|
|
$this->assertNull($db->getTransaction()); |
|
46
|
|
|
$this->assertNotNull($db->getPDO()); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertNull($conn2->getTransaction()); |
|
49
|
|
|
$this->assertNull($conn2->getPDO()); |
|
50
|
|
|
|
|
51
|
|
|
$db->beginTransaction(); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertNotNull($db->getTransaction()); |
|
54
|
|
|
$this->assertNotNull($db->getPDO()); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertNull($conn2->getTransaction()); |
|
57
|
|
|
$this->assertNull($conn2->getPDO()); |
|
58
|
|
|
|
|
59
|
|
|
$conn3 = clone $db; |
|
60
|
|
|
|
|
61
|
|
|
$this->assertNotNull($db->getTransaction()); |
|
62
|
|
|
$this->assertNotNull($db->getPDO()); |
|
63
|
|
|
$this->assertNull($conn3->getTransaction()); |
|
64
|
|
|
$this->assertNull($conn3->getPDO()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testInsertEx(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$db = $this->getConnectionWithData(); |
|
70
|
|
|
|
|
71
|
|
|
$result = $db |
|
72
|
|
|
->createCommand() |
|
73
|
|
|
->insertEx('customer', ['name' => 'testParams', 'email' => '[email protected]', 'address' => '1']); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertIsArray($result); |
|
76
|
|
|
$this->assertNotNull($result['id']); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testQuoteValue(): void |
|
80
|
|
|
{ |
|
81
|
|
|
$db = $this->getConnectionWithData(); |
|
82
|
|
|
|
|
83
|
|
|
$db->createCommand( |
|
84
|
|
|
<<<SQL |
|
85
|
|
|
DELETE FROM {{quoter}} |
|
86
|
|
|
SQL |
|
87
|
|
|
)->execute(); |
|
88
|
|
|
$data = $this->generateQuoterEscapingValues(); |
|
89
|
|
|
|
|
90
|
|
|
foreach ($data as $index => $value) { |
|
91
|
|
|
$quotedName = $db->quoteValue('testValue_' . $index); |
|
92
|
|
|
$quoteValue = $db->quoteValue($value); |
|
93
|
|
|
|
|
94
|
|
|
$db->createCommand( |
|
95
|
|
|
<<<SQL |
|
96
|
|
|
INSERT INTO {{quoter}} ([[name]], [[description]]) values ($quotedName, $quoteValue) |
|
97
|
|
|
SQL |
|
98
|
|
|
)->execute(); |
|
99
|
|
|
$result = $db->createCommand( |
|
100
|
|
|
<<<SQL |
|
101
|
|
|
SELECT * FROM {{quoter}} WHERE [[name]]=$quotedName |
|
102
|
|
|
SQL |
|
103
|
|
|
)->queryOne(); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertSame($value, $result['description']); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testQuoteValueEscapingValueFull() |
|
110
|
|
|
{ |
|
111
|
|
|
$db = $this->getConnectionWithData(); |
|
112
|
|
|
|
|
113
|
|
|
$template = 'aaaaa{1}aaa{1}aaaabbbbb{2}bbbb{2}bbbb'; |
|
114
|
|
|
$db->createCommand( |
|
115
|
|
|
<<<SQL |
|
116
|
|
|
DELETE FROM {{quoter}} |
|
117
|
|
|
SQL |
|
118
|
|
|
)->execute(); |
|
119
|
|
|
|
|
120
|
|
|
for ($symbol1 = 1; $symbol1 <= 127; $symbol1++) { |
|
121
|
|
|
for ($symbol2 = 1; $symbol2 <= 127; $symbol2++) { |
|
122
|
|
|
$quotedName = $db->quoteValue('test_' . $symbol1 . '_' . $symbol2); |
|
123
|
|
|
$testString = str_replace(['{1}', '{2}',], [chr($symbol1), chr($symbol2)], $template); |
|
124
|
|
|
$quoteValue = $db->quoteValue($testString); |
|
125
|
|
|
$db->createCommand( |
|
126
|
|
|
<<<SQL |
|
127
|
|
|
INSERT INTO {{quoter}} ([[name]], [[description]]) values ($quotedName, $quoteValue) |
|
128
|
|
|
SQL |
|
129
|
|
|
)->execute(); |
|
130
|
|
|
$result = $db->createCommand( |
|
131
|
|
|
<<<SQL |
|
132
|
|
|
SELECT * FROM {{quoter}} WHERE [[name]]=$quotedName |
|
133
|
|
|
SQL |
|
134
|
|
|
)->queryOne(); |
|
135
|
|
|
|
|
136
|
|
|
$this->assertSame($testString, $result['description']); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
protected function generateQuoterEscapingValues(): array |
|
142
|
|
|
{ |
|
143
|
|
|
$result = []; |
|
144
|
|
|
$stringLength = 16; |
|
145
|
|
|
|
|
146
|
|
|
for ($i = 32; $i < 128 - $stringLength; $i += $stringLength) { |
|
147
|
|
|
$str = ''; |
|
148
|
|
|
|
|
149
|
|
|
for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) { |
|
150
|
|
|
$str .= mb_chr($symbol, 'UTF-8'); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$result[] = $str; |
|
154
|
|
|
$str = ''; |
|
155
|
|
|
|
|
156
|
|
|
for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) { |
|
157
|
|
|
$str .= mb_chr($symbol, 'UTF-8') . mb_chr($symbol, 'UTF-8'); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$result[] = $str; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $result; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|