138 lines
3.0 KiB
Julia
138 lines
3.0 KiB
Julia
module dbUtil
|
|
|
|
export dictToPostgresKeyValueString, generateInsertSQL, generateUpdateSQL
|
|
|
|
using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames,
|
|
SHA
|
|
using ..util
|
|
|
|
# ---------------------------------------------- 100 --------------------------------------------- #
|
|
|
|
|
|
function dictToPostgresKeyValueString(dict)
|
|
parts = []
|
|
for (k, v) in dict
|
|
if isa(v, Dict)
|
|
push!(parts, "\"$k\": " * dict_to_string(v))
|
|
elseif isa(v, AbstractString)
|
|
push!(parts, "\"$k\": \"$v\"")
|
|
else
|
|
push!(parts, "\"$k\": $v")
|
|
end
|
|
end
|
|
return "{" * join(parts, ", ") * "}"
|
|
end
|
|
|
|
|
|
|
|
"""
|
|
example:
|
|
|
|
insert_data = Dict(
|
|
:grape => "NA",
|
|
:acidity => "0",
|
|
:tannin => "0",
|
|
:country => "NA",
|
|
:description => "NA",
|
|
:region => "NA",
|
|
:winery => "ccc",
|
|
:intensity => "0",
|
|
:sweetness => "0",
|
|
:tasting_notes => "NA",
|
|
:wine_name => "new_wine",
|
|
:wine_id => "9e1deb6a-d57f-4d2c-abbe-da813f4e91ad",
|
|
:wine_type => "NA",
|
|
:other_attributes => "{\"attribute3\":{\"attribute5\":666,\"attribute4\":\"text\"},\"attribute1\":\"hello world\",\"attribute2\":555}",
|
|
:fizziness => "0",
|
|
:serving_temperature => "0",
|
|
:additional_search_term => "{NA1,NA2}")
|
|
"""
|
|
function generateInsertSQL(table_name::String, insert_data::Dict{Symbol, Any})
|
|
columns = String[]
|
|
values = String[]
|
|
|
|
for (key, value) in insert_data
|
|
push!(columns, string(key))
|
|
if key == :other_attributes
|
|
push!(values, "'$value'")
|
|
else
|
|
push!(values, "'$value'")
|
|
end
|
|
end
|
|
|
|
columns_str = join(columns, ", ")
|
|
values_str = join(values, ", ")
|
|
|
|
return "INSERT INTO $table_name ($columns_str) VALUES ($values_str);"
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
example:
|
|
|
|
insert_data = Dict(
|
|
:grape => "NA",
|
|
:acidity => "0",
|
|
:tannin => "0",
|
|
:country => "NA",
|
|
:description => "NA",
|
|
:region => "NA",
|
|
:winery => "ccc",
|
|
:intensity => "0",
|
|
:sweetness => "0",
|
|
:tasting_notes => "NA",
|
|
:wine_name => "new_wine",
|
|
:wine_id => "9e1deb6a-d57f-4d2c-abbe-da813f4e91ad",
|
|
:wine_type => "NA",
|
|
:other_attributes => "{\"attribute3\":{\"attribute5\":666,\"attribute4\":\"text\"},\"attribute1\":\"hello world\",\"attribute2\":555}",
|
|
:fizziness => "0",
|
|
:serving_temperature => "0",
|
|
:additional_search_term => "{NA1,NA2}")
|
|
"""
|
|
function generateUpdateSQL(table_name::String, update_data::Dict{Symbol, Any}, id_keys::Vector{Symbol})
|
|
set_clauses = String[]
|
|
where_clauses = String[]
|
|
|
|
for (key, value) in update_data
|
|
if key in id_keys
|
|
push!(where_clauses, "$key = '$value'")
|
|
else
|
|
if key == :other_attributes
|
|
push!(set_clauses, "$key = '$value'")
|
|
else
|
|
push!(set_clauses, "$key = '$value'")
|
|
end
|
|
end
|
|
end
|
|
|
|
set_clause = join(set_clauses, ", ")
|
|
where_clause = join(where_clauses, " AND ")
|
|
|
|
return "UPDATE $table_name SET $set_clause WHERE $where_clause;"
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end # module |