Passed
Push — master ( 48f23f...3d04cc )
by Jean-Christophe
07:46 queued 13s
created

BulkUpdates::createSQL()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 10
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
namespace Ubiquity\orm\bulk;
4
5
use Ubiquity\orm\OrmUtils;
6
7
/**
8
 * Ubiquity\orm\bulk$BulkUpdates
9
 * This class is part of Ubiquity
10
 *
11
 * @author jcheron <[email protected]>
12
 * @version 1.0.0
13
 *
14
 */
15
class BulkUpdates extends AbstractBulks {
16
	private $sql;
0 ignored issues
show
introduced by
The private property $sql is not used, and could be removed.
Loading history...
17
18
	public function __construct($className) {
19
		parent::__construct ( $className );
20
		$this->insertFields = \implode ( ',', $this->getQuotedKeys ( $this->fields, $this->db->quote ) );
21
	}
22
23
	public function addInstance($instance, $id = null) {
24
		$id = $id ?? OrmUtils::getFirstKeyValue ( $instance );
25
		$this->updateInstanceRest ( $instance );
26
		$this->instances [$id] = $instance;
27
	}
28
29
	public function createSQL() {
30
		switch ($this->dbType) {
31
			case 'mysql' :
32
				return $this->mysqlCreate ();
33
			case 'pgsql' :
34
				return $this->pgCreate ();
35
			default :
36
				throw new \RuntimeException ( $this->dbType . ' does not support bulk updates!' );
37
		}
38
	}
39
40
	private function pgCreate() {
41
		$quote = $this->db->quote;
42
		$tableName = OrmUtils::getTableName ( $this->class );
43
44
		$count = \count ( $this->instances );
45
		$modelField = implode ( '', \array_fill ( 0, $count, ' WHEN ? THEN ? ' ) );
46
47
		$keys = \array_keys ( $this->instances );
48
		$parameters = [ ];
49
		$_rest = [ ];
50
		foreach ( $this->instances as $k => $instance ) {
51
			$_rest [$k] = $instance->_rest;
52
		}
53
54
		$caseFields = [ ];
55
		$pk = $this->pkName;
56
		foreach ( $this->fields as $field ) {
57
			$caseFields [] = "{$quote}{$field}{$quote} = (CASE {$quote}{$pk}{$quote} {$modelField} ELSE {$quote}{$field}{$quote} END)";
58
			foreach ( $_rest as $pkv => $_restInstance ) {
59
				$parameters [] = $pkv;
60
				$parameters [] = $_restInstance [$field];
61
			}
62
		}
63
		$parameters = \array_merge ( $parameters, $keys );
64
		$this->parameters = $parameters;
65
		return "UPDATE {$quote}{$tableName}{$quote} SET " . \implode ( ',', $caseFields ) . " WHERE {$quote}{$pk}{$quote} IN (" . \implode ( ',', \array_fill ( 0, $count, '?' ) ) . ')';
66
	}
67
68
	private function mysqlCreate() {
69
		$quote = $this->db->quote;
70
		$tableName = OrmUtils::getTableName ( $this->class );
71
		$fieldCount = \count ( $this->fields );
72
		$parameters = [ ];
73
		$values = [ ];
74
		$modelFields = '(' . \implode ( ',', \array_fill ( 0, $fieldCount, '?' ) ) . ')';
75
		foreach ( $this->instances as $instance ) {
76
			$parameters = \array_merge ( $parameters, \array_values ( $instance->_rest ) );
77
			$values [] = $modelFields;
78
		}
79
		$duplicateKey = [ ];
80
		foreach ( $this->fields as $field ) {
81
			$duplicateKey [] = "{$quote}{$field}{$quote} = VALUES({$quote}{$field}{$quote})";
82
		}
83
		$this->parameters = $parameters;
84
		return "INSERT INTO {$quote}{$tableName}{$quote} (" . $this->insertFields . ') VALUES ' . \implode ( ',', $values ) . ' ON DUPLICATE KEY UPDATE ' . \implode ( ',', $duplicateKey );
85
	}
86
}
87
88