Passed
Push — master ( f461b3...0c0438 )
by Jean-Christophe
10:20
created

BulkInserts::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 4
c 3
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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 1
	public function __construct($className) {
16 1
		parent::__construct ( $className );
17 1
		if (($key = \array_search ( $this->pkName, $this->fields )) !== false) {
18 1
			unset ( $this->fields [$key] );
19
		}
20 1
		$this->insertFields = \implode ( ',', $this->getQuotedKeys ( $this->fields, $this->db->quote ) );
21 1
	}
22
23 3
	public function addInstance($instance, $id = null) {
24 3
		$this->updateInstanceRest ( $instance );
25 3
		unset ( $instance->_rest [$this->pkName] );
26 3
		$this->instances [] = $instance;
27 3
	}
28
29 2
	public function createSQL() {
30 2
		$quote = $this->db->quote;
31 2
		$fieldCount = \count ( $this->fields );
32 2
		$parameters = [ ];
33 2
		$values = [ ];
34 2
		$modelFields = '(' . \implode ( ',', \array_fill ( 0, $fieldCount, '?' ) ) . ')';
35 2
		foreach ( $this->instances as $instance ) {
36 2
			$parameters = \array_merge ( $parameters, \array_values ( $instance->_rest ) );
37 2
			$values [] = $modelFields;
38
		}
39 2
		$this->parameters = $parameters;
40 2
		return "INSERT INTO {$quote}{$this->tableName}{$quote} (" . $this->insertFields . ') VALUES ' . \implode ( ',', $values );
41
	}
42
43 1
	public function groupOp($count = 5) {
44 1
		$quote = $this->db->quote;
45 1
		$groups = \array_chunk ( $this->instances, $count );
46
47 1
		$insertTable = "INSERT INTO {$quote}{$this->tableName}{$quote} (" . $this->insertFields . ') VALUES(';
48 1
		foreach ( $groups as $group ) {
49 1
			$sql = '';
50 1
			foreach ( $group as $instance ) {
51 1
				$sql .= $insertTable . $this->db->getInsertValues ( $instance->_rest ) . ');';
52
			}
53 1
			$this->execGroupTrans ( $sql );
54
		}
55 1
		$this->instances = [ ];
56 1
		$this->parameters = [ ];
57 1
	}
58
}
59
60