SQLTables is a class for creating and accessing external SQL data, primarily as a resource that is accessible via Text2SQL programmatic inferences. This is an **experimental** feature, and currently supports only use of SQLite, configured as a separate local file-based DB, e.g., sqlite
| 1416 | |
| 1417 | |
| 1418 | class SQLTables: |
| 1419 | |
| 1420 | """ SQLTables is a class for creating and accessing external SQL data, primarily as a resource that is |
| 1421 | accessible via Text2SQL programmatic inferences. |
| 1422 | |
| 1423 | This is an **experimental** feature, and currently supports only use of SQLite, configured as a separate |
| 1424 | local file-based DB, e.g., sqlite-experimental.db |
| 1425 | |
| 1426 | Use of this class will create a separate sqlite_experimental.db per the configs in SQLiteConfig |
| 1427 | |
| 1428 | Please note that the CustomTables class in llmware.resources provides a superset of this functionality, and |
| 1429 | offers support for Postgres, in addition to SQLite. This class is provided for a 'fast example' but |
| 1430 | generally we would recommend using CustomTables for more complex use cases. |
| 1431 | |
| 1432 | """ |
| 1433 | |
| 1434 | def __init__(self, db=None, db_name=None, experimental=True): |
| 1435 | |
| 1436 | self.db = "sqlite" |
| 1437 | |
| 1438 | # check for llmware path & create if not already set up, e.g., "first time use" |
| 1439 | if not os.path.exists(LLMWareConfig.get_llmware_path()): |
| 1440 | LLMWareConfig.setup_llmware_workspace() |
| 1441 | logger.info("SQLTables - Setting up LLMWare Workspace.") |
| 1442 | |
| 1443 | # default config for "db_experimental" = "sqlite_experimental.db" |
| 1444 | self.db_name = SQLiteConfig().get_config("db_experimental") |
| 1445 | |
| 1446 | if experimental: |
| 1447 | self.db_file = SQLiteConfig().get_uri_string_experimental_db() |
| 1448 | logging.info("update: connecting to experimental sqlite db - %s", self.db_file) |
| 1449 | |
| 1450 | else: |
| 1451 | self.db_file = SQLiteConfig().get_uri_string() |
| 1452 | logging.info("warning: connecting to main sqlite db - %s", self.db_file) |
| 1453 | |
| 1454 | self.conn = sqlite3.connect(self.db_file) |
| 1455 | |
| 1456 | self.tables = [] |
| 1457 | |
| 1458 | def get_table_schema(self,table_name): |
| 1459 | |
| 1460 | """ Lookup of table_schema for an input table_name - outputs 'create table schema string' that can |
| 1461 | be used directly as context in a text2sql inference """ |
| 1462 | |
| 1463 | table_schema = "" |
| 1464 | |
| 1465 | sql_query = f"SELECT * FROM sqlite_master WHERE type = 'table' AND name = '{table_name}';" |
| 1466 | |
| 1467 | table_schema_row = self.conn.cursor().execute(sql_query) |
| 1468 | table_schema_row = list(table_schema_row) |
| 1469 | |
| 1470 | if len(table_schema_row) > 0: |
| 1471 | table_schema = table_schema_row[0][4] |
| 1472 | |
| 1473 | return table_schema |
| 1474 | |
| 1475 | def get_column_names(self, table_name): |
no outgoing calls
no test coverage detected