-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlexec.php
More file actions
178 lines (167 loc) · 4.33 KB
/
sqlexec.php
File metadata and controls
178 lines (167 loc) · 4.33 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
<?php
// テキストタイプ
define("TEXT_TYPE_NO_RESULT", 0); // 結果無し
define("TEXT_TYPE_EXEC_RESULT", 1); // 実行結果
define("TEXT_TYPE_QUERY_RESULT", 2); // 検索結果
define("TEXT_TYPE_ERROR", -1); // エラー
/**
* SQLスクリプトの実行
*
* @param PDO $db PDOオブジェクト
* @param string[] $sql_text SQL文の配列
* @return array 実行結果
*/
function executeSqlScript(PDO $db, array $sql_text=[])
{
$json = new stdClass();
$json->error = 0;
$json->execTime = '0';
$json->lines = [];
$sqltime = 0;
foreach( $sql_text as $sql )
{
try
{
// 空行の出力
if ($sql && ($sql[0]=='#'))
{
$json->lines[] = OutputNoResult(substr($sql,1));
continue;
}
// 特別なEVAL文の実行
if ( preg_match( "/^eval\s+(.+)/i", $sql, $reg ) )
{
ob_start();
$ret = eval("{$reg[1]};");
if (isset($ret))
{
$json->lines[] = OutputExecResult($sql, $ret);
ob_end_clean();
}
else
{
$out = ob_get_clean();
if ($out)
{
$json->lines[] = OutputExecResult($sql, $out);
}
else
{
$json->lines[] = OutputNoResult($sql);
}
}
continue;
}
// SELECT文と非SELECT文で処理を分ける
$time1 = microtime_as_float();
if (preg_match("/^(select|show)\s/i", $sql))
{
$res = DoSelect($db, $sql);
$json->lines[] = $res;
if ($res->type==TEXT_TYPE_ERROR) break;
}
elseif($sql)
{
$db->exec($sql);
$info = $db->errorInfo();
if(!empty($info) && $info[0]!=='00000')
{
$json->lines[] = OutputError($sql, $info[2]??'exec() error');
break;
}
$json->lines[] = OutputExecResult($sql, 'ok');
}
$time2 = microtime_as_float();
$sqltime += ($time2-$time1);
}
catch(Throwable $e)
{
$json->lines[] = OutputError($sql, $e->getMessage());
break;
}
}
$json->execTime = sprintf('%01.03f', $sqltime);
return $json;
}
/**
* マイクロ秒取得
*
* @return float
*/
function microtime_as_float()
{
list($usec, $sec) = explode(' ', microtime());
return ((float)$sec + (float)$usec);
}
/**
* SELECT文の実行
*
* @param PDO $db PDOオブジェクト
* @param string $sql SQL文
* @return stdClass JSON Line
*/
function DoSelect(PDO $db, string $sql)
{
// 検索実行
$sth= $db->query($sql);
if($sth===false)
{
$info = $db->errorInfo();
return OutputError($sql, $info[2]??'query() error');
}
$arr=$sth->fetchAll(PDO::FETCH_ASSOC);
if (!empty($arr))
{
$line = outputJsonLine($sql, TEXT_TYPE_QUERY_RESULT, $arr);
}
else
{
$line = outputJsonLine($sql, TEXT_TYPE_QUERY_RESULT, []);
}
return $line;
}
/**
* 結果無しの出力
*
* @param string $command コマンド
*/
function OutputNoResult($command)
{
return outputJsonLine($command, TEXT_TYPE_NO_RESULT);
}
/**
* 実行結果の出力
*
* @param string $command コマンド
* @param string $result 結果
*/
function OutputExecResult(string $command, string $result)
{
return outputJsonLine($command, TEXT_TYPE_EXEC_RESULT, $result);
}
/**
* エラーの出力
*
* @param string $command コマンド
* @param string $result 結果
*/
function OutputError(string $command, string $result)
{
return outputJsonLine($command, TEXT_TYPE_EXEC_RESULT, $result);
}
/**
* JSON result出力
*
* @param string $command コマンド
* @param int $type 結果タイプ
* @param string $result 結果データ
* @return stdClass JSON Line
*/
function outputJsonLine(string $command, int $type, $result=null)
{
$obj = new stdClass();
$obj->command = $command;
$obj->type = $type;
$obj->result = $result;
return $obj;
}