Skip to content

Commit d4fa81b

Browse files
authored
Fix reverse engineering for fk pk sharing same column (#35)
1 parent 71723f2 commit d4fa81b

20 files changed

+1286
-1074
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PostgresORM"
22
uuid = "748b5efa-ed57-4836-b183-a38105a77fdd"
33
authors = ["Vincent Laugier <vincent.laugier@gmail.com>"]
4-
version = "0.5.7"
4+
version = "0.6.0"
55

66
[deps]
77
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"

src/Controller/coreORM.create.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ function create_entity!(new_object::IEntity,
9595
column_names = util_getcolumns(properties_names, columns_selection_and_mapping)
9696
properties_values = collect(values(props))
9797

98+
# Dedup column names and values in case some properties are sharing the same columns
99+
PostgresORMUtil.dedup_colnames_colvalues!(column_names, properties_values)
100+
98101
# Loop over the properties of the object and
99102
# build the appropriate list of columns
100103
if length(props) > 0
@@ -104,7 +107,7 @@ function create_entity!(new_object::IEntity,
104107
end
105108

106109
# Add the prepared statement indexes
107-
query_indexes = string.(collect(1:length(properties_names)))
110+
query_indexes = string.(collect(1:length(column_names)))
108111
query_indexes = string.("\$",query_indexes)
109112
query_string *= (" VALUES ("
110113
* join(query_indexes,",")

src/PostgresORM.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module PostgresORM
7070

7171
# Provides functions to get information about the database structures
7272
module SchemaInfo
73-
include("./SchemaInfo/SchemaInfo-def.jl") # This is only the definition of the
73+
include("./SchemaInfo/_def.jl") # This is only the definition of the
7474
# module. See below for the actual
7575
# implementation.
7676
end #module SchemaInfo
@@ -116,7 +116,7 @@ module PostgresORM
116116
end # module Tool
117117

118118
# Implementation of the SchemaInfo module
119-
include("./SchemaInfo/SchemaInfo-imp.jl")
119+
include("./SchemaInfo/_imp.jl")
120120

121121

122122
include("./exposed-functions-from-submodules.jl")

src/PostgresORMUtil/utils.jl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,59 @@ function remove_spaces_and_split(str::String)
380380
str = replace(str, " " => "")
381381
return split(str,',')
382382
end
383+
384+
"""
385+
dedup_colnames_colvalues!(column_names::Vector{String}, column_values::Vector)
386+
387+
Inplace dedup column names and values by giving priority to non missing elements
388+
389+
Eg.
390+
Applying function to the following arguments:
391+
column_names = ["exam_log_id", "in_date", "in_date", "out_time", "api_url", "in_time", "user_id"]
392+
column_values = ["215c81e9-e002-402e-8482-1382a65ef1e4", "2024-03-11", missing, "2024-03-11T08:27:12.363+01:00", "exam/save-values", "2024-03-11T08:27:09.363+01:00", "b7845f35-0169-488d-b8ce-111f0f07d695"]
393+
394+
will result in a dropping the third element of both vectors
395+
396+
"""
397+
function dedup_colnames_colvalues!(
398+
column_names::Vector{String},
399+
column_values::Vector
400+
)
401+
402+
# Function to check if a value is considered "missing" in your context
403+
is_missing(value) = value === missing # Replace 'nothing' with your definition of a missing value
404+
405+
# Create a dictionary to track the first occurrence and its index
406+
seen = Dict{String, Int}()
407+
408+
# Iterate backwards to prefer the first non-missing value when deduplicating
409+
for i in length(column_names):-1:1
410+
name, value = column_names[i], column_values[i]
411+
if !is_missing(value)
412+
if haskey(seen, name)
413+
# Update with non-missing value if found later
414+
column_values[seen[name]] = value
415+
else
416+
seen[name] = i # Track the first non-missing occurrence
417+
end
418+
elseif !haskey(seen, name)
419+
# Even if it's missing, we need to track the first occurrence
420+
seen[name] = i
421+
end
422+
end
423+
424+
# Filter the original arrays based on the indices stored in 'seen'
425+
# This will also sort them based on their original order preserved by iteration order in 'seen'
426+
filter_indices = sort(collect(values(seen)))
427+
428+
# Assign new values to the beginning of the original arrays
429+
column_names[1:length(filter_indices)] = [column_names[i] for i in filter_indices]
430+
column_values[1:length(filter_indices)] = [column_values[i] for i in filter_indices]
431+
432+
# Resize the original arrays to remove the extra elements
433+
resize!(column_names, length(filter_indices))
434+
resize!(column_values, length(filter_indices))
435+
436+
nothing
437+
438+
end

0 commit comments

Comments
 (0)