-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
269 lines (220 loc) · 9.29 KB
/
database.py
File metadata and controls
269 lines (220 loc) · 9.29 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""Database access layer for SQLite database interactions."""
from __future__ import annotations
from dataclasses import dataclass, field
from pathlib import Path
from typing import Iterable, List, Optional, Sequence, Tuple
import sqlite3
DEFAULT_ROW_LIMIT = 200
QUERY_ROW_LIMIT = 1000
_READ_KEYWORDS = {"SELECT", "WITH", "PRAGMA", "EXPLAIN"}
_DML_KEYWORDS = {"INSERT", "UPDATE", "DELETE", "REPLACE"}
_DDL_KEYWORDS = {"CREATE", "ALTER", "DROP"}
_TCL_KEYWORDS = {"BEGIN", "COMMIT", "ROLLBACK", "SAVEPOINT", "RELEASE"}
class DatabaseError(RuntimeError):
"""Raised when a database operation fails."""
@dataclass(slots=True)
class QueryResult:
"""Container for tabular data returned to the UI layer."""
columns: List[str]
rows: List[Sequence[object]]
truncated: bool = False
row_count: Optional[int] = None
affected_rows: Optional[int] = None
is_write_operation: bool = False
class DatabaseService:
"""High-level helper for SQLite database interactions."""
def __init__(self) -> None:
self._connection: Optional[sqlite3.Connection] = None
self._path: Optional[str] = None
@property
def path(self) -> Optional[str]:
return self._path
def open(self, database_path: str | Path) -> None:
"""Open a SQLite database, closing any previous connection."""
path = Path(database_path).expanduser().resolve()
if not path.exists():
raise DatabaseError(f"Database file not found: {path}")
self.close()
try:
conn = sqlite3.connect(path, check_same_thread=False, isolation_level=None)
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA foreign_keys = ON")
except sqlite3.Error as exc:
raise DatabaseError(f"Failed to open database: {exc}") from exc
self._connection = conn
self._path = str(path)
def close(self) -> None:
"""Close the active database connection if present."""
if self._connection is not None:
self._connection.close()
self._connection = None
self._path = None
def list_tables(self) -> List[str]:
"""Return user tables ordered alphabetically."""
rows = self._execute(
"SELECT name FROM sqlite_master WHERE type IN ('table', 'view') "
"AND name NOT LIKE 'sqlite_%' ORDER BY lower(name)"
)
return [row[0] for row in rows]
def get_table_preview(self, table_name: str, limit: int = DEFAULT_ROW_LIMIT, offset: int = 0) -> QueryResult:
"""Return a preview of the given table."""
connection = self._ensure_connection()
quoted_table = self._quote_identifier(table_name)
try:
cursor = connection.execute(
f"SELECT * FROM {quoted_table} LIMIT ? OFFSET ?",
(limit + 1, offset),
)
rows = cursor.fetchmany(limit + 1)
except sqlite3.Error as exc:
raise DatabaseError(f"Failed to fetch table '{table_name}': {exc}") from exc
columns = [description[0] for description in cursor.description or []]
truncated = len(rows) > limit
trimmed_rows = [tuple(row) for row in rows[:limit]]
row_count = self._get_table_row_count(table_name)
return QueryResult(columns=columns, rows=trimmed_rows, truncated=truncated, row_count=row_count)
def get_table_schema(self, table_name: str) -> str:
"""Return the CREATE statement for the table if available."""
rows = self._execute(
"SELECT sql FROM sqlite_master WHERE name = ? AND type IN ('table', 'view')",
(table_name,),
)
if not rows or rows[0][0] is None:
return "Schema information not found."
return rows[0][0]
def execute_query(self, sql: str, limit: int = QUERY_ROW_LIMIT) -> QueryResult:
"""Execute a SQL statement and return results."""
self._ensure_connection()
sql = sql.strip()
if not sql:
raise DatabaseError("Query is empty.")
try:
cursor = self._connection.execute(sql)
except sqlite3.Error as exc:
raise DatabaseError(f"Failed to execute query: {exc}") from exc
if cursor.description is None:
# Write operation (INSERT/UPDATE/DELETE/DDL/TCL)
affected = cursor.rowcount if cursor.rowcount >= 0 else None
return QueryResult(
columns=[],
rows=[],
affected_rows=affected,
is_write_operation=True,
)
columns = [description[0] for description in cursor.description]
rows = cursor.fetchmany(limit + 1)
truncated = len(rows) > limit
trimmed_rows = [tuple(row) for row in rows[:limit]]
return QueryResult(columns=columns, rows=trimmed_rows, truncated=truncated)
def classify_query(self, sql: str) -> str:
"""Classify a SQL statement as read/dml/ddl/tcl/unknown."""
keyword = self._extract_first_keyword(sql)
if keyword is None:
return "unknown"
if keyword in _READ_KEYWORDS:
return "read"
if keyword in _DML_KEYWORDS:
return "dml"
if keyword in _DDL_KEYWORDS:
return "ddl"
if keyword in _TCL_KEYWORDS:
return "tcl"
return "unknown"
def is_destructive_query(self, sql: str) -> Tuple[bool, str]:
"""Detect potentially destructive queries.
Returns (is_destructive, reason). Checks for:
- DROP statements
- DELETE without a WHERE clause
"""
keyword = self._extract_first_keyword(sql)
if keyword == "DROP":
return True, "This will permanently drop the object."
if keyword == "DELETE":
stripped = self._strip_sql_comments(sql).upper()
if "WHERE" not in stripped:
return True, "DELETE without WHERE will remove all rows."
return False, ""
def _strip_sql_comments(self, sql: str) -> str:
"""Remove SQL comments (-- and /* */) from a statement."""
result = []
i = 0
length = len(sql)
while i < length:
if sql[i] == '-' and i + 1 < length and sql[i + 1] == '-':
newline = sql.find('\n', i + 2)
i = length if newline == -1 else newline + 1
elif sql[i] == '/' and i + 1 < length and sql[i + 1] == '*':
end = sql.find('*/', i + 2)
i = length if end == -1 else end + 2
elif sql[i] == "'":
result.append(sql[i])
i += 1
while i < length:
result.append(sql[i])
if sql[i] == "'" and (i + 1 >= length or sql[i + 1] != "'"):
i += 1
break
if sql[i] == "'" and i + 1 < length and sql[i + 1] == "'":
result.append(sql[i + 1])
i += 2
else:
i += 1
else:
result.append(sql[i])
i += 1
return ''.join(result)
def _get_table_row_count(self, table_name: str) -> Optional[int]:
"""Return row count for table; failure returns None."""
try:
rows = self._execute(
f"SELECT COUNT(*) FROM {self._quote_identifier(table_name)}"
)
except DatabaseError:
return None
return int(rows[0][0]) if rows else None
def _execute(self, sql: str, parameters: Iterable[object] | None = None):
connection = self._ensure_connection()
try:
cursor = connection.execute(sql, tuple(parameters or []))
return cursor.fetchall()
except sqlite3.Error as exc:
raise DatabaseError(str(exc)) from exc
def _ensure_connection(self) -> sqlite3.Connection:
if self._connection is None:
raise DatabaseError("No database open.")
return self._connection
def _quote_identifier(self, identifier: str) -> str:
if not identifier:
raise DatabaseError("Identifier cannot be empty.")
return '"' + identifier.replace('"', '""') + '"'
def _extract_first_keyword(self, sql: str) -> Optional[str]:
index = 0
length = len(sql)
while index < length:
char = sql[index]
if char.isspace():
index += 1
continue
if char == '-' and index + 1 < length and sql[index + 1] == '-':
newline = sql.find('\n', index + 2)
index = length if newline == -1 else newline + 1
continue
if char == '/' and index + 1 < length and sql[index + 1] == '*':
end = sql.find('*/', index + 2)
index = length if end == -1 else end + 2
continue
if char in '([':
index += 1
continue
start = index
while index < length and (sql[index].isalpha() or sql[index] == '_'):
index += 1
if start != index:
return sql[start:index].upper()
index += 1
return None
def __del__(self) -> None: # pragma: no cover - best effort cleanup
try:
self.close()
except Exception:
pass