Compare commits
14 Commits
usability
...
v0.4.0-dic
| Author | SHA1 | Date | |
|---|---|---|---|
| 05d8cb9c02 | |||
| 13de2f90ff | |||
| 22fe810f63 | |||
| abdf6cf3b8 | |||
| f2ba243df0 | |||
| bb2851332a | |||
| 00225f3a06 | |||
| 7cb0bd077f | |||
| 0ba2aa310e | |||
| 1916668c6e | |||
| 919800da42 | |||
| 947580a2ec | |||
| 688b9a22b6 | |||
| 76ce0fc54f |
@@ -121,7 +121,8 @@ function generateInsertSQL(table_name::String, columnToInsert::Vector{Symbol}, d
|
|||||||
for (key, value) in data
|
for (key, value) in data
|
||||||
if key ∈ columnToInsert
|
if key ∈ columnToInsert
|
||||||
push!(columns, string(key))
|
push!(columns, string(key))
|
||||||
push!(values, "'$value'") #[] number should not wrapped in ''
|
value_str = isa(value, AbstractString) ? "'$value'" : "$value"
|
||||||
|
push!(values, value_str)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -131,6 +132,22 @@ function generateInsertSQL(table_name::String, columnToInsert::Vector{Symbol}, d
|
|||||||
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, data::AbstractDict{String, Any})
|
||||||
|
columns = String[]
|
||||||
|
values = String[]
|
||||||
|
|
||||||
|
for (key, value) in data
|
||||||
|
push!(columns, string(key))
|
||||||
|
value_str = isa(value, AbstractString) ? "'$value'" : "$value"
|
||||||
|
push!(values, value_str)
|
||||||
|
end
|
||||||
|
|
||||||
|
columns_str = join(columns, ", ")
|
||||||
|
values_str = join(values, ", ")
|
||||||
|
|
||||||
|
return "INSERT INTO $table_name ($columns_str) VALUES ($values_str);"
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||||
|
|
||||||
@@ -185,5 +202,30 @@ function generateUpdateSQL(table_name::String, pk_column::String, pk_value,
|
|||||||
return "UPDATE $table_name SET $set_clause WHERE $pk_column = $pk_val_str;"
|
return "UPDATE $table_name SET $set_clause WHERE $pk_column = $pk_val_str;"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function generateUpdateSQL(table_name::String, pk_dict::AbstractDict{String, Any},
|
||||||
|
data::AbstractDict{String, Any})
|
||||||
|
# Build SET clause
|
||||||
|
set_parts = String[]
|
||||||
|
for (key, value) in data
|
||||||
|
if key ∉ keys(pk_dict)
|
||||||
|
value_str = isa(value, AbstractString) ? "'$value'" : "$value"
|
||||||
|
push!(set_parts, "$(string(key)) = $value_str")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
set_clause = join(set_parts, ", ")
|
||||||
|
|
||||||
|
# Build WHERE clause for composite keys
|
||||||
|
where_parts = String[]
|
||||||
|
for (col, val) in pk_dict
|
||||||
|
val_str = isa(val, AbstractString) ? "'$val'" : "$val"
|
||||||
|
push!(where_parts, "$(string(col)) = $val_str")
|
||||||
|
end
|
||||||
|
|
||||||
|
where_clause = join(where_parts, " AND ")
|
||||||
|
|
||||||
|
return "UPDATE $table_name SET $set_clause WHERE $where_clause;"
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
end # module
|
end # module
|
||||||
132
src/interface.jl
132
src/interface.jl
@@ -202,13 +202,13 @@ end
|
|||||||
if it is an `AbstractArray` its elements will be processed recursively.
|
if it is an `AbstractArray` its elements will be processed recursively.
|
||||||
|
|
||||||
# Keyword Arguments
|
# Keyword Arguments
|
||||||
|
- `keytype::Type=Any`
|
||||||
|
The key type for the output Dict. Use `String` for `Dict{String,Any}`, `Symbol` for `Dict{Symbol,Any}`, or `Any` to preserve original key types.
|
||||||
- `stringkey::Bool=false`
|
- `stringkey::Bool=false`
|
||||||
If `true`, every dictionary key is converted to `String` via `string(k)`.
|
If `true`, every dictionary key is converted to `String` via `string(k)`. This parameter is ignored when `keytype` is explicitly set.
|
||||||
If `false`, original key objects are preserved (useful when keys are already
|
|
||||||
`String`, `Symbol`, or other types you want to keep).
|
|
||||||
|
|
||||||
# Return
|
# Return
|
||||||
- A newly allocated nested structure composed of `Dict{Any,Any}` and
|
- A newly allocated nested structure composed of `Dict{keytype,Any}` and
|
||||||
`Vector{Any}` that mirrors the input shape but uses plain Julia containers.
|
`Vector{Any}` that mirrors the input shape but uses plain Julia containers.
|
||||||
|
|
||||||
# Notes
|
# Notes
|
||||||
@@ -236,79 +236,46 @@ julia> d = Dict(
|
|||||||
|
|
||||||
julia jsonstring = JSON.json(d)
|
julia jsonstring = JSON.json(d)
|
||||||
julia> A1 = JSON.parse(jsonstring) # A1 type is JSON.Object
|
julia> A1 = JSON.parse(jsonstring) # A1 type is JSON.Object
|
||||||
julia> A2 = dictify(A1)
|
julia> A2 = dictify(A1; keytype=String)
|
||||||
Dict{Any,Any} with 3 entries:
|
Dict{String,Any} with 3 entries:
|
||||||
"a" => 4
|
"a" => 4
|
||||||
"b" => 6
|
"b" => 6
|
||||||
"c" => Dict("d"=>7, "e"=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
|
"c" => Dict("d"=>7, "e"=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
|
||||||
|
|
||||||
julia> A3 = dictify(A1; stringkey=false) # preserves original key objects
|
julia> A3 = dictify(A1; keytype=Symbol)
|
||||||
julia> B1 = dictify(d; stringkey=true) # convert all keys in to string
|
Dict{Symbol,Any} with 3 entries:
|
||||||
Dict{Any, Any} with 3 entries:
|
:a => 4
|
||||||
"c" => Dict{Any, Any}("e"=>Dict{Any, Any}("f"=>"hey", "g"=>Dict{Any, Any}("world"=>Any[1, "2", 3, Dict{Any, Any}("dd"=>4.7)])), "d"=>7)
|
:b => 6
|
||||||
"b" => 6
|
:c => Dict(:d=>7, :e=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
|
||||||
"a" => 4
|
|
||||||
```
|
julia> B1 = dictify(d; keytype=String)
|
||||||
|
Dict{String, Any} with 3 entries:
|
||||||
"""
|
"""
|
||||||
function dictify(x; stringkey::Bool=false)
|
function dictify(x; keytype::Type=Any)
|
||||||
# Dict-like objects
|
# Dict-like objects
|
||||||
if x isa AbstractDict
|
if x isa AbstractDict
|
||||||
# choose output key type container (String keys when requested)
|
# choose output key type container
|
||||||
out = Dict{Any,Any}()
|
out = Dict{keytype,Any}()
|
||||||
for (k,v) in x
|
for (k,v) in x
|
||||||
newk = stringkey ? string(k) : k
|
if keytype === String
|
||||||
out[newk] = dictify(v; stringkey=stringkey)
|
newk = string(k)
|
||||||
|
elseif keytype === Symbol
|
||||||
|
newk = Symbol(string(k))
|
||||||
|
else
|
||||||
|
newk = k
|
||||||
|
end
|
||||||
|
out[newk] = dictify(v; keytype=keytype)
|
||||||
end
|
end
|
||||||
return out
|
return out
|
||||||
# Arrays / vectors: map elements recursively and return a Vector{Any}
|
# Arrays / vectors: map elements recursively and return a Vector{Any}
|
||||||
elseif x isa AbstractArray
|
elseif x isa AbstractArray
|
||||||
return [dictify(element; stringkey=stringkey) for element in x]
|
return [dictify(element; keytype=keytype) for element in x]
|
||||||
# everything else: return as-is (primitives, numbers, strings, etc.)
|
# everything else: return as-is (primitives, numbers, strings, etc.)
|
||||||
else
|
else
|
||||||
return x
|
return x
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
|
||||||
|
|
||||||
""" Array_to_JSON_str(data::AbstractArray)
|
|
||||||
|
|
||||||
encode Array to JSON String
|
|
||||||
|
|
||||||
# Example
|
|
||||||
|
|
||||||
a = [1.23 4.7889; 9987.1 -123.07; -0.0027 -6.75]
|
|
||||||
jsonStr = Array_to_JSON_str(a)
|
|
||||||
jsonStr = "{\"Array\":[[1.23,9987.1,-0.0027],[4.7889,-123.07,-6.75]],\"size\":[3,2]}"
|
|
||||||
"""
|
|
||||||
function Array_to_JSON_str(data::AbstractArray)
|
|
||||||
d = Dict("Array"=> data, "size"=>size(data))
|
|
||||||
jsonStr = JSON.json(d)
|
|
||||||
return jsonStr
|
|
||||||
end
|
|
||||||
|
|
||||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
|
||||||
|
|
||||||
""" JSON_str_to_Array(json_str::String)
|
|
||||||
|
|
||||||
decode JSON String to Array
|
|
||||||
|
|
||||||
# Example
|
|
||||||
|
|
||||||
jsonStr = "{\"Array\":[[1.23,9987.1,-0.0027],[4.7889,-123.07,-6.75]],\"size\":[3,2]}"
|
|
||||||
a = JSON_str_to_Array(jsonStr)
|
|
||||||
|
|
||||||
"""
|
|
||||||
function JSON_str_to_Array(jsonStr::String)
|
|
||||||
jsonObj = JSON.parse(jsonStr)
|
|
||||||
a = Array(jsonObj.Array)
|
|
||||||
array = hcat(a...)
|
|
||||||
return array
|
|
||||||
end
|
|
||||||
|
|
||||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||||
|
|
||||||
""" Recursively convert dictionary-like variable (e.g. JSON.Object) into a dictionary.
|
""" Recursively convert dictionary-like variable (e.g. JSON.Object) into a dictionary.
|
||||||
@@ -328,13 +295,11 @@ end
|
|||||||
processed recursively.
|
processed recursively.
|
||||||
|
|
||||||
# Keyword Arguments
|
# Keyword Arguments
|
||||||
- `stringkey::Bool=false`
|
- `keytype::Type=Any`
|
||||||
If `true`, every dictionary key is converted to `String` via `string(k)`.
|
The key type for the output Dict. Use `String` for `OrderedDict{String,Any}`, `Symbol` for `OrderedDict{Symbol,Any}`, or `Any` to preserve original key types.
|
||||||
If `false`, original key objects are preserved (useful when keys are already
|
|
||||||
`String`, `Symbol`, or other types you want to keep).
|
|
||||||
|
|
||||||
# Return
|
# Return
|
||||||
- A newly allocated nested structure composed of `OrderedDict{Any,Any}` and
|
- A newly allocated nested structure composed of `OrderedDict{keytype,Any}` and
|
||||||
`Vector{Any}` that mirrors the input shape but uses ordered Julia containers.
|
`Vector{Any}` that mirrors the input shape but uses ordered Julia containers.
|
||||||
|
|
||||||
# Notes
|
# Notes
|
||||||
@@ -361,41 +326,52 @@ julia> d = Dict(
|
|||||||
|
|
||||||
julia jsonstring = JSON.json(d)
|
julia jsonstring = JSON.json(d)
|
||||||
julia> A1 = JSON.parse(jsonstring) # A1 type is JSON.Object
|
julia> A1 = JSON.parse(jsonstring) # A1 type is JSON.Object
|
||||||
julia> A2 = OrderedDict(A1)
|
julia> A2 = ordereddictify(A1; keytype=String)
|
||||||
Dict{Any,Any} with 3 entries:
|
OrderedDict{String,Any} with 3 entries:
|
||||||
"a" => 4
|
"a" => 4
|
||||||
"b" => 6
|
"b" => 6
|
||||||
"c" => Dict("d"=>7, "e"=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
|
"c" => OrderedDict("d"=>7, "e"=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
|
||||||
|
|
||||||
julia> A3 = OrderedDict(A1; stringkey=false) # preserves original key objects
|
julia> A3 = ordereddictify(A1; keytype=Symbol)
|
||||||
julia> B1 = OrderedDict(d; stringkey=true) # convert all keys in to string
|
OrderedDict{Symbol,Any} with 3 entries:
|
||||||
Dict{Any, Any} with 3 entries:
|
:a => 4
|
||||||
"c" => Dict{Any, Any}("e"=>Dict{Any, Any}("f"=>"hey", "g"=>Dict{Any, Any}("world"=>Any[1, "2", 3, Dict{Any, Any}("dd"=>4.7)])), "d"=>7)
|
:b => 6
|
||||||
|
:c => OrderedDict(:d=>7, :e=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
|
||||||
|
|
||||||
|
julia> B1 = ordereddictify(d; keytype=String)
|
||||||
|
OrderedDict{String, Any} with 3 entries:
|
||||||
|
"c" => OrderedDict{String, Any}("e"=>OrderedDict{String, Any}("f"=>"hey", "g"=>OrderedDict{String, Any}("world"=>Any[1, "2", 3, OrderedDict{String, Any}("dd"=>4.7)])), "d"=>7)
|
||||||
"b" => 6
|
"b" => 6
|
||||||
"a" => 4
|
"a" => 4
|
||||||
```
|
```
|
||||||
Ref. https://github.com/andyferris/Dictionaries.jl
|
Ref. https://github.com/andyferris/Dictionaries.jl
|
||||||
"""
|
"""
|
||||||
function ordereddictify(x; stringkey::Bool=false)
|
function ordereddictify(x; keytype::Type=Any)
|
||||||
# Dict-like objects
|
# Dict-like objects
|
||||||
if x isa AbstractDict
|
if x isa AbstractDict
|
||||||
# choose output key type container (String keys when requested)
|
# choose output key type container
|
||||||
out = OrderedDict{Any,Any}()
|
out = OrderedDict{keytype,Any}()
|
||||||
for (k,v) in x
|
for (k,v) in x
|
||||||
newk = stringkey ? string(k) : k
|
if keytype === String
|
||||||
out[newk] = ordereddictify(v; stringkey=stringkey)
|
newk = string(k)
|
||||||
|
elseif keytype === Symbol
|
||||||
|
newk = Symbol(string(k))
|
||||||
|
else
|
||||||
|
newk = k
|
||||||
|
end
|
||||||
|
out[newk] = ordereddictify(v; keytype=keytype)
|
||||||
end
|
end
|
||||||
return out
|
return out
|
||||||
# Arrays / vectors: map elements recursively and return a Vector{Any}
|
# Arrays / vectors: map elements recursively and return a Vector{Any}
|
||||||
elseif x isa AbstractArray
|
elseif x isa AbstractArray
|
||||||
return [ordereddictify(element; stringkey=stringkey) for element in x]
|
return [ordereddictify(element; keytype=keytype) for element in x]
|
||||||
# everything else: return as-is (primitives, numbers, strings, etc.)
|
# everything else: return as-is (primitives, numbers, strings, etc.)
|
||||||
else
|
else
|
||||||
return x
|
return x
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#------------------------------------------------------------------------------------------------100
|
#----------------------------------------------100---------------------------------------------
|
||||||
|
|
||||||
"""
|
"""
|
||||||
print time of cpu executtion at the line inwhich this macro is used
|
print time of cpu executtion at the line inwhich this macro is used
|
||||||
|
|||||||
Reference in New Issue
Block a user