|
17 | 17 | import json
|
18 | 18 | import threading
|
19 | 19 | from enum import Enum
|
20 |
| -from typing import Union |
| 20 | +from typing import Union, Dict, Any |
21 | 21 |
|
22 | 22 | import requests
|
23 | 23 | import portpicker
|
@@ -206,49 +206,70 @@ def execute_node_expansion(
|
206 | 206 |
|
207 | 207 | return execute_query(project, instance, database, query, mock=False)
|
208 | 208 |
|
209 |
| -def execute_query(project: str, instance: str, database: str, query: str, mock = False): |
210 |
| - database = get_database_instance(project, instance, database, mock) |
| 209 | +def execute_query( |
| 210 | + project: str, |
| 211 | + instance: str, |
| 212 | + database: str, |
| 213 | + query: str, |
| 214 | + mock: bool = False, |
| 215 | +) -> Dict[str, Any]: |
| 216 | + """Executes a query against a database and formats the result. |
211 | 217 |
|
| 218 | + Connects to a database, runs the query, and processes the resulting object. |
| 219 | + On success, it formats the data into nodes and edges for graph visualization. |
| 220 | + If the query fails, it returns a detailed error message, optionally |
| 221 | + including the database schema to aid in debugging. |
| 222 | +
|
| 223 | + Args: |
| 224 | + project: The cloud project ID. |
| 225 | + instance: The database instance name. |
| 226 | + database: The database name. |
| 227 | + query: The query string to execute. |
| 228 | + mock: If True, use a mock database instance for testing. Defaults to False. |
| 229 | +
|
| 230 | + Returns: |
| 231 | + A dictionary containing either the structured 'response' with nodes, |
| 232 | + edges, and other data, or an 'error' key with a descriptive message. |
| 233 | + """ |
212 | 234 | try:
|
213 |
| - query_result, fields, rows, schema_json, err = database.execute_query(query) |
214 |
| - if len(rows) == 0 and err : # if query returned an error |
215 |
| - if schema_json: # if the schema exists |
216 |
| - return { |
217 |
| - "response": { |
218 |
| - "schema": schema_json, |
219 |
| - "query_result": query_result, |
220 |
| - "nodes": [], |
221 |
| - "edges": [], |
222 |
| - "rows": [] |
223 |
| - }, |
224 |
| - "error": f"We've detected an error in your query. To help you troubleshoot, the graph schema's layout is shown above." + "\n\n" + f"Query error: \n{getattr(err, 'message', str(err))}" |
225 |
| - } |
226 |
| - if not schema_json: # if the schema does not exist |
227 |
| - return { |
228 |
| - "response": { |
229 |
| - "schema": schema_json, |
230 |
| - "query_result": query_result, |
231 |
| - "nodes": [], |
232 |
| - "edges": [], |
233 |
| - "rows": [] |
234 |
| - }, |
235 |
| - "error": f"Query error: \n{getattr(err, 'message', str(err))}" |
236 |
| - } |
237 |
| - nodes, edges = get_nodes_edges(query_result, fields, schema_json) |
238 |
| - |
| 235 | + db_instance = get_database_instance(project, instance, database, mock) |
| 236 | + result: SpannerQueryResult = db_instance.execute_query(query) |
| 237 | + |
| 238 | + if len(result.rows) == 0 and result.err: |
| 239 | + error_message = f"Query error: \n{getattr(result.err, 'message', str(result.err))}" |
| 240 | + if result.schema_json: |
| 241 | + # Prepend a helpful message if the schema is available |
| 242 | + error_message = ( |
| 243 | + "We've detected an error in your query. To help you troubleshoot, " |
| 244 | + "the graph schema's layout is shown above.\n\n" + error_message |
| 245 | + ) |
| 246 | + |
| 247 | + # Consolidate the repetitive error response into a single return |
| 248 | + return { |
| 249 | + "response": { |
| 250 | + "schema": result.schema_json, |
| 251 | + "query_result": result.data, |
| 252 | + "nodes": [], |
| 253 | + "edges": [], |
| 254 | + "rows": [], |
| 255 | + }, |
| 256 | + "error": error_message, |
| 257 | + } |
| 258 | + |
| 259 | + # Process a successful query result |
| 260 | + nodes, edges = get_nodes_edges(result.data, result.fields, result.schema_json) |
| 261 | + |
239 | 262 | return {
|
240 | 263 | "response": {
|
241 | 264 | "nodes": [node.to_json() for node in nodes],
|
242 | 265 | "edges": [edge.to_json() for edge in edges],
|
243 |
| - "schema": schema_json, |
244 |
| - "rows": rows, |
245 |
| - "query_result": query_result |
| 266 | + "schema": result.schema_json, |
| 267 | + "rows": result.rows, |
| 268 | + "query_result": result.data, |
246 | 269 | }
|
247 | 270 | }
|
248 | 271 | except Exception as e:
|
249 |
| - return { |
250 |
| - "error": getattr(e, "message", str(e)) |
251 |
| - } |
| 272 | + return {"error": getattr(e, "message", str(e))} |
252 | 273 |
|
253 | 274 |
|
254 | 275 | class GraphServer:
|
|
0 commit comments