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

342 lines 6.6 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 switch ($field->numeric) {
132 case 0:
133 $data = mysql_real_escape_string($data, $this->wpdb->dbh);
134 $data = "'{$data}'";
135 break;
136 case 1:
137 $data = (int) $data;
138 break;
139 }
140 return $data;
141 }
142
143 /**
144 * put your comment there...
145 *
146 * @param mixed $query
147 */
148 public function exec($query) {
149 // Initialize!
150 $query = $this->resolveTableName($query);
151 $resultSet = array();
152 // Filtering!
153 extract($this->onexec(compact('query', 'resultSet')));
154 // filter can controller the returned value or customize the query!
155 if ($query && empty($result)) {
156 $result = $this->wpdb->query($query);
157 }
158 return $result;
159 }
160
161 /**
162 * put your comment there...
163 *
164 * @param mixed $table
165 */
166 public function getColumns($table) {
167 $columns = array();
168 $this->select("SELECT * FROM {$table} WHERE 1!=1;");
169 // Use field name as element key.
170 foreach ($this->wpdb->col_info as $index => $column) {
171 $columns[$column->name] = $column;
172 }
173 return $columns;
174 }
175
176 /**
177 * put your comment there...
178 *
179 */
180 public function getInsertId () {
181 return $this->wpdb->insert_id;
182 }
183
184 /**
185 * put your comment there...
186 *
187 */
188 public function getTableName($table) {
189 return "{$this->wpdb->prefix}{$table}";
190 }
191
192 /**
193 * put your comment there...
194 *
195 */
196 public function getVar($query, $row = 0, $column = 0) {
197 return $this->wpdb->get_var($query, $row, $column);
198 }
199
200 /**
201 * put your comment there...
202 *
203 * @param mixed $query
204 */
205 public function insert($query) {
206 return $this->addQueue($query);
207 }
208
209 /**
210 * Put your comments here...
211 *
212 *
213 * @return
214 */
215 public function merge($driver) {
216 // Put target driver queue at the end of our queue.
217 $this->queue = array_merge($this->queue, $driver->queue);
218 }
219
220 /**
221 * put your comment there...
222 *
223 * @param mixed $parameters
224 * @param mixed $operators
225 * @param mixed $defaultOperator
226 * @param mixed $excludeNulls
227 */
228 public function prepareQueryParameters($parameters, $operators = array(), $defaultOperator = '=', $excludeNulls = true) {
229 $prepared = array();
230 // For every parameter esacape name value.
231 foreach ($parameters as $name => $value) {
232 if (!$excludeNulls || ($value !== null)) {
233 if (array_key_exists($name, $this->fields) === FALSE) {
234 throw new Exception("Field:{$name} is not found!");
235 }
236 else {
237 $field = $this->fields[$name];
238 // Escape field name and value.
239 $value = $this->escapeValue($value, $field);
240 // Get name-value operator.
241 $operator = isset($operators[$name]) ? $operators[$name] : $defaultOperator;
242 $prepared[] = "`{$name}`{$operator}{$value}";
243 }
244 }
245 }
246 return $prepared;
247 }
248
249 /**
250 * put your comment there...
251 *
252 */
253 public function processQueue() {
254 $queue = $this->onprocessqueue($this->queue);
255 // Collect queue commands.
256 foreach ($queue as $command) {
257 $this->wpdb->query($this->onprocesscommand($command));
258 }
259 // Clear queue.
260 $this->clear();
261 // Chain.
262 return $this;
263 }
264
265 /**
266 * put your comment there...
267 *
268 * @param mixed $query
269 */
270 public function resolveTableName($query) {
271 // Define list of Prefixes to be replaced with Wordpress table prefix!
272 $keywords = array('#__wordpress_' => '', '#__cjtoolbox_' => 'cjtoolbox_');
273 // Replace each keyword with Wordpress table prefix
274 // + if there is any prefix defined for the keyword itself!
275 foreach ($keywords as $search => $prefix) {
276 $query = str_replace($search, "{$this->wpdb->prefix}{$prefix}", $query);
277 }
278 // Return new query with table names resolved.
279 return $query;
280 }
281
282 /**
283 * Put your comments here...
284 *
285 *
286 * @return
287 */
288 public function rollback() {
289 $this->addQueue('ROLLBACK;');
290 return $this;
291 }
292
293 /**
294 * put your comment there...
295 *
296 */
297 public function row($query) {
298 return $this->wpdb->get_row($query);
299 }
300
301 /**
302 * put your comment there...
303 *
304 * @param mixed $query
305 */
306 public function select($query, $returnType = OBJECT_K) {
307 // Initialize!
308 $query = $this->resolveTableName($query);
309 $resultSet = array();
310 // Filtering!
311 extract($this->onselect(compact('query', 'resultSet')));
312 // filter can controller the returned value or customize the query!
313 if ($query && empty($resultSet)) {
314 $resultSet = $this->wpdb->get_results($query, $returnType);
315 }
316 return $resultSet;
317 }
318
319 /**
320 * Put your comments here...
321 *
322 *
323 * @return
324 */
325 public function startTransaction() {
326 $this->addQueue('BEGIN WORK;');
327 return $this;
328 }
329
330 /**
331 * put your comment there...
332 *
333 * @param mixed $query
334 */
335 public function update($query) {
336 return $this->addQueue($query);
337 }
338
339 } // End class.
340
341 // Hooking!
342 CJTMYSQLQueueDriver::define('CJTMYSQLQueueDriver', array('hookType' => CJTWordpressEvents::HOOK_FILTER));