PluginProbe
CSS & JavaScript Toolbox / 11
CSS & JavaScript Toolbox v11
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 11, at framework/db/mysql/queue-driver.inc.php

372 lines 7.3 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 if ( strpos( $query, 'SHOW TABLES' ) !== false && ! $result ) {
164 echo 'Some tables could not be created! Please contact support with the error';
165 echo '<br><strong>' . str_replace( 'SHOW TABLES LIKE ', '', $query ) . '</strong>';
166
167 wp_die();
168 }
169 }
170
171
172 return $result;
173 }
174
175 /**
176 * put your comment there...
177 *
178 * @param mixed $table
179 */
180 public function getColumns($table) {
181 $columns = array();
182 $this->select("SELECT * FROM {$table} WHERE 1!=1 LIMIT 0,1;");
183 // Use field name as element key.
184 foreach ($this->wpdb->col_info as $index => $column) {
185 $columns[$column->name] = $column;
186 }
187 return $columns;
188 }
189
190 /**
191 * put your comment there...
192 *
193 */
194 public function getInsertId () {
195 return $this->wpdb->insert_id;
196 }
197
198 /**
199 * put your comment there...
200 *
201 * @param mixed $query
202 * @param mixed $returnType
203 * @param mixed $default
204 */
205 public function getRow($query, $returnType = OBJECT_K, $default = null) {
206 $row = null;
207 // Fetch al result set!
208 $result = $this->select($query, $returnType);
209 if ($result) {
210 $row = reset($result);
211 }
212 return $row;
213 }
214 /**
215 * put your comment there...
216 *
217 */
218 public function getTableName($table) {
219 return "{$this->wpdb->prefix}{$table}";
220 }
221
222 /**
223 * put your comment there...
224 *
225 */
226 public function getVar($query, $row = 0, $column = 0) {
227 return $this->wpdb->get_var($query, $row, $column);
228 }
229
230 /**
231 * put your comment there...
232 *
233 * @param mixed $query
234 */
235 public function insert($query) {
236 return $this->addQueue($query);
237 }
238
239 /**
240 * Put your comments here...
241 *
242 *
243 * @return
244 */
245 public function merge($driver) {
246 // Put target driver queue at the end of our queue.
247 $this->queue = array_merge($this->queue, $driver->queue);
248 }
249
250 /**
251 * put your comment there...
252 *
253 * @param mixed $parameters
254 * @param mixed $operators
255 * @param mixed $defaultOperator
256 * @param mixed $excludeNulls
257 */
258 public function prepareQueryParameters($parameters, $operators = array(), $defaultOperator = '=', $excludeNulls = true) {
259 $prepared = array();
260 // For every parameter esacape name value.
261 foreach ($parameters as $name => $value) {
262 if (!$excludeNulls || ($value !== null)) {
263 if (array_key_exists($name, $this->fields) === FALSE) {
264 throw new Exception("Field:{$name} is not found!");
265 }
266 else {
267 $field = $this->fields[$name];
268 // Escape field name and value.
269 $value = $this->escapeValue($value, $field);
270 // Get name-value operator.
271 $operator = isset($operators[$name]) ? $operators[$name] : $defaultOperator;
272 $prepared[] = "`{$name}`{$operator}{$value}";
273 }
274 }
275 }
276 return $prepared;
277 }
278
279 /**
280 * put your comment there...
281 *
282 */
283 public function processQueue() {
284 $queue = $this->onprocessqueue($this->queue);
285 // Collect queue commands.
286 foreach ($queue as $command) {
287 $this->wpdb->query($this->onprocesscommand($command));
288 }
289 // Clear queue.
290 $this->clear();
291 // Chain.
292 return $this;
293 }
294
295 /**
296 * put your comment there...
297 *
298 * @param mixed $query
299 */
300 public function resolveTableName($query) {
301 // Define list of Prefixes to be replaced with Wordpress table prefix!
302 $keywords = array('#__wordpress_' => '', '#__cjtoolbox_' => 'cjtoolbox_');
303 // Replace each keyword with Wordpress table prefix
304 // + if there is any prefix defined for the keyword itself!
305 foreach ($keywords as $search => $prefix) {
306 $query = str_replace($search, "{$this->wpdb->prefix}{$prefix}", $query);
307 }
308 // Return new query with table names resolved.
309 return $query;
310 }
311
312 /**
313 * Put your comments here...
314 *
315 *
316 * @return
317 */
318 public function rollback() {
319 $this->addQueue('ROLLBACK;');
320 return $this;
321 }
322
323 /**
324 * put your comment there...
325 *
326 */
327 public function row($query) {
328 return $this->wpdb->get_row($query);
329 }
330
331 /**
332 * put your comment there...
333 *
334 * @param mixed $query
335 */
336 public function select($query, $returnType = OBJECT_K) {
337 // Initialize!
338 $query = $this->resolveTableName($query);
339 $resultSet = array();
340 // Filtering!
341 extract($this->onselect(compact('query', 'resultSet')));
342 // filter can controller the returned value or customize the query!
343 if ($query && empty($resultSet)) {
344 $resultSet = $this->wpdb->get_results($query, $returnType);
345 }
346 return $resultSet;
347 }
348
349 /**
350 * Put your comments here...
351 *
352 *
353 * @return
354 */
355 public function startTransaction() {
356 $this->addQueue('BEGIN WORK;');
357 return $this;
358 }
359
360 /**
361 * put your comment there...
362 *
363 * @param mixed $query
364 */
365 public function update($query) {
366 return $this->addQueue($query);
367 }
368
369 } // End class.
370
371 // Hooking!
372 CJTMYSQLQueueDriver::define('CJTMYSQLQueueDriver', array('hookType' => CJTWordpressEvents::HOOK_FILTER));