update
This commit is contained in:
@@ -6,6 +6,9 @@ export noNegative!, randomWithProb, randomChoiceWithProb, findIndex, limitvalue
|
|||||||
include("util.jl")
|
include("util.jl")
|
||||||
using .util
|
using .util
|
||||||
|
|
||||||
|
include("dbUtil.jl")
|
||||||
|
using .dbUtil
|
||||||
|
|
||||||
include("communication.jl")
|
include("communication.jl")
|
||||||
using .communication
|
using .communication
|
||||||
|
|
||||||
|
|||||||
138
src/dbUtil.jl
Normal file
138
src/dbUtil.jl
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
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
|
||||||
16
src/util.jl
16
src/util.jl
@@ -2,7 +2,7 @@ module util
|
|||||||
|
|
||||||
export timedifference, showstracktrace, findHighestIndexKey, uuid4snakecase, replaceDictKeys,
|
export timedifference, showstracktrace, findHighestIndexKey, uuid4snakecase, replaceDictKeys,
|
||||||
findMatchingDictKey, textToDict, randstring, randstrings, timeout,
|
findMatchingDictKey, textToDict, randstring, randstrings, timeout,
|
||||||
dataframeToCSV, dfToVectorDict, disintegrate_vectorDict, dictToPostgresKeyValueString
|
dataframeToCSV, dfToVectorDict, disintegrate_vectorDict
|
||||||
|
|
||||||
using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames
|
using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames
|
||||||
|
|
||||||
@@ -522,19 +522,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user