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.
Files changed (116) hide show
  1. masonite_framework_orm-3.0.1.dist-info/METADATA +87 -0
  2. masonite_framework_orm-3.0.1.dist-info/RECORD +116 -0
  3. masonite_framework_orm-3.0.1.dist-info/WHEEL +5 -0
  4. masonite_framework_orm-3.0.1.dist-info/entry_points.txt +3 -0
  5. masonite_framework_orm-3.0.1.dist-info/licenses/LICENSE +21 -0
  6. masonite_framework_orm-3.0.1.dist-info/top_level.txt +1 -0
  7. masoniteorm/__init__.py +1 -0
  8. masoniteorm/collection/Collection.py +605 -0
  9. masoniteorm/collection/__init__.py +1 -0
  10. masoniteorm/commands/CanOverrideConfig.py +16 -0
  11. masoniteorm/commands/CanOverrideOptionsDefault.py +22 -0
  12. masoniteorm/commands/Command.py +6 -0
  13. masoniteorm/commands/Entry.py +43 -0
  14. masoniteorm/commands/MakeMigrationCommand.py +57 -0
  15. masoniteorm/commands/MakeModelCommand.py +78 -0
  16. masoniteorm/commands/MakeModelDocstringCommand.py +37 -0
  17. masoniteorm/commands/MakeObserverCommand.py +54 -0
  18. masoniteorm/commands/MakeSeedCommand.py +54 -0
  19. masoniteorm/commands/MigrateCommand.py +46 -0
  20. masoniteorm/commands/MigrateFreshCommand.py +41 -0
  21. masoniteorm/commands/MigrateRefreshCommand.py +41 -0
  22. masoniteorm/commands/MigrateResetCommand.py +25 -0
  23. masoniteorm/commands/MigrateRollbackCommand.py +26 -0
  24. masoniteorm/commands/MigrateStatusCommand.py +51 -0
  25. masoniteorm/commands/SeedRunCommand.py +35 -0
  26. masoniteorm/commands/ShellCommand.py +205 -0
  27. masoniteorm/commands/__init__.py +18 -0
  28. masoniteorm/commands/stubs/create_migration.stub +20 -0
  29. masoniteorm/commands/stubs/create_seed.stub +9 -0
  30. masoniteorm/commands/stubs/model.stub +9 -0
  31. masoniteorm/commands/stubs/observer.stub +101 -0
  32. masoniteorm/commands/stubs/table_migration.stub +19 -0
  33. masoniteorm/config.py +123 -0
  34. masoniteorm/connections/BaseConnection.py +101 -0
  35. masoniteorm/connections/ConnectionFactory.py +59 -0
  36. masoniteorm/connections/ConnectionResolver.py +132 -0
  37. masoniteorm/connections/MSSQLConnection.py +176 -0
  38. masoniteorm/connections/MySQLConnection.py +232 -0
  39. masoniteorm/connections/PostgresConnection.py +225 -0
  40. masoniteorm/connections/SQLiteConnection.py +179 -0
  41. masoniteorm/connections/__init__.py +6 -0
  42. masoniteorm/exceptions.py +38 -0
  43. masoniteorm/expressions/__init__.py +1 -0
  44. masoniteorm/expressions/expressions.py +288 -0
  45. masoniteorm/factories/Factory.py +112 -0
  46. masoniteorm/factories/__init__.py +1 -0
  47. masoniteorm/helpers/__init__.py +0 -0
  48. masoniteorm/helpers/misc.py +22 -0
  49. masoniteorm/migrations/Migration.py +330 -0
  50. masoniteorm/migrations/__init__.py +1 -0
  51. masoniteorm/models/MigrationModel.py +9 -0
  52. masoniteorm/models/Model.py +1209 -0
  53. masoniteorm/models/Model.pyi +1366 -0
  54. masoniteorm/models/Pivot.py +5 -0
  55. masoniteorm/models/__init__.py +1 -0
  56. masoniteorm/observers/ObservesEvents.py +27 -0
  57. masoniteorm/observers/__init__.py +1 -0
  58. masoniteorm/pagination/BasePaginator.py +10 -0
  59. masoniteorm/pagination/LengthAwarePaginator.py +34 -0
  60. masoniteorm/pagination/SimplePaginator.py +28 -0
  61. masoniteorm/pagination/__init__.py +2 -0
  62. masoniteorm/providers/ORMProvider.py +39 -0
  63. masoniteorm/providers/__init__.py +1 -0
  64. masoniteorm/query/EagerRelation.py +42 -0
  65. masoniteorm/query/QueryBuilder.py +2486 -0
  66. masoniteorm/query/__init__.py +1 -0
  67. masoniteorm/query/grammars/BaseGrammar.py +1027 -0
  68. masoniteorm/query/grammars/MSSQLGrammar.py +194 -0
  69. masoniteorm/query/grammars/MySQLGrammar.py +238 -0
  70. masoniteorm/query/grammars/PostgresGrammar.py +213 -0
  71. masoniteorm/query/grammars/SQLiteGrammar.py +228 -0
  72. masoniteorm/query/grammars/__init__.py +4 -0
  73. masoniteorm/query/processors/MSSQLPostProcessor.py +58 -0
  74. masoniteorm/query/processors/MySQLPostProcessor.py +48 -0
  75. masoniteorm/query/processors/PostgresPostProcessor.py +49 -0
  76. masoniteorm/query/processors/SQLitePostProcessor.py +49 -0
  77. masoniteorm/query/processors/__init__.py +4 -0
  78. masoniteorm/relationships/BaseRelationship.py +161 -0
  79. masoniteorm/relationships/BelongsTo.py +124 -0
  80. masoniteorm/relationships/BelongsToMany.py +604 -0
  81. masoniteorm/relationships/HasMany.py +66 -0
  82. masoniteorm/relationships/HasManyThrough.py +269 -0
  83. masoniteorm/relationships/HasOne.py +111 -0
  84. masoniteorm/relationships/HasOneThrough.py +275 -0
  85. masoniteorm/relationships/MorphMany.py +152 -0
  86. masoniteorm/relationships/MorphOne.py +156 -0
  87. masoniteorm/relationships/MorphTo.py +111 -0
  88. masoniteorm/relationships/MorphToMany.py +108 -0
  89. masoniteorm/relationships/__init__.py +10 -0
  90. masoniteorm/schema/Blueprint.py +1161 -0
  91. masoniteorm/schema/Column.py +144 -0
  92. masoniteorm/schema/ColumnDiff.py +0 -0
  93. masoniteorm/schema/Constraint.py +5 -0
  94. masoniteorm/schema/ForeignKeyConstraint.py +28 -0
  95. masoniteorm/schema/Index.py +5 -0
  96. masoniteorm/schema/Schema.py +359 -0
  97. masoniteorm/schema/Table.py +94 -0
  98. masoniteorm/schema/TableDiff.py +86 -0
  99. masoniteorm/schema/__init__.py +3 -0
  100. masoniteorm/schema/platforms/MSSQLPlatform.py +367 -0
  101. masoniteorm/schema/platforms/MySQLPlatform.py +513 -0
  102. masoniteorm/schema/platforms/Platform.py +97 -0
  103. masoniteorm/schema/platforms/PostgresPlatform.py +551 -0
  104. masoniteorm/schema/platforms/SQLitePlatform.py +481 -0
  105. masoniteorm/schema/platforms/__init__.py +4 -0
  106. masoniteorm/scopes/BaseScope.py +6 -0
  107. masoniteorm/scopes/SoftDeleteScope.py +56 -0
  108. masoniteorm/scopes/SoftDeletesMixin.py +13 -0
  109. masoniteorm/scopes/TimeStampsMixin.py +12 -0
  110. masoniteorm/scopes/TimeStampsScope.py +47 -0
  111. masoniteorm/scopes/UUIDPrimaryKeyMixin.py +8 -0
  112. masoniteorm/scopes/UUIDPrimaryKeyScope.py +51 -0
  113. masoniteorm/scopes/__init__.py +8 -0
  114. masoniteorm/scopes/scope.py +15 -0
  115. masoniteorm/seeds/Seeder.py +42 -0
  116. masoniteorm/seeds/__init__.py +1 -0
