Function
execute_query
(path: str, sql: str, write_mode: bool = False)
Source from the content-addressed store, hash-verified
| 131 | |
| 132 | |
| 133 | def execute_query(path: str, sql: str, write_mode: bool = False) -> dict: |
| 134 | conn = open_db(path) |
| 135 | cursor = conn.cursor() |
| 136 | |
| 137 | if not write_mode: |
| 138 | cursor.execute("PRAGMA query_only = ON") |
| 139 | |
| 140 | start = time.time() |
| 141 | cursor.execute(sql) |
| 142 | elapsed = time.time() - start |
| 143 | |
| 144 | if cursor.description: |
| 145 | columns = [desc[0] for desc in cursor.description] |
| 146 | rows = [dict(row) for row in cursor.fetchmany(1000)] |
| 147 | else: |
| 148 | columns = [] |
| 149 | rows = [] |
| 150 | |
| 151 | affected = cursor.rowcount if not cursor.description else 0 |
| 152 | |
| 153 | if write_mode: |
| 154 | conn.commit() |
| 155 | |
| 156 | conn.close() |
| 157 | |
| 158 | return { |
| 159 | "columns": columns, |
| 160 | "rows": rows, |
| 161 | "total": len(rows), |
| 162 | "time": elapsed, |
| 163 | "affected": affected, |
| 164 | } |
Callers
nothing calls this directly
Tested by
no test coverage detected