188 lines
5.7 KiB
Python
188 lines
5.7 KiB
Python
import json
|
|
import sqlite3
|
|
|
|
def buildSave(action, outputs):
|
|
if hasattr(action, "savedata"):
|
|
save_data = {}
|
|
|
|
for save_name in action.savedata:
|
|
save_data[save_name] = outputs[save_name]
|
|
|
|
with open(f"{action.savepath}", "w") as save_file:
|
|
json.dump(save_data, save_file)
|
|
|
|
#print(f"Data saved by {action.name} to {action.savepath}")
|
|
return outputs
|
|
|
|
def loadSave(action, outputs):
|
|
if hasattr(action, "loaddata"):
|
|
with open(f"{action.savepath}", "r") as save_file:
|
|
load_data = json.load(save_file)
|
|
|
|
for load_name in action.loaddata:
|
|
outputs[load_name] = load_data[load_name]
|
|
|
|
#print(f"Data loaded by {action.name} from {action.savepath}")
|
|
return outputs
|
|
|
|
def saveRow(action, outputs, resolved):
|
|
if hasattr(action, "saverow"):
|
|
print("Saverow activated")
|
|
table = action.saverow["table"]
|
|
|
|
columns = action.saverow["columns"]
|
|
column_list = []
|
|
column_creation = []
|
|
for item in columns:
|
|
for key, value in item.items():
|
|
column_list.append(key)
|
|
columnattr = ""
|
|
columnattr = key + " " + value
|
|
|
|
#print(columnattr)
|
|
column_creation.append(columnattr)
|
|
|
|
rowitems = action.saverow["rowitems"]
|
|
|
|
values = []
|
|
|
|
for item in rowitems:
|
|
try:
|
|
if "outputs" in item:
|
|
value = outputs[item["outputs"]]
|
|
elif "resolved" in item:
|
|
value = resolved[item["resolved"]]
|
|
except KeyError as error:
|
|
print(f"The save data for {error} was not located in its respective dictionary during {action.name}")
|
|
print("Bypassing database save")
|
|
return False
|
|
|
|
values.append(value)
|
|
|
|
database = sqlite3.connect("savedata/rmmanager.db")
|
|
|
|
columnitems = ", ".join(column_creation)
|
|
|
|
database.execute(f"""
|
|
CREATE TABLE IF NOT EXISTS {table} (
|
|
{columnitems}
|
|
)
|
|
""")
|
|
|
|
column_names = ", ".join(column_list)
|
|
placeholders = ", ".join(["?"] * len(values))
|
|
|
|
query = f"INSERT INTO {table} ({column_names}) VALUES ({placeholders})"
|
|
|
|
if "onconflict" in action.saverow:
|
|
conflict = action.saverow["onconflict"]
|
|
|
|
target = conflict["target"]
|
|
columns = conflict["update"]
|
|
preserve_null = conflict.get("preservenull", False)
|
|
|
|
if columns:
|
|
if preserve_null:
|
|
assignments = ", ".join(
|
|
f"{column} = COALESCE(excluded.{column}, {table}.{column})"
|
|
for column in columns
|
|
)
|
|
else:
|
|
assignments = ", ".join(
|
|
f"{column} = excluded.{column}"
|
|
for column in columns
|
|
)
|
|
|
|
query += f" ON CONFLICT({target}) DO UPDATE SET {assignments}"
|
|
else:
|
|
query += f" ON CONFLICT({target}) DO NOTHING"
|
|
|
|
database.execute(query, tuple(values))
|
|
database.commit()
|
|
|
|
#database.execute(
|
|
# f"INSERT INTO {table} ({column_names}) VALUES ({placeholders})",
|
|
# tuple(values)
|
|
#)
|
|
|
|
#database.commit()
|
|
#database.close()
|
|
|
|
print(f"Successfully saved data to {table} during {action.name}")
|
|
|
|
return True
|
|
|
|
|
|
def readRow(action, resolved, outputs):
|
|
result = None
|
|
if hasattr(action, "readrow"):
|
|
table = action.readrow["table"]
|
|
|
|
columns = action.readrow["columns"]
|
|
column_list = []
|
|
column_creation = []
|
|
for item in columns:
|
|
for key, value in item.items():
|
|
column_list.append(key)
|
|
columnattr = ""
|
|
columnattr = key + " " + value
|
|
|
|
#print(columnattr)
|
|
column_creation.append(columnattr)
|
|
|
|
rowsearchvalue = action.readrow["rowsearchvalue"]
|
|
searchcolumn = action.readrow["searchcolumn"]
|
|
|
|
values = []
|
|
|
|
for item in rowsearchvalue:
|
|
if "outputs" in item:
|
|
value = outputs[item["outputs"]]
|
|
elif "resolved" in item:
|
|
value = resolved[item["resolved"]]
|
|
|
|
values.append(value)
|
|
|
|
exports = action.readrow["export"]
|
|
for column_index, output_name in exports.items():
|
|
outputs.pop(output_name, None)
|
|
|
|
database = sqlite3.connect("savedata/rmmanager.db")
|
|
|
|
columnitems = ", ".join(column_creation)
|
|
|
|
database.execute(f"""
|
|
CREATE TABLE IF NOT EXISTS {table} (
|
|
{columnitems}
|
|
)
|
|
""")
|
|
|
|
cursor = database.execute(
|
|
f"""
|
|
SELECT * FROM {table}
|
|
WHERE {searchcolumn} = ?
|
|
""",
|
|
(value,)
|
|
)
|
|
#print("DEBUG READROW TARGET ITEM")
|
|
#print(value)
|
|
|
|
result = cursor.fetchone()
|
|
|
|
## extension for matching by unique item vs column find
|
|
if result is not None:
|
|
for column_index, output_name in exports.items():
|
|
dbtarget = result[column_index]
|
|
outputs[output_name] = dbtarget# result[column_index]
|
|
#print("DEBUGGING DATABASE LINE EXTRACTION IN READROW")
|
|
#print(outputs[output_name])
|
|
#print(dbtarget)
|
|
|
|
return resolved, outputs
|
|
else:
|
|
print(f"Search target {value} not found in database table {table}")
|
|
return resolved, outputs
|
|
|
|
#print("READROW SEARCH RESULT")
|
|
#print(target)
|
|
return resolved, outputs |