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.1 |
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
|
3 |
|
protected function getQuotedKeys($fields, $quote) { |
31
|
3 |
|
$ret = array (); |
32
|
3 |
|
foreach ( $fields as $field ) { |
33
|
3 |
|
$ret [] = $quote . $field . $quote; |
34
|
|
|
} |
35
|
3 |
|
return $ret; |
36
|
|
|
} |
37
|
|
|
|
38
|
7 |
|
protected function updateInstanceRest($instance) { |
39
|
7 |
|
foreach ( $this->fields as $field ) { |
40
|
7 |
|
$accessor = 'get' . \ucfirst ( $this->memberNames [$field] ?? $field); |
41
|
7 |
|
$instance->_rest [$field] = $instance->$accessor (); |
42
|
|
|
} |
43
|
7 |
|
} |
44
|
|
|
|
45
|
3 |
|
protected function execGroupTrans($sql) { |
46
|
3 |
|
while ( true ) { |
47
|
|
|
try { |
48
|
3 |
|
$this->db->beginTransaction (); |
49
|
3 |
|
$this->db->execute ( $sql ); |
50
|
3 |
|
$this->db->commit (); |
51
|
3 |
|
return true; |
52
|
|
|
} catch ( \Exception $e ) { |
53
|
|
|
$this->db->rollBack (); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
public function __construct($className) { |
60
|
3 |
|
$this->class = $className; |
61
|
3 |
|
$this->pkName = OrmUtils::getFirstKey ( $className ); |
62
|
3 |
|
$this->fields = OrmUtils::getSerializableFields ( $className ); |
63
|
3 |
|
$this->db = DAO::getDb ( $className ); |
64
|
3 |
|
$this->dbType = $this->db->getDbType (); |
65
|
3 |
|
$this->tableName = OrmUtils::getTableName ( $className ); |
66
|
3 |
|
$this->memberNames = OrmUtils::getAnnotationInfo ( $className, '#memberNames' ); |
67
|
3 |
|
} |
68
|
|
|
|
69
|
4 |
|
public function addInstances($instances) { |
70
|
4 |
|
foreach ( $instances as $instance ) { |
71
|
4 |
|
$this->addInstance ( $instance ); |
72
|
|
|
} |
73
|
4 |
|
} |
74
|
|
|
|
75
|
|
|
public abstract function addInstance($instance); |
76
|
|
|
|
77
|
|
|
public abstract function createSQL(); |
78
|
|
|
|
79
|
3 |
|
public function flush() { |
80
|
3 |
|
if (\count ( $this->instances ) > 0) { |
81
|
3 |
|
$statement = $this->db->getUpdateStatement ( $this->createSQL () ); |
82
|
3 |
|
while ( true ) { |
83
|
|
|
try { |
84
|
3 |
|
$result = $statement->execute ( $this->parameters ); |
85
|
3 |
|
if ($result !== false) { |
86
|
3 |
|
$this->instances = [ ]; |
87
|
3 |
|
$this->parameters = [ ]; |
88
|
3 |
|
return $result; |
89
|
|
|
} |
90
|
|
|
} catch ( \Exception $e ) { |
91
|
|
|
Logger::warn ( "DAOBulkUpdates", $e->getMessage (), \get_class ( $this ) ); |
92
|
|
|
if ($e->errorInfo [0] == 40001 && $this->db->getDbType () == 'mysql' && $e->errorInfo [1] == 1213) { |
93
|
|
|
echo "deadlock"; |
94
|
|
|
} else { |
95
|
|
|
if (Startup::$config ['debug']) { |
96
|
|
|
throw $e; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Remove all instances and parameters from this bulk. |
107
|
|
|
*/ |
108
|
1 |
|
public function clear(): void { |
109
|
1 |
|
$this->instances = [ ]; |
110
|
1 |
|
$this->parameters = [ ]; |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Count all elements in instances to be flush. |
115
|
|
|
* |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
2 |
|
public function count(): int { |
119
|
2 |
|
return \count ( $this->instances ); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|