1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Functional; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\DBAL\Driver\DriverException; |
7
|
|
|
use Doctrine\DBAL\ParameterType; |
8
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
9
|
|
|
use Doctrine\DBAL\Schema\Sequence; |
10
|
|
|
use Doctrine\DBAL\Schema\Table; |
11
|
|
|
use Doctrine\DBAL\Types\Type; |
12
|
|
|
use Doctrine\Tests\DbalFunctionalTestCase; |
13
|
|
|
use Doctrine\Tests\TestUtil; |
14
|
|
|
use Throwable; |
15
|
|
|
use function array_filter; |
16
|
|
|
use function strtolower; |
17
|
|
|
|
18
|
|
|
class WriteTest extends DbalFunctionalTestCase |
19
|
|
|
{ |
20
|
|
|
protected function setUp() |
21
|
|
|
{ |
22
|
|
|
parent::setUp(); |
23
|
|
|
|
24
|
|
|
try { |
25
|
|
|
/** @var AbstractSchemaManager $sm */ |
26
|
|
|
$table = new Table('write_table'); |
27
|
|
|
$table->addColumn('id', 'integer', ['autoincrement' => true]); |
28
|
|
|
$table->addColumn('test_int', 'integer'); |
29
|
|
|
$table->addColumn('test_string', 'string', ['notnull' => false]); |
30
|
|
|
$table->setPrimaryKey(['id']); |
31
|
|
|
|
32
|
|
|
$this->connection->getSchemaManager()->createTable($table); |
33
|
|
|
} catch (Throwable $e) { |
|
|
|
|
34
|
|
|
} |
35
|
|
|
$this->connection->executeUpdate('DELETE FROM write_table'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @group DBAL-80 |
40
|
|
|
*/ |
41
|
|
|
public function testExecuteUpdateFirstTypeIsNull() |
42
|
|
|
{ |
43
|
|
|
$sql = 'INSERT INTO write_table (test_string, test_int) VALUES (?, ?)'; |
44
|
|
|
$this->connection->executeUpdate($sql, ['text', 1111], [null, ParameterType::INTEGER]); |
45
|
|
|
|
46
|
|
|
$sql = 'SELECT * FROM write_table WHERE test_string = ? AND test_int = ?'; |
47
|
|
|
self::assertTrue((bool) $this->connection->fetchColumn($sql, ['text', 1111])); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testExecuteUpdate() |
51
|
|
|
{ |
52
|
|
|
$sql = 'INSERT INTO write_table (test_int) VALUES ( ' . $this->connection->quote(1) . ')'; |
53
|
|
|
$affected = $this->connection->executeUpdate($sql); |
54
|
|
|
|
55
|
|
|
self::assertEquals(1, $affected, 'executeUpdate() should return the number of affected rows!'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testExecuteUpdateWithTypes() |
59
|
|
|
{ |
60
|
|
|
$sql = 'INSERT INTO write_table (test_int, test_string) VALUES (?, ?)'; |
61
|
|
|
$affected = $this->connection->executeUpdate( |
62
|
|
|
$sql, |
63
|
|
|
[1, 'foo'], |
64
|
|
|
[ParameterType::INTEGER, ParameterType::STRING] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
self::assertEquals(1, $affected, 'executeUpdate() should return the number of affected rows!'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testPrepareRowCountReturnsAffectedRows() |
71
|
|
|
{ |
72
|
|
|
$sql = 'INSERT INTO write_table (test_int, test_string) VALUES (?, ?)'; |
73
|
|
|
$stmt = $this->connection->prepare($sql); |
74
|
|
|
|
75
|
|
|
$stmt->bindValue(1, 1); |
76
|
|
|
$stmt->bindValue(2, 'foo'); |
77
|
|
|
$stmt->execute(); |
78
|
|
|
|
79
|
|
|
self::assertEquals(1, $stmt->rowCount()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testPrepareWithPdoTypes() |
83
|
|
|
{ |
84
|
|
|
$sql = 'INSERT INTO write_table (test_int, test_string) VALUES (?, ?)'; |
85
|
|
|
$stmt = $this->connection->prepare($sql); |
86
|
|
|
|
87
|
|
|
$stmt->bindValue(1, 1, ParameterType::INTEGER); |
88
|
|
|
$stmt->bindValue(2, 'foo', ParameterType::STRING); |
89
|
|
|
$stmt->execute(); |
90
|
|
|
|
91
|
|
|
self::assertEquals(1, $stmt->rowCount()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testPrepareWithDbalTypes() |
95
|
|
|
{ |
96
|
|
|
$sql = 'INSERT INTO write_table (test_int, test_string) VALUES (?, ?)'; |
97
|
|
|
$stmt = $this->connection->prepare($sql); |
98
|
|
|
|
99
|
|
|
$stmt->bindValue(1, 1, Type::getType('integer')); |
100
|
|
|
$stmt->bindValue(2, 'foo', Type::getType('string')); |
101
|
|
|
$stmt->execute(); |
102
|
|
|
|
103
|
|
|
self::assertEquals(1, $stmt->rowCount()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testPrepareWithDbalTypeNames() |
107
|
|
|
{ |
108
|
|
|
$sql = 'INSERT INTO write_table (test_int, test_string) VALUES (?, ?)'; |
109
|
|
|
$stmt = $this->connection->prepare($sql); |
110
|
|
|
|
111
|
|
|
$stmt->bindValue(1, 1, 'integer'); |
112
|
|
|
$stmt->bindValue(2, 'foo', 'string'); |
113
|
|
|
$stmt->execute(); |
114
|
|
|
|
115
|
|
|
self::assertEquals(1, $stmt->rowCount()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function insertRows() |
119
|
|
|
{ |
120
|
|
|
self::assertEquals(1, $this->connection->insert('write_table', ['test_int' => 1, 'test_string' => 'foo'])); |
121
|
|
|
self::assertEquals(1, $this->connection->insert('write_table', ['test_int' => 2, 'test_string' => 'bar'])); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testInsert() |
125
|
|
|
{ |
126
|
|
|
$this->insertRows(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testDelete() |
130
|
|
|
{ |
131
|
|
|
$this->insertRows(); |
132
|
|
|
|
133
|
|
|
self::assertEquals(1, $this->connection->delete('write_table', ['test_int' => 2])); |
134
|
|
|
self::assertCount(1, $this->connection->fetchAll('SELECT * FROM write_table')); |
135
|
|
|
|
136
|
|
|
self::assertEquals(1, $this->connection->delete('write_table', ['test_int' => 1])); |
137
|
|
|
self::assertCount(0, $this->connection->fetchAll('SELECT * FROM write_table')); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testUpdate() |
141
|
|
|
{ |
142
|
|
|
$this->insertRows(); |
143
|
|
|
|
144
|
|
|
self::assertEquals(1, $this->connection->update('write_table', ['test_string' => 'bar'], ['test_string' => 'foo'])); |
145
|
|
|
self::assertEquals(2, $this->connection->update('write_table', ['test_string' => 'baz'], ['test_string' => 'bar'])); |
146
|
|
|
self::assertEquals(0, $this->connection->update('write_table', ['test_string' => 'baz'], ['test_string' => 'bar'])); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testLastInsertId() |
150
|
|
|
{ |
151
|
|
|
if (! $this->connection->getDatabasePlatform()->supportsIdentityColumns()) { |
152
|
|
|
$this->markTestSkipped('Test only works on platforms with identity columns.'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
self::assertEquals(1, $this->connection->insert('write_table', ['test_int' => 2, 'test_string' => 'bar'])); |
156
|
|
|
$num = $this->connection->lastInsertId(); |
157
|
|
|
|
158
|
|
|
self::assertGreaterThan(0, $num, 'lastInsertId() should return a positive number.'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testLastInsertIdNewConnection() |
162
|
|
|
{ |
163
|
|
|
$connection = TestUtil::getConnection(); |
164
|
|
|
|
165
|
|
|
$this->expectException(DriverException::class); |
166
|
|
|
$connection->lastInsertId(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testLastInsertIdSequence() |
170
|
|
|
{ |
171
|
|
|
if (! $this->connection->getDatabasePlatform()->supportsSequences()) { |
172
|
|
|
$this->markTestSkipped('Test only works on platforms with sequences.'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$sequence = new Sequence('write_table_id_seq'); |
176
|
|
|
try { |
177
|
|
|
$this->connection->getSchemaManager()->createSequence($sequence); |
178
|
|
|
} catch (Throwable $e) { |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$sequences = $this->connection->getSchemaManager()->listSequences(); |
182
|
|
|
self::assertCount(1, array_filter($sequences, static function ($sequence) { |
183
|
|
|
return strtolower($sequence->getName()) === 'write_table_id_seq'; |
184
|
|
|
})); |
185
|
|
|
|
186
|
|
|
$stmt = $this->connection->query($this->connection->getDatabasePlatform()->getSequenceNextValSQL('write_table_id_seq')); |
187
|
|
|
$nextSequenceVal = $stmt->fetchColumn(); |
188
|
|
|
|
189
|
|
|
$lastInsertId = $this->connection->lastInsertId('write_table_id_seq'); |
190
|
|
|
|
191
|
|
|
self::assertGreaterThan(0, $lastInsertId); |
192
|
|
|
self::assertEquals($nextSequenceVal, $lastInsertId); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testLastInsertIdNoSequenceGiven() |
196
|
|
|
{ |
197
|
|
|
if (! $this->connection->getDatabasePlatform()->supportsSequences() || $this->connection->getDatabasePlatform()->supportsIdentityColumns()) { |
198
|
|
|
$this->markTestSkipped("Test only works consistently on platforms that support sequences and don't support identity columns."); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$this->expectException(DriverException::class); |
202
|
|
|
$this->connection->lastInsertId(null); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function testLastInsertIdSequencesNotSupportedOrSequenceDoesNotExist() |
206
|
|
|
{ |
207
|
|
|
$this->expectException(DriverException::class); |
208
|
|
|
$this->connection->lastInsertId('unknown-sequence'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @group DBAL-445 |
213
|
|
|
*/ |
214
|
|
|
public function testInsertWithKeyValueTypes() |
215
|
|
|
{ |
216
|
|
|
$testString = new DateTime('2013-04-14 10:10:10'); |
217
|
|
|
|
218
|
|
|
$this->connection->insert( |
219
|
|
|
'write_table', |
220
|
|
|
['test_int' => '30', 'test_string' => $testString], |
221
|
|
|
['test_string' => 'datetime', 'test_int' => 'integer'] |
222
|
|
|
); |
223
|
|
|
|
224
|
|
|
$data = $this->connection->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30'); |
225
|
|
|
|
226
|
|
|
self::assertEquals($testString->format($this->connection->getDatabasePlatform()->getDateTimeFormatString()), $data); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @group DBAL-445 |
231
|
|
|
*/ |
232
|
|
|
public function testUpdateWithKeyValueTypes() |
233
|
|
|
{ |
234
|
|
|
$testString = new DateTime('2013-04-14 10:10:10'); |
235
|
|
|
|
236
|
|
|
$this->connection->insert( |
237
|
|
|
'write_table', |
238
|
|
|
['test_int' => '30', 'test_string' => $testString], |
239
|
|
|
['test_string' => 'datetime', 'test_int' => 'integer'] |
240
|
|
|
); |
241
|
|
|
|
242
|
|
|
$testString = new DateTime('2013-04-15 10:10:10'); |
243
|
|
|
|
244
|
|
|
$this->connection->update( |
245
|
|
|
'write_table', |
246
|
|
|
['test_string' => $testString], |
247
|
|
|
['test_int' => '30'], |
248
|
|
|
['test_string' => 'datetime', 'test_int' => 'integer'] |
249
|
|
|
); |
250
|
|
|
|
251
|
|
|
$data = $this->connection->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30'); |
252
|
|
|
|
253
|
|
|
self::assertEquals($testString->format($this->connection->getDatabasePlatform()->getDateTimeFormatString()), $data); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @group DBAL-445 |
258
|
|
|
*/ |
259
|
|
|
public function testDeleteWithKeyValueTypes() |
260
|
|
|
{ |
261
|
|
|
$val = new DateTime('2013-04-14 10:10:10'); |
262
|
|
|
$this->connection->insert( |
263
|
|
|
'write_table', |
264
|
|
|
['test_int' => '30', 'test_string' => $val], |
265
|
|
|
['test_string' => 'datetime', 'test_int' => 'integer'] |
266
|
|
|
); |
267
|
|
|
|
268
|
|
|
$this->connection->delete('write_table', ['test_int' => 30, 'test_string' => $val], ['test_string' => 'datetime', 'test_int' => 'integer']); |
269
|
|
|
|
270
|
|
|
$data = $this->connection->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30'); |
271
|
|
|
|
272
|
|
|
self::assertFalse($data); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function testEmptyIdentityInsert() |
276
|
|
|
{ |
277
|
|
|
$platform = $this->connection->getDatabasePlatform(); |
278
|
|
|
|
279
|
|
|
if (! ($platform->supportsIdentityColumns() || $platform->usesSequenceEmulatedIdentityColumns())) { |
280
|
|
|
$this->markTestSkipped( |
281
|
|
|
'Test only works on platforms with identity columns or sequence emulated identity columns.' |
282
|
|
|
); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
$table = new Table('test_empty_identity'); |
286
|
|
|
$table->addColumn('id', 'integer', ['autoincrement' => true]); |
287
|
|
|
$table->setPrimaryKey(['id']); |
288
|
|
|
|
289
|
|
|
try { |
290
|
|
|
$this->connection->getSchemaManager()->dropTable($table->getQuotedName($platform)); |
291
|
|
|
} catch (Throwable $e) { |
|
|
|
|
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
foreach ($platform->getCreateTableSQL($table) as $sql) { |
295
|
|
|
$this->connection->exec($sql); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
$seqName = $platform->usesSequenceEmulatedIdentityColumns() |
299
|
|
|
? $platform->getIdentitySequenceName('test_empty_identity', 'id') |
300
|
|
|
: null; |
301
|
|
|
|
302
|
|
|
$sql = $platform->getEmptyIdentityInsertSQL('test_empty_identity', 'id'); |
303
|
|
|
|
304
|
|
|
$this->connection->exec($sql); |
305
|
|
|
|
306
|
|
|
$firstId = $this->connection->lastInsertId($seqName); |
307
|
|
|
|
308
|
|
|
$this->connection->exec($sql); |
309
|
|
|
|
310
|
|
|
$secondId = $this->connection->lastInsertId($seqName); |
311
|
|
|
|
312
|
|
|
self::assertGreaterThan($firstId, $secondId); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @group DBAL-2688 |
317
|
|
|
*/ |
318
|
|
|
public function testUpdateWhereIsNull() |
319
|
|
|
{ |
320
|
|
|
$this->connection->insert( |
321
|
|
|
'write_table', |
322
|
|
|
['test_int' => '30', 'test_string' => null], |
323
|
|
|
['test_string' => 'string', 'test_int' => 'integer'] |
324
|
|
|
); |
325
|
|
|
|
326
|
|
|
$data = $this->connection->fetchAll('SELECT * FROM write_table WHERE test_int = 30'); |
327
|
|
|
|
328
|
|
|
self::assertCount(1, $data); |
329
|
|
|
|
330
|
|
|
$this->connection->update('write_table', ['test_int' => 10], ['test_string' => null], ['test_string' => 'string', 'test_int' => 'integer']); |
331
|
|
|
|
332
|
|
|
$data = $this->connection->fetchAll('SELECT * FROM write_table WHERE test_int = 30'); |
333
|
|
|
|
334
|
|
|
self::assertCount(0, $data); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
public function testDeleteWhereIsNull() |
338
|
|
|
{ |
339
|
|
|
$this->connection->insert( |
340
|
|
|
'write_table', |
341
|
|
|
['test_int' => '30', 'test_string' => null], |
342
|
|
|
['test_string' => 'string', 'test_int' => 'integer'] |
343
|
|
|
); |
344
|
|
|
|
345
|
|
|
$data = $this->connection->fetchAll('SELECT * FROM write_table WHERE test_int = 30'); |
346
|
|
|
|
347
|
|
|
self::assertCount(1, $data); |
348
|
|
|
|
349
|
|
|
$this->connection->delete('write_table', ['test_string' => null], ['test_string' => 'string']); |
350
|
|
|
|
351
|
|
|
$data = $this->connection->fetchAll('SELECT * FROM write_table WHERE test_int = 30'); |
352
|
|
|
|
353
|
|
|
self::assertCount(0, $data); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|