|
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 $db; |
|
23
|
|
|
protected $instances = [ ]; |
|
24
|
|
|
protected $parameters; |
|
25
|
|
|
protected $dbType; |
|
26
|
|
|
protected $insertFields; |
|
27
|
|
|
|
|
28
|
|
|
protected function getQuotedKeys($fields, $quote) { |
|
29
|
|
|
$ret = array (); |
|
30
|
|
|
foreach ( $fields as $field ) { |
|
31
|
|
|
$ret [] = $quote . $field . $quote; |
|
32
|
|
|
} |
|
33
|
|
|
return $ret; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function updateInstanceRest($instance) { |
|
37
|
|
|
foreach ( $this->fields as $field ) { |
|
38
|
|
|
$accessor = "get" . \ucfirst ( $field ); |
|
39
|
|
|
$instance->_rest [$field] = $instance->$accessor (); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function __construct($className) { |
|
44
|
|
|
$this->class = $className; |
|
45
|
|
|
$this->pkName = OrmUtils::getFirstKey ( $className ); |
|
46
|
|
|
$this->fields = OrmUtils::getSerializableFields ( $className ); |
|
47
|
|
|
$this->db = DAO::getDb ( $className ); |
|
48
|
|
|
$this->dbType = $this->db->getDbType (); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function addInstances($instances) { |
|
52
|
|
|
foreach ( $instances as $instance ) { |
|
53
|
|
|
$this->addInstance ( $instance ); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public abstract function addInstance($instance); |
|
58
|
|
|
|
|
59
|
|
|
public abstract function createSQL(); |
|
60
|
|
|
|
|
61
|
|
|
public function flush() { |
|
62
|
|
|
$statement = $this->db->getUpdateStatement ( $this->createSQL () ); |
|
63
|
|
|
while ( true ) { |
|
64
|
|
|
try { |
|
65
|
|
|
$result = $statement->execute ( $this->parameters ); |
|
66
|
|
|
if ($result !== false) { |
|
67
|
|
|
$this->instances = [ ]; |
|
68
|
|
|
$this->parameters = [ ]; |
|
69
|
|
|
return $result; |
|
70
|
|
|
} |
|
71
|
|
|
} catch ( \Exception $e ) { |
|
72
|
|
|
Logger::warn ( "DAOBulkUpdates", $e->getMessage (), \get_class ( $this ) ); |
|
73
|
|
|
if ($e->errorInfo [0] == 40001 && $this->db->getDbType () == 'mysql' && $e->errorInfo [1] == 1213) { |
|
74
|
|
|
echo "deadlock"; |
|
75
|
|
|
} else { |
|
76
|
|
|
if (Startup::$config ['debug']) { |
|
77
|
|
|
throw $e; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|