Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
AbstractBulks.php
Go to the documentation of this file.
1<?php
2
4
9
18abstract 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 protected function getQuotedKeys($fields, $quote) {
31 $ret = array ();
32 foreach ( $fields as $field ) {
33 $ret [] = $quote . $field . $quote;
34 }
35 return $ret;
36 }
37
38 protected function updateInstanceRest($instance) {
39 foreach ( $this->fields as $field ) {
40 $accessor = 'get' . \ucfirst ( $this->memberNames [$field] ?? $field);
41 $instance->_rest [$field] = $instance->$accessor ();
42 }
43 }
44
45 protected function execGroupTrans($sql) {
46 while ( true ) {
47 try {
48 $this->db->beginTransaction ();
49 $this->db->execute ( $sql );
50 $this->db->commit ();
51 return true;
52 } catch ( \Exception $e ) {
53 $this->db->rollBack ();
54 }
55 }
56 return false;
57 }
58
59 public function __construct($className) {
60 $this->class = $className;
61 $this->pkName = OrmUtils::getFirstKey ( $className );
62 $this->fields = OrmUtils::getSerializableFields ( $className );
63 $this->db = DAO::getDb ( $className );
64 $this->dbType = $this->db->getDbType ();
65 $this->tableName = OrmUtils::getTableName ( $className );
66 $this->memberNames = OrmUtils::getAnnotationInfo ( $className, '#memberNames' );
67 }
68
69 public function addInstances($instances) {
70 foreach ( $instances as $instance ) {
71 $this->addInstance ( $instance );
72 }
73 }
74
75 public abstract function addInstance($instance);
76
77 public abstract function createSQL();
78
79 public function flush() {
80 if (\count ( $this->instances ) > 0) {
81 $statement = $this->db->getUpdateStatement ( $this->createSQL () );
82 while ( true ) {
83 try {
84 $result = $statement->execute ( $this->parameters );
85 if ($result !== false) {
86 $this->instances = [ ];
87 $this->parameters = [ ];
88 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
108 public function clear(): void {
109 $this->instances = [ ];
110 $this->parameters = [ ];
111 }
112
118 public function count(): int {
119 return \count ( $this->instances );
120 }
121}
122
Starts the framework.
Definition Startup.php:19
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Definition Logger.php:14
Gateway class between database and object model.
Definition DAO.php:33
static getDb($model)
Definition DAO.php:51
Object/relational mapping utilities.
Definition OrmUtils.php:17
static getAnnotationInfo($class, $keyAnnotation)
Definition OrmUtils.php:151
static getTableName($class)
Definition OrmUtils.php:45
Ubiquity\orm\bulk$AbstractBulks This class is part of Ubiquity.
clear()
Remove all instances and parameters from this bulk.
count()
Count all elements in instances to be flush.