-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathService.class.php
194 lines (172 loc) · 6.33 KB
/
Service.class.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
<?php
namespace services;
class Service {
protected $tabName = '';
protected $errorInfo = array();
static public $paramsKeyWord = array(
'GROUPBY',
'HAVING' ,
'ORDERBY',
'LIMIT',
);
/**
* @var \PDO
*/
protected $conn = NULL;
public function __construct(){
$this->conn = new \PDO (
'mysql:host=localhost;dbname=lscms',
'root',
'root'
);
$this->conn->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
}
/**
* 执行数据库操作<br />
* 当为select时,返回结果为查询到的结果集(取决于$getType指定的方式)<br/>
* 当为update、insert或delete时返回收影响的记录数
* @param string $sqlName
* @param string $getType
* @param array $params
* @throws Exception
* @return Ambigous <NULL, multitype:>|boolean
*/
public function execute($sqlName ,$getType, $params = array()){
try{
$sql = $this->_getSqlStatement($sqlName, $params);
$st = $this->conn->prepare($sql);
$st->execute($params);
$errInfo = $st->errorInfo();
if($errInfo[0] !== '00000'){
throw new \Exception($errInfo[2],intval($errInfo[1]));
}
$rs = null;
$getType = strtoupper($getType);
switch ($getType){
case 'GETONE':
$rs = $st->fetchColumn(0);
break;
case 'GETROW':
$rs = $st->fetchAll();
$rs = isset($rs[0]) ? $rs[0] : array();
break;
case 'GETALL':
$rs = $st->fetchAll();
break;
case 'UPDATE':
case 'INSERT':
case 'DELETE':
$rs = $st->rowCount();
break;
default:
throw new \Exception('未知的数据库操作类型!');
}
return $rs;
}catch (\Exception $e){
throw $e;
}
}
// private function _processParams($params){
// foreach ($params as $k => $v){
// if($v )
// }
// }
/**
* 获取最后插入的id
* @return string
*/
public function getLastInsertId(){
return $this->conn->lastInsertId();
}
/**
* 获取指定sqlName的sql语句
* @param string $sqlName
* @param array $params
* @return string
*/
private function _getSqlStatement($sqlName , &$params){
$xml = new \SimpleXMLElement(
file_get_contents(
APP_PATH . 'includes/sql/'. $this->tabName .'.xml'
));
return trim($this->_mkSqlStatement($xml, $sqlName , $params))
.$this->_mkSqlStatementSuffix($params);
}
/**
* 生成指定sqlName的sql语句
* @param \SimpleXMLElement $xml
* @param string $sqlName
* @param array $params
* @return string
*/
private function _mkSqlStatement( \SimpleXMLElement $xml , $sqlName,&$params){
$statement = '';
foreach ($xml->children() as $key => $sqlGroup){
$_sqlName = (string)$sqlGroup->attributes()->name;
if($_sqlName == $sqlName){
$statement = (string) $sqlGroup->sql;
$hasWhere = (int) $sqlGroup->has_where;
if ($hasWhere == 0){
$where = '';
foreach ($sqlGroup->options->option as $option){
$optName = (string)$option->attributes()->name;
if(isset($params[$optName])){
$linkType = (string)$option->attributes()->link;
$condition = trim((string)$option);
$subWhere = '';
if($where != '') {
$subWhere = ' ' . $linkType .' ' .$condition;
}else{
$subWhere = ' ' . $condition;
}
$where .= $subWhere;
}
}
if($where != '') $statement .= ' WHERE' . $where;
}
break;
}
}
foreach ($params as $key => $param){
if(is_array($param)){
foreach ($param as $k => $val){
$param[$k] = "'{$val}'";
}
$optVal = implode(',', $param);
$statement = str_replace($key, $optVal, $statement);
unset($params[$key]);
}
}
return $statement;
}
/**
* 生成sql语句后缀(指语句末尾的 order by 和limit等).<br />
* 当前支持 GROUP BY、HAVING、ORDER BY、LIMIT,支持的列表参见self::$paramsKeyWord数组
* @param array $params
* @return string
*/
private function _mkSqlStatementSuffix(&$params){
$keyWords = array();
$keyWordStr = '';
foreach ($params as $key => $param){
$_key = strtoupper($key);
if(in_array($_key, self::$paramsKeyWord)){
unset($params[$key]);
$keyWords[] = array('key' => $_key,'val' => $param);
}
}
if(!empty($keyWords)){
usort($keyWords, function($val1 , $val2){
$key1 = array_search($val1['key'], Service::$paramsKeyWord);
$key2 = array_search($val2['key'], Service::$paramsKeyWord);
return $key1 > $key2;
});
foreach ($keyWords as $keyWord){
if($keyWord['key'] == 'ORDERBY') $keyWord['key'] = 'ORDER BY';
if($keyWord['key'] == 'GROUPBY') $keyWord['key'] = 'GROUP BY';
$keyWordStr .= ' '.$keyWord['key'].' '.$keyWord['val'];
}
}
return $keyWordStr;
}
}