(sql_query: str)
| 94 | |
| 95 | |
| 96 | def execute_sql(sql_query: str): |
| 97 | if not is_read_only_query(sql_query): |
| 98 | raise NotReadOnlyException("Only read-only queries are allowed.") |
| 99 | |
| 100 | with ENGINE.connect() as connection: |
| 101 | connection = connection.execution_options(postgresql_readonly=True) |
| 102 | with connection.begin(): |
| 103 | result = connection.execute(text(sql_query)) |
| 104 | |
| 105 | column_names = list(result.keys()) |
| 106 | |
| 107 | rows = [list(r) for r in result.all()] |
| 108 | |
| 109 | # Check for null values |
| 110 | # for row in rows: |
| 111 | # for value in row: |
| 112 | # if value is None: |
| 113 | # raise NullValueException("Make sure each value in the result table is not null.") |
| 114 | |
| 115 | |
| 116 | results = [] |
| 117 | for row in rows: |
| 118 | result = OrderedDict() |
| 119 | for i, column_name in enumerate(column_names): |
| 120 | result[column_name] = row[i] |
| 121 | results.append(result) |
| 122 | |
| 123 | result_dict = { |
| 124 | "column_names": column_names, |
| 125 | "results": results, |
| 126 | } |
| 127 | if results: |
| 128 | result_dict["column_types"] = [type(r).__name__ for r in results[0]] |
| 129 | |
| 130 | return result_dict |
| 131 | |
| 132 | |
| 133 | def text_to_sql_with_retry(natural_language_query, table_names, k=3, messages=None): |
no test coverage detected