14 Commits

Author SHA1 Message Date
05d8cb9c02 update 2026-06-24 12:29:01 +07:00
13de2f90ff update 2026-06-07 17:22:24 +07:00
22fe810f63 update 2026-06-07 17:20:52 +07:00
ton
abdf6cf3b8 Merge pull request 'update' (#6) from add_generateupdateSQL into main
Reviewed-on: #6
2026-06-07 09:32:41 +00:00
f2ba243df0 update 2026-06-07 16:27:08 +07:00
ton
bb2851332a Merge pull request 'update' (#5) from add_generateupdateSQL into main
Reviewed-on: #5
2026-06-07 09:20:55 +00:00
00225f3a06 update 2026-06-07 16:16:52 +07:00
ton
7cb0bd077f Merge pull request 'update' (#4) from add_generateupdateSQL into main
Reviewed-on: #4
2026-06-07 09:04:33 +00:00
0ba2aa310e update 2026-06-07 15:32:49 +07:00
ton
1916668c6e Merge pull request 'add_generateupdateSQL' (#3) from add_generateupdateSQL into main
Reviewed-on: #3
2026-06-07 06:57:28 +00:00
919800da42 add generateUpdateSQL 2026-06-07 13:56:07 +07:00
947580a2ec update 2026-06-07 13:46:04 +07:00
688b9a22b6 add generateUpdateSQL 2026-06-07 13:43:47 +07:00
ton
76ce0fc54f Merge pull request 'usability' (#2) from usability into main
Reviewed-on: #2
2026-05-29 04:17:51 +00:00
2 changed files with 97 additions and 79 deletions

View File

@@ -121,7 +121,8 @@ function generateInsertSQL(table_name::String, columnToInsert::Vector{Symbol}, d
for (key, value) in data
if key columnToInsert
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
@@ -131,6 +132,22 @@ function generateInsertSQL(table_name::String, columnToInsert::Vector{Symbol}, d
return "INSERT INTO $table_name ($columns_str) VALUES ($values_str);"
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 --------------------------------------------- #
@@ -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;"
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

View File

@@ -202,13 +202,13 @@ end
if it is an `AbstractArray` its elements will be processed recursively.
# 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`
If `true`, every dictionary key is converted to `String` via `string(k)`.
If `false`, original key objects are preserved (useful when keys are already
`String`, `Symbol`, or other types you want to keep).
If `true`, every dictionary key is converted to `String` via `string(k)`. This parameter is ignored when `keytype` is explicitly set.
# 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.
# Notes
@@ -236,79 +236,46 @@ julia> d = Dict(
julia jsonstring = JSON.json(d)
julia> A1 = JSON.parse(jsonstring) # A1 type is JSON.Object
julia> A2 = dictify(A1)
Dict{Any,Any} with 3 entries:
julia> A2 = dictify(A1; keytype=String)
Dict{String,Any} with 3 entries:
"a" => 4
"b" => 6
"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> B1 = dictify(d; stringkey=true) # convert all keys in to string
Dict{Any, Any} with 3 entries:
"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
"a" => 4
```
julia> A3 = dictify(A1; keytype=Symbol)
Dict{Symbol,Any} with 3 entries:
:a => 4
:b => 6
:c => Dict(:d=>7, :e=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
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
if x isa AbstractDict
# choose output key type container (String keys when requested)
out = Dict{Any,Any}()
# choose output key type container
out = Dict{keytype,Any}()
for (k,v) in x
newk = stringkey ? string(k) : k
out[newk] = dictify(v; stringkey=stringkey)
if keytype === String
newk = string(k)
elseif keytype === Symbol
newk = Symbol(string(k))
else
newk = k
end
out[newk] = dictify(v; keytype=keytype)
end
return out
# Arrays / vectors: map elements recursively and return a Vector{Any}
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.)
else
return x
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 --------------------------------------------- #
""" Recursively convert dictionary-like variable (e.g. JSON.Object) into a dictionary.
@@ -328,13 +295,11 @@ end
processed recursively.
# Keyword Arguments
- `stringkey::Bool=false`
If `true`, every dictionary key is converted to `String` via `string(k)`.
If `false`, original key objects are preserved (useful when keys are already
`String`, `Symbol`, or other types you want to keep).
- `keytype::Type=Any`
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.
# 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.
# Notes
@@ -361,41 +326,52 @@ julia> d = Dict(
julia jsonstring = JSON.json(d)
julia> A1 = JSON.parse(jsonstring) # A1 type is JSON.Object
julia> A2 = OrderedDict(A1)
Dict{Any,Any} with 3 entries:
julia> A2 = ordereddictify(A1; keytype=String)
OrderedDict{String,Any} with 3 entries:
"a" => 4
"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> B1 = OrderedDict(d; stringkey=true) # convert all keys in to string
Dict{Any, Any} with 3 entries:
"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)
julia> A3 = ordereddictify(A1; keytype=Symbol)
OrderedDict{Symbol,Any} with 3 entries:
:a => 4
: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
"a" => 4
```
Ref. https://github.com/andyferris/Dictionaries.jl
"""
function ordereddictify(x; stringkey::Bool=false)
function ordereddictify(x; keytype::Type=Any)
# Dict-like objects
if x isa AbstractDict
# choose output key type container (String keys when requested)
out = OrderedDict{Any,Any}()
# choose output key type container
out = OrderedDict{keytype,Any}()
for (k,v) in x
newk = stringkey ? string(k) : k
out[newk] = ordereddictify(v; stringkey=stringkey)
if keytype === String
newk = string(k)
elseif keytype === Symbol
newk = Symbol(string(k))
else
newk = k
end
out[newk] = ordereddictify(v; keytype=keytype)
end
return out
# Arrays / vectors: map elements recursively and return a Vector{Any}
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.)
else
return x
end
end
#------------------------------------------------------------------------------------------------100
#----------------------------------------------100---------------------------------------------
"""
print time of cpu executtion at the line inwhich this macro is used