SQLEnumController.php
8.2 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
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Contracts\Validation\Validator;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Config;
use DB;
use Exception;
use Log;
class SQLEnumController extends Controller
{
private function buildSelectStatement(Request $request, $field, $config, $datas, $schema)
{
$vals = [];
if (isset($schema->adv_config->filter) && isset($schema->adv_config->filter->$field))
$vals[] = $schema->adv_config->filter->$field;
foreach ($datas as $data) {
$val = null;
if (is_array($data) && isset($data[$field])) {
$val = $data[$field];
} else if (isset($data->$field)) {
$val = $data->$field;
}
if (is_array($val)) {
foreach ($val as $v)
$vals[] = $v;
} else if ($val !== null) {
$vals[] = $val;
}
}
if (count($vals) == 0)
return [null, null];
$sql = preg_replace('/{[^\}]+}/', implode(',', array_fill(0, count($vals), '?')), $config->mapSql);
return [$sql, $vals];
}
private function buildUnionStatement(Request $request, $field, $config, $datas, $schema)
{
$sql = $config->sql;
$vals = [];
$sqls = [];
preg_match_all('/{([^\}]+)}/', $sql, $fields);
$sql = preg_replace('/{[^\}]+}/', '?', $sql);
if ($sql != $config->sql) {
foreach ($datas as $data) {
foreach ($fields[1] as $f) {
if (is_array($data))
$vals[] = $data[$f];
else
$vals[] = $data->$f;
}
$sqls[] = $sql;
}
$sql = implode(' UNION ALL ', $sqls);
}
return [$sql, $vals];
}
private function fillEnumsData(Request $request, $field, $schema, $datas)
{
$config = $schema->adv_config->sqlDicts->$field;
// 不需要转换
if ($config->type == 0)
return true;
// datas是object(数据库接口)或则是array(history接口转换过来的), 所以对取值需要兼容
// 对于history接口的数据,field对应的字段可能不存在
// (数据库增加了字段,老的历史数据会不存在这个字段)
$host = $config->host;
$port = $config->port;
$user = $config->user;
$passwd = $config->passwd;
$dbname = $config->dbname;
$sql = '';
$vals = [];
if (isset($config->mapSql)) {
list($sql, $vals) = $this->buildSelectStatement($request, $field, $config, $datas, $schema);
} else {
list($sql, $vals) = $this->buildUnionStatement($request, $field, $config, $datas, $schema);
}
if ($sql == null)
return true;
$confname = $host . ':' . $port;
$conn = [
'driver' => 'mysql',
'host' => $host,
'port' => $port,
'database' => $dbname,
'username' => $user,
'password' => $passwd,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
];
Config::set('database.connections.' . $confname, $conn);
$result = DB::connection($confname)->select($sql, $vals);
$out = [];
$ret = [];
if (count($result) == 0)
return $ret;
if (count((array)($result[0])) == 1)
return $ret;
foreach ($result as $item) {
$arr = [];
foreach ($item as $k => $v) {
$arr[] = $v;
if (count($arr) == 2)
break;
}
$out[$arr[0]] = $arr[1];
}
foreach ($datas as $data) {
$val = is_array($data) ? (isset($data[$field]) ? $data[$field] : "") : $data->$field;
if (is_array($val)) {
foreach ($val as $v) {
if (isset($out[$v]))
$ret[$v] = $out[$v];
else
$ret[$v] = $v;
}
} else {
if (isset($out[$val]))
$ret[$val] = $out[$val];
else
$ret[$val] = $val;
}
}
if (isset($schema->adv_config->filter) && isset($schema->adv_config->filter->$field)) {
$v = $schema->adv_config->filter->$field;
if (isset($out[$v]))
$ret[$v] = $out[$v];
else
$ret[$v] = $v;
}
return $ret;
}
public function getEnumsByData(Request $request, $schema, $datas)
{
if (!isset($schema->adv_config) || !isset($schema->adv_config->sqlDicts))
return [];
$ret = [];
foreach ($schema->adv_config->sqlDicts as $f => $config)
$ret[$f] = $this->fillEnumsData($request, $f, $schema, $datas);
return $ret;
}
public function getEnumsByQuery(Request $request)
{
$ret = $this->getEnums($request);
if ($ret['errcode'] != 0)
return ['q' => $request->input('q', ''), 'results' => []];
$result = [];
foreach ($ret['data'] as $data) {
$result[] = ['id' => $data['key'], 'text' => $data['name']];
}
return ['q' => $request->input('q', ''), 'results' => $result];
}
public function getEnums(Request $request)
{
$host = $request->input('host', '');
$port = $request->input('port', '');
$user = $request->input('user', '');
$passwd = $request->input('passwd', '');
$dbname = $request->input('dbname', '');
$sql = $request->input('sql', '');
$type = $request->input('type', 0);
if (empty($host) || empty($port) || empty($user) || empty($passwd) || empty($dbname) || empty($sql))
return ["errcode" => 404, "errmsg" => "Bad request"];
$vals = [];
$q = null;
if (preg_match_all('/{([^\}]+)}/', $sql, $fields) > 0) {
foreach ($fields[1] as $f) {
if ($f == 'user.email') {
$vals[] = $request->user->email;
} else if ($f == 'user.userId') {
$vals[] = $request->user->userId;
} else {
if ($f == 'request.q') {
$q = $request->input('q', '');
if ($q == '')
return ['errcode' => 404, "errmsg" => "Bad request"];
$q = str_replace(['%', '_', '\'', '"', ';'], ['\%', '\_', '', '', ''], $q);
continue;
}
$v = $request->input($f, '');
if (strlen($v) == 0)
return ["errcode" => 404, "errmsg" => "Bad request: {$f}的值为空"];
$vals[] = $v;
}
}
}
if (!empty($q))
$sql = preg_replace('/{request.q}/', $q, $sql);
$sql = preg_replace('/{[^\}]+}/', '?', $sql);
$confname = $host . ':' . $port;
$conn = [
'driver' => 'mysql',
'host' => $host,
'port' => $port,
'database' => $dbname,
'username' => $user,
'password' => $passwd,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
];
Config::set('database.connections.' . $confname, $conn);
$result = DB::connection($confname)->select($sql, $vals);
$out = [];
foreach ($result as $item) {
$arr = [];
foreach ($item as $k => $v) {
$arr[] = $v;
if ($type != 0 || count($arr) == 2)
break;
}
if (count($arr) == 1) {
$arr[] = $arr[0];
} else if (!empty($q)) {
$arr[1] = $arr[1] . '(' . $arr[0] . ')';
}
$out[] = [ 'key' => $arr[0], 'name' => $arr[1] ];
}
return ['errcode' => 0, 'data' => $out];
}
};