Skip to content

Commit

Permalink
Merge pull request #119 from lanl/avoid_yaml_toml_dups
Browse files Browse the repository at this point in the history
avoid duplicate sql statements
  • Loading branch information
jpulidojr authored Sep 23, 2024
2 parents 05e203a + 8b7fffd commit 10f54ae
Showing 1 changed file with 15 additions and 5 deletions.
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

0 comments on commit 10f54ae

Please sign in to comment.