PluginProbe
CSS & JavaScript Toolbox / 9.0
CSS & JavaScript Toolbox v9.0
trunk 0.3 0.8 10 10.1 11 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.9.1 12 12.0 12.0.1 12.0.3 12.0.4 12.0.5 12.0.6 12.0.7 6.0 6.0.11 All 60 releases
css-javascript-toolbox / framework / db / mysql / queue-driver.inc.php

queue-driver.inc.php in CSS & JavaScript Toolbox 9.0, at framework/db/mysql/queue-driver.inc.php

363 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 *
4 */
5
6 // Disallow direct access.
7 defined('ABSPATH') or die("Access denied");
8
9 /**
10 *
11 */
12 class CJTMYSQLQueueDriver extends CJTHookableClass {
13
14 /**
15 * put your comment there...
16 *
17 * @var mixed
18 */
19 protected $queue = array();
20
21 /**
22 * put your comment there...
23 *
24 * @var mixed
25 */
26 protected $onexec = array('parameters' => array('param'));
27
28 /**
29 * put your comment there...
30 *
31 * @var mixed
32 */
33 protected $onprocessqueue = array('parameters' => array('queue'));
34
35 /**
36 * put your comment there...
37 *
38 * @var mixed
39 */
40 protected $onprocesscommand = array('parameters' => array('command'));
41
42 /**
43 * put your comment there...
44 *
45 * @var mixed
46 */
47 protected $onqueue = array('parameters' => array('query'));
48
49 /**
50 * put your comment there...
51 *
52 * @var mixed
53 */
54 protected $onqueuereturn = array('parameters' => array('driver'));
55
56 /**
57 * put your comment there...
58 *
59 * @var mixed
60 */
61 protected $onselect = array('parameters' => array('param'));
62
63 /**
64 * put your comment there...
65 *
66 * @var mixed
67 */
68 private $wpdb = null;
69
70 /**
71 * put your comment there...
72 *
73 */
74 public function __construct($mysqlDriver) {
75 // Hookable!
76 parent::__construct();
77 // Internal DB engine!
78 $this->wpdb = $mysqlDriver;
79 }
80
81 /**
82 * put your comment there...
83 *
84 * @param mixed $query
85 */
86 protected function addQueue($query) {
87 $query = $this->resolveTableName($query);
88 $key = md5($query);
89 if ($query = $this->onqueue($query)) {
90 $this->queue[$key] = $query;
91 }
92 return $this->onqueuereturn($this);
93 }
94
95 /**
96 * put your comment there...
97 *
98 */
99 public function clear() {
100 $this->queue = array();
101 }
102
103 /**
104 * Put your comments here...
105 *
106 *
107 * @return
108 */
109 public function commit() {
110 $this->addQueue('COMMIT;');
111 return $this;
112 }
113
114 /**
115 * put your comment there...
116 *
117 * @param mixed $query
118 */
119 public function delete($query) {
120 return $this->addQueue($query);
121 }
122
123 /**
124 * put your comment there...
125 *
126 * @param string $data
127 * @param mixed $field
128 * @return string
129 */
130 public function escapeValue($data, $field) {
131 # MYSQLI doesn't has numeric field.
132 if ($this->wpdb->use_mysqli) {
133 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
134 }
135 # Check if numeric
136 switch ($field->numeric) {
137 case 0:
138 $data = esc_sql($data);
139 $data = "'{$data}'";
140 break;
141 case 1:
142 $data = (int) $data;
143 break;
144 }
145 return $data;
146 }
147
148 /**
149 * put your comment there...
150 *
151 * @param mixed $query
152 */
153 public function exec($query) {
154 // Initialize!
155 $query = $this->resolveTableName($query);
156 $resultSet = array();
157 // Filtering!
158 extract($this->onexec(compact('query', 'resultSet')));
159 // filter can controller the returned value or customize the query!
160 if ($query && empty($result)) {
161 $result = $this->wpdb->query($query);
162 }
163 return $result;
164 }
165
166 /**
167 * put your comment there...
168 *
169 * @param mixed $table
170 */
171 public function getColumns($table) {
172 $columns = array();
173 $this->select("SELECT * FROM {$table} WHERE 1!=1 LIMIT 0,1;");
174 // Use field name as element key.
175 foreach ($this->wpdb->col_info as $index => $column) {
176 $columns[$column->name] = $column;
177 }
178 return $columns;
179 }
180
181 /**
182 * put your comment there...
183 *
184 */
185 public function getInsertId () {
186 return $this->wpdb->insert_id;
187 }
188
189 /**
190 * put your comment there...
191 *
192 * @param mixed $query
193 * @param mixed $returnType
194 * @param mixed $default
195 */
196 public function getRow($query, $returnType = OBJECT_K, $default = null) {
197 $row = null;
198 // Fetch al result set!
199 $result = $this->select($query, $returnType);
200 if ($result) {
201 $row = reset($result);
202 }
203 return $row;
204 }
205 /**
206 * put your comment there...
207 *
208 */
209 public function getTableName($table) {
210 return "{$this->wpdb->prefix}{$table}";
211 }
212
213 /**
214 * put your comment there...
215 *
216 */
217 public function getVar($query, $row = 0, $column = 0) {
218 return $this->wpdb->get_var($query, $row, $column);
219 }
220
221 /**
222 * put your comment there...
223 *
224 * @param mixed $query
225 */
226 public function insert($query) {
227 return $this->addQueue($query);
228 }
229
230 /**
231 * Put your comments here...
232 *
233 *
234 * @return
235 */
236 public function merge($driver) {
237 // Put target driver queue at the end of our queue.
238 $this->queue = array_merge($this->queue, $driver->queue);
239 }
240
241 /**
242 * put your comment there...
243 *
244 * @param mixed $parameters
245 * @param mixed $operators
246 * @param mixed $defaultOperator
247 * @param mixed $excludeNulls
248 */
249 public function prepareQueryParameters($parameters, $operators = array(), $defaultOperator = '=', $excludeNulls = true) {
250 $prepared = array();
251 // For every parameter esacape name value.
252 foreach ($parameters as $name => $value) {
253 if (!$excludeNulls || ($value !== null)) {
254 if (array_key_exists($name, $this->fields) === FALSE) {
255 throw new Exception("Field:{$name} is not found!");
256 }
257 else {
258 $field = $this->fields[$name];
259 // Escape field name and value.
260 $value = $this->escapeValue($value, $field);
261 // Get name-value operator.
262 $operator = isset($operators[$name]) ? $operators[$name] : $defaultOperator;
263 $prepared[] = "`{$name}`{$operator}{$value}";
264 }
265 }
266 }
267 return $prepared;
268 }
269
270 /**
271 * put your comment there...
272 *
273 */
274 public function processQueue() {
275 $queue = $this->onprocessqueue($this->queue);
276 // Collect queue commands.
277 foreach ($queue as $command) {
278 $this->wpdb->query($this->onprocesscommand($command));
279 }
280 // Clear queue.
281 $this->clear();
282 // Chain.
283 return $this;
284 }
285
286 /**
287 * put your comment there...
288 *
289 * @param mixed $query
290 */
291 public function resolveTableName($query) {
292 // Define list of Prefixes to be replaced with Wordpress table prefix!
293 $keywords = array('#__wordpress_' => '', '#__cjtoolbox_' => 'cjtoolbox_');
294 // Replace each keyword with Wordpress table prefix
295 // + if there is any prefix defined for the keyword itself!
296 foreach ($keywords as $search => $prefix) {
297 $query = str_replace($search, "{$this->wpdb->prefix}{$prefix}", $query);
298 }
299 // Return new query with table names resolved.
300 return $query;
301 }
302
303 /**
304 * Put your comments here...
305 *
306 *
307 * @return
308 */
309 public function rollback() {
310 $this->addQueue('ROLLBACK;');
311 return $this;
312 }
313
314 /**
315 * put your comment there...
316 *
317 */
318 public function row($query) {
319 return $this->wpdb->get_row($query);
320 }
321
322 /**
323 * put your comment there...
324 *
325 * @param mixed $query
326 */
327 public function select($query, $returnType = OBJECT_K) {
328 // Initialize!
329 $query = $this->resolveTableName($query);
330 $resultSet = array();
331 // Filtering!
332 extract($this->onselect(compact('query', 'resultSet')));
333 // filter can controller the returned value or customize the query!
334 if ($query && empty($resultSet)) {
335 $resultSet = $this->wpdb->get_results($query, $returnType);
336 }
337 return $resultSet;
338 }
339
340 /**
341 * Put your comments here...
342 *
343 *
344 * @return
345 */
346 public function startTransaction() {
347 $this->addQueue('BEGIN WORK;');
348 return $this;
349 }
350
351 /**
352 * put your comment there...
353 *
354 * @param mixed $query
355 */
356 public function update($query) {
357 return $this->addQueue($query);
358 }
359
360 } // End class.
361
362 // Hooking!
363 CJTMYSQLQueueDriver::define('CJTMYSQLQueueDriver', array('hookType' => CJTWordpressEvents::HOOK_FILTER));