-
Notifications
You must be signed in to change notification settings - Fork 0
/
Db_mysql_pdo.php
296 lines (261 loc) · 6.51 KB
/
Db_mysql_pdo.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
<?php
/**
* @Copyright (C).2016 - [email protected]
* @desc mysql pdo 操作类
* @author kevensuu
* @update 2016/4/29 11:16
*/
/**
* Class Db_mysql_pdo
* @desc mysql pdo 操作类
*/
class Db_mysql_pdo
{
/**
* @desc 配置信息
* @var null
*/
protected $setting = null;
/**
* @desc pdo对象
* @var null
*/
protected static $pdo = null;
/**
* @desc 数据库
* @var null
*/
public $dbname = null;
/**
* @desc
* @param string $dbname 数据库标识
*/
public function __construct($dbname = 'default')
{
$this->database = $dbname;
$this->connect();
}
/**
* @desc 数据库连接
* @return null|PDO
*/
protected function connect()
{
if(!self::$pdo)
{
// 获取数据库配置信息
$this->setting = parse_ini_file('data.ini', true);
$databaseInfo = $this->setting[$this->database];
try
{
self::$pdo = new PDO("{$databaseInfo['database']}:host={$databaseInfo['host']};port={$databaseInfo['port']};dbname={$databaseInfo['dbname']}", $databaseInfo['username'], $databaseInfo['password']);
self::$pdo->query('set names utf8');
}
catch(PDOException $e)
{
exit('PDOException: ' . $e->getMessage());
}
}
}
/**
* @desc 执行SQL语句
* @param string $sql SQL语句
* @param array $parameters 需要绑定的参数值
* @param int $fetchmode
* @return null
*/
public function query($sql, $parameters = array(), $fetchmode = PDO::FETCH_ASSOC)
{
$stmt = self::$pdo->prepare($sql);
$stmt->execute($parameters);
$rawStatement = explode(" ", preg_replace("/\s+|\t+|\n+/", " ", $sql));
$statement = strtolower($rawStatement[0]);
if($statement === 'select')
{
return $stmt->fetchAll($fetchmode);
}
elseif($statement === 'insert' || $statement === 'update' || $statement === 'delete')
{
return $stmt->rowCount();
}
else
{
return null;
}
}
/**
* @desc 获取最后插入ID
* @return mixed
*/
public function lastInsertId()
{
return self::$pdo->lastInsertId();
}
/**
* @desc 添加一条记录
* @param string $tableName 数据库表名
* @param array $data 需要添加的数据,如:array('field1'=>'value1', 'field2'=>'value2')
* @return int 返回影响行数
*/
public function insert($tableName, array $data)
{
$fields = '`' . implode('`,`', array_keys($data)) . '`';
$values = implode(',', array_fill(0, count($data), '?'));
$sql = "INSERT INTO `{$tableName}`({$fields}) VALUES ({$values})";
return $this->query($sql, array_values($data));
}
/**
* @desc 添加多条数据
* @param string $tableName 数据库表名
* @param array $data 需要添加的数据,为一个二维数组,如:$data = array(array('fileld1'=>'value1','fileld2'=>'value2'),array('fileld1'=>'value1','fileld2'=>'value2'))
* @return int 返回影响行数
*/
public function insertBatch($tableName, array $data)
{
$fields = '`' . implode('`,`', array_keys($data[0])) . '`';
$tmp = $tmp2 = array();
foreach($data as $value)
{
$tmp[] = implode(',', array_fill(0, count($value), '?'));
foreach($value as $v)
{
$tmp2[] = $v;
}
}
$values = "(" . implode("),(", $tmp) . ")";
$sql = "INSERT INTO `{$tableName}`({$fields}) VALUES {$values}";
return $this->query($sql, $tmp2);
}
/**
* @desc 更新
* @param string $tableName 数据库表名
* @param array $where 更新条件,为 key|value 对应的数组,如:array('id'=>233)
* @param array $data 更新数据,为 key|value 对应的数组,如:array('field1'=>'value1','field12'=>'value2')
* @param array $other 参照 formatOhterCondition 中的参数说明
* @return int 返回影响行数
*/
public function update($tableName, array $where, array $data, array $other=array())
{
if(!$where || !$data){return false;}
$tmp1 = $tmp2 = $tmp3 = array();
// 条件
foreach($where as $key=>$value)
{
$tmp1[] = "{$key}=?";
$tmp2[] = $value;
}
$tmp1 = implode(' and ', $tmp1);
// 组合更新数据
foreach($data as $key=>$value)
{
$tmp3[] = "`{$key}`='{$value}'";
}
$tmp3 = implode(',', $tmp3);
$other = $this->formatOhterCondition($other);
$sql = "UPDATE `{$tableName}` SET {$tmp3} WHERE {$tmp1} {$other}";
return $this->query($sql, $tmp2);
}
/**
* @desc 删除
* @param string $tableName 数据库表名
* @param array $where 删除条件,为 key|value 对应的数组,如:array('id'=>233)
* @param array $other 参照 formatOhterCondition 中的参数说明
* @return int 返回影响行数
*/
public function delete($tableName, array $where, array $other=array())
{
if(!$where){return false;}
$tmp1 = $tmp2 = array();
// 条件
foreach($where as $key=>$value)
{
$tmp1[] = "{$key}=?";
$tmp2[] = $value;
}
$tmp1 = implode(' and ', $tmp1);
$other = $this->formatOhterCondition($other);
$sql = "DELETE FROM `{$tableName}` WHERE {$tmp1} {$other}";
return $this->query($sql, $tmp2);
}
/**
* @desc 开启事务
* @return mixed
*/
public function beginTransaction()
{
return self::$pdo->beginTransaction();
}
/**
* @desc 提交事务
* @return mixed
*/
public function executeTransaction()
{
return self::$pdo->commit();
}
/**
* @desc 回滚事务
* @return mixed
*/
public function rollBack()
{
return self::$pdo->rollBack();
}
/**
* @desc 格式化 where 条件
* @param string|array $where
* 字符串: id=1 and field1=value1
* 数组: array(
* array('id', '=', 1),
* array('field1', '=', 'value1'),
* array('field2', 'in', array(1,2,3)),
* )
* @return string
*/
public function formatWhere($where)
{
// 字符串
if(!is_array($where))
{
if($where){return " where {$where}";}
return '';
}
// 空数组
if(!$where){return '';}
$tmp = array();
foreach($where as $value)
{
switch($value[1])
{
case 'in':
$value[2] = '('.implode(',', $value[2]).')';
$tmp[] = "{$value[0]} {$value[1]} {$value[2]}";
break;
default:
$tmp[] = "{$value[0]} {$value[1]} '{$value[2]}'";
break;
}
}
$where = implode(' and ', $tmp);
return " where {$where}";
}
/**
* @desc 格式化其他条件
* @param array $other = array(
'order'=>'id desc',
'limit'=>'0,10',
);
* @return string
*/
public function formatOhterCondition(array $other = array())
{
if(!$other){return '';}
$tmp = '';
foreach($other as $key=>$value)
{
if($key === 'order'){$tmp .= " order by {$value}";}
if($key === 'limit'){$tmp .= " limit {$value}";}
}
return $tmp;
}
}