Skip to content

Commit 4225052

Browse files
committed
Added MassInsert
1 parent 8f46435 commit 4225052

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

src/Builder/Insert.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ class Insert extends InsertUpdateStatement {
99
* @var array
1010
*/
1111
private $fields = array();
12-
1312
/**
1413
* @var array
1514
*/
1615
private $update = array();
17-
1816
/**
1917
* @var string
2018
*/
2119
private $table = null;
22-
2320
/**
2421
* @var string
2522
*/
2623
private $keyField = null;
27-
2824
/**
2925
* @var bool
3026
*/
3127
private $ignore = false;
28+
/**
29+
* @var Select
30+
*/
31+
private $from = null;
3232

3333
/**
3434
* @param string $table
@@ -172,6 +172,15 @@ public function addOrUpdateAll(array $data) {
172172
return $this;
173173
}
174174

175+
/**
176+
* @param Select $select
177+
* @return $this
178+
*/
179+
public function from(Select $select) {
180+
$this->from = $select;
181+
return $this;
182+
}
183+
175184
/**
176185
* @throws Exception
177186
* @return string
@@ -181,25 +190,35 @@ public function __toString() {
181190
throw new Exception('Specify a table-name');
182191
}
183192

184-
$fields = $this->fields;
185-
$tableFields = $this->db()->getTableFields($this->table);
186-
187-
$insertData = $this->buildFieldList($fields, $tableFields);
188-
$updateData = $this->buildUpdate();
189-
190-
if (empty($insertData)) {
191-
throw new Exception('No field-data found');
192-
}
193-
194193
$tableName = (new AliasReplacer($this->db()->getAliasRegistry()))->replace($this->table);
195194

196195
$queryArr = array();
197196
$ignoreStr = $this->ignore ? ' IGNORE' : '';
198197
$queryArr[] = "INSERT{$ignoreStr} INTO\n\t{$tableName}\n";
199-
$queryArr[] = "SET\n{$insertData}\n";
198+
199+
if($this->from !== null) {
200+
$fields = $this->from->getFields();
201+
$queryArr[] = sprintf("\t(%s)\n", join(', ', array_keys($fields)));
202+
$queryArr[] = trim(trim($this->from), ';');
203+
} else {
204+
$fields = $this->fields;
205+
$tableFields = $this->db()->getTableFields($this->table);
206+
207+
$insertData = $this->buildFieldList($fields, $tableFields);
208+
209+
if (empty($insertData)) {
210+
throw new Exception('No field-data found');
211+
}
212+
213+
$queryArr[] = "SET\n{$insertData}\n";
214+
215+
}
216+
217+
$updateData = $this->buildUpdate();
200218
if($updateData) {
201219
$queryArr[] = "{$updateData}\n";
202220
}
221+
203222
$queryArr[] = ";\n";
204223

205224
$query = join('', $queryArr);

src/Builder/Select.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ public function fields(array $fields) {
8282
return $this;
8383
}
8484

85+
/**
86+
* @return array
87+
*/
88+
public function getFields() {
89+
return $this->fields;
90+
}
91+
8592
/**
8693
* @param string $alias
8794
* @param string $table

tests.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
timeoutForSmallTests="5">
77
<testsuites>
88
<testsuite name="Streams">
9-
<file>tests/Builder/SelectTest.php</file>
10-
<file>tests/Builder/UpdateTest.php</file>
11-
<file>tests/Builder/InsertTest.php</file>
12-
<file>tests/Builder/DeleteTest.php</file>
9+
<directory>tests</directory>
1310
</testsuite>
1411
</testsuites>
1512
</phpunit>

tests/Builder/InsertTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Kir\MySQL\Builder;
33

44
use Kir\MySQL\Builder\InsertTest\TestInsert;
5+
use Kir\MySQL\Builder\SelectTest\TestSelect;
56

67
class InsertTest extends \PHPUnit_Framework_TestCase {
78
public function testAlias() {
@@ -12,4 +13,20 @@ public function testAlias() {
1213

1314
$this->assertEquals($query, 'INSERT'.' INTO shop.orders_items SET last_update=NOW() ;');
1415
}
16+
17+
public function testMassInsert() {
18+
$select = TestSelect::create()
19+
->fields(['a' => 'b'])
20+
->from('oi', 'orders#items')
21+
->where('1!=2');
22+
23+
$query = TestInsert::create()
24+
->into('orders#items')
25+
->from($select)
26+
->updateExpr('a = VALUES(a)')
27+
->debug()
28+
->asString();
29+
30+
$this->assertEquals('', $query);
31+
}
1532
}

0 commit comments

Comments
 (0)