1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace QB\PostgreSQL\Statement; |
6
|
|
|
|
7
|
|
|
use QB\Generic\Clause\Table; |
8
|
|
|
use QB\Generic\Statement\IInsert; |
9
|
|
|
use QB\Generic\Statement\InsertTest as GenericInsertTest; |
10
|
|
|
|
11
|
|
|
class InsertTest extends GenericInsertTest |
12
|
|
|
{ |
13
|
|
|
public function testWitDefaultValues() |
14
|
|
|
{ |
15
|
|
|
$sql = (string)$this->getSut('foo'); |
16
|
|
|
|
17
|
|
|
$parts = []; |
18
|
|
|
$parts[] = 'INSERT INTO foo'; |
19
|
|
|
$parts[] = 'DEFAULT VALUES'; |
20
|
|
|
|
21
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
22
|
|
|
|
23
|
|
|
$this->assertSame($expectedSql, $sql); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testAddMultipleRows() |
27
|
|
|
{ |
28
|
|
|
$query = $this->getSut('offices') |
29
|
|
|
->setInto(new Table('offices')) |
30
|
|
|
->addColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
31
|
|
|
->addValues('abc', 'Berlin', '+49 101 123 4567', '', 'Germany', '10111', 'NA') |
32
|
|
|
->addValues('bcd', 'Budapest', '+36 70 101 1234', '', 'Hungary', '1011', 'NA') |
33
|
|
|
->addValues('cde', 'Pécs', '+36 70 222 3456', 'Rákóczi út', 'Hungary', '723', 'NA') |
34
|
|
|
->setReturning('*'); |
35
|
|
|
|
36
|
|
|
$parts = []; |
37
|
|
|
$parts[] = 'INSERT INTO offices (officeCode, city, phone, addressLine1, country, postalCode, territory)'; |
38
|
|
|
$parts[] = 'VALUES (?, ?, ?, ?, ?, ?, ?),'; |
39
|
|
|
$parts[] = '(?, ?, ?, ?, ?, ?, ?),'; |
40
|
|
|
$parts[] = '(?, ?, ?, ?, ?, ?, ?)'; |
41
|
|
|
$parts[] = 'RETURNING *'; |
42
|
|
|
|
43
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
44
|
|
|
|
45
|
|
|
$this->assertSame($expectedSql, (string)$query); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testAddOnConflictDoNothing() |
49
|
|
|
{ |
50
|
|
|
$query = $this->getSut('offices') |
51
|
|
|
->setInto(new Table('offices')) |
52
|
|
|
->addColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
53
|
|
|
->addValues('abc', 'Berlin', '+49 101 123 4567', '', 'Germany', '10111', 'NA') |
54
|
|
|
->setDoNothing(); |
55
|
|
|
|
56
|
|
|
$parts = []; |
57
|
|
|
$parts[] = 'INSERT INTO offices (officeCode, city, phone, addressLine1, country, postalCode, territory)'; |
58
|
|
|
$parts[] = 'VALUES (?, ?, ?, ?, ?, ?, ?)'; |
59
|
|
|
$parts[] = 'ON CONFLICT DO NOTHING'; |
60
|
|
|
|
61
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
62
|
|
|
|
63
|
|
|
$this->assertSame($expectedSql, (string)$query); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testAddOnConflictDoUpdate() |
67
|
|
|
{ |
68
|
|
|
$query = $this->getSut('offices') |
69
|
|
|
->setInto(new Table('offices')) |
70
|
|
|
->addColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
71
|
|
|
->addValues('abc', 'Berlin', '+49 101 123 4567', '', 'Germany', '10111', 'NA') |
72
|
|
|
->setOnConflict('officeCode', 'city') |
73
|
|
|
->setDoUpdate('officeCode = EXCLUDED.officeCode', 'city = EXCLUDED.city'); |
74
|
|
|
|
75
|
|
|
$parts = []; |
76
|
|
|
$parts[] = 'INSERT INTO offices (officeCode, city, phone, addressLine1, country, postalCode, territory)'; |
77
|
|
|
$parts[] = 'VALUES (?, ?, ?, ?, ?, ?, ?)'; |
78
|
|
|
$parts[] = 'ON CONFLICT (officeCode, city) DO UPDATE'; |
79
|
|
|
$parts[] = 'SET officeCode = EXCLUDED.officeCode, city = EXCLUDED.city'; |
80
|
|
|
|
81
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
82
|
|
|
|
83
|
|
|
$this->assertSame($expectedSql, (string)$query); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $table |
88
|
|
|
* |
89
|
|
|
* @return Insert |
90
|
|
|
*/ |
91
|
|
|
protected function getSut(string $table): IInsert |
92
|
|
|
{ |
93
|
|
|
return (new Insert())->setInto($table); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|