sqlacodegen 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqlacodegen/__init__.py +0 -0
- sqlacodegen/__main__.py +3 -0
- sqlacodegen/cli.py +115 -0
- sqlacodegen/generators.py +1512 -0
- sqlacodegen/models.py +79 -0
- sqlacodegen/py.typed +0 -0
- sqlacodegen/utils.py +204 -0
- sqlacodegen-3.0.0.dist-info/LICENSE +19 -0
- sqlacodegen-3.0.0.dist-info/METADATA +246 -0
- sqlacodegen-3.0.0.dist-info/RECORD +13 -0
- sqlacodegen-3.0.0.dist-info/WHEEL +5 -0
- sqlacodegen-3.0.0.dist-info/entry_points.txt +8 -0
- sqlacodegen-3.0.0.dist-info/top_level.txt +1 -0
sqlacodegen/__init__.py
ADDED
|
File without changes
|
sqlacodegen/__main__.py
ADDED
sqlacodegen/cli.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import sys
|
|
5
|
+
from contextlib import ExitStack
|
|
6
|
+
from typing import TextIO
|
|
7
|
+
|
|
8
|
+
from sqlalchemy.engine import create_engine
|
|
9
|
+
from sqlalchemy.schema import MetaData
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
import citext
|
|
13
|
+
except ImportError:
|
|
14
|
+
citext = None
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
import geoalchemy2
|
|
18
|
+
except ImportError:
|
|
19
|
+
geoalchemy2 = None
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
import pgvector.sqlalchemy
|
|
23
|
+
except ImportError:
|
|
24
|
+
pgvector = None
|
|
25
|
+
|
|
26
|
+
if sys.version_info < (3, 10):
|
|
27
|
+
from importlib_metadata import entry_points, version
|
|
28
|
+
else:
|
|
29
|
+
from importlib.metadata import entry_points, version
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def main() -> None:
|
|
33
|
+
generators = {ep.name: ep for ep in entry_points(group="sqlacodegen.generators")}
|
|
34
|
+
parser = argparse.ArgumentParser(
|
|
35
|
+
description="Generates SQLAlchemy model code from an existing database."
|
|
36
|
+
)
|
|
37
|
+
parser.add_argument("url", nargs="?", help="SQLAlchemy url to the database")
|
|
38
|
+
parser.add_argument(
|
|
39
|
+
"--options", help="options (comma-delimited) passed to the generator class"
|
|
40
|
+
)
|
|
41
|
+
parser.add_argument(
|
|
42
|
+
"--version", action="store_true", help="print the version number and exit"
|
|
43
|
+
)
|
|
44
|
+
parser.add_argument(
|
|
45
|
+
"--schemas", help="load tables from the given schemas (comma-delimited)"
|
|
46
|
+
)
|
|
47
|
+
parser.add_argument(
|
|
48
|
+
"--generator",
|
|
49
|
+
choices=generators,
|
|
50
|
+
default="declarative",
|
|
51
|
+
help="generator class to use",
|
|
52
|
+
)
|
|
53
|
+
parser.add_argument(
|
|
54
|
+
"--tables", help="tables to process (comma-delimited, default: all)"
|
|
55
|
+
)
|
|
56
|
+
parser.add_argument(
|
|
57
|
+
"--noviews",
|
|
58
|
+
action="store_true",
|
|
59
|
+
help="ignore views (always true for sqlmodels generator)",
|
|
60
|
+
)
|
|
61
|
+
parser.add_argument("--outfile", help="file to write output to (default: stdout)")
|
|
62
|
+
args = parser.parse_args()
|
|
63
|
+
|
|
64
|
+
if args.version:
|
|
65
|
+
print(version("sqlacodegen"))
|
|
66
|
+
return
|
|
67
|
+
|
|
68
|
+
if not args.url:
|
|
69
|
+
print("You must supply a url\n", file=sys.stderr)
|
|
70
|
+
parser.print_help()
|
|
71
|
+
return
|
|
72
|
+
|
|
73
|
+
if citext:
|
|
74
|
+
print(f"Using sqlalchemy-citext {version('citext')}")
|
|
75
|
+
|
|
76
|
+
if geoalchemy2:
|
|
77
|
+
print(f"Using geoalchemy2 {version('geoalchemy2')}")
|
|
78
|
+
|
|
79
|
+
if pgvector:
|
|
80
|
+
print(f"Using pgvector {version('pgvector')}")
|
|
81
|
+
|
|
82
|
+
# Use reflection to fill in the metadata
|
|
83
|
+
engine = create_engine(args.url)
|
|
84
|
+
metadata = MetaData()
|
|
85
|
+
tables = args.tables.split(",") if args.tables else None
|
|
86
|
+
schemas = args.schemas.split(",") if args.schemas else [None]
|
|
87
|
+
options = set(args.options.split(",")) if args.options else set()
|
|
88
|
+
|
|
89
|
+
# Instantiate the generator
|
|
90
|
+
generator_class = generators[args.generator].load()
|
|
91
|
+
generator = generator_class(metadata, engine, options)
|
|
92
|
+
|
|
93
|
+
if not generator.views_supported:
|
|
94
|
+
name = generator_class.__name__
|
|
95
|
+
print(
|
|
96
|
+
f"VIEW models will not be generated when using the '{name}' generator",
|
|
97
|
+
file=sys.stderr,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
for schema in schemas:
|
|
101
|
+
metadata.reflect(
|
|
102
|
+
engine, schema, (generator.views_supported and not args.noviews), tables
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
# Open the target file (if given)
|
|
106
|
+
with ExitStack() as stack:
|
|
107
|
+
outfile: TextIO
|
|
108
|
+
if args.outfile:
|
|
109
|
+
outfile = open(args.outfile, "w", encoding="utf-8")
|
|
110
|
+
stack.enter_context(outfile)
|
|
111
|
+
else:
|
|
112
|
+
outfile = sys.stdout
|
|
113
|
+
|
|
114
|
+
# Write the generated model code to the specified file or standard output
|
|
115
|
+
outfile.write(generator.generate())
|