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

BulkInserts   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
eloc 28
c 4
b 0
f 0
dl 0
loc 44
ccs 33
cts 33
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A addInstance() 0 4 1
A createSQL() 0 12 2
A groupOp() 0 14 3
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