Hi!
I need a bit of help of how to do this.
I have a named query:
system.db.runNamedQuery(
"FUNCTIONS/UPDATE",
{
"ID": id,
"PARAM_1": value1,
"PARAM_2": value2,
[...]
"PARAM_N": valuen
}
)
UPDATE MY_TABLE
PARAM_1 = COALESCE(:PARAM_1, PARAM_1),
PARAM_2 = COALESCE(:PARAM_2, PARAM_2),
[...]
PARAM_N = COALESCE(:PARAM_N, PARAM_N),
WHERE ID = :ID
The function update the table depending of the ID and the values that receives that are not null.
My problems is that I recive the values in a dictionary, how may or may not contains the values to update.
For example, this two are possible values that I could receive:
dict = {
"PARAM_1": value1,
"PARAM_3": value3,
"PARAM_8": value8
}
dict = {
"PARAM_3": value3,
"PARAM_4": value4,
"PARAM_5": value5,
"PARAM_8": value8
}
My first idea was to do this:
system.db.runNamedQuery(
"FUNCTIONS/UPDATE",
{
"ID": id,
"PARAM_1": dict[PARAM_1],
"PARAM_2": dict[PARAM_2],
[...]
"PARAM_N": dict[PARAM_N]
}
)
But if the dictionary doesn't contain the param it throws an exception.
Also I thought of making a loop where I check if it exist and adding a null value if not, but I don't really like, is an ugly way of doing it (in my opinion).
Any idea of how I could do this in a more elegant way?
Any restructuring of the code, query or the idea itself are valid. The only thing I can't change is how I recive the data.
Thanks in advance!