@@ -10,7 +10,162 @@ meta: Python example code for the ARRAY constant from the sqlalchemy.dialects.po
1010ARRAY is a constant within the sqlalchemy.dialects.postgresql module of the SQLAlchemy project.
1111
1212
13- ## Example 1 from sqlalchemy-utils
13+ ## Example 1 from sqlacodegen
14+ [ sqlacodegen] ( https://github.com/agronholm/sqlacodegen )
15+ ([ PyPI package information] ( https://pypi.org/project/sqlacodegen/ ) )
16+ is a tool for
17+ reading from an existing [ relational database] ( /databases.html ) to
18+ generate code to create [ SQLAlchemy] ( /sqlalchemy.html ) models based
19+ on that database. The project is primarily written and maintained
20+ by [ Alex Grönholm (agronholm)] ( https://github.com/agronholm ) and it
21+ is open sourced under the
22+ [ MIT license] ( https://github.com/agronholm/sqlacodegen/blob/master/LICENSE ) .
23+
24+ [ ** sqlacodegen / sqlacodegen / codegen.py** ] ( https://github.com/agronholm/sqlacodegen/blob/master/sqlacodegen/./codegen.py )
25+
26+ ``` python
27+ # codegen.py
28+ from __future__ import unicode_literals, division, print_function, absolute_import
29+
30+ import inspect
31+ import re
32+ import sys
33+ from collections import defaultdict
34+ from importlib import import_module
35+ from inspect import ArgSpec
36+ from keyword import iskeyword
37+
38+ import sqlalchemy
39+ import sqlalchemy.exc
40+ from sqlalchemy import (
41+ Enum, ForeignKeyConstraint, PrimaryKeyConstraint, CheckConstraint, UniqueConstraint, Table,
42+ Column, Float)
43+ from sqlalchemy.schema import ForeignKey
44+ from sqlalchemy.sql.sqltypes import NullType
45+ from sqlalchemy.types import Boolean, String
46+ from sqlalchemy.util import OrderedDict
47+
48+ try :
49+ from sqlalchemy import ARRAY
50+ except ImportError :
51+ ~~ from sqlalchemy.dialects.postgresql import ARRAY
52+
53+ try :
54+ from sqlalchemy import Computed
55+ except ImportError :
56+ Computed = None
57+
58+ try :
59+ import geoalchemy2 # noqa: F401
60+ except ImportError :
61+ pass
62+
63+ _re_boolean_check_constraint = re.compile(r " (?:(?: . *? ) \. ) ? ( . *? ) IN \( 0, 1\) " )
64+ _re_column_name = re.compile(r ' (?: ( ["` ]? ) (?: . * ) \1 \. ) ? ( ["` ]? ) ( . * ) \2 ' )
65+ _re_enum_check_constraint = re.compile(r " (?:(?: . *? ) \. ) ? ( . *? ) IN \( ( . + ) \) " )
66+ _re_enum_item = re.compile(r " '( . *? ) (?<! \\ ) '" )
67+ _re_invalid_identifier = re.compile(r ' [^ a-zA-Z0-9_ ]' if sys.version_info[0 ] < 3 else r ' (?u) \W ' )
68+
69+
70+ class _DummyInflectEngine (object ):
71+ @ staticmethod
72+ def singular_noun (noun ):
73+ return noun
74+
75+
76+
77+
78+ # # ... source file abbreviated to get to ARRAY examples ...
79+
80+
81+ names.add(name)
82+
83+
84+ class Model (object ):
85+ def __init__ (self , table ):
86+ super (Model, self ).__init__ ()
87+ self .table = table
88+ self .schema = table.schema
89+
90+ for column in table.columns:
91+ if not isinstance (column.type, NullType):
92+ column.type = self ._get_adapted_type(column.type, column.table.bind)
93+
94+ def _get_adapted_type (self , coltype , bind ):
95+ compiled_type = coltype.compile(bind.dialect)
96+ for supercls in coltype.__class__ .__mro__ :
97+ if not supercls.__name__ .startswith(' _' ) and hasattr (supercls, ' __visit_name__' ):
98+ kw = {}
99+ if supercls is Enum:
100+ kw[' name' ] = coltype.name
101+
102+ new_coltype = coltype.adapt(supercls)
103+ for key, value in kw.items():
104+ setattr (new_coltype, key, value)
105+
106+ ~~ if isinstance (coltype, ARRAY ):
107+ new_coltype.item_type = self ._get_adapted_type(new_coltype.item_type, bind)
108+
109+ try :
110+ if new_coltype.compile(bind.dialect) != compiled_type:
111+ if not isinstance (new_coltype, Float) and \
112+ not (isinstance (new_coltype, ARRAY ) and
113+ isinstance (new_coltype.item_type, Float)):
114+ break
115+ except sqlalchemy.exc.CompileError:
116+ break
117+
118+ coltype = new_coltype
119+ if supercls.__name__ != supercls.__name__ .upper():
120+ break
121+
122+ return coltype
123+
124+ def add_imports (self , collector ):
125+ if self .table.columns:
126+ collector.add_import(Column)
127+
128+ for column in self .table.columns:
129+ collector.add_import(column.type)
130+ if column.server_default:
131+ if isinstance (column.server_default, Computed):
132+ collector.add_literal_import(' sqlalchemy' , ' Computed' )
133+ else :
134+ collector.add_literal_import(' sqlalchemy' , ' text' )
135+
136+ ~~ if isinstance (column.type, ARRAY ):
137+ collector.add_import(column.type.item_type.__class__ )
138+
139+ for constraint in sorted (self .table.constraints, key = _get_constraint_sort_key):
140+ if isinstance (constraint, ForeignKeyConstraint):
141+ if len (constraint.columns) > 1 :
142+ collector.add_literal_import(' sqlalchemy' , ' ForeignKeyConstraint' )
143+ else :
144+ collector.add_literal_import(' sqlalchemy' , ' ForeignKey' )
145+ elif isinstance (constraint, UniqueConstraint):
146+ if len (constraint.columns) > 1 :
147+ collector.add_literal_import(' sqlalchemy' , ' UniqueConstraint' )
148+ elif not isinstance (constraint, PrimaryKeyConstraint):
149+ collector.add_import(constraint)
150+
151+ for index in self .table.indexes:
152+ if len (index.columns) > 1 :
153+ collector.add_import(index)
154+
155+ @ staticmethod
156+ def _convert_to_valid_identifier (name ):
157+ assert name, ' Identifier cannot be empty'
158+ if name[0 ].isdigit() or iskeyword(name):
159+ name = ' _' + name
160+ elif name == ' metadata' :
161+
162+
163+ # # ... source file continues with no further ARRAY examples...
164+
165+ ```
166+
167+
168+ ## Example 2 from sqlalchemy-utils
14169[ sqlalchemy-utils] ( https://github.com/kvesteri/sqlalchemy-utils )
15170([ project documentation] ( https://sqlalchemy-utils.readthedocs.io/en/latest/ )
16171and
0 commit comments