-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSQLExecuter.php
312 lines (258 loc) · 11.3 KB
/
SQLExecuter.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
312
<?php
/**
* Created by PhpStorm.
* User: VeroFess
* Date: 2018/1/26
* Time: 21:22
*/
namespace Binklac\Basic\SQLExecuter;
use Binklac\Basic\Helper as Helper;
/*
* About Helper
*
static public function IS_SUCCESS($Status) {
if (!is_array($Status)) {
return FALSE;
} elseif (!isset($Status['Status'])){
return FALSE;
} elseif (!($Status['Status'] === 'STATUS_SUCCESS')){
return FALSE;
} else {
return TRUE;
}
}
static public function GET_STATUS_DATA_OR_NULL($Status){
if (!is_array($Status)) {
return '';
} elseif (!isset($Status['Status'])){
return '';
} elseif (!isset($Status['Data'])){
return '';
} elseif (!($Status['Status'] === 'STATUS_SUCCESS')){
return '';
} else {
return $Status['Data'];
}
}
* */
class SQLExecuter{
const DATA_TYPE_INT_VAL = 1;
const DATA_TYPE_FLOAT_VAL = 2;
const DATA_TYPE_BOOL_VAL = 3;
const DATA_TYPE_STRING_VAL = 4;
private static $TheInstance;
private static $IsInit;
private static $EnableDebug;
private static $dbh;
private function __construct(){
self::$IsInit = FALSE;
self::$EnableDebug = FALSE;
}
public function __clone(){
trigger_error('Binklac Web Framework 4: Clone is not allowed.',E_USER_ERROR);
}
static public function Instance(){
if(!isset(self::$TheInstance)){
self::$TheInstance = new self();
}
return self::$TheInstance;
}
public function SQL_PREPARE_DATA($Name, $Data, $DataType){
if(!self::$IsInit){
return array("Status"=>"STATUS_SQL_NEED_INIT", "Data"=>"");
}
//检查名称, 无条件信任 $Name 部分,自己做死谁都拦不住 = =
if(!is_string($Name)){
return array("Status"=>"STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data"=>"");
}
//如果是数字类型
if ($DataType === self::DATA_TYPE_INT_VAL || $DataType === self::DATA_TYPE_FLOAT_VAL){
$TrimedData = trim(trim($Data), " \f\n\r\t\v\0\x0B"); //部分版本默认没有 \f, 造成is_numeric绕过,清除两次确保清除干净
if(is_numeric($TrimedData)){ //清除完了再判断是不是数字
if($DataType === self::DATA_TYPE_FLOAT_VAL){ //浮点数,特殊处理
$CleanData = floatval($Data);//加上引号,防止奇葩错误
return array("Status"=>"STATUS_SUCCESS", "Data"=>(array("K"=>$Name, "V"=>$CleanData)));
} else { //十六进制,八进制之类的进到这里处理
$CleanData = intval($Data);
return array("Status"=>"STATUS_SUCCESS", "Data"=>(array("K"=>$Name, "V"=>$CleanData)));
}
} else {
return array("Status"=>"STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data"=>"");
}
}
if ($DataType === self::DATA_TYPE_STRING_VAL) { //字符串类型
return array("Status"=>"STATUS_SUCCESS", "Data"=>(array("K"=>$Name, "V"=>$Data)));
}
if ($DataType === self::DATA_TYPE_BOOL_VAL) { //布尔,直接强制转换
$CleanData = $Data ? 1 : 0;
return array("Status"=>"STATUS_SUCCESS", "Data"=>(array("K"=>$Name, "V"=>$CleanData)));
}
return array("Status"=>"STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data"=>"");
}
public function SQL_EASY_BUILD_DATA($SQLRowData){
if(!self::$IsInit){
return array("Status"=>"STATUS_SQL_NEED_INIT", "Data"=>"");
}
$SQLData = array();
if(!(count($SQLRowData) > 0)) {
return array("Status" => "STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data" => "");
}
foreach ($SQLRowData as $EachRowData){
if(Helper::IS_SUCCESS($EachRowData)){
$SQLData[Helper::GET_STATUS_DATA_OR_NULL($EachRowData)["K"]] = Helper::GET_STATUS_DATA_OR_NULL($EachRowData)["V"];
} else {
return array("Status" => "STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data" => "");
}
}
return array("Status"=>"STATUS_SUCCESS", "Data"=>$SQLData);
}
public function SetupConnection($Server, $Database, $UserName, $Password, $EnableDebugMessage = FALSE){
try{
self::$dbh = new \PDO("mysql:host=" . $Server . ";dbname=" . $Database . ";charset=utf8", $UserName, $Password);
if(!self::$dbh->setAttribute(\PDO::ATTR_EMULATE_PREPARES, FALSE)){
return array("Status"=>"STATUS_EXCEPTION_COON_DB", "Data"=>"");
}
}catch(\PDOException $e){
return array("Status"=>"STATUS_EXCEPTION_COON_DB", "Data"=>"");
}
self::$EnableDebug = is_bool($EnableDebugMessage) ? $EnableDebugMessage : FALSE;
self::$IsInit = TRUE;
return array("Status"=>"STATUS_SUCCESS", "Data"=>"");
}
public function Select($Table, $QueriesData, $Target = "*"){
if(!self::$IsInit){
return array("Status"=>"STATUS_SQL_NEED_INIT", "Data"=>"");
}
if(!(is_string($Table) || is_string($Target))){
return array("Status"=>"STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data"=>"");
}
if(!Helper::IS_SUCCESS($QueriesData)) {
return array("Status" => "STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data" => "");
}
$SQLCommand = "SELECT " . ($Target === "*" ? $Target : ("`" . $Target . "`")) . " FROM `" . $Table . "` WHERE ";
$Queries = Helper::GET_STATUS_DATA_OR_NULL($QueriesData);
$QueriesCount = count($Queries);
$QueryDataArray = array();
foreach($Queries as $EachQueryName=>$EachQueryData){
$SQLCommand = $SQLCommand . $EachQueryName . " = ?";
array_push($QueryDataArray, $EachQueryData);
if(--$QueriesCount){
$SQLCommand = $SQLCommand . " AND ";
}
}
$sth = self::$dbh->prepare($SQLCommand);
$sth->execute($QueryDataArray);
if($sth->rowCount() > 0){
return array("Status"=>"STATUS_SUCCESS", "Data"=>($sth->fetchAll(\PDO::FETCH_ASSOC)));
}
return array("Status"=>"STATUS_SQL_SELECT_EXCEPTION", "Data"=>self::$EnableDebug ? $sth->errorInfo() : "");
}
public function Update($Table, $QueriesData, $Targets = FALSE){
if(!self::$IsInit){
return array("Status"=>"STATUS_SQL_NEED_INIT", "Data"=>"");
}
if(!is_string($Table)){
return array("Status"=>"STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data"=>"");
}
if(!Helper::IS_SUCCESS($QueriesData)) {
return array("Status" => "STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data" => "");
}
if(!($Targets === FALSE)){
if(!((count($Targets) > 0) || is_array($Targets))) {
return array("Status" => "STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data" => "");
}
}
$SQLCommand = "UPDATE " . $Table . " SET ";
$Queries = Helper::GET_STATUS_DATA_OR_NULL($QueriesData);
$QueriesCount = count($Queries);
$QueryDataArray = array();
foreach($Queries as $EachQueryName=>$EachQueryData){
$SQLCommand = $SQLCommand . $EachQueryName . " = ?";
array_push($QueryDataArray, $EachQueryData);
if(--$QueriesCount){
$SQLCommand = $SQLCommand . " , ";
}
}
if(!($Targets === FALSE)){
$SQLCommand = $SQLCommand . " WHERE ";
$TargetsCount = count($Targets);
foreach($Targets as $EachTargetName=>$EachTargetData){
$SQLCommand = $SQLCommand . $EachTargetName . " = ?";
array_push($QueryDataArray, $EachTargetData);
if(--$TargetsCount){
$SQLCommand = $SQLCommand . " AND ";
}
}
}
$sth = self::$dbh->prepare($SQLCommand);
$sth->execute($QueryDataArray);
if($sth->rowCount() > 0){
return array("Status"=>"STATUS_SUCCESS", "Data"=>"");
}
return array("Status"=>"STATUS_SQL_SELECT_EXCEPTION", "Data"=>self::$EnableDebug ? $sth->errorInfo() : "");
}
public function Delete($Table, $TargetsData){
if(!self::$IsInit){
return array("Status"=>"STATUS_SQL_NEED_INIT", "Data"=>"");
}
if(!is_string($Table)){
return array("Status"=>"STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data"=>"");
}
if(!Helper::IS_SUCCESS($TargetsData)) {
return array("Status" => "STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data" => "");
}
$SQLCommand = "DELETE FROM " . $Table . " WHERE ";
$Targets = Helper::GET_STATUS_DATA_OR_NULL($TargetsData);
$TargetsCount = count($Targets);
$QueryDataArray = array();
foreach($Targets as $EachTargetName=>$EachTargetData){
$SQLCommand = $SQLCommand . $EachTargetName . " = ?";
array_push($QueryDataArray, $EachTargetData);
if(--$TargetsCount){
$SQLCommand = $SQLCommand . " AND ";
}
}
$sth = self::$dbh->prepare($SQLCommand);
$sth->execute($QueryDataArray);
if($sth->rowCount() > 0){
return array("Status"=>"STATUS_SUCCESS", "Data"=>"");
}
return array("Status"=>"STATUS_SQL_DELETE_EXCEPTION", "Data"=>self::$EnableDebug ? $sth->errorInfo() : "");
}
public function Insert($Table, $TargetsData){
if(!self::$IsInit){
return array("Status"=>"STATUS_SQL_NEED_INIT", "Data"=>"");
}
if(!is_string($Table)){
return array("Status"=>"STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data"=>"");
}
if(!Helper::IS_SUCCESS($TargetsData)) {
return array("Status" => "STATUS_SQL_EXCEPTION_INVALID_ARGUMENT", "Data" => "");
}
$SQLCommand = "INSERT INTO `" . $Table . "` ( ";
$Targets = Helper::GET_STATUS_DATA_OR_NULL($TargetsData);
$TargetsCount = count($Targets);
$QueryDataArray = array();
foreach($Targets as $EachTargetName=>$EachTargetData){
$SQLCommand = $SQLCommand . $EachTargetName;
array_push($QueryDataArray, $EachTargetData);
if(--$TargetsCount){
$SQLCommand = $SQLCommand . " , ";
}
}
$SQLCommand = $SQLCommand . " ) VALUES (";
for($i = count($Targets); $i > 0 ;){
$SQLCommand = $SQLCommand . " ?";
if(--$i){
$SQLCommand = $SQLCommand . " ,";
}
}
$SQLCommand = $SQLCommand . " );";
$sth = self::$dbh->prepare($SQLCommand);
$sth->execute($QueryDataArray);
if($sth->rowCount() > 0){
return array("Status"=>"STATUS_SUCCESS", "Data"=>"");
}
return array("Status"=>"STATUS_SQL_INSERT_EXCEPTION", "Data"=>self::$EnableDebug ? $sth->errorInfo() : "");
}
}