|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\bulk; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Ubiquity\orm\bulk$BulkInserts |
|
7
|
|
|
* This class is part of Ubiquity |
|
8
|
|
|
* |
|
9
|
|
|
* @author jcheron <[email protected]> |
|
10
|
|
|
* @version 1.0.1 |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
class BulkInserts extends AbstractBulks { |
|
14
|
|
|
|
|
15
|
|
|
public function __construct($className) { |
|
16
|
|
|
parent::__construct ( $className ); |
|
17
|
|
|
if (($key = \array_search ( $this->pkName, $this->fields )) !== false) { |
|
18
|
|
|
unset ( $this->fields [$key] ); |
|
19
|
|
|
} |
|
20
|
|
|
$this->insertFields = \implode ( ',', $this->getQuotedKeys ( $this->fields, $this->db->quote ) ); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function addInstance($instance, $id = null) { |
|
24
|
|
|
$this->updateInstanceRest ( $instance ); |
|
25
|
|
|
unset ( $instance->_rest [$this->pkName] ); |
|
26
|
|
|
$this->instances [] = $instance; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function createSQL() { |
|
30
|
|
|
$quote = $this->db->quote; |
|
31
|
|
|
$fieldCount = \count ( $this->fields ); |
|
32
|
|
|
$parameters = [ ]; |
|
33
|
|
|
$values = [ ]; |
|
34
|
|
|
$modelFields = '(' . \implode ( ',', \array_fill ( 0, $fieldCount, '?' ) ) . ')'; |
|
35
|
|
|
foreach ( $this->instances as $instance ) { |
|
36
|
|
|
$parameters = \array_merge ( $parameters, \array_values ( $instance->_rest ) ); |
|
37
|
|
|
$values [] = $modelFields; |
|
38
|
|
|
} |
|
39
|
|
|
$this->parameters = $parameters; |
|
40
|
|
|
return "INSERT INTO {$quote}{$this->tableName}{$quote} (" . $this->insertFields . ') VALUES ' . \implode ( ',', $values ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function groupOp($count = 5) { |
|
44
|
|
|
$quote = $this->db->quote; |
|
45
|
|
|
$groups = \array_chunk ( $this->instances, $count ); |
|
46
|
|
|
|
|
47
|
|
|
$insertTable = "INSERT INTO {$quote}{$this->tableName}{$quote} (" . $this->insertFields . ') VALUES('; |
|
48
|
|
|
foreach ( $groups as $group ) { |
|
49
|
|
|
$sql = ''; |
|
50
|
|
|
foreach ( $group as $instance ) { |
|
51
|
|
|
$sql .= $insertTable . $this->db->getInsertValues ( $instance->_rest ) . ');'; |
|
52
|
|
|
} |
|
53
|
|
|
$this->execGroupTrans ( $sql ); |
|
54
|
|
|
} |
|
55
|
|
|
$this->instances = [ ]; |
|
56
|
|
|
$this->parameters = [ ]; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|