Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid duplicate sql statements #119

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions dsi/backends/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ def tomlToSqlite(self, filenames, db_name, deleteSql=True):

`deleteSql`: flag to delete temp SQL file that creates the database. Default is True, but change to False for testing or comparing outputs
"""

sql_statements = []
if isinstance(filenames, str):
filenames = [filenames]

Expand All @@ -667,10 +669,16 @@ def tomlToSqlite(self, filenames, db_name, deleteSql=True):
else:
createStmt += f"{key} {data_types[type(val)]}, "
insertUnitStmt+= "NULL, "

sql_file.write(createStmt[:-2] + ");\n\n")
sql_file.write(createUnitStmt[:-2] + ");\n\n")
sql_file.write(insertUnitStmt[:-2] + ");\n\n")

if createStmt not in sql_statements:
sql_statements.append(createStmt)
sql_file.write(createStmt[:-2] + ");\n\n")
if createUnitStmt not in sql_statements:
sql_statements.append(createUnitStmt)
sql_file.write(createUnitStmt[:-2] + ");\n\n")
if insertUnitStmt not in sql_statements:
sql_statements.append(insertUnitStmt)
sql_file.write(insertUnitStmt[:-2] + ");\n\n")

insertStmt = f"INSERT INTO {tableName} VALUES( "
for val in tableData.values():
Expand All @@ -681,7 +689,9 @@ def tomlToSqlite(self, filenames, db_name, deleteSql=True):
else:
insertStmt+= f"{val}, "

sql_file.write(insertStmt[:-2] + ");\n\n")
if insertStmt not in sql_statements:
sql_statements.append(insertStmt)
sql_file.write(insertStmt[:-2] + ");\n\n")

subprocess.run(["sqlite3", db_name+".db"], stdin= open(db_name+".sql", "r"))

Expand Down