Coverage for src / sql_tool / core / query_source.py: 100%
17 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-14 15:28 -0500
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-14 15:28 -0500
1"""Query source resolution for SQL Tool.
3Resolves the SQL query text from one of three sources:
41. Inline (-e flag) — highest priority
52. File path — middle priority
63. stdin — lowest priority
7"""
9from __future__ import annotations
11import sys
12from pathlib import Path
14from sql_tool.core.exceptions import InputError
17def resolve_query_source(
18 inline: str | None,
19 file_path: str | None,
20) -> str:
21 """Resolve SQL query from inline, file, or stdin.
23 Precedence: inline > file > stdin.
24 Raises InputError when no source is available.
25 """
26 if inline is not None:
27 return inline
29 if file_path is not None:
30 p = Path(file_path)
31 if not p.exists():
32 msg = (
33 f"Query file not found: {file_path}\n"
34 "Use -e for inline queries or pipe query via stdin."
35 )
36 raise InputError(msg)
37 return p.read_text()
39 if not sys.stdin.isatty():
40 return sys.stdin.read()
42 msg = "No query provided. Use -e, file path, or pipe to stdin."
43 raise InputError(msg)