From b8d6796920d889001e872a6d4d7a047801a5806b Mon Sep 17 00:00:00 2001 From: narawat lamaiin Date: Tue, 8 Oct 2024 13:03:08 +0700 Subject: [PATCH] update --- src/dbUtil.jl | 136 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 99 insertions(+), 37 deletions(-) diff --git a/src/dbUtil.jl b/src/dbUtil.jl index 1058d24..33f6fbd 100644 --- a/src/dbUtil.jl +++ b/src/dbUtil.jl @@ -25,39 +25,62 @@ 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}") +""" Get characters between specified characters. + +# Arguments + - `text::T` + a text being searched + - `startChar::Char` + start character + - `endChar::Char` + end character +# Keyword Arguments + - `endCharLocation::String` + end character position after startChar. Can be "next" or "end". "next" means the closed + endChar just after startChar. "end" means the furthest endChar. + - `includeChar::Bool` + whether to include the startChar and endChar. Default is true +# Return + the characters between specified characters. + +# Example +```jldoctest +julia> using Revise +julia> using GeneralUtils +julia> 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}") +``` +# TODO + - [] update docs + +# Signature """ -function generateInsertSQL(table_name::String, insert_data::Dict{Symbol, Any}) +function generateInsertSQL(table_name::String, columnToInsert::Vector{Symbol}, insert_data::Dict{Symbol, Any}) columns = String[] values = String[] - + for (key, value) in insert_data + if key ∈ columnToInsert push!(columns, string(key)) - if key == :other_attributes - push!(values, "'$value'") - else - push!(values, "'$value'") - end + push!(values, "'$value'") #[] number should not wrapped in '' + end end columns_str = join(columns, ", ") @@ -65,7 +88,24 @@ function generateInsertSQL(table_name::String, insert_data::Dict{Symbol, Any}) return "INSERT INTO $table_name ($columns_str) VALUES ($values_str);" end - +# 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 @@ -92,20 +132,44 @@ insert_data = Dict( :fizziness => "0", :serving_temperature => "0", :additional_search_term => "{NA1,NA2}") + +id_keys is the primary key columns """ -function generateUpdateSQL(table_name::String, update_data::Dict{Symbol, Any}, id_keys::Vector{Symbol}) +# 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 + +function generateUpdateSQL(table_name::String, columnToUpdate::Vector{Symbol}, + updatedata::Dict{Symbol, Any}, id_keys::Vector{Symbol}) + set_clauses = String[] where_clauses = String[] - for (key, value) in update_data + for (key, value) in updatedata 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 + if key ∈ columnToUpdate # update only specified columns + push!(set_clauses, "$key = '$value'") + end end end @@ -132,7 +196,5 @@ end - - end # module \ No newline at end of file