-
Notifications
You must be signed in to change notification settings - Fork 20
/
class.lpdo.php
311 lines (294 loc) · 6.57 KB
/
class.lpdo.php
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
<?php
/**
*
* @Author : Poplax [Email:[email protected]];
* @Date : Fri Jun 03 10:17:17 2011;
* @Filename class.lpdo.php;
*/
/**
* class lpdo PDO
* one table support only
*/
class lpdo extends PDO
{
public $sql = '';
public $tail = '';
private $charset = 'UTF8';
private $options;
/**
*
* @Function : __construct;
* @Param $ : $options Array DB config ;
* @Return Void ;
*/
public function __construct($options)
{
$this->options = $options;
$dsn = $this->createdsn($options);
$attrs = empty($options['charset']) ? array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES " . $this->charset) : array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES " . $options['charset']);
try
{
parent::__construct($dsn, $options['username'], $options['password'], $attrs);
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
}
/**
*
* @Function : createdsn;
* @Param $ : $options Array;
* @Return String ;
*/
private function createdsn($options)
{
return $options['dbtype'] . ':host=' . $options['host'] . ';dbname=' . $options['dbname'];
}
/**
*
* @Function : get_fields;
* @Param $ : $data Array;
* @Return String ;
*/
private function get_fields($data)
{
$fields = array();
if (is_int(key($data)))
{
$fields = implode(',', $data);
}
else if (!empty($data))
{
$fields = implode(',', array_keys($data));
}
else
{
$fields = '*';
}
return $fields;
}
/**
*
* @Function : get_condition;
* @Param $ : $condition Array, $oper String, $logc String;
* @Return String ;
*/
private function get_condition($condition, $oper = '=', $logc = 'AND')
{
$cdts = '';
if (empty($condition))
{
return $cdts = '';
}
else if (is_array($condition))
{
$_cdta = array();
foreach($condition as $k => $v)
{
if (!is_array($v))
{
if (strtolower($oper) == 'like')
{
$v = '\'%' . $v . '%\'';
}
else if (is_string($v))
{
$v = '\'' . $v . '\'';
}
$_cdta[] = ' ' . $k . ' ' . $oper . ' ' . $v . ' ' ;
}
else if (is_array($v))
{
$_cdta[] = $this->split_condition($k, $v);
}
}
$cdts .= implode($logc, $_cdta);
}
return $cdts;
}
/**
*
* @Function : split_condition;
* @Param $ : $field String, $cdt Array;
* @Return String ;
*/
private function split_condition($field, $cdt)
{
$cdts = array();
$oper = empty($cdt[1]) ? '=' : $cdt[1];
$logc = empty($cdt[2]) ? 'AND' : $cdt[2];
if (!is_array($cdt[0]))
{
$cdt[0] = is_string($cdt[0]) ? "'$cdt[0]'" : $cdt[0];
}
else if (is_array($cdt[0]) || strtoupper(trim($cdt[1])) == 'IN')
{
$cdt[0] = '(' . implode(',', $cdt[0]) . ')';
}
$cdta[] = " $field $oper {$cdt[0]} ";
if (!empty($cdt[3]))
{
$cdta[] = $this->get_condition($cdt[3]);
}
$cdts = ' ( ' . implode($logc, $cdta) . ' ) ';
return $cdts;
}
/**
*
* @Function : get_fields_datas;
* @Param $ : $data Array;
* @Return Array ;
*/
private function get_fields_datas($data)
{
$arrf = $arrd = array();
foreach($data as $f => $d)
{
$arrf[] = '`' . $f . '`';
$arrd[] = is_string($d) ? '\'' . $d . '\'' : $d;
}
$res = array(implode(',', $arrf), implode(',', $arrd));
return $res;
}
/**
*
* @Function : get_rows;
* @Param $ : $table String, $getRes Boolean, $condition Array, $column Array;
* @Return Array |Object;
*/
public function get_rows($table, $condition = array(), $getRes = false, $column = array())
{
$fields = $this->get_fields($column);
$cdts = $this->get_condition($condition);
$where = empty($condition) ? '' : ' where ' . $cdts;
$this->sql = 'select ' . $fields . ' from ' . $table . $where;
try
{
$this->sql .= $this->tail;
$rs = parent::query($this->sql);
}
catch(PDOException $e)
{
trigger_error("get_rows: ", E_USER_ERROR);
echo $e->getMessage() . "<br/>\n";
}
$rs = $getRes ? $rs : $rs->fetchAll(parent::FETCH_ASSOC);
return $rs;
}
/**
*
* @Function : get_all;
* @Param $ : $table String, $condition Array, $getRes Boolean;
* @Return Array |Object;
*/
public function get_all($table, $getRes = false, $condition = array())
{
return $this->get_rows($table, $condition, $getRes);
}
/**
*
* @Function : get_one;
* @Param $ : $table String, $condition Array, $getRes Boolean, $column Array;
* @Return Array ;
*/
public function get_one($table, $condition = array(), $column = array())
{
$rs = $this->get_rows($table, $condition, true, $column);
$rs = $rs ? $rs->fetch(parent::FETCH_ASSOC) : $rs;
return $rs;
}
/**
*
* @Function : insert;
* @Param $ : $table String, $data Array;
* @Return Int ;
*/
public function insert($table, $data)
{
list($strf, $strd) = $this->get_fields_datas($data);
$this->sql = 'insert into `' . $table . '` (' . $strf . ') values (' . $strd . '); ';
return $this->exec($this->sql, __METHOD__);
}
/**
*
* @Function : update;
* @Param $ : $table String, $data Array, $condition Array;
* @Return Int ;
*/
public function update($table, $data, $condition)
{
$cdt = $this->get_condition($condition);
$arrd = array();
foreach($data as $f => $d)
{
$arrd[] = "`$f` = '$d'";
}
$strd = implode(',', $arrd);
$this->sql = 'update ' . $table . ' set ' . $strd . ' where ' . $cdt;
return $this->exec($this->sql, __METHOD__);
}
/**
*
* @Function : save;
* @Param $ : $table String, $data Array, $condition Array;
* @Return Int ;
*/
public function save($table, $data, $condition = array())
{
$cdt = $this->get_condition($condition);
list($strf, $strd) = $this->get_fields_datas($data);
$has1 = $this->get_one($table, $condition);
if (!$has1)
{
$enum = $this->insert($table, $data);
}
else
{
$enum = $this->update($table, $data, $condition);
}
return $enum;
}
/**
*
* @Function : delete;
* @Param $ : $table String, $condition Array;
* @Return Int ;
*/
public function delete($table, $condition)
{
$cdt = $this->get_condition($condition);
$this->sql = 'delete from ' . $table . ' where ' . $cdt;
return $this->exec($this->sql, __METHOD__);
}
/**
*
* @Function : exec;
* @Param $ : $sql, $method;
* @Return Int ;
*/
public function exec($sql, $method = '')
{
try
{
$this->sql = $sql . $this->tail;
$efnum = parent::exec($this->sql);
}
catch(PDOException $e)
{
echo 'PDO ' . $method . ' Error: ' . $e->getMessage();
}
return intval($efnum);
}
/**
*
* @Function : setLimit;
* @Param $ : $start, $length;
* @Return ;
*/
public function set_limit($start = 0, $length = 20)
{
$this->tail = ' limit ' . $start . ', ' . $length;
}
}
?>