Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit 3a88162

Browse files
authored
Merge pull request #72 from roxblnfk/feature/insert-default-values
Allow insert default values (empty dataset)
2 parents 04f94c9 + 23d0957 commit 3a88162

File tree

7 files changed

+75
-19
lines changed

7 files changed

+75
-19
lines changed

src/Driver/Compiler.php

+7
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ protected function insertQuery(QueryParameters $params, Quoter $q, array $tokens
155155
$values[] = $this->value($params, $q, $value);
156156
}
157157

158+
if ($tokens['columns'] === []) {
159+
return sprintf(
160+
'INSERT INTO %s DEFAULT VALUES',
161+
$this->name($params, $q, $tokens['table'], true)
162+
);
163+
}
164+
158165
return sprintf(
159166
'INSERT INTO %s (%s) VALUES %s',
160167
$this->name($params, $q, $tokens['table'], true),

src/Driver/MySQL/MySQLCompiler.php

+18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@
2222
*/
2323
class MySQLCompiler extends Compiler implements CachingCompilerInterface
2424
{
25+
/**
26+
* @param QueryParameters $params
27+
* @param Quoter $q
28+
* @param array $tokens
29+
* @return string
30+
*/
31+
protected function insertQuery(QueryParameters $params, Quoter $q, array $tokens): string
32+
{
33+
if ($tokens['columns'] === []) {
34+
return sprintf(
35+
'INSERT INTO %s () VALUES ()',
36+
$this->name($params, $q, $tokens['table'], true)
37+
);
38+
}
39+
40+
return parent::insertQuery($params, $q, $tokens);
41+
}
42+
2543
/**
2644
* {@inheritdoc}
2745
*

src/Driver/SQLite/SQLiteCompiler.php

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ protected function selectQuery(QueryParameters $params, Quoter $q, array $tokens
6565
*/
6666
protected function insertQuery(QueryParameters $params, Quoter $q, array $tokens): string
6767
{
68+
if ($tokens['columns'] === []) {
69+
return sprintf(
70+
'INSERT INTO %s DEFAULT VALUES',
71+
$this->name($params, $q, $tokens['table'], true)
72+
);
73+
}
74+
6875
// @todo possibly different statement for versions higher than 3.7.11
6976
if (count($tokens['values']) === 1) {
7077
return parent::insertQuery($params, $q, $tokens);

src/Query/InsertQuery.php

+1-14
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function values($rowsets): InsertQuery
101101
}
102102

103103
if ($rowsets === []) {
104-
throw new BuilderException('Insert rowsets must not be empty');
104+
return $this;
105105
}
106106

107107
//Checking if provided set is array of multiple
@@ -122,19 +122,6 @@ public function values($rowsets): InsertQuery
122122
return $this;
123123
}
124124

125-
/**
126-
* @param QueryParameters|null $parameters
127-
* @return string
128-
*/
129-
public function sqlStatement(QueryParameters $parameters = null): string
130-
{
131-
if ($this->values === []) {
132-
throw new BuilderException('Insert rowsets must not be empty');
133-
}
134-
135-
return parent::sqlStatement($parameters);
136-
}
137-
138125
/**
139126
* Run the query and return last insert id.
140127
*

tests/Database/Driver/MySQL/InsertQueryTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,24 @@
1818
class InsertQueryTest extends \Spiral\Database\Tests\InsertQueryTest
1919
{
2020
public const DRIVER = 'mysql';
21+
22+
public function testCompileQueryDefaults(): void
23+
{
24+
$insert = $this->db()->insert('table')->values([]);
25+
26+
$this->assertSameQuery(
27+
"INSERT INTO {table} () VALUES ()",
28+
(string)$insert
29+
);
30+
}
31+
32+
public function testSimpleInsertEmptyDataset(): void
33+
{
34+
$insert = $this->database->insert()->into('table')->values([]);
35+
36+
$this->assertSameQuery(
37+
"INSERT INTO {table} () VALUES ()",
38+
$insert
39+
);
40+
}
2141
}

tests/Database/InsertQueryTest.php

+21-5
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,36 @@ public function testCompileQuery(): void
3838
);
3939
}
4040

41-
public function testSimpleInsert(): void
41+
public function testCompileQueryDefaults(): void
4242
{
43-
$insert = $this->database->insert()->into('table')->values(
44-
[
45-
'name' => 'Anton'
46-
]
43+
$insert = $this->db()->insert('table')->values([]);
44+
45+
$this->assertSameQuery(
46+
"INSERT INTO {table} DEFAULT VALUES",
47+
(string)$insert
4748
);
49+
}
50+
51+
public function testSimpleInsert(): void
52+
{
53+
$insert = $this->database->insert()->into('table')->values(['name' => 'Anton']);
4854

4955
$this->assertSameQuery(
5056
'INSERT INTO {table} ({name}) VALUES (?)',
5157
$insert
5258
);
5359
}
5460

61+
public function testSimpleInsertEmptyDataset(): void
62+
{
63+
$insert = $this->database->insert()->into('table')->values([]);
64+
65+
$this->assertSameQuery(
66+
"INSERT INTO {table} DEFAULT VALUES",
67+
$insert
68+
);
69+
}
70+
5571
public function testSimpleInsertWithStatesValues(): void
5672
{
5773
$insert = $this->database->insert()->into('table')

tests/docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ version: "3"
33
services:
44
sqlserver:
55
image: mcr.microsoft.com/mssql/server:2019-latest
6+
restart: always
67
ports:
78
- "11433:1433"
89
environment:

0 commit comments

Comments
 (0)