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,194 @@
|
|
|
1
|
+
from .BaseGrammar import BaseGrammar
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class MSSQLGrammar(BaseGrammar):
|
|
5
|
+
"""Microsoft SQL Server grammar class."""
|
|
6
|
+
|
|
7
|
+
aggregate_options = {
|
|
8
|
+
"SUM": "SUM",
|
|
9
|
+
"MAX": "MAX",
|
|
10
|
+
"MIN": "MIN",
|
|
11
|
+
"AVG": "AVG",
|
|
12
|
+
"COUNT": "COUNT",
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
join_keywords = {
|
|
16
|
+
"inner": "INNER JOIN",
|
|
17
|
+
"join": "INNER JOIN",
|
|
18
|
+
"outer": "OUTER JOIN",
|
|
19
|
+
"left": "LEFT JOIN",
|
|
20
|
+
"right": "RIGHT JOIN",
|
|
21
|
+
"left_inner": "LEFT INNER JOIN",
|
|
22
|
+
"right_inner": "RIGHT INNER JOIN",
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
column_strings = {
|
|
26
|
+
"select": "{table}.[{column}]{alias}{separator}",
|
|
27
|
+
"select_all": "{table}.*{separator}",
|
|
28
|
+
"insert": "{table}.[{column}]{separator}",
|
|
29
|
+
"update": "{table}.[{column}]{separator}",
|
|
30
|
+
"delete": "{table}.[{column}]{separator}",
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
locks = {"share": "WITH(ROWLOCK)", "update": "WITH(ROWLOCK)"}
|
|
34
|
+
|
|
35
|
+
def select_no_table(self):
|
|
36
|
+
return "SELECT {columns}"
|
|
37
|
+
|
|
38
|
+
def select_format(self):
|
|
39
|
+
return "SELECT {keyword} {limit} {columns} FROM {table} {lock} {joins} {wheres} {group_by} {having} {order_by} {offset}"
|
|
40
|
+
|
|
41
|
+
def update_format(self):
|
|
42
|
+
return "UPDATE {table} SET {key_equals} {wheres}"
|
|
43
|
+
|
|
44
|
+
def insert_format(self):
|
|
45
|
+
return "INSERT INTO {table} ({columns}) VALUES ({values})"
|
|
46
|
+
|
|
47
|
+
def bulk_insert_format(self):
|
|
48
|
+
return "INSERT INTO {table} ({columns}) VALUES {values}"
|
|
49
|
+
|
|
50
|
+
def delete_format(self):
|
|
51
|
+
return "DELETE FROM {table} {wheres}"
|
|
52
|
+
|
|
53
|
+
def create_column_string(self):
|
|
54
|
+
return "{column} {data_type}{length}{nullable}, "
|
|
55
|
+
|
|
56
|
+
def create_start(self):
|
|
57
|
+
return "CREATE TABLE {table} "
|
|
58
|
+
|
|
59
|
+
def having_string(self):
|
|
60
|
+
return "HAVING {column}"
|
|
61
|
+
|
|
62
|
+
def where_exists_string(self):
|
|
63
|
+
return "{keyword} EXISTS {value}"
|
|
64
|
+
|
|
65
|
+
def where_not_exists_string(self):
|
|
66
|
+
return "{keyword} NOT EXISTS {value}"
|
|
67
|
+
|
|
68
|
+
def where_like_string(self):
|
|
69
|
+
return "{keyword} {column} LIKE {value}"
|
|
70
|
+
|
|
71
|
+
def where_not_like_string(self):
|
|
72
|
+
return "{keyword} {column} NOT LIKE {value}"
|
|
73
|
+
|
|
74
|
+
def where_date_string(self):
|
|
75
|
+
return "{keyword} DATE({column}) {equality} {value}"
|
|
76
|
+
|
|
77
|
+
def where_regexp_string(self):
|
|
78
|
+
return self.where_like_string()
|
|
79
|
+
|
|
80
|
+
def where_not_regexp_string(self):
|
|
81
|
+
return self.where_not_like_string()
|
|
82
|
+
|
|
83
|
+
def having_equality_string(self):
|
|
84
|
+
return "HAVING {column} {equality} {value}"
|
|
85
|
+
|
|
86
|
+
def aggregate_string_without_alias(self):
|
|
87
|
+
return "{aggregate_function}({column})"
|
|
88
|
+
|
|
89
|
+
def create_column_length(self):
|
|
90
|
+
return "({length})"
|
|
91
|
+
|
|
92
|
+
def limit_string(self, offset=False):
|
|
93
|
+
if offset:
|
|
94
|
+
return ""
|
|
95
|
+
return "TOP {limit}"
|
|
96
|
+
|
|
97
|
+
def first_where_string(self):
|
|
98
|
+
return "WHERE"
|
|
99
|
+
|
|
100
|
+
def additional_where_string(self):
|
|
101
|
+
return "AND"
|
|
102
|
+
|
|
103
|
+
def join_string(self):
|
|
104
|
+
return "{keyword} {foreign_table}{alias} {on}"
|
|
105
|
+
|
|
106
|
+
def aggregate_string(self):
|
|
107
|
+
return "{aggregate_function}({column}) AS {alias}"
|
|
108
|
+
|
|
109
|
+
def subquery_string(self):
|
|
110
|
+
return "({query})"
|
|
111
|
+
|
|
112
|
+
def subquery_alias_string(self):
|
|
113
|
+
return "AS {alias}"
|
|
114
|
+
|
|
115
|
+
def where_group_string(self):
|
|
116
|
+
return "{keyword} {value}"
|
|
117
|
+
|
|
118
|
+
def or_where_string(self):
|
|
119
|
+
return "OR"
|
|
120
|
+
|
|
121
|
+
def raw_query_string(self):
|
|
122
|
+
return "{keyword} {query}"
|
|
123
|
+
|
|
124
|
+
def where_in_string(self):
|
|
125
|
+
return "WHERE IN ({values})"
|
|
126
|
+
|
|
127
|
+
def value_equal_string(self):
|
|
128
|
+
return "{keyword} {value1} = {value2}"
|
|
129
|
+
|
|
130
|
+
def where_null_string(self):
|
|
131
|
+
return " {keyword} {column} IS NULL"
|
|
132
|
+
|
|
133
|
+
def between_string(self):
|
|
134
|
+
return "{keyword} {column} BETWEEN {low} AND {high}"
|
|
135
|
+
|
|
136
|
+
def not_between_string(self):
|
|
137
|
+
return "{keyword} {column} NOT BETWEEN {low} AND {high}"
|
|
138
|
+
|
|
139
|
+
def where_not_null_string(self):
|
|
140
|
+
return " {keyword} {column} IS NOT NULL"
|
|
141
|
+
|
|
142
|
+
def where_string(self):
|
|
143
|
+
return " {keyword} {column} {equality} {value}"
|
|
144
|
+
|
|
145
|
+
def offset_string(self):
|
|
146
|
+
return "OFFSET {offset} ROWS FETCH NEXT {limit} ROWS ONLY"
|
|
147
|
+
|
|
148
|
+
def increment_string(self):
|
|
149
|
+
return "{column} = {column} + {value}"
|
|
150
|
+
|
|
151
|
+
def decrement_string(self):
|
|
152
|
+
return "{column} = {column} - {value}"
|
|
153
|
+
|
|
154
|
+
def aggregate_string_with_alias(self):
|
|
155
|
+
return "{aggregate_function}({column}) AS {alias}"
|
|
156
|
+
|
|
157
|
+
def key_value_string(self):
|
|
158
|
+
return "{column} = {value}{separator}"
|
|
159
|
+
|
|
160
|
+
def column_value_string(self):
|
|
161
|
+
return "{column} = {value}{separator}"
|
|
162
|
+
|
|
163
|
+
def table_string(self):
|
|
164
|
+
return "[{table}]"
|
|
165
|
+
|
|
166
|
+
def order_by_format(self):
|
|
167
|
+
return "{column} {direction}"
|
|
168
|
+
|
|
169
|
+
def order_by_string(self):
|
|
170
|
+
return "ORDER BY {order_columns}"
|
|
171
|
+
|
|
172
|
+
def column_string(self):
|
|
173
|
+
return "[{column}]{separator}"
|
|
174
|
+
|
|
175
|
+
def table_column_string(self):
|
|
176
|
+
return "[{table}].[{column}]{separator}"
|
|
177
|
+
|
|
178
|
+
def table_update_column_string(self):
|
|
179
|
+
return "[{table}].[{column}]{separator}"
|
|
180
|
+
|
|
181
|
+
def table_insert_column_string(self):
|
|
182
|
+
return "[{table}].[{column}]{separator}"
|
|
183
|
+
|
|
184
|
+
def value_string(self):
|
|
185
|
+
return "'{value}'{separator}"
|
|
186
|
+
|
|
187
|
+
def wrap_table(self, table_name):
|
|
188
|
+
return self.table_string().format(table=table_name)
|
|
189
|
+
|
|
190
|
+
def truncate_table(self, table, foreign_keys=False):
|
|
191
|
+
return f"TRUNCATE TABLE {self.wrap_table(table)}"
|
|
192
|
+
|
|
193
|
+
def compile_random(self, seed):
|
|
194
|
+
return "NEWID()"
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
from .BaseGrammar import BaseGrammar
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class MySQLGrammar(BaseGrammar):
|
|
5
|
+
"""MySQL grammar class."""
|
|
6
|
+
|
|
7
|
+
aggregate_options = {
|
|
8
|
+
"SUM": "SUM",
|
|
9
|
+
"MAX": "MAX",
|
|
10
|
+
"MIN": "MIN",
|
|
11
|
+
"AVG": "AVG",
|
|
12
|
+
"COUNT": "COUNT",
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
join_keywords = {
|
|
16
|
+
"inner": "INNER JOIN",
|
|
17
|
+
"join": "INNER JOIN",
|
|
18
|
+
"outer": "OUTER JOIN",
|
|
19
|
+
"left": "LEFT JOIN",
|
|
20
|
+
"right": "RIGHT JOIN",
|
|
21
|
+
"left_inner": "LEFT INNER JOIN",
|
|
22
|
+
"right_inner": "RIGHT INNER JOIN",
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
"""Column strings are formats for how columns and key values should be formatted
|
|
26
|
+
on specific queries. These can be different depending on the type of query.
|
|
27
|
+
|
|
28
|
+
For example for Postgres, You can specify columns as "users"."name":
|
|
29
|
+
|
|
30
|
+
SELECT "users"."name" from "users"
|
|
31
|
+
|
|
32
|
+
But on updates we can only specify the column name and cannot have the table prefixed:
|
|
33
|
+
|
|
34
|
+
UPDATE "users" SET "name" = "value"
|
|
35
|
+
|
|
36
|
+
This dictionary allows you to modify the format depending on the type
|
|
37
|
+
of query we are generating. For most databases these will be the same
|
|
38
|
+
but this allows you to modify formats depending on the database.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
column_strings = {
|
|
42
|
+
"select": "{table}.`{column}`{alias}{separator}",
|
|
43
|
+
"select_all": "{table}.*{separator}",
|
|
44
|
+
"insert": "{table}.`{column}`{separator}",
|
|
45
|
+
"update": "{table}.`{column}`{separator}",
|
|
46
|
+
"delete": "{table}.`{column}`{separator}",
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
locks = {"share": "LOCK IN SHARE MODE", "update": "FOR UPDATE"}
|
|
50
|
+
|
|
51
|
+
def select_format(self):
|
|
52
|
+
return "SELECT {keyword} {columns} FROM {table} {joins} {wheres} {group_by} {having} {order_by} {limit} {offset} {lock}"
|
|
53
|
+
|
|
54
|
+
def select_no_table(self):
|
|
55
|
+
return "SELECT {columns} {lock}"
|
|
56
|
+
|
|
57
|
+
def update_format(self):
|
|
58
|
+
return "UPDATE {table} SET {key_equals} {wheres}"
|
|
59
|
+
|
|
60
|
+
def insert_format(self):
|
|
61
|
+
return "INSERT INTO {table} ({columns}) VALUES ({values})"
|
|
62
|
+
|
|
63
|
+
def bulk_insert_format(self):
|
|
64
|
+
return "INSERT INTO {table} ({columns}) VALUES {values}"
|
|
65
|
+
|
|
66
|
+
def delete_format(self):
|
|
67
|
+
return "DELETE FROM {table} {wheres}"
|
|
68
|
+
|
|
69
|
+
def aggregate_string_with_alias(self):
|
|
70
|
+
return "{aggregate_function}({column}) AS {alias}"
|
|
71
|
+
|
|
72
|
+
def aggregate_string_without_alias(self):
|
|
73
|
+
return "{aggregate_function}({column})"
|
|
74
|
+
|
|
75
|
+
def subquery_string(self):
|
|
76
|
+
return "({query})"
|
|
77
|
+
|
|
78
|
+
def raw_query_string(self):
|
|
79
|
+
return "{keyword} {query}"
|
|
80
|
+
|
|
81
|
+
def where_group_string(self):
|
|
82
|
+
return "{keyword} {value}"
|
|
83
|
+
|
|
84
|
+
def between_string(self):
|
|
85
|
+
return "{keyword} {column} BETWEEN {low} AND {high}"
|
|
86
|
+
|
|
87
|
+
def not_between_string(self):
|
|
88
|
+
return "{keyword} {column} NOT BETWEEN {low} AND {high}"
|
|
89
|
+
|
|
90
|
+
def where_exists_string(self):
|
|
91
|
+
return "{keyword} EXISTS {value}"
|
|
92
|
+
|
|
93
|
+
def where_date_string(self):
|
|
94
|
+
return "{keyword} DATE({column}) {equality} {value}"
|
|
95
|
+
|
|
96
|
+
def where_not_exists_string(self):
|
|
97
|
+
return "{keyword} NOT EXISTS {value}"
|
|
98
|
+
|
|
99
|
+
def where_like_string(self):
|
|
100
|
+
return "{keyword} {column} LIKE {value}"
|
|
101
|
+
|
|
102
|
+
def where_not_like_string(self):
|
|
103
|
+
return "{keyword} {column} NOT LIKE {value}"
|
|
104
|
+
|
|
105
|
+
def get_true_column_string(self):
|
|
106
|
+
return "{keyword} {column} = '1'"
|
|
107
|
+
|
|
108
|
+
def get_false_column_string(self):
|
|
109
|
+
return "{keyword} {column} = '0'"
|
|
110
|
+
|
|
111
|
+
def process_table(self, table):
|
|
112
|
+
"""Compiles a given table name.
|
|
113
|
+
|
|
114
|
+
Arguments:
|
|
115
|
+
table {string} -- The table name to compile.
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
self
|
|
119
|
+
"""
|
|
120
|
+
if not table:
|
|
121
|
+
return ""
|
|
122
|
+
if isinstance(table, str):
|
|
123
|
+
return ".".join(
|
|
124
|
+
self.table_string().format(table=t) for t in table.split(".")
|
|
125
|
+
)
|
|
126
|
+
if table.raw:
|
|
127
|
+
return table.name
|
|
128
|
+
return ".".join(
|
|
129
|
+
self.table_string().format(table=t) for t in table.name.split(".")
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
def subquery_alias_string(self):
|
|
133
|
+
return "AS {alias}"
|
|
134
|
+
|
|
135
|
+
def key_value_string(self):
|
|
136
|
+
return "{column} = {value}{separator}"
|
|
137
|
+
|
|
138
|
+
def column_value_string(self):
|
|
139
|
+
return "{column} = {value}{separator}"
|
|
140
|
+
|
|
141
|
+
def increment_string(self):
|
|
142
|
+
return "{column} = {column} + {value}"
|
|
143
|
+
|
|
144
|
+
def decrement_string(self):
|
|
145
|
+
return "{column} = {column} - {value}"
|
|
146
|
+
|
|
147
|
+
def create_column_string(self):
|
|
148
|
+
return "{column} {data_type}{length}{nullable}{default_value}, "
|
|
149
|
+
|
|
150
|
+
def column_exists_string(self):
|
|
151
|
+
return "SHOW COLUMNS FROM {table} LIKE {value}"
|
|
152
|
+
|
|
153
|
+
def table_exists_string(self):
|
|
154
|
+
return "SELECT * from information_schema.tables where table_name='{clean_table}' AND table_schema = '{database}'"
|
|
155
|
+
|
|
156
|
+
def create_column_length(self, column_type):
|
|
157
|
+
return "({length})"
|
|
158
|
+
|
|
159
|
+
def table_string(self):
|
|
160
|
+
return "`{table}`"
|
|
161
|
+
|
|
162
|
+
def order_by_format(self):
|
|
163
|
+
return "{column} {direction}"
|
|
164
|
+
|
|
165
|
+
def order_by_string(self):
|
|
166
|
+
return "ORDER BY {order_columns}"
|
|
167
|
+
|
|
168
|
+
def column_string(self):
|
|
169
|
+
return "`{column}`{separator}"
|
|
170
|
+
|
|
171
|
+
def value_string(self):
|
|
172
|
+
return "'{value}'{separator}"
|
|
173
|
+
|
|
174
|
+
def join_string(self):
|
|
175
|
+
return "{keyword} {foreign_table}{alias} {on}"
|
|
176
|
+
|
|
177
|
+
def limit_string(self, offset=False):
|
|
178
|
+
return "LIMIT {limit}"
|
|
179
|
+
|
|
180
|
+
def offset_string(self):
|
|
181
|
+
return "OFFSET {offset}"
|
|
182
|
+
|
|
183
|
+
def first_where_string(self):
|
|
184
|
+
return "WHERE"
|
|
185
|
+
|
|
186
|
+
def additional_where_string(self):
|
|
187
|
+
return "AND"
|
|
188
|
+
|
|
189
|
+
def or_where_string(self):
|
|
190
|
+
return "OR"
|
|
191
|
+
|
|
192
|
+
def where_in_string(self):
|
|
193
|
+
return "WHERE IN ({values})"
|
|
194
|
+
|
|
195
|
+
def value_equal_string(self):
|
|
196
|
+
return "{keyword} {value1} = {value2}"
|
|
197
|
+
|
|
198
|
+
def where_string(self):
|
|
199
|
+
return " {keyword} {column} {equality} {value}"
|
|
200
|
+
|
|
201
|
+
def having_string(self):
|
|
202
|
+
return "HAVING {column}"
|
|
203
|
+
|
|
204
|
+
def having_equality_string(self):
|
|
205
|
+
return "HAVING {column} {equality} {value}"
|
|
206
|
+
|
|
207
|
+
def where_null_string(self):
|
|
208
|
+
return " {keyword} {column} IS NULL"
|
|
209
|
+
|
|
210
|
+
def where_not_null_string(self):
|
|
211
|
+
return " {keyword} {column} IS NOT NULL"
|
|
212
|
+
|
|
213
|
+
def enable_foreign_key_constraints(self):
|
|
214
|
+
return "SET FOREIGN_KEY_CHECKS=1"
|
|
215
|
+
|
|
216
|
+
def disable_foreign_key_constraints(self):
|
|
217
|
+
return "SET FOREIGN_KEY_CHECKS=0"
|
|
218
|
+
|
|
219
|
+
def truncate_table(self, table, foreign_keys=False):
|
|
220
|
+
"""Specifies a truncate table expression.
|
|
221
|
+
|
|
222
|
+
Arguments;
|
|
223
|
+
table {string} -- The name of the table to truncate.
|
|
224
|
+
|
|
225
|
+
Returns:
|
|
226
|
+
self
|
|
227
|
+
"""
|
|
228
|
+
if not foreign_keys:
|
|
229
|
+
return f"TRUNCATE TABLE {self.wrap_table(table)}"
|
|
230
|
+
|
|
231
|
+
return [
|
|
232
|
+
self.disable_foreign_key_constraints(),
|
|
233
|
+
f"TRUNCATE TABLE {self.wrap_table(table)}",
|
|
234
|
+
self.enable_foreign_key_constraints(),
|
|
235
|
+
]
|
|
236
|
+
|
|
237
|
+
def compile_random(self):
|
|
238
|
+
return "RAND()"
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
from .BaseGrammar import BaseGrammar
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class PostgresGrammar(BaseGrammar):
|
|
7
|
+
"""Postgres grammar class."""
|
|
8
|
+
|
|
9
|
+
aggregate_options = {
|
|
10
|
+
"SUM": "SUM",
|
|
11
|
+
"MAX": "MAX",
|
|
12
|
+
"MIN": "MIN",
|
|
13
|
+
"AVG": "AVG",
|
|
14
|
+
"COUNT": "COUNT",
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
join_keywords = {
|
|
18
|
+
"inner": "INNER JOIN",
|
|
19
|
+
"join": "INNER JOIN",
|
|
20
|
+
"outer": "OUTER JOIN",
|
|
21
|
+
"left": "LEFT JOIN",
|
|
22
|
+
"right": "RIGHT JOIN",
|
|
23
|
+
"left_inner": "LEFT INNER JOIN",
|
|
24
|
+
"right_inner": "RIGHT INNER JOIN",
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
column_strings = {
|
|
28
|
+
"select": '{table}."{column}"{alias}{separator}',
|
|
29
|
+
"select_all": "{table}.*{separator}",
|
|
30
|
+
"insert": '"{column}"{separator}',
|
|
31
|
+
"update": '"{column}"{separator}',
|
|
32
|
+
"delete": '{table}."{column}"{separator}',
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
locks = {"share": "FOR SHARE", "update": "FOR UPDATE"}
|
|
36
|
+
|
|
37
|
+
def select_no_table(self):
|
|
38
|
+
return "SELECT {columns} {lock}"
|
|
39
|
+
|
|
40
|
+
def select_format(self):
|
|
41
|
+
return "SELECT {keyword} {columns} FROM {table} {joins} {wheres} {group_by} {having} {order_by} {limit} {offset} {lock}"
|
|
42
|
+
|
|
43
|
+
def update_format(self):
|
|
44
|
+
return "UPDATE {table} SET {key_equals} {wheres}"
|
|
45
|
+
|
|
46
|
+
def insert_format(self):
|
|
47
|
+
return "INSERT INTO {table} ({columns}) VALUES ({values}) RETURNING *"
|
|
48
|
+
|
|
49
|
+
def bulk_insert_format(self):
|
|
50
|
+
return "INSERT INTO {table} ({columns}) VALUES {values} RETURNING *"
|
|
51
|
+
|
|
52
|
+
def delete_format(self):
|
|
53
|
+
return "DELETE FROM {table} {wheres}"
|
|
54
|
+
|
|
55
|
+
def aggregate_string_with_alias(self):
|
|
56
|
+
return "{aggregate_function}({column}) AS {alias}"
|
|
57
|
+
|
|
58
|
+
def aggregate_string_without_alias(self):
|
|
59
|
+
return "{aggregate_function}({column})"
|
|
60
|
+
|
|
61
|
+
def get_true_column_string(self):
|
|
62
|
+
return "{keyword} {column} IS True"
|
|
63
|
+
|
|
64
|
+
def get_false_column_string(self):
|
|
65
|
+
return "{keyword} {column} IS False"
|
|
66
|
+
|
|
67
|
+
def subquery_string(self):
|
|
68
|
+
return "({query})"
|
|
69
|
+
|
|
70
|
+
def raw_query_string(self):
|
|
71
|
+
return "{keyword} {query}"
|
|
72
|
+
|
|
73
|
+
def where_group_string(self):
|
|
74
|
+
return "{keyword} {value}"
|
|
75
|
+
|
|
76
|
+
def between_string(self):
|
|
77
|
+
return "{keyword} {column} BETWEEN {low} AND {high}"
|
|
78
|
+
|
|
79
|
+
def not_between_string(self):
|
|
80
|
+
return "{keyword} {column} NOT BETWEEN {low} AND {high}"
|
|
81
|
+
|
|
82
|
+
def where_exists_string(self):
|
|
83
|
+
return "{keyword} EXISTS {value}"
|
|
84
|
+
|
|
85
|
+
def where_not_exists_string(self):
|
|
86
|
+
return "{keyword} NOT EXISTS {value}"
|
|
87
|
+
|
|
88
|
+
def where_like_string(self):
|
|
89
|
+
return "{keyword} {column} ILIKE {value}"
|
|
90
|
+
|
|
91
|
+
def where_not_like_string(self):
|
|
92
|
+
return "{keyword} {column} NOT ILIKE {value}"
|
|
93
|
+
|
|
94
|
+
def subquery_alias_string(self):
|
|
95
|
+
return "AS {alias}"
|
|
96
|
+
|
|
97
|
+
def key_value_string(self):
|
|
98
|
+
return "{column} = {value}{separator}"
|
|
99
|
+
|
|
100
|
+
def column_value_string(self):
|
|
101
|
+
return "{column} = {value}{separator}"
|
|
102
|
+
|
|
103
|
+
def increment_string(self):
|
|
104
|
+
return "{column} = {column} + {value}"
|
|
105
|
+
|
|
106
|
+
def decrement_string(self):
|
|
107
|
+
return "{column} = {column} - {value}"
|
|
108
|
+
|
|
109
|
+
def create_column_string(self):
|
|
110
|
+
return "{column} {data_type}{length}{nullable}, "
|
|
111
|
+
|
|
112
|
+
def column_exists_string(self):
|
|
113
|
+
return "SELECT column_name FROM information_schema.columns WHERE table_name='{clean_table}' and column_name={value}"
|
|
114
|
+
|
|
115
|
+
def table_exists_string(self):
|
|
116
|
+
return "SELECT * from information_schema.tables where table_name='{clean_table}'"
|
|
117
|
+
|
|
118
|
+
def create_column_length(self, column_type):
|
|
119
|
+
if column_type in self.types_without_lengths:
|
|
120
|
+
return ""
|
|
121
|
+
return "({length})"
|
|
122
|
+
|
|
123
|
+
def to_sql(self):
|
|
124
|
+
"""Cleans up the SQL string and returns the SQL
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
string
|
|
128
|
+
"""
|
|
129
|
+
|
|
130
|
+
if self.queries and (not self._columns and not self._creates):
|
|
131
|
+
sql = ""
|
|
132
|
+
for query in self.queries:
|
|
133
|
+
query += "; "
|
|
134
|
+
sql += re.sub(" +", " ", query)
|
|
135
|
+
return sql.rstrip(" ")
|
|
136
|
+
else:
|
|
137
|
+
sql = re.sub(" +", " ", self._sql.strip().replace(",)", ")"))
|
|
138
|
+
for query in self.queries:
|
|
139
|
+
sql += "; "
|
|
140
|
+
sql += re.sub(" +", " ", query.strip())
|
|
141
|
+
|
|
142
|
+
return sql
|
|
143
|
+
|
|
144
|
+
def table_string(self):
|
|
145
|
+
return '"{table}"'
|
|
146
|
+
|
|
147
|
+
def order_by_format(self):
|
|
148
|
+
return "{column} {direction}"
|
|
149
|
+
|
|
150
|
+
def order_by_string(self):
|
|
151
|
+
return "ORDER BY {order_columns}"
|
|
152
|
+
|
|
153
|
+
def column_string(self):
|
|
154
|
+
return '"{column}"{separator}'
|
|
155
|
+
|
|
156
|
+
def value_string(self):
|
|
157
|
+
return "'{value}'{separator}"
|
|
158
|
+
|
|
159
|
+
def join_string(self):
|
|
160
|
+
return "{keyword} {foreign_table}{alias} {on}"
|
|
161
|
+
|
|
162
|
+
def limit_string(self, offset=False):
|
|
163
|
+
return "LIMIT {limit}"
|
|
164
|
+
|
|
165
|
+
def offset_string(self):
|
|
166
|
+
return "OFFSET {offset}"
|
|
167
|
+
|
|
168
|
+
def first_where_string(self):
|
|
169
|
+
return "WHERE"
|
|
170
|
+
|
|
171
|
+
def additional_where_string(self):
|
|
172
|
+
return "AND"
|
|
173
|
+
|
|
174
|
+
def or_where_string(self):
|
|
175
|
+
return "OR"
|
|
176
|
+
|
|
177
|
+
def where_in_string(self):
|
|
178
|
+
return "WHERE IN ({values})"
|
|
179
|
+
|
|
180
|
+
def where_date_string(self):
|
|
181
|
+
return "{keyword} DATE({column}) {equality} {value}"
|
|
182
|
+
|
|
183
|
+
def value_equal_string(self):
|
|
184
|
+
return "{keyword} {value1} = {value2}"
|
|
185
|
+
|
|
186
|
+
def where_string(self):
|
|
187
|
+
return " {keyword} {column} {equality} {value}"
|
|
188
|
+
|
|
189
|
+
def having_string(self):
|
|
190
|
+
return "HAVING {column}"
|
|
191
|
+
|
|
192
|
+
def having_equality_string(self):
|
|
193
|
+
return "HAVING {column} {equality} {value}"
|
|
194
|
+
|
|
195
|
+
def where_null_string(self):
|
|
196
|
+
return " {keyword} {column} IS NULL"
|
|
197
|
+
|
|
198
|
+
def where_not_null_string(self):
|
|
199
|
+
return " {keyword} {column} IS NOT NULL"
|
|
200
|
+
|
|
201
|
+
def truncate_table(self, table, foreign_keys=False):
|
|
202
|
+
"""Specifies a truncate table expression.
|
|
203
|
+
|
|
204
|
+
Arguments;
|
|
205
|
+
table {string} -- The name of the table to truncate.
|
|
206
|
+
|
|
207
|
+
Returns:
|
|
208
|
+
string
|
|
209
|
+
"""
|
|
210
|
+
return f"TRUNCATE TABLE {self.wrap_table(table)}"
|
|
211
|
+
|
|
212
|
+
def compile_random(self):
|
|
213
|
+
return "random()"
|