-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
357 lines (308 loc) · 11.1 KB
/
Copy pathdb.php
File metadata and controls
357 lines (308 loc) · 11.1 KB
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
<?php
// Author: Cory Cook
class Parameter
{
public $parameters = array();
public $prefix;
public $suffix;
public $join;
public function __construct($pre, $junc = ', ', $post = '') {
$this->prefix = $pre;
$this->join = $junc;
$this->suffix = $post;
}
public function add($column, $argument = NULL) {
if (is_null($argument)) {
$this->parameters[] = $column;
} else {
$this->parameters[$column] = $argument;
}
}
public function reset() {
$this->parameters = array();
}
public function serialize() {
if (count($this->parameters))
return $this->prefix.implode($this->join, $this->parameters).$this->suffix;
return NULL;
}
}
class ValuesParameter extends Parameter
{
protected $stringset;
public function __construct($prefix, $join = ', ', $suffix = '') {
parent::__construct($prefix, $join, $suffix);
$this->stringset = function(&$value, $key) {
if (is_string($value)) $value = "'$value'";
};
}
public function serialize() {
if (count($this->parameters)) {
$result_set = $this->parameters;
array_walk($result_set, $this->stringset);
return $this->prefix.implode($this->join, $result_set).$this->suffix;
}
return NULL;
}
}
class SearchParameter extends ValuesParameter
{
protected $set;
public function __construct() {
parent::__construct(' WHERE ', ' AND ');
$this->set = function(&$value, $key) {
if (is_int($key)) {
} else if (is_string($value)) {
$value = "$key LIKE '$value'";
} else if (is_array($value)) {
array_walk($value, $this->stringset);
$value = "$key IN (".implode(', ', $value).")";
} else if ($value instanceof DbEngine) {
$value = "[$key] IN (".$value->tosql().")";
}
else {
$value = "[$key]=$value";
}
};
}
public function serialize() {
if (count($this->parameters)) {
$result_set = $this->parameters;
array_walk($result_set, $this->set);
return $this->prefix.implode($this->join, $result_set);
}
return NULL;
}
}
class SetParameter extends ValuesParameter
{
protected $set;
public function __construct() {
parent::__construct(' SET ', ', ');
$this->set = function(&$value, $key) {
$value = "[$key]=$value";
};
}
public function serialize() {
if (count($this->parameters)) {
$result_set = $this->parameters;
array_walk($result_set, $this->stringset);
array_walk($result_set, $this->set);
return $this->prefix.implode($this->join, $result_set).$this->suffix;
}
return NULL;
}
}
class SelectParameter extends Parameter
{
public function __construct() {
parent::__construct('SELECT ');
}
public function serialize() {
if (count($this->parameters)) {
return parent::serialize();
}
return $this->prefix.'*';
}
}
class DbEngine
{
protected $connection;
protected $parameters = array();
protected $state, $checkstate, $res;
protected $tablename;
public function __construct($db, $username = NULL, $password = NULL) {
$this->parameters['search'] = new SearchParameter();
if ($db instanceof DbEngine) {
$this->connection = $db->connection;
if (!is_null($db->tablename)) $this->from($db->tablename);
$this->parameters['search']->parameters = $db->parameters['search']->parameters;
} else {
$this->connection = odbc_connect($db, $username, $password) or
die('Database Connection Failed');
}
$this->refresh();
}
public function __destruct() {
if (is_resource($this->connection)) {
odbc_close($this->connection);
}
}
public function tosql() {
$sql = '';
foreach ($this->parameters as $param) {
$sql .= $param->serialize();
}
return $sql;
}
protected function parameter($param, $col = NULL, $arg = NULL) {
if (is_array($col)) {
if (!is_array($arg)) $arg = array($arg);
foreach($col as $i => $column) {
$this->parameters[$param]->add($column, array_key_exists($i, $arg) ? $arg[$i] : NULL);
}
} else if (is_null($col)) {
$this->parameters[$param]->reset();
}
else {
$this->parameters[$param]->add($col, $arg);
}
$this->refresh();
}
public function from($t = NULL, $alias = NULL) {
$this->tablename = $t;
$this->parameter('from', $t, $alias);
return $this;
}
public function search($col = NULL, $arg = NULL) {
$this->parameter('search', $col, $arg);
return $this;
}
public function toarray() {
$result = array();
while ($arr = odbc_fetch_array($this->result())) {
$result[] = $arr;
}
$this->refresh();
return $result;
}
public function result() {
if ($this->state == $this->checkstate) return $this->res;
$this->res = odbc_exec($this->connection, $this->tosql());
$this->checkstate = $this->state;
return $this->res;
}
public function tables() {
$result = odbc_tables($this->connection);
$result_set = array();
while(odbc_fetch_row($result)) {
if (odbc_result($result, "TABLE_TYPE")=="TABLE")
$result_set[] = odbc_result($result, "TABLE_NAME");
}
return $result_set;
}
public function count() {
$db = new Db($this);
$db->select('COUNT(*) as val');
return odbc_fetch_array($db->result())['val'];
}
public function columns() {
$result = array();
$set = odbc_columns($this->connection);
while ($arr = odbc_fetch_array($set)) {
if (in_array($arr['TABLE_NAME'], $this->parameters['from']->parameters))
$result[] = $arr;
}
return $result;
}
protected function refresh() {
$this->state = rand();
if (is_resource($this->res)) {
odbc_free_result($this->res);
}
}
public function reset() {
foreach($this->parameters as $parameter) {
$parameter->reset();
$this->from($tablename);
}
}
}
class SelectEngine extends DbEngine
{
public function __construct($db, $username = NULL, $password = NULL) {
$this->parameters['select'] = new SelectParameter();
$this->parameters['from'] = new Parameter(' FROM ');
parent::__construct($db, $username, $password);
$this->parameters['group'] = new Parameter(' GROUP BY ');
$this->parameters['sort'] = new Parameter(' ORDER BY ');
}
public function group($col = NULL) {
$this->parameter('group', $col);
return $this;
}
public function sort($col = NULL) {
$this->parameter('sort', $col);
return $this;
}
public function select($col = NULL, $alias = NULL, $raw = FALSE) {
$this->parameter('select', $col);
return $this;
}
public function result($col = NULL) {
if (!is_null($col)) {
$this->select();
$this->select($col);
}
return parent::result();
}
public function tohtml($col = NULL) {
if (!$this->count()) echo '<p>No data</p>';
else odbc_result_all($this->result($col));
$this->refresh();
}
}
class InsertEngine extends DbEngine
{
public function __construct($db, $username = NULL, $password = NULL) {
$this->parameters['from'] = new Parameter('INSERT INTO ');
$this->parameters['keys'] = new Parameter(' (', ', ', ')');
$this->parameters['values'] = new ValuesParameter(' VALUES (', ', ', ')');
parent::__construct($db, $username, $password);
$this->parameters['search']->reset();
}
public function insert($arr) {
foreach ($arr as $key => $value) {
$this->parameter('keys', $key);
$this->parameter('values', $value);
}
return $this->result();
}
}
class UpdateEngine extends DbEngine
{
public function __construct($db, $username = NULL, $password = NULL) {
$this->parameters['from'] = new Parameter('UPDATE ');
$this->parameters['set'] = new SetParameter();
parent::__construct($db, $username, $password);
}
public function update($arr) {
foreach ($arr as $key => $value) {
$this->parameter('set', $key, $value);
}
return $this->result();
}
public function set($col = NULL, $arg = NULL) {
$this->parameter('set', $col, $arg);
return $this;
}
}
class DeleteEngine extends DbEngine
{
public function __construct($db, $username = NULL, $password = NULL) {
$this->parameters['from'] = new Parameter('DELETE FROM ');
parent::__construct($db, $username, $password);
}
public function delete() {
return $this->result();
}
}
class Db extends SelectEngine
{
public function __construct($db, $username = NULL, $password = NULL) {
parent::__construct($db, $username, $password);
}
public function insert($arr) {
$Eng = new InsertEngine($this);
$Eng->insert($arr);
}
public function update($arr) {
$Eng = new UpdateEngine($this);
$Eng->update($arr);
}
public function delete() {
$Eng = new DeleteEngine($this);
$Eng->delete();
}
}
?>