Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions sqlalchemy_cauldron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@
__author__ = "Michael Faust"

__all__ = [
"Dialect",
"Generator",
# Configuration
"GeneratorConfig",
# Data structures
"ParseResult",
"SchemaExtractResult",
"__version__",
# Main functions
"brew",
"normalize_and_parse",
"extract_schema_and_tables",
# Data structures
"ParseResult",
"SchemaExtractResult",
# Configuration
"GeneratorConfig",
"Generator",
"Dialect",
"normalize_and_parse",
]
42 changes: 24 additions & 18 deletions sqlalchemy_cauldron/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ class SchemaOutput:
)


def version_callback(value: bool):
def version_callback(value: bool) -> None:
"""Callback to display version information and exit."""
if value:
print(f"SQLAlchemy-Cauldron {package_version}")
raise typer.Exit()
typer.echo(f"sqlalchemy-cauldron {package_version}")
raise typer.Exit


def get_sql(sql: str | None, file: str | None) -> str:
Expand All @@ -55,7 +56,7 @@ def get_sql(sql: str | None, file: str | None) -> str:
typer.Exit: If neither sql nor file provided
"""
if file:
return Path(file).read_text()
return Path(file).read_text(encoding="utf-8")
if sql:
return sql
typer.echo("Error: Provide SQL as argument or use --file", err=True)
Expand All @@ -78,7 +79,7 @@ def output_code(
success_msg: Success message to display when writing to file.
"""
if output:
Path(output).write_text(code)
Path(output).write_text(code, encoding="utf-8")
typer.echo(f"[OK] {success_msg} written to {output}")
else:
typer.echo(code)
Expand All @@ -90,15 +91,17 @@ def main(
bool | None,
typer.Option("--version", "-V", callback=version_callback, help="Show the version and exit"),
] = None,
): ...
) -> None:
"""Main entry point for the CLI."""


@app.command()
def orm(
sql: Annotated[str | None, typer.Argument(help="SQL query string")] = None,
file: Annotated[str | None, typer.Option("--file", "-f", help="Read SQL from file")] = None,
dialect: Annotated[
str, typer.Option("--dialect", "-d", help=f"SQL dialect (default: {Dialect.POSTGRES})")
str,
typer.Option("--dialect", "-d", help=f"SQL dialect (default: {Dialect.POSTGRES})"),
] = Dialect.POSTGRES.value,
optimize: Annotated[bool, typer.Option("--optimize", help="Optimize SQL before generation")] = False,
resolve_aliases: Annotated[bool, typer.Option("--resolve-aliases", help="Resolve table aliases")] = False,
Expand Down Expand Up @@ -135,19 +138,19 @@ def orm(
output_code(code, output, "ORM code")
except Exception as e:
typer.echo(f"Error: {e}", err=True)
raise typer.Exit(code=1)
raise typer.Exit(code=1) from None


@app.command()
def core(
sql: Annotated[str | None, typer.Argument(help="SQL query string")] = None,
file: Annotated[str | None, typer.Option("--file", "-f", help="Read SQL from file")] = None,
dialect: Annotated[
str, typer.Option("--dialect", "-d", help=f"SQL dialect (default: {Dialect.POSTGRES})")
str,
typer.Option("--dialect", "-d", help=f"SQL dialect (default: {Dialect.POSTGRES})"),
] = Dialect.POSTGRES.value,
optimize: Annotated[bool, typer.Option("--optimize", help="Optimize SQL before generation")] = False,
resolve_aliases: Annotated[bool, typer.Option("--resolve-aliases", help="Resolve table aliases")] = False,
include_test: Annotated[bool, typer.Option("--include-test", help="Include self-contained test code")] = False,
output: Annotated[str | None, typer.Option("--output", "-o", help="Write to file instead of stdout")] = None,
) -> None:
"""Generate SQLAlchemy CORE code from SQL.
Expand Down Expand Up @@ -178,15 +181,16 @@ def core(
output_code(code, output, "CORE code")
except Exception as e:
typer.echo(f"Error: {e}", err=True)
raise typer.Exit(code=1)
raise typer.Exit(code=1) from None


@app.command()
def normalize(
sql: Annotated[str | None, typer.Argument(help="SQL query string")] = None,
file: Annotated[str | None, typer.Option("--file", "-f", help="Read SQL from file")] = None,
dialect: Annotated[
str, typer.Option("--dialect", "-d", help=f"SQL dialect (default: {Dialect.POSTGRES})")
str,
typer.Option("--dialect", "-d", help=f"SQL dialect (default: {Dialect.POSTGRES})"),
] = Dialect.POSTGRES.value,
optimize: Annotated[bool, typer.Option("--optimize", help="Optimize SQL before generation")] = False,
resolve_aliases: Annotated[bool, typer.Option("--resolve-aliases", help="Resolve table aliases")] = False,
Expand All @@ -211,15 +215,16 @@ def normalize(
output_code(sql_out, output, "Normalized SQL")
except Exception as e:
typer.echo(f"Error: {e}", err=True)
raise typer.Exit(code=1)
raise typer.Exit(code=1) from None


@app.command()
def schema(
sql: Annotated[str | None, typer.Argument(help="SQL query string")] = None,
file: Annotated[str | None, typer.Option("--file", "-f", help="Read SQL from file")] = None,
dialect: Annotated[
str, typer.Option("--dialect", "-d", help=f"SQL dialect (default: {Dialect.POSTGRES})")
str,
typer.Option("--dialect", "-d", help=f"SQL dialect (default: {Dialect.POSTGRES})"),
] = Dialect.POSTGRES.value,
output: Annotated[str | None, typer.Option("--output", "-o", help="Write to file instead of stdout")] = None,
) -> None:
Expand Down Expand Up @@ -252,15 +257,16 @@ def schema(
output_code(code, output, "Schema")
except Exception as e:
typer.echo(f"Error: {e}", err=True)
raise typer.Exit(code=1)
raise typer.Exit(code=1) from None


@app.command(name="json")
def schema_json(
sql: Annotated[str | None, typer.Argument(help="SQL query string")] = None,
file: Annotated[str | None, typer.Option("--file", "-f", help="Read SQL from file")] = None,
dialect: Annotated[
str, typer.Option("--dialect", "-d", help=f"SQL dialect (default: {Dialect.POSTGRES})")
str,
typer.Option("--dialect", "-d", help=f"SQL dialect (default: {Dialect.POSTGRES})"),
] = Dialect.POSTGRES.value,
output: Annotated[str | None, typer.Option("--output", "-o", help="Write to file instead of stdout")] = None,
) -> None:
Expand All @@ -287,14 +293,14 @@ def schema_json(
"columns": [col.name for col in (table_info.columns or [])],
}
for table_info in schema_result.table_infos
]
],
)

json_output = json.dumps(schema_data.__dict__, indent=2)
output_code(json_output, output, "Schema JSON")
except Exception as e:
typer.echo(f"Error: {e}", err=True)
raise typer.Exit(code=1)
raise typer.Exit(code=1) from None


if __name__ == "__main__":
Expand Down
5 changes: 4 additions & 1 deletion sqlalchemy_cauldron/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

from dataclasses import dataclass
from enum import StrEnum
from pathlib import Path
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pathlib import Path

FALLBACK_PK: str = "recid"

Expand Down
Loading