-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresultset.py
More file actions
147 lines (112 loc) · 3.85 KB
/
resultset.py
File metadata and controls
147 lines (112 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from enum import Enum
from typing import Any, Dict, List, Optional
class SQLITECLOUD_VALUE_TYPE(Enum):
INTEGER = "INTEGER"
FLOAT = "REAL"
TEXT = "TEXT"
BLOB = "BLOB"
NULL = "NULL"
class SQLITECLOUD_RESULT_TYPE(Enum):
RESULT_OK = 0
RESULT_ERROR = 1
RESULT_STRING = 2
RESULT_INTEGER = 3
RESULT_FLOAT = 4
RESULT_ROWSET = 5
RESULT_ARRAY = 6
RESULT_NONE = 7
RESULT_JSON = 8
RESULT_BLOB = 9
class SQLiteCloudResult:
def __init__(
self, tag: SQLITECLOUD_RESULT_TYPE, result: Optional[any] = None
) -> None:
self.tag: SQLITECLOUD_RESULT_TYPE = tag
self.nrows: int = 0
self.ncols: int = 0
self.version: int = 0
# table values are stored in 1-dimensional array
self.data: List[Any] = []
self.colname: List[str] = []
self.decltype: List[str] = []
self.dbname: List[str] = []
self.tblname: List[str] = []
self.origname: List[str] = []
self.notnull: List[str] = []
self.prikey: List[str] = []
self.autoinc: List[str] = []
self.is_result: bool = False
if result is not None:
self.init_data(result)
def init_data(self, result: any) -> None:
self.nrows = 1
self.ncols = 1
self.data = [result]
self.is_result = True
def _compute_index(self, row: int, col: int) -> int:
if row < 0 or row >= self.nrows:
return -1
if col < 0 or col >= self.ncols:
return -1
return row * self.ncols + col
def get_value(self, row: int, col: int) -> Optional[any]:
index = self._compute_index(row, col)
if index < 0 or not self.data or index >= len(self.data):
return None
return self.data[index]
def get_name(self, col: int) -> Optional[str]:
if col < 0 or col >= self.ncols:
return None
return self.colname[col]
def get_decltype(self, col: int) -> Optional[str]:
if col < 0 or col >= self.ncols or col >= len(self.decltype):
return None
return self.decltype[col]
class SQLiteCloudResultSet:
def __init__(self, result: SQLiteCloudResult) -> None:
self._iter_row: int = 0
self._result: SQLiteCloudResult = result
def __getattr__(self, attr: str) -> Optional[Any]:
return getattr(self._result, attr)
def __iter__(self):
return self
def __next__(self):
if self._result.data and self._iter_row < self._result.nrows:
out: Dict[str, any] = {}
if self._result.is_result:
out = {"result": self.get_value(0, 0)}
self._iter_row += 1
else:
for col in range(self._result.ncols):
out[self.get_name(col)] = self.get_value(self._iter_row, col)
self._iter_row += 1
return out
raise StopIteration
def get_value(self, row: int, col: int) -> Optional[any]:
return self._result.get_value(row, col)
def get_name(self, col: int) -> Optional[str]:
return self._result.get_name(col)
def get_result(self) -> Optional[any]:
return self.get_value(0, 0)
class SQLiteCloudOperationResult:
"""Result of a DML operation in a SQLite statement."""
def __init__(self, result: SQLiteCloudResult) -> None:
self._result = result
@property
def type(self) -> int:
return self._result.data[0][0]
@property
def index(self) -> int:
return self._result.data[0][1]
@property
def rowid(self) -> int:
return self._result.data[0][2]
@property
def changes(self) -> int:
return self._result.data[0][3]
@property
def total_changes(self) -> int:
return self._result.data[0][4]
@property
def finalized(self) -> int:
return self._result.data[0][5]