update
This commit is contained in:
132
src/dbUtil.jl
132
src/dbUtil.jl
@@ -25,39 +25,62 @@ end
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
example:
|
|
||||||
|
|
||||||
insert_data = Dict(
|
""" Get characters between specified characters.
|
||||||
:grape => "NA",
|
|
||||||
:acidity => "0",
|
# Arguments
|
||||||
:tannin => "0",
|
- `text::T`
|
||||||
:country => "NA",
|
a text being searched
|
||||||
:description => "NA",
|
- `startChar::Char`
|
||||||
:region => "NA",
|
start character
|
||||||
:winery => "ccc",
|
- `endChar::Char`
|
||||||
:intensity => "0",
|
end character
|
||||||
:sweetness => "0",
|
# Keyword Arguments
|
||||||
:tasting_notes => "NA",
|
- `endCharLocation::String`
|
||||||
:wine_name => "new_wine",
|
end character position after startChar. Can be "next" or "end". "next" means the closed
|
||||||
:wine_id => "9e1deb6a-d57f-4d2c-abbe-da813f4e91ad",
|
endChar just after startChar. "end" means the furthest endChar.
|
||||||
:wine_type => "NA",
|
- `includeChar::Bool`
|
||||||
:other_attributes => "{\"attribute3\":{\"attribute5\":666,\"attribute4\":\"text\"},\"attribute1\":\"hello world\",\"attribute2\":555}",
|
whether to include the startChar and endChar. Default is true
|
||||||
:fizziness => "0",
|
# Return
|
||||||
:serving_temperature => "0",
|
the characters between specified characters.
|
||||||
:additional_search_term => "{NA1,NA2}")
|
|
||||||
|
# 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[]
|
columns = String[]
|
||||||
values = String[]
|
values = String[]
|
||||||
|
|
||||||
for (key, value) in insert_data
|
for (key, value) in insert_data
|
||||||
|
if key ∈ columnToInsert
|
||||||
push!(columns, string(key))
|
push!(columns, string(key))
|
||||||
if key == :other_attributes
|
push!(values, "'$value'") #[] number should not wrapped in ''
|
||||||
push!(values, "'$value'")
|
end
|
||||||
else
|
|
||||||
push!(values, "'$value'")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
columns_str = join(columns, ", ")
|
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);"
|
return "INSERT INTO $table_name ($columns_str) VALUES ($values_str);"
|
||||||
end
|
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",
|
:fizziness => "0",
|
||||||
:serving_temperature => "0",
|
:serving_temperature => "0",
|
||||||
:additional_search_term => "{NA1,NA2}")
|
: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[]
|
set_clauses = String[]
|
||||||
where_clauses = String[]
|
where_clauses = String[]
|
||||||
|
|
||||||
for (key, value) in update_data
|
for (key, value) in updatedata
|
||||||
if key in id_keys
|
if key in id_keys
|
||||||
push!(where_clauses, "$key = '$value'")
|
push!(where_clauses, "$key = '$value'")
|
||||||
else
|
else
|
||||||
if key == :other_attributes
|
if key ∈ columnToUpdate # update only specified columns
|
||||||
push!(set_clauses, "$key = '$value'")
|
push!(set_clauses, "$key = '$value'")
|
||||||
else
|
end
|
||||||
push!(set_clauses, "$key = '$value'")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -132,7 +196,5 @@ end
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
end # module
|
end # module
|
||||||
Reference in New Issue
Block a user