Skip to content

Commit 8674168

Browse files
committed
Add cli
1 parent 988f03a commit 8674168

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import asyncio
2+
from importlib import metadata
3+
from pathlib import Path
4+
from typing import Annotated
5+
from text_2_sql_core.utils.database import DatabaseEngine
6+
7+
import typer
8+
from rich import print as rich_print
9+
10+
cli = typer.Typer(pretty_exceptions_show_locals=False, no_args_is_help=True)
11+
12+
__version__ = metadata.version(__package__)
13+
14+
15+
def version_callback(value: bool) -> None:
16+
"""Print the version of the CLI."""
17+
if value:
18+
print(__version__)
19+
raise typer.Exit()
20+
21+
22+
@cli.callback()
23+
def callback(
24+
_: bool = typer.Option(None, "--version", "-v", callback=version_callback)
25+
) -> None:
26+
"""Text2SQL Data Dictionary Creator CLI."""
27+
28+
29+
@cli.command()
30+
def create(
31+
engine: DatabaseEngine,
32+
output_directory: Annotated[
33+
Path | None,
34+
typer.Option(
35+
"--output-directory",
36+
"-o",
37+
help="Optional directory that the script will write the output files to.",
38+
show_default=False,
39+
exists=True,
40+
file_okay=True,
41+
dir_okay=False,
42+
writable=True,
43+
readable=True,
44+
resolve_path=True,
45+
),
46+
] = None,
47+
single_file: Annotated[
48+
bool,
49+
typer.Option(
50+
"--single_file",
51+
"-s",
52+
help="Optional flag that writes all schemas to a single file.",
53+
),
54+
] = False,
55+
) -> None:
56+
"""Execute a Text2SQL Data Dictionary Creator YAML file.
57+
58+
Args:
59+
----
60+
engine (DatabaseEngine):
61+
62+
Returns:
63+
-------
64+
None
65+
66+
"""
67+
68+
try:
69+
if engine == DatabaseEngine.DATABRICKS:
70+
from text_2_sql_core.data_dictionary.databricks_data_dictionary_creator import (
71+
DatabricksDataDictionaryCreator,
72+
)
73+
74+
data_dictionary_creator = DatabricksDataDictionaryCreator()
75+
elif engine == DatabaseEngine.SNOWFLAKE:
76+
from text_2_sql_core.data_dictionary.snowflake_data_dictionary_creator import (
77+
SnowflakeDataDictionaryCreator,
78+
)
79+
80+
data_dictionary_creator = SnowflakeDataDictionaryCreator()
81+
elif engine == DatabaseEngine.TSQL:
82+
from text_2_sql_core.data_dictionary.tsql_data_dictionary_creator import (
83+
TSQLDataDictionaryCreator,
84+
)
85+
86+
data_dictionary_creator = TSQLDataDictionaryCreator()
87+
except ImportError:
88+
detailed_error = f"""Failed to import {
89+
engine.value} Data Dictionary Creator. Check you have installed the optional dependencies for this database engine."""
90+
rich_print("Text2SQL Data Dictionary Creator Failed ❌")
91+
rich_print(detailed_error)
92+
93+
raise typer.Exit(code=1)
94+
95+
try:
96+
asyncio.run(data_dictionary_creator.create_data_dictionary())
97+
except Exception as e:
98+
rich_print("Text2SQL Data Dictionary Creator Failed ❌")
99+
100+
rich_print(f"Error Messages: {e}")
101+
102+
raise typer.Exit(code=1)
103+
else:
104+
rich_print("Text2SQL Data Dictionary Creator Completed Successfully ✅")

text_2_sql/text_2_sql_core/src/text_2_sql_core/data_dictionary/tsql_data_dictionary_creator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from text_2_sql_core.utils.database import DatabaseEngine
77

88

9-
class SqlServerDataDictionaryCreator(DataDictionaryCreator):
9+
class TSQLDataDictionaryCreator(DataDictionaryCreator):
1010
def __init__(
1111
self,
1212
entities: list[str] = None,
@@ -115,5 +115,5 @@ def extract_entity_relationships_sql_query(self) -> str:
115115

116116

117117
if __name__ == "__main__":
118-
data_dictionary_creator = SqlServerDataDictionaryCreator()
118+
data_dictionary_creator = TSQLDataDictionaryCreator()
119119
asyncio.run(data_dictionary_creator.create_data_dictionary())

text_2_sql/text_2_sql_core/src/text_2_sql_core/utils/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
class DatabaseEngine(StrEnum):
55
"""An enumeration to represent a database engine."""
66

7+
DATABRICKS = "DATABRICKS"
78
SNOWFLAKE = "SNOWFLAKE"
89
TSQL = "TSQL"
9-
DATABRICKS = "DATABRICKS"

0 commit comments

Comments
 (0)