Skip to content

Commit c949f73

Browse files
authored
feat: implement Replace query functionality (#413)
1 parent 6ce2aa2 commit c949f73

File tree

4 files changed

+233
-1
lines changed

4 files changed

+233
-1
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace System\Database\MyQuery;
6+
7+
class Replace extends Insert
8+
{
9+
protected function builder(): string
10+
{
11+
[$binds, ,$columns] = $this->bindsDestructur();
12+
13+
$strings_binds = [];
14+
/** @var array<int, array<int, string>> */
15+
$chunk = array_chunk($binds, count($columns), true);
16+
foreach ($chunk as $group) {
17+
$strings_binds[] = '(' . implode(', ', $group) . ')';
18+
}
19+
20+
$stringBinds = implode(', ', $strings_binds);
21+
$stringColumn = implode(', ', $columns);
22+
23+
return $this->_query = "REPLACE INTO {$this->_table} ({$stringColumn}) VALUES {$stringBinds}";
24+
}
25+
}

src/System/Database/MyQuery/Table.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct($table_name, MyPDO $PDO)
3232
}
3333

3434
/**
35-
* Perform select query.
35+
* Perform insert query.
3636
*
3737
* @return Insert
3838
*/
@@ -41,6 +41,16 @@ public function insert()
4141
return new Insert($this->table_name, $this->PDO);
4242
}
4343

44+
/**
45+
* Perform replace query.
46+
*
47+
* @return Replace
48+
*/
49+
public function replace()
50+
{
51+
return new Replace($this->table_name, $this->PDO);
52+
}
53+
4454
/**
4555
* Perform select query.
4656
*

tests/DataBase/Query/ReplaceTest.php

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace System\Test\Database\Query;
6+
7+
use System\Database\MyQuery;
8+
9+
final class ReplaceTest extends \QueryStringTest
10+
{
11+
/** @test */
12+
public function itCorrectInsert(): void
13+
{
14+
$insert = MyQuery::from('test', $this->PDO)
15+
->replace()
16+
->value('a', 1)
17+
;
18+
19+
$this->assertEquals(
20+
'REPLACE INTO test (a) VALUES (:bind_a)',
21+
$insert->__toString()
22+
);
23+
24+
$this->assertEquals(
25+
'REPLACE INTO test (a) VALUES (1)',
26+
$insert->queryBind()
27+
);
28+
}
29+
30+
/** @test */
31+
public function itCorrectInsertValues(): void
32+
{
33+
$insert = MyQuery::from('test', $this->PDO)
34+
->replace()
35+
->values([
36+
'a' => 'b',
37+
'c' => 'd',
38+
'e' => 'f',
39+
])
40+
;
41+
42+
$this->assertEquals(
43+
'REPLACE INTO test (a, c, e) VALUES (:bind_a, :bind_c, :bind_e)',
44+
$insert->__toString()
45+
);
46+
47+
$this->assertEquals(
48+
"REPLACE INTO test (a, c, e) VALUES ('b', 'd', 'f')",
49+
$insert->queryBind()
50+
);
51+
}
52+
53+
/** @test */
54+
public function itCorrectInsertQueryMultyValues(): void
55+
{
56+
$insert = MyQuery::from('test', $this->PDO)
57+
->replace()
58+
->values([
59+
'a' => 'b',
60+
'c' => 'd',
61+
'e' => 'f',
62+
])
63+
->value('g', 'h')
64+
;
65+
66+
$this->assertEquals(
67+
'REPLACE INTO test (a, c, e, g) VALUES (:bind_a, :bind_c, :bind_e, :bind_g)',
68+
$insert->__toString()
69+
);
70+
71+
$this->assertEquals(
72+
"REPLACE INTO test (a, c, e, g) VALUES ('b', 'd', 'f', 'h')",
73+
$insert->queryBind()
74+
);
75+
}
76+
77+
/** @test */
78+
public function itCorrectInsertQueryMultyRaws(): void
79+
{
80+
$insert = MyQuery::from('test', $this->PDO)
81+
->replace()
82+
->rows([
83+
[
84+
'a' => 'b',
85+
'c' => 'd',
86+
'e' => 'f',
87+
], [
88+
'a' => 'b',
89+
'c' => 'd',
90+
'e' => 'f',
91+
],
92+
]);
93+
94+
$this->assertEquals(
95+
'REPLACE INTO test (a, c, e) VALUES (:bind_0_a, :bind_0_c, :bind_0_e), (:bind_1_a, :bind_1_c, :bind_1_e)',
96+
$insert->__toString()
97+
);
98+
99+
$this->assertEquals(
100+
"REPLACE INTO test (a, c, e) VALUES ('b', 'd', 'f'), ('b', 'd', 'f')",
101+
$insert->queryBind()
102+
);
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace System\Test\Database\RealDatabase;
6+
7+
use System\Database\MyQuery;
8+
9+
final class ReplaceTest extends \RealDatabaseConnectionTest
10+
{
11+
/**
12+
* @test
13+
*
14+
* @group database
15+
*/
16+
public function itCanReplaceOnNewData()
17+
{
18+
MyQuery::from('users', $this->pdo)
19+
->replace()
20+
->values([
21+
'user' => 'sony',
22+
'pwd' => 'secret',
23+
'stat' => 99,
24+
])
25+
->execute();
26+
27+
$this->assertUserExist('sony');
28+
}
29+
30+
/**
31+
* @test
32+
*
33+
* @group database
34+
*/
35+
public function itCanReplaceOnExistData()
36+
{
37+
MyQuery::from('users', $this->pdo)
38+
->insert()
39+
->values([
40+
'user' => 'sony',
41+
'pwd' => 'secret',
42+
'stat' => 99,
43+
])
44+
->execute();
45+
46+
MyQuery::from('users', $this->pdo)
47+
->replace()
48+
->values([
49+
'user' => 'sony',
50+
'pwd' => 'secret',
51+
'stat' => 66,
52+
])
53+
->execute();
54+
55+
$this->assertUserStat('sony', 66);
56+
}
57+
58+
/**
59+
* @test
60+
*
61+
* @group database
62+
*/
63+
public function itCanUpdateInsertusingOneQuery()
64+
{
65+
MyQuery::from('users', $this->pdo)
66+
->insert()
67+
->values([
68+
'user' => 'sony',
69+
'pwd' => 'secret',
70+
'stat' => 99,
71+
])
72+
->execute();
73+
74+
MyQuery::from('users', $this->pdo)
75+
->replace()
76+
->rows([
77+
[
78+
'user' => 'sony',
79+
'pwd' => 'secret',
80+
'stat' => 66,
81+
],
82+
[
83+
'user' => 'sony2',
84+
'pwd' => 'secret',
85+
'stat' => 66,
86+
],
87+
])
88+
->execute();
89+
90+
$this->assertUserStat('sony', 66);
91+
$this->assertUserExist('sony2');
92+
}
93+
}

0 commit comments

Comments
 (0)