slinn-orm 0.0.5__tar.gz → 0.0.7__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: slinn-orm
3
- Version: 0.0.5
3
+ Version: 0.0.7
4
4
  Summary: ORM for Slinn
5
5
  Author-email: Mark Radin <openmiot@gmail.com>
6
6
  Requires-Python: >=3.14
@@ -48,3 +48,13 @@ class PoolProtocol(Protocol):
48
48
 
49
49
  def get_driver_name(dsn: str) -> str:
50
50
  return DSN_PATTERN.findall(dsn)[0][0].lower()
51
+
52
+
53
+ def get_pool(dsn: str) -> PoolProtocol:
54
+ match get_driver_name(dsn):
55
+ case 'sqlite' | 'sqlite3':
56
+ from orm.sqlite import SQLite
57
+ return SQLite(dsn)
58
+ case 'postgres' | 'pg':
59
+ from orm.postgres import Postgres
60
+ return Postgres(dsn)
@@ -3,6 +3,7 @@ from typing import Literal, Optional
3
3
  from datetime import timedelta, datetime
4
4
  from typing import NamedTuple, Any
5
5
  from ipaddress import IPv4Address, IPv6Address, ip_address
6
+ import enum
6
7
 
7
8
 
8
9
  class Model(ABC):
@@ -94,6 +95,11 @@ class BoolField(Field): ...
94
95
  class IntegerField(Field):
95
96
  def __init__(self, *, size: Literal[2, 4, 8] = 4):
96
97
  self.size = size
98
+
99
+ def __serialize__(self, value: Any) -> int:
100
+ if isinstance(value, enum.Enum):
101
+ return value.value
102
+ return int(value)
97
103
 
98
104
 
99
105
  class DecimalField(Field): ...
@@ -104,7 +110,11 @@ class FloatField(Field):
104
110
  self.double = double
105
111
 
106
112
 
107
- class TextField(Field): ...
113
+ class TextField(Field):
114
+ def __serialize__(self, value: Any) -> str:
115
+ if isinstance(value, enum.Enum):
116
+ return value.name
117
+ return str(value)
108
118
 
109
119
 
110
120
  class TimeField(Field):
@@ -160,12 +160,11 @@ class PostgresConnection(ConnectionProtocol):
160
160
  self._connection = connection
161
161
  self._transaction = transaction
162
162
  self._autocommit = autocommit
163
- self._models = set()
164
163
  self.collection = self.__getattr__
165
164
 
166
165
  def __getattr__(self, key) -> CollectionProtocol:
167
166
  return PostgresCollection(
168
- self, key, model = next(iter(model for model in self._models if model.__collection_name__() == key))
167
+ self, key, model = next(iter(model for model in self._pool._models if model.__collection_name__() == key))
169
168
  )
170
169
 
171
170
  async def rollback(self) -> None:
@@ -205,7 +204,7 @@ class PostgresConnection(ConnectionProtocol):
205
204
  {f'PRIMARY KEY ({','.join(model._meta_primary_keys)})' if model._meta_primary_keys else ''}
206
205
  );
207
206
  '''.strip())
208
- self._models.add(model)
207
+ self._pool._models.add(model)
209
208
 
210
209
  async def create_index(
211
210
  self,
@@ -258,7 +257,8 @@ class PostgresPool(PoolProtocol):
258
257
  self._pool_factory = pool_factory
259
258
  self._autocommit = autocommit
260
259
  self._pool = None
261
- self._tasks = []
260
+ self._tasks = []
261
+ self._models = set()
262
262
 
263
263
  async def acquire(self) -> PostgresConnection:
264
264
  if not self._pool:
@@ -197,12 +197,11 @@ class SQLiteConnection(ConnectionProtocol):
197
197
  self._connection = connection
198
198
  self._autocommit = autocommit
199
199
  self._in_transaction = False
200
- self._models = set()
201
200
  self.collection = self.__getattr__
202
201
 
203
202
  def __getattr__(self, key) -> CollectionProtocol:
204
203
  return SQLiteCollection(
205
- self, key, model = next(iter(model for model in self._models if model.__collection_name__() == key))
204
+ self, key, model = next(iter(model for model in self._pool._models if model.__collection_name__() == key))
206
205
  )
207
206
 
208
207
  async def rollback(self) -> None:
@@ -244,7 +243,7 @@ class SQLiteConnection(ConnectionProtocol):
244
243
  {f',PRIMARY KEY ({','.join(model._meta_primary_keys)})' if len(model._meta_primary_keys) > 1 else ''}
245
244
  );
246
245
  '''.strip())
247
- self._models.add(model)
246
+ self._pool._models.add(model)
248
247
 
249
248
  async def create_index(
250
249
  self,
@@ -252,8 +251,8 @@ class SQLiteConnection(ConnectionProtocol):
252
251
  collection_name: str,
253
252
  fields: tuple[str],
254
253
  *,
255
- exists_ok: bool=False,
256
- unique: bool=False
254
+ exists_ok: bool = False,
255
+ unique: bool = False
257
256
  ) -> None:
258
257
  print(f"""
259
258
  CREATE {'UNIQUE ' if unique else ''}INDEX {'IF NOT EXISTS ' if exists_ok else ''}{index_name} ON {collection_name}({','.join(fields)});
@@ -310,6 +309,7 @@ class SQLitePool(PoolProtocol):
310
309
  self._lock = asyncio.Lock()
311
310
  self._tasks: list[Coroutine[Any, Any, Any]] = []
312
311
  self._closed = False
312
+ self._models = set()
313
313
 
314
314
  async def acquire(self) -> SQLiteConnection:
315
315
  async with self._lock:
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "slinn-orm"
7
- version = "0.0.5"
7
+ version = "0.0.7"
8
8
  description = "ORM for Slinn"
9
9
  requires-python = ">=3.14"
10
10
  authors = [{ name = "Mark Radin", email = "openmiot@gmail.com" }]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: slinn-orm
3
- Version: 0.0.5
3
+ Version: 0.0.7
4
4
  Summary: ORM for Slinn
5
5
  Author-email: Mark Radin <openmiot@gmail.com>
6
6
  Requires-Python: >=3.14
File without changes
File without changes
File without changes
File without changes