Create a query executor function for the pipeline. Args: conn: Database connection object. Returns: A callable that executes queries by ID.
(conn)
| 22 | |
| 23 | |
| 24 | def create_query_executor(conn) -> callable: |
| 25 | """Create a query executor function for the pipeline. |
| 26 | |
| 27 | Args: |
| 28 | conn: Database connection object. |
| 29 | |
| 30 | Returns: |
| 31 | A callable that executes queries by ID. |
| 32 | """ |
| 33 | def executor(query_id: str, context: dict) -> dict[str, Any]: |
| 34 | """Execute a query by ID. |
| 35 | |
| 36 | Args: |
| 37 | query_id: The query identifier from QUERIES registry. |
| 38 | context: Execution context (may contain schema_id for filtering). |
| 39 | |
| 40 | Returns: |
| 41 | Dictionary with query results. |
| 42 | """ |
| 43 | query_def = QUERIES.get(query_id) |
| 44 | if not query_def: |
| 45 | return {'error': f'Unknown query: {query_id}', 'rows': []} |
| 46 | |
| 47 | sql = query_def['sql'] |
| 48 | |
| 49 | # Check if query requires an extension |
| 50 | required_ext = query_def.get('requires_extension') |
| 51 | if required_ext: |
| 52 | check_sql = """ |
| 53 | SELECT EXISTS ( |
| 54 | SELECT 1 FROM pg_catalog.pg_extension WHERE extname = %s |
| 55 | ) as available |
| 56 | """ |
| 57 | status, result = conn.execute_dict(check_sql, [required_ext]) |
| 58 | if not (status and result and |
| 59 | result.get('rows', [{}])[0].get('available', False)): |
| 60 | return { |
| 61 | 'note': f"Extension '{required_ext}' not installed", |
| 62 | 'rows': [] |
| 63 | } |
| 64 | |
| 65 | # Handle schema-scoped queries |
| 66 | schema_id = context.get('schema_id') |
| 67 | if schema_id and '%s' in sql: |
| 68 | status, result = conn.execute_dict(sql, [schema_id]) |
| 69 | else: |
| 70 | status, result = conn.execute_dict(sql) |
| 71 | |
| 72 | if status and result: |
| 73 | return {'rows': result.get('rows', [])} |
| 74 | else: |
| 75 | return {'error': 'Query failed', 'rows': []} |
| 76 | |
| 77 | return executor |
| 78 | |
| 79 | |
| 80 | def generate_report_streaming( |
no outgoing calls
no test coverage detected