masonite-framework-orm 3.0.1__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.
- masonite_framework_orm-3.0.1.dist-info/METADATA +87 -0
- masonite_framework_orm-3.0.1.dist-info/RECORD +116 -0
- masonite_framework_orm-3.0.1.dist-info/WHEEL +5 -0
- masonite_framework_orm-3.0.1.dist-info/entry_points.txt +3 -0
- masonite_framework_orm-3.0.1.dist-info/licenses/LICENSE +21 -0
- masonite_framework_orm-3.0.1.dist-info/top_level.txt +1 -0
- masoniteorm/__init__.py +1 -0
- masoniteorm/collection/Collection.py +605 -0
- masoniteorm/collection/__init__.py +1 -0
- masoniteorm/commands/CanOverrideConfig.py +16 -0
- masoniteorm/commands/CanOverrideOptionsDefault.py +22 -0
- masoniteorm/commands/Command.py +6 -0
- masoniteorm/commands/Entry.py +43 -0
- masoniteorm/commands/MakeMigrationCommand.py +57 -0
- masoniteorm/commands/MakeModelCommand.py +78 -0
- masoniteorm/commands/MakeModelDocstringCommand.py +37 -0
- masoniteorm/commands/MakeObserverCommand.py +54 -0
- masoniteorm/commands/MakeSeedCommand.py +54 -0
- masoniteorm/commands/MigrateCommand.py +46 -0
- masoniteorm/commands/MigrateFreshCommand.py +41 -0
- masoniteorm/commands/MigrateRefreshCommand.py +41 -0
- masoniteorm/commands/MigrateResetCommand.py +25 -0
- masoniteorm/commands/MigrateRollbackCommand.py +26 -0
- masoniteorm/commands/MigrateStatusCommand.py +51 -0
- masoniteorm/commands/SeedRunCommand.py +35 -0
- masoniteorm/commands/ShellCommand.py +205 -0
- masoniteorm/commands/__init__.py +18 -0
- masoniteorm/commands/stubs/create_migration.stub +20 -0
- masoniteorm/commands/stubs/create_seed.stub +9 -0
- masoniteorm/commands/stubs/model.stub +9 -0
- masoniteorm/commands/stubs/observer.stub +101 -0
- masoniteorm/commands/stubs/table_migration.stub +19 -0
- masoniteorm/config.py +123 -0
- masoniteorm/connections/BaseConnection.py +101 -0
- masoniteorm/connections/ConnectionFactory.py +59 -0
- masoniteorm/connections/ConnectionResolver.py +132 -0
- masoniteorm/connections/MSSQLConnection.py +176 -0
- masoniteorm/connections/MySQLConnection.py +232 -0
- masoniteorm/connections/PostgresConnection.py +225 -0
- masoniteorm/connections/SQLiteConnection.py +179 -0
- masoniteorm/connections/__init__.py +6 -0
- masoniteorm/exceptions.py +38 -0
- masoniteorm/expressions/__init__.py +1 -0
- masoniteorm/expressions/expressions.py +288 -0
- masoniteorm/factories/Factory.py +112 -0
- masoniteorm/factories/__init__.py +1 -0
- masoniteorm/helpers/__init__.py +0 -0
- masoniteorm/helpers/misc.py +22 -0
- masoniteorm/migrations/Migration.py +330 -0
- masoniteorm/migrations/__init__.py +1 -0
- masoniteorm/models/MigrationModel.py +9 -0
- masoniteorm/models/Model.py +1209 -0
- masoniteorm/models/Model.pyi +1366 -0
- masoniteorm/models/Pivot.py +5 -0
- masoniteorm/models/__init__.py +1 -0
- masoniteorm/observers/ObservesEvents.py +27 -0
- masoniteorm/observers/__init__.py +1 -0
- masoniteorm/pagination/BasePaginator.py +10 -0
- masoniteorm/pagination/LengthAwarePaginator.py +34 -0
- masoniteorm/pagination/SimplePaginator.py +28 -0
- masoniteorm/pagination/__init__.py +2 -0
- masoniteorm/providers/ORMProvider.py +39 -0
- masoniteorm/providers/__init__.py +1 -0
- masoniteorm/query/EagerRelation.py +42 -0
- masoniteorm/query/QueryBuilder.py +2486 -0
- masoniteorm/query/__init__.py +1 -0
- masoniteorm/query/grammars/BaseGrammar.py +1027 -0
- masoniteorm/query/grammars/MSSQLGrammar.py +194 -0
- masoniteorm/query/grammars/MySQLGrammar.py +238 -0
- masoniteorm/query/grammars/PostgresGrammar.py +213 -0
- masoniteorm/query/grammars/SQLiteGrammar.py +228 -0
- masoniteorm/query/grammars/__init__.py +4 -0
- masoniteorm/query/processors/MSSQLPostProcessor.py +58 -0
- masoniteorm/query/processors/MySQLPostProcessor.py +48 -0
- masoniteorm/query/processors/PostgresPostProcessor.py +49 -0
- masoniteorm/query/processors/SQLitePostProcessor.py +49 -0
- masoniteorm/query/processors/__init__.py +4 -0
- masoniteorm/relationships/BaseRelationship.py +161 -0
- masoniteorm/relationships/BelongsTo.py +124 -0
- masoniteorm/relationships/BelongsToMany.py +604 -0
- masoniteorm/relationships/HasMany.py +66 -0
- masoniteorm/relationships/HasManyThrough.py +269 -0
- masoniteorm/relationships/HasOne.py +111 -0
- masoniteorm/relationships/HasOneThrough.py +275 -0
- masoniteorm/relationships/MorphMany.py +152 -0
- masoniteorm/relationships/MorphOne.py +156 -0
- masoniteorm/relationships/MorphTo.py +111 -0
- masoniteorm/relationships/MorphToMany.py +108 -0
- masoniteorm/relationships/__init__.py +10 -0
- masoniteorm/schema/Blueprint.py +1161 -0
- masoniteorm/schema/Column.py +144 -0
- masoniteorm/schema/ColumnDiff.py +0 -0
- masoniteorm/schema/Constraint.py +5 -0
- masoniteorm/schema/ForeignKeyConstraint.py +28 -0
- masoniteorm/schema/Index.py +5 -0
- masoniteorm/schema/Schema.py +359 -0
- masoniteorm/schema/Table.py +94 -0
- masoniteorm/schema/TableDiff.py +86 -0
- masoniteorm/schema/__init__.py +3 -0
- masoniteorm/schema/platforms/MSSQLPlatform.py +367 -0
- masoniteorm/schema/platforms/MySQLPlatform.py +513 -0
- masoniteorm/schema/platforms/Platform.py +97 -0
- masoniteorm/schema/platforms/PostgresPlatform.py +551 -0
- masoniteorm/schema/platforms/SQLitePlatform.py +481 -0
- masoniteorm/schema/platforms/__init__.py +4 -0
- masoniteorm/scopes/BaseScope.py +6 -0
- masoniteorm/scopes/SoftDeleteScope.py +56 -0
- masoniteorm/scopes/SoftDeletesMixin.py +13 -0
- masoniteorm/scopes/TimeStampsMixin.py +12 -0
- masoniteorm/scopes/TimeStampsScope.py +47 -0
- masoniteorm/scopes/UUIDPrimaryKeyMixin.py +8 -0
- masoniteorm/scopes/UUIDPrimaryKeyScope.py +51 -0
- masoniteorm/scopes/__init__.py +8 -0
- masoniteorm/scopes/scope.py +15 -0
- masoniteorm/seeds/Seeder.py +42 -0
- masoniteorm/seeds/__init__.py +1 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
from .Column import Column
|
|
2
|
+
from .Table import Table
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class TableDiff(Table):
|
|
6
|
+
def __init__(self, name):
|
|
7
|
+
self.name = name
|
|
8
|
+
self.from_table = None
|
|
9
|
+
self.new_name = None
|
|
10
|
+
self.removed_indexes = []
|
|
11
|
+
self.removed_unique_indexes = []
|
|
12
|
+
self.added_indexes = {}
|
|
13
|
+
self.added_columns = {}
|
|
14
|
+
self.changed_columns = {}
|
|
15
|
+
self.dropped_columns = []
|
|
16
|
+
self.dropped_foreign_keys = []
|
|
17
|
+
self.dropped_primary_keys = []
|
|
18
|
+
self.renamed_columns = {}
|
|
19
|
+
self.removed_constraints = {}
|
|
20
|
+
self.added_constraints = {}
|
|
21
|
+
self.added_foreign_keys = {}
|
|
22
|
+
self.comment = None
|
|
23
|
+
|
|
24
|
+
def remove_constraint(self, name):
|
|
25
|
+
self.removed_constraints.update(
|
|
26
|
+
{name: self.from_table.get_constraint(name)}
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
def get_removed_constraints(self):
|
|
30
|
+
return self.removed_constraints
|
|
31
|
+
|
|
32
|
+
def get_renamed_columns(self):
|
|
33
|
+
return self.renamed_columns
|
|
34
|
+
|
|
35
|
+
def rename_column(
|
|
36
|
+
self,
|
|
37
|
+
original_name,
|
|
38
|
+
new_name,
|
|
39
|
+
column_type=None,
|
|
40
|
+
length=None,
|
|
41
|
+
nullable=False,
|
|
42
|
+
default=None,
|
|
43
|
+
):
|
|
44
|
+
self.renamed_columns.update(
|
|
45
|
+
{
|
|
46
|
+
original_name: Column(
|
|
47
|
+
new_name,
|
|
48
|
+
column_type,
|
|
49
|
+
length=length,
|
|
50
|
+
nullable=nullable,
|
|
51
|
+
default=default,
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
def remove_index(self, name):
|
|
57
|
+
self.removed_indexes.append(name)
|
|
58
|
+
|
|
59
|
+
def remove_unique_index(self, name):
|
|
60
|
+
self.removed_unique_indexes.append(name)
|
|
61
|
+
|
|
62
|
+
def drop_column(self, name):
|
|
63
|
+
self.dropped_columns.append(name)
|
|
64
|
+
|
|
65
|
+
def get_dropped_columns(self):
|
|
66
|
+
return self.dropped_columns
|
|
67
|
+
|
|
68
|
+
def get_dropped_foreign_keys(self):
|
|
69
|
+
return self.dropped_foreign_keys
|
|
70
|
+
|
|
71
|
+
def drop_foreign(self, name):
|
|
72
|
+
self.dropped_foreign_keys.append(name)
|
|
73
|
+
return self
|
|
74
|
+
|
|
75
|
+
def drop_primary(self, name):
|
|
76
|
+
self.dropped_primary_keys.append(name)
|
|
77
|
+
return self
|
|
78
|
+
|
|
79
|
+
def change_column(self, added_column):
|
|
80
|
+
self.added_columns.pop(added_column.name)
|
|
81
|
+
|
|
82
|
+
self.changed_columns.update({added_column.name: added_column})
|
|
83
|
+
|
|
84
|
+
def add_comment(self, comment):
|
|
85
|
+
self.comment = comment
|
|
86
|
+
return self
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
from ..Table import Table
|
|
2
|
+
from .Platform import Platform
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class MSSQLPlatform(Platform):
|
|
6
|
+
types_without_lengths = [
|
|
7
|
+
"integer",
|
|
8
|
+
"big_integer",
|
|
9
|
+
"tiny_integer",
|
|
10
|
+
"small_integer",
|
|
11
|
+
"medium_integer",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
type_map = {
|
|
15
|
+
"string": "VARCHAR",
|
|
16
|
+
"char": "CHAR",
|
|
17
|
+
"big_increments": "BIGINT IDENTITY",
|
|
18
|
+
"integer": "INT",
|
|
19
|
+
"big_integer": "BIGINT",
|
|
20
|
+
"tiny_integer": "TINYINT",
|
|
21
|
+
"small_integer": "SMALLINT",
|
|
22
|
+
"medium_integer": "MEDIUMINT",
|
|
23
|
+
"integer_unsigned": "INT",
|
|
24
|
+
"big_integer_unsigned": "BIGINT",
|
|
25
|
+
"tiny_integer_unsigned": "TINYINT",
|
|
26
|
+
"small_integer_unsigned": "SMALLINT",
|
|
27
|
+
"medium_integer_unsigned": "MEDIUMINT",
|
|
28
|
+
"increments": "INT IDENTITY",
|
|
29
|
+
"uuid": "CHAR",
|
|
30
|
+
"binary": "LONGBLOB",
|
|
31
|
+
"boolean": "BOOLEAN",
|
|
32
|
+
"decimal": "DECIMAL",
|
|
33
|
+
"double": "DOUBLE",
|
|
34
|
+
"enum": "VARCHAR",
|
|
35
|
+
"text": "TEXT",
|
|
36
|
+
"tiny_text": "TINYTEXT",
|
|
37
|
+
"float": "FLOAT",
|
|
38
|
+
"geometry": "GEOMETRY",
|
|
39
|
+
"json": "JSON",
|
|
40
|
+
"jsonb": "LONGBLOB",
|
|
41
|
+
"inet": "VARCHAR",
|
|
42
|
+
"cidr": "VARCHAR",
|
|
43
|
+
"macaddr": "VARCHAR",
|
|
44
|
+
"long_text": "LONGTEXT",
|
|
45
|
+
"point": "POINT",
|
|
46
|
+
"time": "TIME",
|
|
47
|
+
"timestamp": "DATETIME",
|
|
48
|
+
"date": "DATE",
|
|
49
|
+
"year": "YEAR",
|
|
50
|
+
"datetime": "DATETIME",
|
|
51
|
+
"tiny_increments": "TINYINT IDENTITY",
|
|
52
|
+
"unsigned": "INT",
|
|
53
|
+
"unsigned_integer": "INT",
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
premapped_nulls = {True: "NULL", False: "NOT NULL"}
|
|
57
|
+
|
|
58
|
+
premapped_defaults = {
|
|
59
|
+
"current": " DEFAULT CURRENT_TIMESTAMP",
|
|
60
|
+
"now": " DEFAULT NOW()",
|
|
61
|
+
"null": " DEFAULT NULL",
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
def compile_create_sql(self, table, if_not_exists=False):
|
|
65
|
+
sql = []
|
|
66
|
+
table_create_format = (
|
|
67
|
+
self.create_if_not_exists_format()
|
|
68
|
+
if if_not_exists
|
|
69
|
+
else self.create_format()
|
|
70
|
+
)
|
|
71
|
+
sql.append(
|
|
72
|
+
table_create_format.format(
|
|
73
|
+
table=self.wrap_table(table.name),
|
|
74
|
+
columns=", ".join(
|
|
75
|
+
self.columnize(table.get_added_columns())
|
|
76
|
+
).strip(),
|
|
77
|
+
constraints=(
|
|
78
|
+
", "
|
|
79
|
+
+ ", ".join(
|
|
80
|
+
self.constraintize(
|
|
81
|
+
table.get_added_constraints(), table
|
|
82
|
+
)
|
|
83
|
+
)
|
|
84
|
+
if table.get_added_constraints()
|
|
85
|
+
else ""
|
|
86
|
+
),
|
|
87
|
+
foreign_keys=(
|
|
88
|
+
", "
|
|
89
|
+
+ ", ".join(
|
|
90
|
+
self.foreign_key_constraintize(
|
|
91
|
+
table.name, table.added_foreign_keys
|
|
92
|
+
)
|
|
93
|
+
)
|
|
94
|
+
if table.added_foreign_keys
|
|
95
|
+
else ""
|
|
96
|
+
),
|
|
97
|
+
)
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
if table.added_indexes:
|
|
101
|
+
for name, index in table.added_indexes.items():
|
|
102
|
+
sql.append(
|
|
103
|
+
"CREATE INDEX {name} ON {table}({column})".format(
|
|
104
|
+
name=index.name,
|
|
105
|
+
table=self.wrap_table(table.name),
|
|
106
|
+
column=",".join(index.column),
|
|
107
|
+
)
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
return sql
|
|
111
|
+
|
|
112
|
+
def compile_alter_sql(self, table):
|
|
113
|
+
sql = []
|
|
114
|
+
|
|
115
|
+
if table.added_columns:
|
|
116
|
+
sql.append(
|
|
117
|
+
self.alter_format().format(
|
|
118
|
+
table=self.wrap_table(table.name),
|
|
119
|
+
columns="ADD "
|
|
120
|
+
+ ", ".join(self.columnize(table.added_columns)).strip(),
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
if table.changed_columns:
|
|
125
|
+
sql.append(
|
|
126
|
+
self.alter_format().format(
|
|
127
|
+
table=self.wrap_table(table.name),
|
|
128
|
+
columns="ALTER COLUMN "
|
|
129
|
+
+ ", ".join(self.columnize(table.changed_columns)).strip(),
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
if table.renamed_columns:
|
|
134
|
+
for name, column in table.get_renamed_columns().items():
|
|
135
|
+
sql.append(
|
|
136
|
+
self.rename_column_string(
|
|
137
|
+
table.name, name, column.name
|
|
138
|
+
).strip()
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
if table.dropped_columns:
|
|
142
|
+
dropped_sql = []
|
|
143
|
+
|
|
144
|
+
for name in table.get_dropped_columns():
|
|
145
|
+
dropped_sql.append(
|
|
146
|
+
self.drop_column_string().format(name=name).strip()
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
sql.append(
|
|
150
|
+
self.alter_format().format(
|
|
151
|
+
table=self.wrap_table(table.name),
|
|
152
|
+
columns="DROP COLUMN " + ", ".join(dropped_sql),
|
|
153
|
+
)
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
if table.added_foreign_keys:
|
|
157
|
+
for (
|
|
158
|
+
column,
|
|
159
|
+
foreign_key_constraint,
|
|
160
|
+
) in table.get_added_foreign_keys().items():
|
|
161
|
+
cascade = ""
|
|
162
|
+
if foreign_key_constraint.delete_action:
|
|
163
|
+
cascade += f" ON DELETE {self.foreign_key_actions.get(foreign_key_constraint.delete_action.lower())}"
|
|
164
|
+
if foreign_key_constraint.update_action:
|
|
165
|
+
cascade += f" ON UPDATE {self.foreign_key_actions.get(foreign_key_constraint.update_action.lower())}"
|
|
166
|
+
sql.append(
|
|
167
|
+
f"ALTER TABLE {self.wrap_table(table.name)} ADD "
|
|
168
|
+
+ self.get_foreign_key_constraint_string().format(
|
|
169
|
+
constraint_name=foreign_key_constraint.constraint_name,
|
|
170
|
+
column=self.wrap_column(column),
|
|
171
|
+
table=self.wrap_table(table.name),
|
|
172
|
+
foreign_table=self.wrap_table(
|
|
173
|
+
foreign_key_constraint.foreign_table
|
|
174
|
+
),
|
|
175
|
+
foreign_column=self.wrap_column(
|
|
176
|
+
foreign_key_constraint.foreign_column
|
|
177
|
+
),
|
|
178
|
+
cascade=cascade,
|
|
179
|
+
)
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
if table.dropped_foreign_keys:
|
|
183
|
+
constraints = table.dropped_foreign_keys
|
|
184
|
+
for constraint in constraints:
|
|
185
|
+
sql.append(
|
|
186
|
+
f"ALTER TABLE {self.wrap_table(table.name)} DROP CONSTRAINT {constraint}"
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
if table.added_indexes:
|
|
190
|
+
for name, index in table.added_indexes.items():
|
|
191
|
+
sql.append(
|
|
192
|
+
"CREATE INDEX {name} ON {table}({column})".format(
|
|
193
|
+
name=index.name,
|
|
194
|
+
table=self.wrap_table(table.name),
|
|
195
|
+
column=",".join(index.column),
|
|
196
|
+
)
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
if (
|
|
200
|
+
table.removed_indexes
|
|
201
|
+
or table.removed_unique_indexes
|
|
202
|
+
or table.dropped_primary_keys
|
|
203
|
+
):
|
|
204
|
+
constraints = table.removed_indexes
|
|
205
|
+
constraints += table.removed_unique_indexes
|
|
206
|
+
constraints += table.dropped_primary_keys
|
|
207
|
+
for constraint in constraints:
|
|
208
|
+
sql.append(
|
|
209
|
+
f"DROP INDEX {self.wrap_table(table.name)}.{self.wrap_table(constraint)}"
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
if table.added_constraints:
|
|
213
|
+
for name, constraint in table.added_constraints.items():
|
|
214
|
+
if constraint.constraint_type == "unique":
|
|
215
|
+
sql.append(
|
|
216
|
+
f"ALTER TABLE {self.wrap_table(table.name)} ADD CONSTRAINT {constraint.name} UNIQUE({','.join(constraint.columns)})"
|
|
217
|
+
)
|
|
218
|
+
elif constraint.constraint_type == "fulltext":
|
|
219
|
+
pass
|
|
220
|
+
elif constraint.constraint_type == "primary_key":
|
|
221
|
+
sql.append(
|
|
222
|
+
f"ALTER TABLE {self.wrap_table(table.name)} ADD CONSTRAINT {constraint.name} PRIMARY KEY ({','.join(constraint.columns)})"
|
|
223
|
+
)
|
|
224
|
+
return sql
|
|
225
|
+
|
|
226
|
+
def add_column_string(self):
|
|
227
|
+
return "{name} {data_type}{length}"
|
|
228
|
+
|
|
229
|
+
def drop_column_string(self):
|
|
230
|
+
return "{name}"
|
|
231
|
+
|
|
232
|
+
def rename_column_string(self, table, old, new):
|
|
233
|
+
return f"EXEC sp_rename '{table}.{old}', '{new}', 'COLUMN'"
|
|
234
|
+
|
|
235
|
+
def columnize(self, columns):
|
|
236
|
+
sql = []
|
|
237
|
+
for name, column in columns.items():
|
|
238
|
+
if column.length:
|
|
239
|
+
length = self.create_column_length(column.column_type).format(
|
|
240
|
+
length=column.length
|
|
241
|
+
)
|
|
242
|
+
else:
|
|
243
|
+
length = ""
|
|
244
|
+
|
|
245
|
+
if column.default == "":
|
|
246
|
+
default = " DEFAULT ''"
|
|
247
|
+
elif column.default in (0,):
|
|
248
|
+
default = f" DEFAULT {column.default}"
|
|
249
|
+
elif column.default in self.premapped_defaults.keys():
|
|
250
|
+
default = self.premapped_defaults.get(column.default)
|
|
251
|
+
elif column.default:
|
|
252
|
+
if (
|
|
253
|
+
isinstance(column.default, (str,))
|
|
254
|
+
and not column.default_is_raw
|
|
255
|
+
):
|
|
256
|
+
default = f" DEFAULT '{column.default}'"
|
|
257
|
+
else:
|
|
258
|
+
default = f" DEFAULT {column.default}"
|
|
259
|
+
else:
|
|
260
|
+
default = ""
|
|
261
|
+
|
|
262
|
+
constraint = ""
|
|
263
|
+
column_constraint = ""
|
|
264
|
+
if column.primary:
|
|
265
|
+
constraint = " PRIMARY KEY"
|
|
266
|
+
|
|
267
|
+
if column.column_type == "enum":
|
|
268
|
+
values = ", ".join(f"'{x}'" for x in column.values)
|
|
269
|
+
column_constraint = f" CHECK([{column.name}] IN ({values}))"
|
|
270
|
+
|
|
271
|
+
sql.append(
|
|
272
|
+
self.columnize_string()
|
|
273
|
+
.format(
|
|
274
|
+
name=column.name,
|
|
275
|
+
data_type=self.type_map.get(column.column_type, ""),
|
|
276
|
+
column_constraint=column_constraint,
|
|
277
|
+
length=length,
|
|
278
|
+
constraint=constraint,
|
|
279
|
+
nullable=self.premapped_nulls.get(column.is_null) or "",
|
|
280
|
+
default=default,
|
|
281
|
+
)
|
|
282
|
+
.strip()
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
return sql
|
|
286
|
+
|
|
287
|
+
def columnize_string(self):
|
|
288
|
+
return "[{name}] {data_type}{length} {nullable}{default}{column_constraint}{constraint}"
|
|
289
|
+
|
|
290
|
+
def constraintize(self, constraints, table):
|
|
291
|
+
sql = []
|
|
292
|
+
for name, constraint in constraints.items():
|
|
293
|
+
sql.append(
|
|
294
|
+
getattr(
|
|
295
|
+
self, f"get_{constraint.constraint_type}_constraint_string"
|
|
296
|
+
)().format(
|
|
297
|
+
columns=", ".join(constraint.columns),
|
|
298
|
+
name_columns="_".join(constraint.columns),
|
|
299
|
+
constraint_name=constraint.name,
|
|
300
|
+
table=table.name,
|
|
301
|
+
)
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
return sql
|
|
305
|
+
|
|
306
|
+
def get_table_string(self):
|
|
307
|
+
return "[{table}]"
|
|
308
|
+
|
|
309
|
+
def get_column_string(self):
|
|
310
|
+
return "[{column}]"
|
|
311
|
+
|
|
312
|
+
def create_format(self):
|
|
313
|
+
return "CREATE TABLE {table} ({columns}{constraints}{foreign_keys})"
|
|
314
|
+
|
|
315
|
+
def create_if_not_exists_format(self):
|
|
316
|
+
return "CREATE TABLE IF NOT EXISTS {table} ({columns}{constraints}{foreign_keys})"
|
|
317
|
+
|
|
318
|
+
def alter_format(self):
|
|
319
|
+
return "ALTER TABLE {table} {columns}"
|
|
320
|
+
|
|
321
|
+
def get_foreign_key_constraint_string(self):
|
|
322
|
+
return "CONSTRAINT {constraint_name} FOREIGN KEY ({column}) REFERENCES {foreign_table}({foreign_column}){cascade}"
|
|
323
|
+
|
|
324
|
+
def get_primary_key_constraint_string(self):
|
|
325
|
+
return "CONSTRAINT {constraint_name} PRIMARY KEY ({columns})"
|
|
326
|
+
|
|
327
|
+
def get_unique_constraint_string(self):
|
|
328
|
+
return "CONSTRAINT {constraint_name} UNIQUE ({columns})"
|
|
329
|
+
|
|
330
|
+
def compile_table_exists(self, table, database=None, schema=None):
|
|
331
|
+
return f"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{table}'"
|
|
332
|
+
|
|
333
|
+
def compile_truncate(self, table, foreign_keys=False):
|
|
334
|
+
if not foreign_keys:
|
|
335
|
+
return f"TRUNCATE TABLE {self.wrap_table(table)}"
|
|
336
|
+
|
|
337
|
+
return [
|
|
338
|
+
f"ALTER TABLE {self.wrap_table(table)} NOCHECK CONSTRAINT ALL",
|
|
339
|
+
f"TRUNCATE TABLE {self.wrap_table(table)}",
|
|
340
|
+
f"ALTER TABLE {self.wrap_table(table)} WITH CHECK CHECK CONSTRAINT ALL",
|
|
341
|
+
]
|
|
342
|
+
|
|
343
|
+
def compile_rename_table(self, current_name, new_name):
|
|
344
|
+
return f"EXEC sp_rename {self.wrap_table(current_name)}, {self.wrap_table(new_name)}"
|
|
345
|
+
|
|
346
|
+
def compile_drop_table_if_exists(self, table):
|
|
347
|
+
return f"DROP TABLE IF EXISTS {self.wrap_table(table)}"
|
|
348
|
+
|
|
349
|
+
def compile_drop_table(self, table):
|
|
350
|
+
return f"DROP TABLE {self.wrap_table(table)}"
|
|
351
|
+
|
|
352
|
+
def compile_column_exists(self, table, column):
|
|
353
|
+
return f"SELECT 1 FROM sys.columns WHERE Name = N'{column}' AND Object_ID = Object_ID(N'{table}')"
|
|
354
|
+
|
|
355
|
+
def compile_get_all_tables(self, database, schema=None):
|
|
356
|
+
return f"SELECT name FROM {database}.sys.tables"
|
|
357
|
+
|
|
358
|
+
def get_current_schema(self, connection, table_name, schema=None):
|
|
359
|
+
return Table(table_name)
|
|
360
|
+
|
|
361
|
+
def enable_foreign_key_constraints(self):
|
|
362
|
+
"""MSSQL does not allow a global way to enable foreign key constraints"""
|
|
363
|
+
return ""
|
|
364
|
+
|
|
365
|
+
def disable_foreign_key_constraints(self):
|
|
366
|
+
"""MSSQL does not allow a global way to disable foreign key constraints"""
|
|
367
|
+
return ""
|