Test Failed
Push — master ( ccf43a...ee4cc3 )
by Jean-Christophe
17:42
created

BulkInserts::groupOp()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 0
cp 0
rs 9.9332
cc 3
nc 3
nop 1
crap 12
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