@an5/adapters 0.1.1 → 0.1.3
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.
- package/package.json +1 -1
- package/python/table_client.py +23 -13
package/package.json
CHANGED
package/python/table_client.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import json
|
|
2
2
|
import uuid
|
|
3
3
|
from typing import Dict, List, Optional
|
|
4
|
-
|
|
4
|
+
try:
|
|
5
|
+
from .base import DIALECT_MSSQL, DIALECT_POSTGRES, model_fields, _build_order_by, _parse_where, _quote, _quote_table, _resolve_table
|
|
6
|
+
except ImportError:
|
|
7
|
+
from base import DIALECT_MSSQL, DIALECT_POSTGRES, model_fields, _build_order_by, _parse_where, _quote, _quote_table, _resolve_table
|
|
5
8
|
|
|
6
9
|
# ─── Table Client ───────────────────────────────────────────────────────────────────
|
|
7
10
|
|
|
@@ -23,13 +26,26 @@ class AdapterTableClient:
|
|
|
23
26
|
def _nolock(self) -> str:
|
|
24
27
|
return "" if self._dialect == DIALECT_POSTGRES else " WITH (NOLOCK)"
|
|
25
28
|
|
|
29
|
+
@property
|
|
30
|
+
def _fields(self) -> List[Dict]:
|
|
31
|
+
fields = model_fields.get(self._model, [])
|
|
32
|
+
if isinstance(fields, dict):
|
|
33
|
+
normalized = []
|
|
34
|
+
for name, value in fields.items():
|
|
35
|
+
if isinstance(value, dict):
|
|
36
|
+
normalized.append({"name": name, **value})
|
|
37
|
+
else:
|
|
38
|
+
normalized.append({"name": name, "type": value})
|
|
39
|
+
return normalized
|
|
40
|
+
return fields
|
|
41
|
+
|
|
26
42
|
def _pagination(self, take: Optional[int], skip: int, order_sql: str) -> str:
|
|
27
43
|
if take is None:
|
|
28
44
|
return ""
|
|
29
45
|
if self._dialect == DIALECT_POSTGRES:
|
|
30
46
|
return f" LIMIT {take} OFFSET {skip}"
|
|
31
|
-
|
|
32
|
-
return f"{
|
|
47
|
+
order_prefix = "" if order_sql else " ORDER BY (SELECT NULL)"
|
|
48
|
+
return f"{order_prefix} OFFSET {skip} ROWS FETCH NEXT {take} ROWS ONLY"
|
|
33
49
|
|
|
34
50
|
def find_many(self, where=None, order_by=None, skip: int = 0, take: Optional[int] = None, select=None) -> List[Dict]:
|
|
35
51
|
params: Dict = {}
|
|
@@ -39,12 +55,9 @@ class AdapterTableClient:
|
|
|
39
55
|
query = f"SELECT * FROM {self._table_sql}{self._nolock}"
|
|
40
56
|
if where_sql:
|
|
41
57
|
query += f" WHERE {where_sql}"
|
|
42
|
-
if
|
|
43
|
-
query +=
|
|
44
|
-
|
|
45
|
-
if order_sql:
|
|
46
|
-
query += f" {order_sql}"
|
|
47
|
-
query += self._pagination(take, skip, order_sql)
|
|
58
|
+
if order_sql:
|
|
59
|
+
query += f" {order_sql}"
|
|
60
|
+
query += self._pagination(take, skip, order_sql)
|
|
48
61
|
return self._adapter.exec(query, list(params.values()))
|
|
49
62
|
|
|
50
63
|
def find_first(self, where=None, order_by=None, select=None) -> Optional[Dict]:
|
|
@@ -64,8 +77,7 @@ class AdapterTableClient:
|
|
|
64
77
|
return int(rows[0]["cnt"]) if rows else 0
|
|
65
78
|
|
|
66
79
|
def create(self, data: Dict) -> Dict:
|
|
67
|
-
|
|
68
|
-
id_field = next((f for f in fields if f.get("isId")), None)
|
|
80
|
+
id_field = next((f for f in self._fields if f.get("isId")), None)
|
|
69
81
|
if id_field and id_field["name"] not in data:
|
|
70
82
|
data = {**data, id_field["name"]: str(uuid.uuid4())}
|
|
71
83
|
|
|
@@ -205,5 +217,3 @@ class AdapterTableClient:
|
|
|
205
217
|
|
|
206
218
|
scored.sort(key=lambda x: x[1])
|
|
207
219
|
return [{**row, "distance": dist} for row, dist in scored[:take]]
|
|
208
|
-
|
|
209
|
-
|