update
This commit is contained in:
@@ -25,10 +25,30 @@ end
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
example:
|
|
||||||
|
|
||||||
insert_data = Dict(
|
""" 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",
|
:grape => "NA",
|
||||||
:acidity => "0",
|
:acidity => "0",
|
||||||
:tannin => "0",
|
:tannin => "0",
|
||||||
@@ -46,17 +66,20 @@ insert_data = Dict(
|
|||||||
:fizziness => "0",
|
:fizziness => "0",
|
||||||
:serving_temperature => "0",
|
:serving_temperature => "0",
|
||||||
:additional_search_term => "{NA1,NA2}")
|
: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'")
|
|
||||||
else
|
|
||||||
push!(values, "'$value'")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -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,18 +132,42 @@ 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'")
|
|
||||||
else
|
|
||||||
push!(set_clauses, "$key = '$value'")
|
push!(set_clauses, "$key = '$value'")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -132,7 +196,5 @@ end
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
end # module
|
end # module
|
||||||
Reference in New Issue
Block a user