PluginProbe
FluentSnippets – High-Performance Code Snippets, Header & Footer Code, Custom CSS & PHP Code Manager / 1.2.1
FluentSnippets – High-Performance Code Snippets, Header & Footer Code, Custom CSS & PHP Code Manager v1.2.1
10.56 1.2.1 10 10.1 10.2 10.3 10.31 10.32 10.33 10.34 10.50 10.51 10.52 10.53 10.55 9.0 9.0.1 9.4 trunk 1.0.0 1.1 1.2
easy-code-manager / framework / db / mysql / queue-driver.inc.php

queue-driver.inc.php in FluentSnippets – High-Performance Code Snippets, Header & Footer Code, Custom CSS & PHP Code Manager 1.2.1, at framework/db/mysql/queue-driver.inc.php

417 lines 8.2 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 {
111 $this->addQueue('COMMIT;');
112 return $this;
113 }
114
115 /**
116 * put your comment there...
117 *
118 * @param mixed $createStmt
119 * @param mixed $tableName
120 */
121 public function createTable($createStmt, & $tableName)
122 {
123
124 $result = $this->exec($createStmt, $createStmt);
125
126 // Check table existance
127 preg_match('/CREATE\s+TABLE\s+(IF\s+NOT\s+EXISTS\s+)?\`([a-zA-Z0-9_-]+)\`/i', $createStmt, $createTable);
128
129 $tableName = $createTable[2];
130
131 return $result;
132 }
133
134 /**
135 * put your comment there...
136 *
137 * @param mixed $query
138 */
139 public function delete($query) {
140 return $this->addQueue($query);
141 }
142
143 /**
144 * put your comment there...
145 *
146 * @param string $data
147 * @param mixed $field
148 * @return string
149 */
150 public function escapeValue($data, $field) {
151 # MYSQLI doesn't has numeric field.
152 if ($this->wpdb->use_mysqli) {
153 $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
154 }
155 # Check if numeric
156 switch ($field->numeric) {
157 case 0:
158 $data = esc_sql($data);
159 $data = "'{$data}'";
160 break;
161 case 1:
162 $data = (int) $data;
163 break;
164 }
165 return $data;
166 }
167
168 /**
169 * put your comment there...
170 *
171 * @param mixed $query
172 */
173 public function exec($query, & $rQuery = null)
174 {
175
176 // Initialize!
177 $query = $this->resolveTableName($query);
178
179 // Return copy of resolved query
180 $rQuery = $query;
181
182 $resultSet = array();
183
184 // Filtering!
185 extract($this->onexec(compact('query', 'resultSet')));
186
187 // filter can controller the returned value or customize the query!
188 if ($query && empty($resultSet))
189 {
190 $resultSet = $this->wpdb->query($query);
191 }
192
193 return $resultSet;
194 }
195
196 /**
197 * put your comment there...
198 *
199 * @param mixed $table
200 */
201 public function getColumns($table) {
202 $columns = array();
203 $this->select("SELECT * FROM {$table} WHERE 1!=1 LIMIT 0,1;");
204 // Use field name as element key.
205 foreach ($this->wpdb->col_info as $index => $column) {
206 $columns[$column->name] = $column;
207 }
208 return $columns;
209 }
210
211 /**
212 * put your comment there...
213 *
214 */
215 public function getInsertId () {
216 return $this->wpdb->insert_id;
217 }
218
219 /**
220 * put your comment there...
221 *
222 * @param mixed $query
223 * @param mixed $returnType
224 * @param mixed $default
225 */
226 public function getRow($query, $returnType = OBJECT_K, $default = null) {
227 $row = null;
228 // Fetch al result set!
229 $result = $this->select($query, $returnType);
230 if ($result) {
231 $row = reset($result);
232 }
233 return $row;
234 }
235 /**
236 * put your comment there...
237 *
238 */
239 public function getTableName($table) {
240 return "{$this->wpdb->prefix}{$table}";
241 }
242
243 /**
244 * put your comment there...
245 *
246 */
247 public function getVar($query, $row = 0, $column = 0) {
248 return $this->wpdb->get_var($query, $row, $column);
249 }
250
251 /**
252 * put your comment there...
253 *
254 * @param mixed $query
255 */
256 public function insert($query) {
257 return $this->addQueue($query);
258 }
259
260 /**
261 * Put your comments here...
262 *
263 *
264 * @return
265 */
266 public function merge($driver) {
267 // Put target driver queue at the end of our queue.
268 $this->queue = array_merge($this->queue, $driver->queue);
269 }
270
271 /**
272 * put your comment there...
273 *
274 * @param mixed $parameters
275 * @param mixed $operators
276 * @param mixed $defaultOperator
277 * @param mixed $excludeNulls
278 */
279 public function prepareQueryParameters($parameters, $operators = array(), $defaultOperator = '=', $excludeNulls = true) {
280 $prepared = array();
281 // For every parameter esacape name value.
282 foreach ($parameters as $name => $value) {
283 if (!$excludeNulls || ($value !== null)) {
284 if (array_key_exists($name, $this->fields) === FALSE) {
285 throw new Exception("Field:{$name} is not found!");
286 }
287 else {
288 $field = $this->fields[$name];
289 // Escape field name and value.
290 $value = $this->escapeValue($value, $field);
291 // Get name-value operator.
292 $operator = isset($operators[$name]) ? $operators[$name] : $defaultOperator;
293 $prepared[] = "`{$name}`{$operator}{$value}";
294 }
295 }
296 }
297 return $prepared;
298 }
299
300 /**
301 * put your comment there...
302 *
303 */
304 public function processQueue() {
305 $queue = $this->onprocessqueue($this->queue);
306 // Collect queue commands.
307 foreach ($queue as $command) {
308 $this->wpdb->query($this->onprocesscommand($command));
309 }
310 // Clear queue.
311 $this->clear();
312 // Chain.
313 return $this;
314 }
315
316 /**
317 * put your comment there...
318 *
319 * @param mixed $query
320 */
321 public function resolveTableName($query) {
322 // Define list of Prefixes to be replaced with Wordpress table prefix!
323 $keywords = array('#__wordpress_' => '', '#__cjtoolbox_' => 'cjtoolbox_');
324 // Replace each keyword with Wordpress table prefix
325 // + if there is any prefix defined for the keyword itself!
326 foreach ($keywords as $search => $prefix) {
327 $query = str_replace($search, "{$this->wpdb->prefix}{$prefix}", $query);
328 }
329 // Return new query with table names resolved.
330 return $query;
331 }
332
333 /**
334 * Put your comments here...
335 *
336 *
337 * @return
338 */
339 public function rollback() {
340 $this->addQueue('ROLLBACK;');
341 return $this;
342 }
343
344 /**
345 * put your comment there...
346 *
347 */
348 public function row($query) {
349 return $this->wpdb->get_row($query);
350 }
351
352 /**
353 * put your comment there...
354 *
355 * @param mixed $query
356 */
357 public function select($query, $returnType = OBJECT_K) {
358 // Initialize!
359 $query = $this->resolveTableName($query);
360 $resultSet = array();
361 // Filtering!
362 extract($this->onselect(compact('query', 'resultSet')));
363 // filter can controller the returned value or customize the query!
364 if ($query && empty($resultSet)) {
365 $resultSet = $this->wpdb->get_results($query, $returnType);
366 }
367 return $resultSet;
368 }
369
370 /**
371 * put your comment there...
372 *
373 */
374 public function showTables()
375 {
376
377 $result = $this->wpdb->get_results('SHOW TABLES;', ARRAY_N);
378
379 if (!$result)
380 {
381 throw new Exception('Unexpected Error ... Could not read database tables!!!');
382 }
383
384 $tables = array();
385
386 foreach ($result as $row)
387 {
388 $tables[] = $row[0];
389 }
390
391 return $tables;
392 }
393
394 /**
395 * Put your comments here...
396 *
397 *
398 * @return
399 */
400 public function startTransaction() {
401 $this->addQueue('BEGIN WORK;');
402 return $this;
403 }
404
405 /**
406 * put your comment there...
407 *
408 * @param mixed $query
409 */
410 public function update($query) {
411 return $this->addQueue($query);
412 }
413
414 } // End class.
415
416 // Hooking!
417 CJTMYSQLQueueDriver::define('CJTMYSQLQueueDriver', array('hookType' => CJTWordpressEvents::HOOK_FILTER));