Passed
Push — master ( b494ba...29c51f )
by Jean-Christophe
11:04
created

AbstractBulks::getQuotedKeys()   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 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Ubiquity\orm\bulk;
4
5
use Ubiquity\orm\OrmUtils;
6
use Ubiquity\orm\DAO;
7
use Ubiquity\log\Logger;
8
use Ubiquity\controllers\Startup;
9
10
/**
11
 * Ubiquity\orm\bulk$AbstractBulks
12
 * This class is part of Ubiquity
13
 *
14
 * @author jcheron <[email protected]>
15
 * @version 1.0.0
16
 *
17
 */
18
abstract class AbstractBulks {
19
	protected $class;
20
	protected $pkName;
21
	protected $fields;
22
	protected $tableName;
23
	protected $db;
24
	protected $instances = [ ];
25
	protected $parameters;
26
	protected $dbType;
27
	protected $insertFields;
28
	protected $memberNames;
29
30 2
	protected function getQuotedKeys($fields, $quote) {
31 2
		$ret = array ();
32 2
		foreach ( $fields as $field ) {
33 2
			$ret [] = $quote . $field . $quote;
34
		}
35 2
		return $ret;
36
	}
37
38 4
	protected function updateInstanceRest($instance) {
39 4
		foreach ( $this->fields as $field ) {
40 4
			$accessor = 'get' . \ucfirst ( $this->memberNames[$field]??$field );
41 4
			$instance->_rest [$field] = $instance->$accessor ();
42
		}
43 4
	}
44
45 1
	protected function execGroupTrans($sql) {
46 1
		while ( true ) {
47
			try {
48 1
				$this->db->beginTransaction ();
49 1
				$this->db->execute ( $sql );
50 1
				$this->db->commit ();
51 1
				return true;
52
			} catch ( \Exception $e ) {
53
				$this->db->rollBack ();
54
			}
55
		}
56
		return false;
57
	}
58
59 2
	public function __construct($className) {
60 2
		$this->class = $className;
61 2
		$this->pkName = OrmUtils::getFirstKey ( $className );
62 2
		$this->fields = OrmUtils::getSerializableFields ( $className );
63 2
		$this->db = DAO::getDb ( $className );
64 2
		$this->dbType = $this->db->getDbType ();
65 2
		$this->tableName = OrmUtils::getTableName ( $className );
66 2
		$this->memberNames=OrmUtils::getAnnotationInfo($className,'#memberNames');
67 2
	}
68
69 3
	public function addInstances($instances) {
70 3
		foreach ( $instances as $instance ) {
71 3
			$this->addInstance ( $instance );
72
		}
73 3
	}
74
75
	public abstract function addInstance($instance);
76
77
	public abstract function createSQL();
78
79 3
	public function flush() {
80 3
		$statement = $this->db->getUpdateStatement ( $this->createSQL () );
81 3
		while ( true ) {
82
			try {
83 3
				$result = $statement->execute ( $this->parameters );
84 3
				if ($result !== false) {
85 3
					$this->instances = [ ];
86 3
					$this->parameters = [ ];
87 3
					return $result;
88
				}
89
			} catch ( \Exception $e ) {
90
				Logger::warn ( "DAOBulkUpdates", $e->getMessage (), \get_class ( $this ) );
91
				if ($e->errorInfo [0] == 40001 && $this->db->getDbType () == 'mysql' && $e->errorInfo [1] == 1213) {
92
					echo "deadlock";
93
				} else {
94
					if (Startup::$config ['debug']) {
95
						throw $e;
96
					}
97
				}
98
			}
99
		}
100
		return false;
101
	}
102
}
103
104