@@ -0,0 +1,225 @@
1
+ from ..exceptions import DriverNotFound, QueryException
2
+ from ..query.grammars import PostgresGrammar
3
+ from ..query.processors import PostgresPostProcessor
4
+ from ..schema.platforms import PostgresPlatform
5
+ from .BaseConnection import BaseConnection
6
+
7
+ CONNECTION_POOL = []
8
+
9
+
10
+ class PostgresConnection(BaseConnection):
11
+ """Postgres Connection class."""
12
+
13
+ name = "postgres"
14
+
15
+ def __init__(
16
+ self,
17
+ host=None,
18
+ database=None,
19
+ user=None,
20
+ port=None,
21
+ password=None,
22
+ prefix=None,
23
+ options=None,
24
+ full_details=None,
25
+ name=None,
26
+ ):
27
+ self.host = host
28
+ if port:
29
+ self.port = int(port)
30
+ else:
31
+ self.port = port
32
+ self.database = database
33
+ self.user = user
34
+ self.password = password
35
+
36
+ self.prefix = prefix
37
+ self.full_details = full_details or {}
38
+ self.connection_pool_size = full_details.get(
39
+ "connection_pooling_max_size", 100
40
+ )
41
+ self.options = options or {}
42
+ self._cursor = None
43
+ self.transaction_level = 0
44
+ self.open = 0
45
+ self.schema = None
46
+ if name:
47
+ self.name = name
48
+
49
+ def make_connection(self):
50
+ """This sets the connection on the connection class"""
51
+ if self.has_global_connection():
52
+ return self.get_global_connection()
53
+
54
+ self._connection = self.create_connection()
55
+
56
+ self._connection.autocommit = True
57
+
58
+ self.enable_disable_foreign_keys()
59
+
60
+ self.open = 1
61
+
62
+ return self
63
+
64
+ def create_connection(self):
65
+ try:
66
+ import psycopg2
67
+ except ModuleNotFoundError:
68
+ raise DriverNotFound(
69
+ "You must have the 'psycopg2' package installed to make a connection to Postgres. Please install it using 'pip install psycopg2-binary'"
70
+ )
71
+
72
+ # Initialize the connection pool if the option is set
73
+ initialize_size = self.full_details.get("connection_pooling_min_size")
74
+ if (
75
+ self.full_details.get("connection_pooling_enabled")
76
+ and initialize_size
77
+ and len(CONNECTION_POOL) < initialize_size
78
+ ):
79
+ for _ in range(initialize_size - len(CONNECTION_POOL)):
80
+ connection = psycopg2.connect(
81
+ database=self.database,
82
+ user=self.user,
83
+ password=self.password,
84
+ host=self.host,
85
+ port=self.port,
86
+ sslmode=self.options.get("sslmode"),
87
+ sslcert=self.options.get("sslcert"),
88
+ sslkey=self.options.get("sslkey"),
89
+ sslrootcert=self.options.get("sslrootcert"),
90
+ options=(
91
+ f"-c search_path={self.schema or self.full_details.get('schema')}"
92
+ if self.schema or self.full_details.get("schema")
93
+ else ""
94
+ ),
95
+ )
96
+ CONNECTION_POOL.append(connection)
97
+
98
+ if (
99
+ self.full_details.get("connection_pooling_enabled")
100
+ and CONNECTION_POOL
101
+ and len(CONNECTION_POOL) > 0
102
+ ):
103
+ connection = CONNECTION_POOL.pop()
104
+ else:
105
+ connection = psycopg2.connect(
106
+ database=self.database,
107
+ user=self.user,
108
+ password=self.password,
109
+ host=self.host,
110
+ port=self.port,
111
+ sslmode=self.options.get("sslmode"),
112
+ sslcert=self.options.get("sslcert"),
113
+ sslkey=self.options.get("sslkey"),
114
+ sslrootcert=self.options.get("sslrootcert"),
115
+ options=(
116
+ f"-c search_path={self.schema or self.full_details.get('schema')}"
117
+ if self.schema or self.full_details.get("schema")
118
+ else ""
119
+ ),
120
+ )
121
+
122
+ return connection
123
+
124
+ def get_database_name(self):
125
+ return self.database
126
+
127
+ @classmethod
128
+ def get_default_query_grammar(cls):
129
+ return PostgresGrammar
130
+
131
+ @classmethod
132
+ def get_default_platform(cls):
133
+ return PostgresPlatform
134
+
135
+ @classmethod
136
+ def get_default_post_processor(cls):
137
+ return PostgresPostProcessor
138
+
139
+ def reconnect(self):
140
+ pass
141
+
142
+ def close_connection(self):
143
+ if (
144
+ self.full_details.get("connection_pooling_enabled")
145
+ and len(CONNECTION_POOL) < self.connection_pool_size
146
+ ):
147
+ CONNECTION_POOL.append(self._connection)
148
+ else:
149
+ self._connection.close()
150
+
151
+ self._connection = None
152
+
153
+ def commit(self):
154
+ """Transaction"""
155
+ if self.get_transaction_level() == 1:
156
+ self._connection.commit()
157
+ self._connection.autocommit = True
158
+
159
+ self.transaction_level -= 1
160
+
161
+ def begin(self):
162
+ """Postgres Transaction"""
163
+ self._connection.autocommit = False
164
+ self.transaction_level += 1
165
+ return self
166
+
167
+ def rollback(self):
168
+ """Transaction"""
169
+ if self.get_transaction_level() == 1:
170
+ self._connection.rollback()
171
+ self._connection.autocommit = True
172
+
173
+ self.transaction_level -= 1
174
+
175
+ def get_transaction_level(self):
176
+ """Transaction"""
177
+ return self.transaction_level
178
+
179
+ def set_cursor(self):
180
+ from psycopg2.extras import RealDictCursor
181
+
182
+ self._cursor = self._connection.cursor(cursor_factory=RealDictCursor)
183
+ return self._cursor
184
+
185
+ def query(self, query, bindings=(), results="*"):
186
+ """Make the actual query that will reach the database and come back with a result.
187
+
188
+ Arguments:
189
+ query {string} -- A string query. This could be a qmarked string or a regular query.
190
+ bindings {tuple} -- A tuple of bindings
191
+
192
+ Keyword Arguments:
193
+ results {str|1} -- If the results is equal to an asterisks it will call 'fetchAll'
194
+ else it will return 'fetchOne' and return a single record. (default: {"*"})
195
+
196
+ Returns:
197
+ dict|None -- Returns a dictionary of results or None
198
+ """
199
+ try:
200
+ if not self._connection or self._connection.closed:
201
+ self.make_connection()
202
+
203
+ self.set_cursor()
204
+
205
+ with self._cursor as cursor:
206
+ if isinstance(query, list) and not self._dry:
207
+ for q in query:
208
+ self.statement(q, ())
209
+ return
210
+
211
+ query = query.replace("?", "%s")
212
+ self.statement(query, bindings)
213
+ if results == 1:
214
+ return dict(cursor.fetchone() or {})
215
+ else:
216
+ if "SELECT" in cursor.statusmessage:
217
+ return cursor.fetchall()
218
+ return {}
219
+ except Exception as e:
220
+ raise QueryException(str(e)) from e
221
+ finally:
222
+ if self.get_transaction_level() <= 0:
223
+ self.open = 0
224
+ self.close_connection()
225
+ # self._connection.close()
@@ -0,0 +1,179 @@
1
+ import re
2
+
3
+ from ..exceptions import DriverNotFound, QueryException
4
+ from ..query.grammars import SQLiteGrammar
5
+ from ..query.processors import SQLitePostProcessor
6
+ from ..schema.platforms import SQLitePlatform
7
+ from .BaseConnection import BaseConnection
8
+
9
+
10
+ def regexp(expr, item):
11
+ reg = re.compile(expr)
12
+ return reg.search(item) is not None
13
+
14
+
15
+ class SQLiteConnection(BaseConnection):
16
+ """SQLite Connection class."""
17
+
18
+ name = "sqlite"
19
+
20
+ _connection = None
21
+
22
+ def __init__(
23
+ self,
24
+ host=None,
25
+ database=None,
26
+ user=None,
27
+ port=None,
28
+ password=None,
29
+ prefix=None,
30
+ full_details=None,
31
+ options=None,
32
+ name=None,
33
+ ):
34
+ self.host = host
35
+ if port:
36
+ self.port = int(port)
37
+ else:
38
+ self.port = port
39
+ self.database = database
40
+ self.user = user
41
+ self.password = password
42
+ self.prefix = prefix
43
+ self.full_details = full_details or {}
44
+ self.options = options or {}
45
+ self._cursor = None
46
+ self.transaction_level = 0
47
+ self.open = 0
48
+ if name:
49
+ self.name = name
50
+
51
+ def make_connection(self):
52
+ """This sets the connection on the connection class"""
53
+ try:
54
+ import sqlite3
55
+ except ModuleNotFoundError:
56
+ raise DriverNotFound(
57
+ "You must have the 'sqlite3' package installed to make a connection to SQLite."
58
+ )
59
+
60
+ if self.has_global_connection():
61
+ return self.get_global_connection()
62
+
63
+ self._connection = sqlite3.connect(self.database, isolation_level=None)
64
+ self._connection.create_function("REGEXP", 2, regexp)
65
+
66
+ self._connection.row_factory = sqlite3.Row
67
+
68
+ self.enable_disable_foreign_keys()
69
+
70
+ self.open = 1
71
+
72
+ return self
73
+
74
+ @classmethod
75
+ def get_default_query_grammar(cls):
76
+ return SQLiteGrammar
77
+
78
+ @classmethod
79
+ def get_default_platform(cls):
80
+ return SQLitePlatform
81
+
82
+ @classmethod
83
+ def get_default_post_processor(cls):
84
+ return SQLitePostProcessor
85
+
86
+ def get_database_name(self):
87
+ return self.database
88
+
89
+ def reconnect(self):
90
+ pass
91
+
92
+ def commit(self):
93
+ """Transaction"""
94
+
95
+ if self.get_transaction_level() == 1:
96
+ self.transaction_level -= 1
97
+ self._connection.commit()
98
+ self._connection.isolation_level = None
99
+ self._connection.close()
100
+ self.open = 0
101
+
102
+ self.transaction_level -= 1
103
+ return self
104
+
105
+ def begin(self):
106
+ """Sqlite Transaction"""
107
+ self._connection.isolation_level = "DEFERRED"
108
+ self.transaction_level += 1
109
+ return self
110
+
111
+ def rollback(self):
112
+ """Transaction"""
113
+ if self.get_transaction_level() == 1:
114
+ self.transaction_level -= 1
115
+ self._connection.rollback()
116
+ self._connection.close()
117
+ self.open = 0
118
+
119
+ self.transaction_level -= 1
120
+ return self
121
+
122
+ def get_cursor(self):
123
+ return self._cursor
124
+
125
+ def get_transaction_level(self):
126
+ return self.transaction_level
127
+
128
+ def query(self, query, bindings=(), results="*"):
129
+ """Make the actual query that will reach the database and come back with a result.
130
+
131
+ Arguments:
132
+ query {string} -- A string query. This could be a qmarked string or a regular query.
133
+ bindings {tuple} -- A tuple of bindings
134
+
135
+ Keyword Arguments:
136
+ results {str|1} -- If the results is equal to an asterisks it will call 'fetchAll'
137
+ else it will return 'fetchOne' and return a single record. (default: {"*"})
138
+
139
+ Returns:
140
+ dict|None -- Returns a dictionary of results or None
141
+ """
142
+ if not self.open:
143
+ self.make_connection()
144
+
145
+ try:
146
+ self._cursor = self._connection.cursor()
147
+
148
+ if isinstance(query, list):
149
+ for query in query:
150
+ self.statement(query)
151
+ else:
152
+ self.statement(query, bindings)
153
+ if results == 1:
154
+ result = [dict(row) for row in self._cursor.fetchall()]
155
+ if result:
156
+ return result[0]
157
+ else:
158
+ return [dict(row) for row in self._cursor.fetchall()]
159
+ except Exception as e:
160
+ raise QueryException(str(e)) from e
161
+ finally:
162
+ if self.get_transaction_level() <= 0:
163
+ self._connection.close()
164
+ self.open = 0
165
+
166
+ def format_cursor_results(self, cursor_result):
167
+ return [dict(row) for row in cursor_result]
168
+
169
+ def select_many(self, query, bindings, amount):
170
+ self._cursor = self._connection.cursor()
171
+ self.statement(query)
172
+ if not self.open:
173
+ self.make_connection()
174
+
175
+ result = self.format_cursor_results(self._cursor.fetchmany(amount))
176
+ while result:
177
+ yield result
178
+
179
+ result = self.format_cursor_results(self._cursor.fetchmany(amount))
@@ -0,0 +1,6 @@
1
+ from .ConnectionFactory import ConnectionFactory
2
+ from .ConnectionResolver import ConnectionResolver
3
+ from .MSSQLConnection import MSSQLConnection
4
+ from .MySQLConnection import MySQLConnection
5
+ from .PostgresConnection import PostgresConnection
6
+ from .SQLiteConnection import SQLiteConnection
@@ -0,0 +1,38 @@
1
+ class DriverNotFound(Exception):
2
+ pass
3
+
4
+
5
+ class ModelNotFound(Exception):
6
+ pass
7
+
8
+
9
+ class HTTP404(Exception):
10
+ pass
11
+
12
+
13
+ class ConnectionNotRegistered(Exception):
14
+ pass
15
+
16
+
17
+ class QueryException(Exception):
18
+ pass
19
+
20
+
21
+ class MigrationNotFound(Exception):
22
+ pass
23
+
24
+
25
+ class ConfigurationNotFound(Exception):
26
+ pass
27
+
28
+
29
+ class InvalidUrlConfiguration(Exception):
30
+ pass
31
+
32
+
33
+ class MultipleRecordsFound(Exception):
34
+ pass
35
+
36
+
37
+ class InvalidArgument(Exception):
38
+ pass
@@ -0,0 +1 @@
1
+ from .expressions import JoinClause, Raw