-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute_sql.py
More file actions
271 lines (217 loc) · 8.43 KB
/
Copy pathexecute_sql.py
File metadata and controls
271 lines (217 loc) · 8.43 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
269
270
271
#!/usr/bin/env python3
"""ESB2 NL2SQL — SQL Executor
Reads a SELECT SQL statement from stdin, executes it against the ESB2 MySQL
database, and outputs structured JSON to stdout.
Safety: Only SELECT statements are permitted. A LIMIT clause is appended
automatically if the query lacks one.
Configuration — priority (highest to lowest):
1. Environment variables (DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME,
DB_TIMEOUT, ROW_LIMIT)
2. External config file: db_config.json (JSON, alongside this script)
3. Hardcoded defaults (auto-detect WSL2 gateway for host)
Usage:
python3 execute_sql.py <<'EOSQL'
SELECT realname, department FROM zzvc_user WHERE realname = '肖少峰'
EOSQL
"""
import json
import os
import re
import subprocess
import sys
from pathlib import Path
import pymysql
from pymysql.cursors import DictCursor
# ---------------------------------------------------------------------------
# Configuration loader — reads from external db_config.json, env vars override
# ---------------------------------------------------------------------------
# Path to the config file (same directory as this script)
_CONFIG_DIR = Path(__file__).resolve().parent
_CONFIG_PATH = _CONFIG_DIR / "db_config.json"
def _detect_wsl_gateway():
"""Auto-detect the WSL2 gateway IP (the Windows host)."""
try:
result = subprocess.run(
["ip", "route", "show", "default"],
capture_output=True, text=True, timeout=3,
)
for line in result.stdout.strip().splitlines():
parts = line.split()
# "default via 172.22.112.1 dev eth0 ..."
if len(parts) >= 3 and parts[0] == "default" and parts[1] == "via":
return parts[2]
except Exception:
pass
return "127.0.0.1"
def _load_file_config():
"""Load DB configuration from the external JSON file.
Returns a dict of key-value pairs read from db_config.json, or an empty
dict if the file is missing or unreadable. Never raises — a missing or
malformed config file degrades gracefully to defaults.
"""
if not _CONFIG_PATH.is_file():
return {}
try:
text = _CONFIG_PATH.read_text(encoding="utf-8")
cfg = json.loads(text)
if not isinstance(cfg, dict):
return {}
return cfg
except (json.JSONDecodeError, OSError):
return {}
def _build_db_config():
"""Merge file config, env vars, and hardcoded defaults into DB_CONFIG.
Priority (highest to lowest):
1. Environment variable (if set)
2. Value from db_config.json
3. Hardcoded default (or WSL2 gateway auto-detection for host)
"""
file_cfg = _load_file_config()
# Mapping: env_var_name → (config_file_key, default_value)
# Use a factory for dynamic defaults (e.g. gateway detection).
_SPEC = [
("DB_HOST", "host", _detect_wsl_gateway),
("DB_PORT", "port", 3306),
("DB_USER", "user", "root"),
("DB_PASSWORD", "password", "root"),
("DB_NAME", "database", "esb2"),
("DB_TIMEOUT", "connect_timeout", 5),
("DB_CHARSET", "charset", "utf8mb4"),
]
cfg = {}
for env_key, file_key, default in _SPEC:
env_val = os.environ.get(env_key)
if env_val is not None:
cfg[file_key] = env_val
elif file_key in file_cfg and file_cfg[file_key] is not None:
cfg[file_key] = file_cfg[file_key]
else:
cfg[file_key] = default() if callable(default) else default
# Coerce integer fields
for int_key in ("port", "connect_timeout"):
cfg[int_key] = int(cfg[int_key])
return cfg
DB_CONFIG = _build_db_config()
ROW_LIMIT = int(os.environ.get("ROW_LIMIT")
or _load_file_config().get("row_limit", 200))
# ---------------------------------------------------------------------------
# SQL safety — read‑only enforcement
# ---------------------------------------------------------------------------
# Blacklist of SQL prefixes that must be rejected
_FORBIDDEN_PREFIXES = [
"INSERT", "UPDATE", "DELETE", "DROP", "ALTER", "TRUNCATE",
"CREATE", "REPLACE", "RENAME", "CALL", "LOAD", "GRANT",
"REVOKE", "SET", "EXECUTE", "EXEC", "FLUSH", "KILL",
"SHUTDOWN", "LOCK", "UNLOCK", "HANDLER", "INSTALL",
"UNINSTALL", "OPTIMIZE", "REPAIR", "ANALYZE", "CHECK",
"ASSIGN", "CACHE", "RESET", "PURGE", "CHANGE", "STOP",
"START", "XA", "SAVEPOINT", "ROLLBACK", "COMMIT", "BEGIN",
"DESCRIBE", "DESC", "EXPLAIN", "SHOW",
]
def _strip_comments(sql):
"""Remove /* block comments */, -- line comments, and # line comments."""
sql = re.sub(r"/\*.*?\*/", "", sql, flags=re.DOTALL)
sql = re.sub(r"--[^\n]*", "", sql)
sql = re.sub(r"#[^\n]*", "", sql)
return sql
def _first_keyword(sql):
"""Return the first SQL keyword after stripping comments and whitespace."""
cleaned = _strip_comments(sql).strip()
match = re.match(r"(\w+)", cleaned, re.IGNORECASE)
return match.group(0).upper() if match else ""
def _has_limit(sql):
"""Check if a SELECT statement already contains a LIMIT clause."""
cleaned = _strip_comments(sql)
return bool(re.search(r"\bLIMIT\s+\d+", cleaned, re.IGNORECASE))
def _validate_select(sql):
"""Raise ValueError if *sql* is not a SELECT statement."""
keyword = _first_keyword(sql)
if not keyword:
raise ValueError("Empty SQL input")
if keyword in _FORBIDDEN_PREFIXES:
raise ValueError(
f"Only SELECT queries are allowed. Got: {keyword}"
)
def _ensure_limit(sql):
"""Append LIMIT if the SELECT lacks one."""
if _has_limit(sql):
return sql
return sql.rstrip().rstrip(";").rstrip() + f"\nLIMIT {ROW_LIMIT}"
# ---------------------------------------------------------------------------
# Main execution
# ---------------------------------------------------------------------------
def main():
# 1. Read SQL from stdin
sql = sys.stdin.read().strip()
if not sql:
output_error("Empty SQL input — nothing to execute", -1, 0.0)
return
# 2. Safety check
try:
_validate_select(sql)
except ValueError as exc:
output_error(str(exc), -1, 0.0)
return
# 3. Auto-append LIMIT for bounded queries
sql = _ensure_limit(sql)
# 4. Connect & execute
import time
t0 = time.time()
try:
conn = pymysql.connect(**DB_CONFIG)
except pymysql.MySQLError as exc:
elapsed = time.time() - t0
output_error(
f"Database connection failed — host={DB_CONFIG['host']}:{DB_CONFIG['port']} "
f"error=({exc.args[0]}) {exc.args[1] if len(exc.args) > 1 else ''}",
exc.args[0] if exc.args else -2,
round(elapsed, 4),
)
return
try:
with conn:
with conn.cursor(DictCursor) as cur:
cur.execute(sql)
rows = cur.fetchall()
columns = [desc[0] for desc in cur.description] if cur.description else []
elapsed = round(time.time() - t0, 4)
row_count = len(rows)
truncated = row_count >= ROW_LIMIT
result = {
"success": True,
"columns": columns,
"rows": rows,
"row_count": row_count,
"truncated": truncated,
"elapsed_seconds": elapsed,
}
# PyMySQL’s default JSON encoder struggles with Decimal / bytes / date
print(json.dumps(result, ensure_ascii=False, default=_json_default))
except pymysql.MySQLError as exc:
elapsed = round(time.time() - t0, 4)
output_error(
f"SQL execution error — ({exc.args[0]}) {exc.args[1] if len(exc.args) > 1 else ''}",
exc.args[0] if exc.args else -3,
elapsed,
)
def _json_default(obj):
"""Fallback serializer for types that json.dumps can't handle natively."""
if hasattr(obj, "isoformat"):
return obj.isoformat()
if isinstance(obj, bytes):
return obj.decode("utf-8", errors="replace")
if isinstance(obj, set):
return list(obj)
return str(obj)
def output_error(message, code, elapsed):
"""Print a structured error result to stdout and exit."""
result = {
"success": False,
"error": message,
"error_code": code,
"elapsed_seconds": elapsed,
}
print(json.dumps(result, ensure_ascii=False))
sys.exit(1)
if __name__ == "__main__":
main()