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