import argparse
import json
import os
import sys
for _stream in (sys.stdout, sys.stderr):
try:
_stream.reconfigure(encoding="utf-8", errors="replace")
except Exception:
pass
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _db_client import preflight, emit, classify_error, failure
def explain_sql(sql: str, static_only: bool = False) -> dict:
try:
result = preflight(sql, static_only=static_only)
if result.get("ok") is False:
return failure("SQL_ERROR", str(result.get("error")), stage="SQL_EXPLAIN")
plan = "\n".join(str(r[0]) for r in result.get("explain", []) if r)
return {**result, "ok": True, "plan": plan, "estimated_rows": "unknown"}
except Exception as exc:
kind, why, retry = classify_error(exc)
return failure(kind, why, stage="SQL_EXPLAIN", retry=retry)
def main():
parser = argparse.ArgumentParser(description="SQL explain plan precheck")
parser.add_argument("--sql", required=True)
parser.add_argument("--static-only", action="store_true", help="Validate syntax without database execution")
args = parser.parse_args()
result = explain_sql(args.sql, static_only=args.static_only)
if not result.get("ok"):
print(json.dumps(result, ensure_ascii=True, default=str), file=sys.stderr)
sys.exit(1)
emit(result)
if __name__ == "__main__":
main()