Spaces:
Sleeping
Sleeping
Melika Kheirieh
commited on
Commit
·
65f2e2c
1
Parent(s):
630e8fe
chore(logging): add informative logs to SQLiteAdapter init and execute
Browse files
adapters/db/sqlite_adapter.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
import sqlite3
|
|
|
|
| 2 |
from typing import List, Tuple, Any
|
| 3 |
from adapters.db.base import DBAdapter
|
| 4 |
|
|
|
|
|
|
|
| 5 |
|
| 6 |
class SQLiteAdapter(DBAdapter):
|
| 7 |
name = "sqlite"
|
|
@@ -9,6 +12,7 @@ class SQLiteAdapter(DBAdapter):
|
|
| 9 |
|
| 10 |
def __init__(self, path: str):
|
| 11 |
self.path = path
|
|
|
|
| 12 |
|
| 13 |
def preview_schema(self, limit_per_table: int = 0) -> str:
|
| 14 |
with sqlite3.connect(self.path, uri=True) as conn:
|
|
@@ -25,9 +29,12 @@ class SQLiteAdapter(DBAdapter):
|
|
| 25 |
def execute(self, sql: str) -> Tuple[List[Tuple[Any, ...]], List[str]]:
|
| 26 |
# enforce read-only connection
|
| 27 |
uri = f"file:{self.path}?mode=ro&uri=true"
|
|
|
|
| 28 |
with sqlite3.connect(uri, uri=True, timeout=3) as conn:
|
| 29 |
cur = conn.cursor()
|
|
|
|
| 30 |
cur.execute(sql)
|
| 31 |
rows = cur.fetchall()
|
| 32 |
cols = [desc[0] for desc in cur.description]
|
|
|
|
| 33 |
return rows, cols
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
+
import logging
|
| 3 |
from typing import List, Tuple, Any
|
| 4 |
from adapters.db.base import DBAdapter
|
| 5 |
|
| 6 |
+
log = logging.getLogger(__name__)
|
| 7 |
+
|
| 8 |
|
| 9 |
class SQLiteAdapter(DBAdapter):
|
| 10 |
name = "sqlite"
|
|
|
|
| 12 |
|
| 13 |
def __init__(self, path: str):
|
| 14 |
self.path = path
|
| 15 |
+
log.info("SQLiteAdapter initialized with DB path: %s", self.path)
|
| 16 |
|
| 17 |
def preview_schema(self, limit_per_table: int = 0) -> str:
|
| 18 |
with sqlite3.connect(self.path, uri=True) as conn:
|
|
|
|
| 29 |
def execute(self, sql: str) -> Tuple[List[Tuple[Any, ...]], List[str]]:
|
| 30 |
# enforce read-only connection
|
| 31 |
uri = f"file:{self.path}?mode=ro&uri=true"
|
| 32 |
+
log.info("SQLiteAdapter opening read-only connection to: %s", uri)
|
| 33 |
with sqlite3.connect(uri, uri=True, timeout=3) as conn:
|
| 34 |
cur = conn.cursor()
|
| 35 |
+
log.debug("Executing SQL: %s", sql.strip().replace("\n", " "))
|
| 36 |
cur.execute(sql)
|
| 37 |
rows = cur.fetchall()
|
| 38 |
cols = [desc[0] for desc in cur.description]
|
| 39 |
+
log.info("Query executed successfully. Returned %d rows.", len(rows))
|
| 40 |
return rows, cols
|