1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
namespace Khansia\Db;
use Khansia\Db\Storage\Helper;
class Storage {
const DRIVER_MYSQL = 0;
const DRIVER_OCI8 = 1;
protected $_db;
protected $_sql;
protected $_driver;
protected $_helper;
protected $_tables = array();
protected $_now;
protected function _($string, $prefix = '') {
if ($this->_driver == self::DRIVER_OCI8) {
return $prefix . strtoupper($string);
}
return $prefix . $string;
}
protected function __($list) {
if ($this->_driver == self::DRIVER_OCI8) {
return array_change_key_case($list, CASE_UPPER);
}
return $list;
}
protected function ___($list) {
if ($this->_driver == self::DRIVER_OCI8) {
foreach ($list as $key => $item) {
$list[$key] = strtoupper($item);
}
}
return $list;
}
public function __construct(\Zend\Db\Adapter\Adapter $adapter, $config = array()) {
$this->_db = $adapter;
$this->_sql = new \Zend\Db\Sql\Sql($this->_db);
/* set default global table name */
$this->_tables = array_merge($this->_tables, array(
'config' => 'config', /* global lib config */
));
/* get tables from config */
if (isset($config['tables']) && is_array($config['tables'])) {
$tables = $config['tables'];
foreach ($tables as $key => $value) {
if ($value) {
$this->_tables[$key] = $this->_($value);
}
}
}
if ($this->_db->getDriver() instanceof \Zend\Db\Adapter\Driver\Oci8\Oci8) {
$this->_driver = self::DRIVER_OCI8;
$this->_now = new \Zend\Db\Sql\Expression("SYSDATE");
} else {
$this->_driver = self::DRIVER_MYSQL;
$this->_now = new \Zend\Db\Sql\Expression("now()");
}
$this->_helper = new Helper($this);
}
public function getAdapter() {
return $this->_db;
}
public function getDriver() {
return $this->_driver;
}
public function getHelper() {
return $this->_helper;
}
public function getTables() {
return $this->_tables;
}
public function select() {
return $this->_sql->select();
}
public function insert() {
return new Sql\Insert();
return $this->_sql->insert();
}
public function update() {
return $this->_sql->update();
}
public function delete() {
return $this->_sql->delete();
}
public function store() {
return new Sql\Store($this->_db);
}
public function recycle() {
return new Sql\Recycle($this->_db);
}
public function fetchAll(\Zend\Db\Sql\Select $select, $raw = true) {
$statement = $this->_sql->prepareStatementForSqlObject($select);
//var_dump($statement); echo '<hr>';
if ($result = $statement->execute()) {
if (($this->_driver != self::DRIVER_OCI8) && (count($result) == 0)) { return false; }
if ((!$raw) && ($result instanceof ResultInterface)) {
$resultset = new \Zend\Db\ResultSet\ResultSet();
$resultset->initialize($result);
return $resultset;
}
return $result;
}
return false;
}
public function fetchRow(\Zend\Db\Sql\Select $select) {
if ($this->_driver != self::DRIVER_OCI8) {
$select->limit(1);
}
if ($result = $this->fetchAll($select)) {
foreach ($result as $row) {
return $row;
}
}
return null;
}
public function fetchValue(\Zend\Db\Sql\Select $select, $field = null) {
if ($row = $this->fetchRow($select)) {
if ($field) {
if (array_key_exists($field, $row)) {
return $row[$field];
}
} else {
foreach ($row as $data) {
return $data;
}
}
}
return null;
}
public function fetchConfig($code) {
$select = $this->select()
->from($this->_tables['config'])
->where($this->__(array('config_code' => $code)));
if ($row = $this->fetchRow($select)) {
if ($data = @json_decode(stripslashes($row[$this->_('config_data')]), true)) {
return new \Zend\Config\Config($data);
}
}
return new \Zend\Config\Config(array());
}
public function execute(\Zend\Db\Sql\SqlInterface $query) {
/* recycle? */
if ($query instanceof Sql\Recycle) {
/* get recycle target data */
if ($target = $query->getSqlString()) {
/* init */
$count = $target->set->count();
$saved = 0;
/* table, primary, flag field exists? */
if (($target->table) && ($target->primary) && ($target->flag)) {
/* any data to save? */
if ($count > 0) {
/* select existing */
$select = $this->select()
->from($target->table, array($target->primary, $target->flag))
->where($target->where);
if ($existing = $this->fetchAll($select)) {
/* loop existing and update */
foreach ($existing as $row) {
/* all new record saved? */
if ($saved >= $count) {
/* flag remaining records to unused */
$update = $this->update()
->table($target->table)
->set(array($target->flag => $query::FLAG_UNUSED))
->where(array($target->primary => $row[$target->primary]));
$this->execute($update);
} else {
/* overwrite existing record with new data */
$update = $this->update()
->table($target->table)
->set(array_merge(
$target->set->item($saved)->pull(array_merge(
array(
$target->primary,
$target->flag,
),
$target->exclude
)),
array(
$target->flag => $query::FLAG_USED,
)
))
->where(array($target->primary => $row[$target->primary]));
if ($this->execute($update)) {
/* assign new id to updated item */
$target->set->item($saved)->push(array(
$target->primary => $row[$target->primary],
));
}
/* increment saved */
$saved++;
} //all saved
} //each existing
} //fetch
/* any remaining unsaved records? */
if ($saved < $count) {
for ($index = $saved; $index < $count; $index++) {
/* insert remaining record */
$insert = $this->insert()
->into($target->table)
->values(array_merge(
$target->set->item($index)->pull(array_merge(
array(
$target->primary,
$target->flag,
),
$target->exclude
)),
array(
$target->flag => $query::FLAG_USED,
)
));
if ($id = $this->execute($insert)) {
/* assign new id to set item */
$target->set->item($index)->push(array(
$target->primary => $id,
));
}
} //each remaining record
} //remaining?
/* return! */
return ($saved > 0);
} else {
/* flag related existing record to unused */
$update = $this->update()
->table($target->table)
->set(array($target->flag => $query::FLAG_UNUSED))
->where($target->where);
return $this->execute($update);
} //any data to save
} //target primary check
} //get data
/* default return false */
return false;
/* store statement */
} elseif ($query instanceof Sql\Store) {
/* get store data */
$target = $query->getSqlString();
$values = $target->values;
/* get id if any */
$id = null;
if (isset($values[$target->primary])) {
$id = $values[$target->primary];
}
/* have id? update */
if ($id) {
/* unset id */
unset($values[$target->primary]);
/* generate update statement */
$update = $this->update()
->table($target->into)
->set($values)
->where(array($target->primary => $id));
/* execute update! */
if ($this->execute($update)) {
return $id;
}
/* no id, insert */
} else {
/* unset id if its an autoincrement */
if ($target->auto == true) { unset($values[$target->primary]); }
/* generate insert */
$insert = $this->insert()
->into($target->into)
->values($values);
/* execute insert */
return $this->execute($insert);
}
/* return false as default */
return false;
/* other normal statement */
} else {
/* prepare & execute */
$statement = $this->_sql->prepareStatementForSqlObject($query);
$result = $statement->execute();
/* insert, return last generated value (auto increment) */
if ($result && ($query instanceof Sql\Insert)) {
/* oci8? */
if ($this->_driver == self::DRIVER_OCI8) {
/* has ai field? */
if (($field = $query->hasAutoincrement()) !== false) {
if ($id = $query->{$field}) {
return $id;
}
}
} else {
/* others - mysql */
if ($id = $this->_db->getDriver()->getLastGeneratedValue()) {
return $id;
}
}
}
/* return result */
return $result;
} //recycle or not
}
public function now() {
return $this->_now;
}
public function expression($expression) {
return new \Zend\Db\Sql\Expression($expression);
}
}