SQL execution tester for comparing and evaluating SQL queries. Provides database schema overview and SQL execution comparison capabilities.
| 11 | import random |
| 12 | |
| 13 | class sqlTester: |
| 14 | """ |
| 15 | SQL execution tester for comparing and evaluating SQL queries. |
| 16 | Provides database schema overview and SQL execution comparison capabilities. |
| 17 | """ |
| 18 | |
| 19 | def __init__(self, |
| 20 | tables_file: str = "tables.json", |
| 21 | databases_dir: str = "databases"): |
| 22 | """ |
| 23 | Initialize SQL tester with database schemas and execution logic |
| 24 | |
| 25 | Args: |
| 26 | tables_file: Path to JSON file containing database schemas |
| 27 | databases_dir: Directory containing SQLite database files |
| 28 | """ |
| 29 | self.tables_file = tables_file |
| 30 | self.databases_dir = databases_dir |
| 31 | |
| 32 | # Load database schemas from tables file |
| 33 | if os.path.exists(tables_file): |
| 34 | with open(tables_file, 'r', encoding='utf-8') as f: |
| 35 | self.tables = {db['db_id']: db for db in json.load(f)} |
| 36 | else: |
| 37 | self.tables = {} |
| 38 | |
| 39 | def extract_dbid(self, question: str) -> str: |
| 40 | """Extract database ID from question. |
| 41 | |
| 42 | Args: |
| 43 | question: String in format "dbid|question" |
| 44 | |
| 45 | Returns: |
| 46 | Database ID string |
| 47 | """ |
| 48 | if "|" in question: |
| 49 | return question.split("|")[0] |
| 50 | return question |
| 51 | |
| 52 | def get_database_overview(self, db_id: str) -> str: |
| 53 | """ |
| 54 | Generate formatted overview of database structure |
| 55 | |
| 56 | Args: |
| 57 | db_id: Database identifier |
| 58 | |
| 59 | Returns: |
| 60 | Formatted string containing database schema information |
| 61 | """ |
| 62 | if db_id not in self.tables: |
| 63 | return f"Database {db_id} not found in tables data" |
| 64 | |
| 65 | db_info = self.tables[db_id] |
| 66 | overview_parts = [] |
| 67 | |
| 68 | # Database overview section |
| 69 | db_overview = db_info.get('db_overview', 'No overview available') |
| 70 | overview_parts.append(f"Database Overview: {db_overview}") |