162 lines
4.4 KiB
Python
162 lines
4.4 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}")
|
|
|
|
database = sqlite3.connect("savedata/rmmanager.db")
|
|
for key, value in save_data.items():
|
|
database.execute("""
|
|
CREATE TABLE IF NOT EXISTS output_test (
|
|
dictionary TEXT PRIMARY KEY,
|
|
value TEXT NOT NULL
|
|
)
|
|
""")
|
|
|
|
database.execute("""
|
|
INSERT INTO output_test(dictionary, value)
|
|
VALUES (?, ?)
|
|
ON CONFLICT (dictionary)
|
|
DO UPDATE SET value = excluded.value
|
|
""", (key, value)
|
|
)
|
|
|
|
database.commit()
|
|
database.close()
|
|
|
|
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"):
|
|
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:
|
|
if "outputs" in item:
|
|
value = outputs[item["outputs"]]
|
|
elif "resolved" in item:
|
|
value = resolved[item["resolved"]]
|
|
|
|
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))
|
|
|
|
database.execute(
|
|
f"INSERT INTO {table} ({column_names}) VALUES ({placeholders})",
|
|
tuple(values)
|
|
)
|
|
|
|
database.commit()
|
|
database.close()
|
|
|
|
|
|
|
|
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)
|
|
|
|
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 RESOLVED TARGET ITEM")
|
|
print(value)
|
|
|
|
result = cursor.fetchone()
|
|
print(result)
|
|
|
|
|
|
try:
|
|
target = result[action.readrow["columnnumber"]]
|
|
except TypeError:
|
|
target = None
|
|
print("Message ID not found in database")
|
|
|
|
outputs["databasetarget"] = target
|
|
|
|
print("READROW TEST")
|
|
print(target)
|
|
return resolved, outputs |