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",
|
||||
:acidity => "0",
|
||||
:tannin => "0",
|
||||
@@ -46,17 +66,20 @@ insert_data = Dict(
|
||||
: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'")
|
||||
push!(values, "'$value'") #[] number should not wrapped in ''
|
||||
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);"
|
||||
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",
|
||||
: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
|
||||
if key ∈ columnToUpdate # update only specified columns
|
||||
push!(set_clauses, "$key = '$value'")
|
||||
end
|
||||
end
|
||||
@@ -132,7 +196,5 @@ end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module
|
||||
Reference in New Issue
Block a user