1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Ubiquity\orm\traits$DAOBulkUpdatesTrait |
7
|
|
|
* This class is part of Ubiquity |
8
|
|
|
* |
9
|
|
|
* @author jcheron <[email protected]> |
10
|
|
|
* @version 1.0.4 |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
trait DAOBulkUpdatesTrait { |
14
|
|
|
protected static $bulks = [ 'insert' => [ ],'update' => [ ],'delete' => [ ] ]; |
15
|
|
|
|
16
|
7 |
|
protected static function getBulk($class, $operation = 'update') { |
17
|
7 |
|
if (! isset ( self::$bulks [$operation] [$class] )) { |
18
|
3 |
|
$bulkClass = '\\Ubiquity\\orm\\bulk\\Bulk' . \ucfirst ( $operation ) . 's'; |
19
|
3 |
|
self::$bulks [$operation] [$class] = new $bulkClass ( $class ); |
20
|
|
|
} |
21
|
7 |
|
return self::$bulks [$operation] [$class]; |
22
|
|
|
} |
23
|
|
|
|
24
|
7 |
|
protected static function toOperation($instance, string $operation): void { |
25
|
7 |
|
$class = \get_class ( $instance ); |
26
|
7 |
|
self::getBulk ( $class, $operation )->addInstance ( $instance ); |
27
|
7 |
|
} |
28
|
|
|
|
29
|
4 |
|
protected static function toOperations(array $instances, string $operation): void { |
30
|
4 |
|
$instance = \current ( $instances ); |
31
|
4 |
|
if (isset ( $instance )) { |
32
|
4 |
|
$class = \get_class ( $instance ); |
33
|
4 |
|
self::getBulk ( $class, $operation )->addInstances ( $instances ); |
34
|
|
|
} |
35
|
4 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Adds an instance in the bulk list of objects to insert. |
39
|
|
|
* Call flush to commit the operation |
40
|
|
|
* |
41
|
|
|
* @param object $instance |
42
|
|
|
*/ |
43
|
4 |
|
public static function toInsert($instance): void { |
44
|
4 |
|
self::toOperation ( $instance, 'insert' ); |
45
|
4 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Adds an array of instances in the bulk list of objects to insert. |
49
|
|
|
* Call flush to commit the operation |
50
|
|
|
* |
51
|
|
|
* @param array $instances |
52
|
|
|
*/ |
53
|
2 |
|
public static function toInserts(array $instances): void { |
54
|
2 |
|
self::toOperations ( $instances, 'insert' ); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Executes all waiting insert operations |
59
|
|
|
*/ |
60
|
1 |
|
public static function flushInserts(): void { |
61
|
1 |
|
$bulks = self::$bulks ['insert']; |
62
|
1 |
|
foreach ( $bulks as $bulk ) { |
63
|
1 |
|
$bulk->flush (); |
64
|
|
|
} |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Adds an instance in the bulk list of objects to update. |
69
|
|
|
* Call flush to commit the operation |
70
|
|
|
* |
71
|
|
|
* @param object $instance |
72
|
|
|
*/ |
73
|
5 |
|
public static function toUpdate($instance): void { |
74
|
5 |
|
self::toOperation ( $instance, 'update' ); |
75
|
5 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Adds an array of instances in the bulk list of objects to update. |
79
|
|
|
* Call flush to commit the operation |
80
|
|
|
* |
81
|
|
|
* @param array $instances |
82
|
|
|
*/ |
83
|
2 |
|
public static function toUpdates(array $instances): void { |
84
|
2 |
|
self::toOperations ( $instances, 'update' ); |
85
|
2 |
|
} |
86
|
|
|
|
87
|
2 |
|
public static function updateGroups($count = 5) { |
88
|
2 |
|
$bulks = self::$bulks ['update']; |
89
|
2 |
|
foreach ( $bulks as $bulk ) { |
90
|
2 |
|
$bulk->groupOp ( $count ); |
91
|
|
|
} |
92
|
2 |
|
} |
93
|
|
|
|
94
|
1 |
|
public static function insertGroups($count = 5) { |
95
|
1 |
|
$bulks = self::$bulks ['insert']; |
96
|
1 |
|
foreach ( $bulks as $bulk ) { |
97
|
1 |
|
$bulk->groupOp ( $count ); |
98
|
|
|
} |
99
|
1 |
|
} |
100
|
|
|
|
101
|
1 |
|
public static function deleteGroups($count = 5) { |
102
|
1 |
|
$bulks = self::$bulks ['delete']; |
103
|
1 |
|
foreach ( $bulks as $bulk ) { |
104
|
1 |
|
$bulk->groupOp ( $count ); |
105
|
|
|
} |
106
|
1 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Executes all waiting update operations |
110
|
|
|
*/ |
111
|
1 |
|
public static function flushUpdates(): void { |
112
|
1 |
|
$bulks = self::$bulks ['update']; |
113
|
1 |
|
foreach ( $bulks as $bulk ) { |
114
|
1 |
|
$bulk->flush (); |
115
|
|
|
} |
116
|
1 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Adds an instance in the bulk list of objects to delete. |
120
|
|
|
* Call flush to commit the operation |
121
|
|
|
* |
122
|
|
|
* @param object $instance |
123
|
|
|
*/ |
124
|
2 |
|
public static function toDelete($instance): void { |
125
|
2 |
|
self::toOperation ( $instance, 'delete' ); |
126
|
2 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Adds an array of instances in the bulk list of objects to delete. |
130
|
|
|
* Call flush to commit the operation |
131
|
|
|
* |
132
|
|
|
* @param array $instances |
133
|
|
|
*/ |
134
|
2 |
|
public static function toDeletes(array $instances): void { |
135
|
2 |
|
self::toOperations ( $instances, 'delete' ); |
136
|
2 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Executes all waiting delete operations |
140
|
|
|
*/ |
141
|
1 |
|
public static function flushDeletes(): void { |
142
|
1 |
|
$bulks = self::$bulks ['delete']; |
143
|
1 |
|
foreach ( $bulks as $bulk ) { |
144
|
1 |
|
$bulk->flush (); |
145
|
|
|
} |
146
|
1 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Executes all waiting operations (inserts, updates, deletes) |
150
|
|
|
*/ |
151
|
1 |
|
public static function flush(): void { |
152
|
1 |
|
foreach ( self::$bulks as $bulks ) { |
153
|
1 |
|
foreach ( $bulks as $bulk ) { |
154
|
1 |
|
$bulk->flush (); |
155
|
|
|
} |
156
|
|
|
} |
157
|
1 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Clear bulk and clear instances waiting for operations. |
161
|
|
|
* |
162
|
|
|
* @param array $operations |
163
|
|
|
* @param array $classes |
164
|
|
|
*/ |
165
|
1 |
|
public static function clearBulks(?array $operations = null, ?array $classes = null): void { |
166
|
1 |
|
$operations ??= \array_keys ( self::$bulks ); |
167
|
1 |
|
foreach ( $operations as $op ) { |
168
|
1 |
|
$thisClasses = $classes ?? \array_keys ( self::$bulks [$op] ); |
169
|
1 |
|
foreach ( $thisClasses as $class ) { |
170
|
1 |
|
if (isset ( self::$bulks [$op] [$class] )) { |
171
|
1 |
|
self::$bulks [$op] [$class]->clear (); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
1 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Return the count of instances waiting for flushing in a bulk. |
179
|
|
|
* |
180
|
|
|
* @param string $class |
181
|
|
|
* @param string $operation |
182
|
|
|
* @return int |
183
|
|
|
*/ |
184
|
2 |
|
public static function countInstancesBulk(string $class, string $operation = 'update'): int { |
185
|
2 |
|
$bulk = self::$bulks [$operation] [$class] ?? null; |
186
|
2 |
|
if (isset ( $bulk )) { |
187
|
2 |
|
return $bulk->count (); |
188
|
|
|
} |
189
|
1 |
|
return 0; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|