mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-27 08:58:48 +00:00
fix for database compatibility when getting table DDL (#1129)
#1081 introduced a method to get DDL (table definitions) in a manner specific to sqlite3, thus breaking compatibility with other non-sqlite3 databases. This uses the sqlite3 command if the detected dialect is sqlite, and otherwise uses the standard SQL `SHOW CREATE TABLE`. This should fix #1103.
This commit is contained in:
parent
1ed708391e
commit
c39ef70aa4
@ -96,22 +96,33 @@ class SQLDatabase:
|
|||||||
tables = []
|
tables = []
|
||||||
for table_name in all_table_names:
|
for table_name in all_table_names:
|
||||||
columns = []
|
columns = []
|
||||||
create_table = self.run(
|
if self.dialect in ("sqlite", "duckdb"):
|
||||||
(
|
create_table = self.run(
|
||||||
"SELECT sql FROM sqlite_master WHERE "
|
(
|
||||||
f"type='table' AND name='{table_name}'"
|
"SELECT sql FROM sqlite_master WHERE "
|
||||||
),
|
f"type='table' AND name='{table_name}'"
|
||||||
fetch="one",
|
),
|
||||||
)
|
fetch="one",
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
create_table = self.run(
|
||||||
|
f"SHOW CREATE TABLE `{table_name}`;",
|
||||||
|
)
|
||||||
|
|
||||||
for column in self._inspector.get_columns(table_name, schema=self._schema):
|
for column in self._inspector.get_columns(table_name, schema=self._schema):
|
||||||
columns.append(column["name"])
|
columns.append(column["name"])
|
||||||
|
|
||||||
if self._sample_rows_in_table_info:
|
if self._sample_rows_in_table_info:
|
||||||
select_star = (
|
if self.dialect in ("sqlite", "duckdb"):
|
||||||
f"SELECT * FROM '{table_name}' LIMIT "
|
select_star = (
|
||||||
f"{self._sample_rows_in_table_info}"
|
f"SELECT * FROM '{table_name}' LIMIT "
|
||||||
)
|
f"{self._sample_rows_in_table_info}"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
select_star = (
|
||||||
|
f"SELECT * FROM `{table_name}` LIMIT "
|
||||||
|
f"{self._sample_rows_in_table_info}"
|
||||||
|
)
|
||||||
|
|
||||||
sample_rows = self.run(select_star)
|
sample_rows = self.run(select_star)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user