Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 136 additions & 88 deletions Manifest.toml

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
name = "PostgresORM"
uuid = "748b5efa-ed57-4836-b183-a38105a77fdd"
authors = ["Vincent Laugier <vincent.laugier@gmail.com>"]
version = "0.7.1"
version = "0.8.0"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
LibPQ = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
Query = "1a8c2f83-1ff3-5112-b086-8aa67b057ba1"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
StringCases = "f22f4433-750e-5048-95f9-cae576f2c120"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
TickTock = "9ff05d80-102d-5586-aa04-3a8bd1a90d20"
Expand Down
14 changes: 14 additions & 0 deletions src/Cache/_def.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# A dictionary to hold the cache, initialized later
const _cache_ref = Ref{Dict{Any, Any}}()

function add_to_cache end
function get_from_cache end
function remove_from_cache end
function clear_cache end
function cache_keys end
function cache_values end
function _ensure_cache_initialized end
function get_db_entry_key_for_cache end
function generate_fk2pk_mapping_cache end
function generate_cache end
function __init__ end
205 changes: 205 additions & 0 deletions src/Cache/_imp.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# module CacheModule

using .Cache
using .PostgresORM: FKInfo

"""
add_to_cache(key, value)

Add a key-value pair to the cache.
"""
function Cache.add_to_cache(key, value)
cache = Cache._ensure_cache_initialized()
cache[key] = value
end

"""
get_from_cache(key, default=missing)

Retrieve a value from the cache by its key.
Return `default` if the key is not found.
"""
function Cache.get_from_cache(key, default=missing)
cache = Cache._ensure_cache_initialized()
return get(cache, key, default)
end

"""
remove_from_cache(key)

Remove a key-value pair from the cache by its key.
"""
function Cache.remove_from_cache(key)
cache = Cache._ensure_cache_initialized()
delete!(cache, key)
end

"""
clear_cache()

Clear all items from the cache.
"""
function Cache.clear_cache()
cache = Cache._ensure_cache_initialized()
empty!(cache)
end

"""
cache_keys()

Return a list of all keys in the cache.
"""
function Cache.cache_keys()
cache = Cache._ensure_cache_initialized()
return keys(cache)
end

"""
cache_values()

Return a list of all values in the cache.
"""
function Cache.cache_values()
cache = Cache._ensure_cache_initialized()
return values(cache)
end

# function Cache.to ensure cache is initialized
function Cache._ensure_cache_initialized()
if isnothing(Cache._cache_ref[])
Cache._cache_ref[] = Dict{Any, Any}()
end
return Cache._cache_ref[]
end

function Cache.get_db_entry_key_for_cache(dbconn::LibPQ.Connection)

if !isopen(dbconn)
error("PostgreSQL connection (closed)")
end

keywords_of_interest = [
"host", "port", "dbname"
]

conninfo = LibPQ.conninfo(dbconn)

result_elts = String[]
for keyword in keywords_of_interest
for ci_opt in conninfo
if ci_opt.keyword == keyword
push!(result_elts, string(ci_opt.val))
end
end
end

return join(result_elts, "/")

end

function Cache.generate_fk2pk_mapping_cache(dbconn::LibPQ.Connection)

fkinfos = FKInfo[]

for referencing_schema in SchemaInfo.get_schemas(dbconn)
for referencing_table in SchemaInfo.get_tables(referencing_schema, dbconn)
for (constraint_name,v) in SchemaInfo.get_fks(referencing_table, referencing_schema, dbconn)

referenced_table = v[:referenced_table][:table]
referenced_schema = v[:referenced_table][:schema]
for (referencing_col,referenced_col) in zip(v[:referencing_cols],v[:referenced_cols])
push!(
fkinfos,
FKInfo(
constraint_name = constraint_name,
referencing_table = referencing_table,
referencing_schema = referencing_schema,
referencing_col = referencing_col,
referenced_table = referenced_table,
referenced_schema = referenced_schema,
referenced_col = referenced_col
)
)
end

end
end
end

#
cache = Cache._ensure_cache_initialized()

db_key = Cache.get_db_entry_key_for_cache(dbconn::LibPQ.Connection)
if ismissing(Cache.get_from_cache(db_key, missing))
Cache.add_to_cache(db_key, Dict{Symbol, Any}())
end

if !haskey(cache[db_key], :fkcol2pkcol)
cache[db_key][:fkcol2pkcol] = Dict{
String, # schema
Dict{
String, # table
Dict{
String, # fk col in referencing table
Dict{
String, # referenced table (a fk col can be involved in several FKs)
NamedTuple
}
}
}
}()
end

for fkinfo in fkinfos

# Check that entry for referencing shema exists
if !haskey(cache[db_key][:fkcol2pkcol], fkinfo.referencing_schema)
cache[db_key][:fkcol2pkcol][fkinfo.referencing_schema] = Dict{
String, # table
Dict{
String, # fk col in referencing table
Dict{
String, # referenced table (a fk col can be involved in several FKs)
NamedTuple
}
}
}()
end

# Check that entry for referencing table exists
if !haskey(cache[db_key][:fkcol2pkcol][fkinfo.referencing_schema], fkinfo.referencing_table)
cache[db_key][:fkcol2pkcol][fkinfo.referencing_schema][fkinfo.referencing_table] = Dict{
String, # fk col in referencing table
Dict{
String, # referenced table (a fk col can be involved in several FKs)
NamedTuple
}
}()
end

# Check that entry for referencing column exists
if !haskey(cache[db_key][:fkcol2pkcol][fkinfo.referencing_schema], fkinfo.referencing_table)
cache[db_key][:fkcol2pkcol][fkinfo.referencing_schema][fkinfo.referencing_table][fkinfo.referencing_col] = Dict{
String, # fk col in referencing table
Dict{
String, # referenced table (a fk col can be involved in several FKs)
NamedTuple
}
}()
end

cache[db_key][:fkcol2pkcol][fkinfo.referencing_schema][fkinfo.referencing_table][fkinfo.referencing_col] =
(schema=fkinfo.referenced_schema, table=fkinfo.referenced_table, col=fkinfo.referenced_col)
end

end

function Cache.__init__()
@info("Init PostgresORM.Cache module")
# Initialize the cache when the module is loaded
Cache._cache_ref[] = Dict{Any, Any}()
end

export add_to_cache, get_from_cache, remove_from_cache, clear_cache, cache_keys, cache_values

# end # module
4 changes: 3 additions & 1 deletion src/Controller/coreORM.create.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ function create_entity!(new_object::IEntity,
# @info "dataframe is transformed to vector of named tuple"
# laptimer()

result =
Serialization.serialize("/home/orfead/CODE/ORFEAD.jl/tmp/result.jls", result)

result =
util_convert_namedtuple_to_object.(result,data_type,
true, # retrieve_complex_props
dbconn)
Expand Down
Loading
Loading