|
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\Expr\Expr; |
|
9
|
|
|
use QB\Generic\Statement\InsertTest as GenericInsertTest; |
|
10
|
|
|
use RuntimeException; |
|
11
|
|
|
|
|
12
|
|
|
class InsertTest extends GenericInsertTest |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @suppress PhanNoopCast |
|
16
|
|
|
*/ |
|
17
|
|
|
public function testToStringThrowsAnExceptionIfNotInitialized() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->expectException(RuntimeException::class); |
|
20
|
|
|
|
|
21
|
|
|
(string)(new Insert()); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testWitDefaultValues() |
|
25
|
|
|
{ |
|
26
|
|
|
$sql = (string)$this->getSut('foo'); |
|
27
|
|
|
|
|
28
|
|
|
$parts = []; |
|
29
|
|
|
$parts[] = 'INSERT INTO foo'; |
|
30
|
|
|
$parts[] = 'DEFAULT VALUES'; |
|
31
|
|
|
|
|
32
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertSame($expectedSql, $sql); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testComplex() |
|
38
|
|
|
{ |
|
39
|
|
|
$sql = (string)$this->getSut('foo') |
|
40
|
|
|
->columns('id', 'bar_id', 'baz') |
|
41
|
|
|
->values('1234', new Expr('?', ['a']), '"a"') |
|
42
|
|
|
->values('3456', '4567', '"b"'); |
|
43
|
|
|
|
|
44
|
|
|
$parts = []; |
|
45
|
|
|
$parts[] = 'INSERT INTO foo (id, bar_id, baz)'; |
|
46
|
|
|
$parts[] = 'VALUES (1234, ?, "a"),'; |
|
47
|
|
|
$parts[] = '(3456, 4567, "b")'; |
|
48
|
|
|
|
|
49
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertSame($expectedSql, $sql); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testAddMultipleRows() |
|
55
|
|
|
{ |
|
56
|
|
|
$query = $this->getSut('offices') |
|
57
|
|
|
->into(new Table('offices')) |
|
58
|
|
|
->columns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
|
59
|
|
|
->values('"abc"', '"Berlin"', '"+49 101 123 4567"', '""', '"Germany"', '"10111"', '"NA"') |
|
60
|
|
|
->values('"bcd"', '"Budapest"', '"+36 70 101 1234"', '""', '"Hungary"', '"1011"', '"NA"') |
|
61
|
|
|
->values('"cde"', '"Pécs"', '"+36 70 222 3456"', '"Rákóczi út"', '"Hungary"', '"723"', '"NA"') |
|
62
|
|
|
->returning('*'); |
|
63
|
|
|
|
|
64
|
|
|
$parts = []; |
|
65
|
|
|
$parts[] = 'INSERT INTO offices (officeCode, city, phone, addressLine1, country, postalCode, territory)'; |
|
66
|
|
|
$parts[] = 'VALUES ("abc", "Berlin", "+49 101 123 4567", "", "Germany", "10111", "NA"),'; |
|
67
|
|
|
$parts[] = '("bcd", "Budapest", "+36 70 101 1234", "", "Hungary", "1011", "NA"),'; |
|
68
|
|
|
$parts[] = '("cde", "Pécs", "+36 70 222 3456", "Rákóczi út", "Hungary", "723", "NA")'; |
|
69
|
|
|
$parts[] = 'RETURNING *'; |
|
70
|
|
|
|
|
71
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertSame($expectedSql, (string)$query); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testAddOnConflictDoNothing() |
|
77
|
|
|
{ |
|
78
|
|
|
$query = $this->getSut('offices') |
|
79
|
|
|
->into(new Table('offices')) |
|
80
|
|
|
->columns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
|
81
|
|
|
->values('"abc"', '"Berlin"', '"+49 101 123 4567"', '""', '"Germany"', '"10111"', '"NA"') |
|
82
|
|
|
->doNothing(); |
|
83
|
|
|
|
|
84
|
|
|
$parts = []; |
|
85
|
|
|
$parts[] = 'INSERT INTO offices (officeCode, city, phone, addressLine1, country, postalCode, territory)'; |
|
86
|
|
|
$parts[] = 'VALUES ("abc", "Berlin", "+49 101 123 4567", "", "Germany", "10111", "NA")'; |
|
87
|
|
|
$parts[] = 'ON CONFLICT DO NOTHING'; |
|
88
|
|
|
|
|
89
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertSame($expectedSql, (string)$query); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testAddOnConflictDoUpdate() |
|
95
|
|
|
{ |
|
96
|
|
|
$query = $this->getSut('offices') |
|
97
|
|
|
->into(new Table('offices')) |
|
98
|
|
|
->columns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory') |
|
99
|
|
|
->values('"abc"', '"Berlin"', '"+49 101 123 4567"', '""', '"Germany"', '"10111"', '"NA"') |
|
100
|
|
|
->onConflict('officeCode', 'city') |
|
101
|
|
|
->doUpdate('officeCode = EXCLUDED.officeCode', 'city = EXCLUDED.city') |
|
102
|
|
|
->returning('*'); |
|
103
|
|
|
|
|
104
|
|
|
$parts = []; |
|
105
|
|
|
$parts[] = 'INSERT INTO offices (officeCode, city, phone, addressLine1, country, postalCode, territory)'; |
|
106
|
|
|
$parts[] = 'VALUES ("abc", "Berlin", "+49 101 123 4567", "", "Germany", "10111", "NA")'; |
|
107
|
|
|
$parts[] = 'ON CONFLICT (officeCode, city) DO UPDATE'; |
|
108
|
|
|
$parts[] = 'SET officeCode = EXCLUDED.officeCode, city = EXCLUDED.city'; |
|
109
|
|
|
$parts[] = 'RETURNING *'; |
|
110
|
|
|
|
|
111
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertSame($expectedSql, (string)$query); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $table |
|
118
|
|
|
* |
|
119
|
|
|
* @return Insert |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function getSut(string $table): Insert |
|
122
|
|
|
{ |
|
123
|
|
|
return (new Insert())->into($table); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|