darkfire514 commited on
Commit
b9ac4cb
·
verified ·
1 Parent(s): 46e3043

Update openspace/skill_engine/store.py

Browse files
Files changed (1) hide show
  1. openspace/skill_engine/store.py +95 -2
openspace/skill_engine/store.py CHANGED
@@ -28,6 +28,98 @@ try:
28
  except ImportError:
29
  libsql = None
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  from .patch import collect_skill_snapshot, compute_unified_diff
32
  from .types import (
33
  EvolutionSuggestion,
@@ -216,8 +308,9 @@ class SkillStore:
216
 
217
  if turso_url and libsql is not None:
218
  # Connect to remote Turso database
219
- conn = libsql.connect(turso_url, auth_token=turso_token)
220
- conn.row_factory = sqlite3.Row
 
221
  return conn
222
 
223
  conn = sqlite3.connect(
 
28
  except ImportError:
29
  libsql = None
30
 
31
+ class _LibsqlCursorProxy:
32
+ def __init__(self, cursor, conn_proxy):
33
+ self._cursor = cursor
34
+ self._conn_proxy = conn_proxy
35
+
36
+ def execute(self, *args, **kwargs):
37
+ self._cursor.execute(*args, **kwargs)
38
+ return self
39
+
40
+ def executescript(self, *args, **kwargs):
41
+ self._cursor.executescript(*args, **kwargs)
42
+ return self
43
+
44
+ def fetchone(self):
45
+ row = self._cursor.fetchone()
46
+ if row is not None and self._conn_proxy.row_factory:
47
+ return self._conn_proxy.row_factory(self, row)
48
+ return row
49
+
50
+ def fetchall(self):
51
+ rows = self._cursor.fetchall()
52
+ if self._conn_proxy.row_factory:
53
+ return [self._conn_proxy.row_factory(self, row) for row in rows]
54
+ return rows
55
+
56
+ @property
57
+ def description(self):
58
+ return getattr(self._cursor, "description", [])
59
+
60
+ @property
61
+ def rowcount(self):
62
+ return getattr(self._cursor, "rowcount", -1)
63
+
64
+ @property
65
+ def lastrowid(self):
66
+ return getattr(self._cursor, "lastrowid", None)
67
+
68
+ class _LibsqlConnectionProxy:
69
+ def __init__(self, conn):
70
+ self._conn = conn
71
+ self.row_factory = None
72
+
73
+ def execute(self, *args, **kwargs):
74
+ cursor = self.cursor()
75
+ return cursor.execute(*args, **kwargs)
76
+
77
+ def executescript(self, *args, **kwargs):
78
+ cursor = self.cursor()
79
+ return cursor.executescript(*args, **kwargs)
80
+
81
+ def cursor(self):
82
+ return _LibsqlCursorProxy(self._conn.cursor(), self)
83
+
84
+ def commit(self):
85
+ if hasattr(self._conn, "commit"):
86
+ return self._conn.commit()
87
+
88
+ def rollback(self):
89
+ if hasattr(self._conn, "rollback"):
90
+ return self._conn.rollback()
91
+
92
+ def close(self):
93
+ if hasattr(self._conn, "close"):
94
+ return self._conn.close()
95
+
96
+ class _RowProxy:
97
+ def __init__(self, row, description):
98
+ self._row = row
99
+ self._description = description
100
+ self._col_map = {col[0]: idx for idx, col in enumerate(description)}
101
+
102
+ def __getitem__(self, item):
103
+ if isinstance(item, int):
104
+ return self._row[item]
105
+ if item in self._col_map:
106
+ return self._row[self._col_map[item]]
107
+ raise KeyError(item)
108
+
109
+ def keys(self):
110
+ return self._col_map.keys()
111
+
112
+ def __iter__(self):
113
+ return iter(self._row)
114
+
115
+ def __len__(self):
116
+ return len(self._row)
117
+
118
+ def _dict_factory(cursor, row):
119
+ if hasattr(cursor, "description") and cursor.description:
120
+ return _RowProxy(row, cursor.description)
121
+ return row
122
+
123
  from .patch import collect_skill_snapshot, compute_unified_diff
124
  from .types import (
125
  EvolutionSuggestion,
 
308
 
309
  if turso_url and libsql is not None:
310
  # Connect to remote Turso database
311
+ raw_conn = libsql.connect(turso_url, auth_token=turso_token)
312
+ conn = _LibsqlConnectionProxy(raw_conn)
313
+ conn.row_factory = _dict_factory
314
  return conn
315
 
316
  conn = sqlite3.connect(