Skip to content

Commit a933528

Browse files
author
Vincent Laugier
committed
changes
1 parent 27bda54 commit a933528

10 files changed

Lines changed: 604 additions & 146 deletions

File tree

Manifest.toml

Lines changed: 136 additions & 88 deletions
Large diffs are not rendered by default.

Project.toml

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

66
[deps]
77
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
88
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
99
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
1010
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1111
LibPQ = "194296ae-ab2e-5f79-8cd4-7183a0a5a0d1"
12+
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
1213
Query = "1a8c2f83-1ff3-5112-b086-8aa67b057ba1"
14+
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
1315
StringCases = "f22f4433-750e-5048-95f9-cae576f2c120"
1416
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
1517
TickTock = "9ff05d80-102d-5586-aa04-3a8bd1a90d20"

src/Cache/_def.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# A dictionary to hold the cache, initialized later
2+
const _cache_ref = Ref{Dict{Any, Any}}()
3+
4+
function add_to_cache end
5+
function get_from_cache end
6+
function remove_from_cache end
7+
function clear_cache end
8+
function cache_keys end
9+
function cache_values end
10+
function _ensure_cache_initialized end
11+
function get_db_entry_key_for_cache end
12+
function generate_fk2pk_mapping_cache end
13+
function generate_cache end
14+
function __init__ end

src/Cache/_imp.jl

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# module CacheModule
2+
3+
using .Cache
4+
using .PostgresORM: FKInfo
5+
6+
"""
7+
add_to_cache(key, value)
8+
9+
Add a key-value pair to the cache.
10+
"""
11+
function Cache.add_to_cache(key, value)
12+
cache = Cache._ensure_cache_initialized()
13+
cache[key] = value
14+
end
15+
16+
"""
17+
get_from_cache(key, default=missing)
18+
19+
Retrieve a value from the cache by its key.
20+
Return `default` if the key is not found.
21+
"""
22+
function Cache.get_from_cache(key, default=missing)
23+
cache = Cache._ensure_cache_initialized()
24+
return get(cache, key, default)
25+
end
26+
27+
"""
28+
remove_from_cache(key)
29+
30+
Remove a key-value pair from the cache by its key.
31+
"""
32+
function Cache.remove_from_cache(key)
33+
cache = Cache._ensure_cache_initialized()
34+
delete!(cache, key)
35+
end
36+
37+
"""
38+
clear_cache()
39+
40+
Clear all items from the cache.
41+
"""
42+
function Cache.clear_cache()
43+
cache = Cache._ensure_cache_initialized()
44+
empty!(cache)
45+
end
46+
47+
"""
48+
cache_keys()
49+
50+
Return a list of all keys in the cache.
51+
"""
52+
function Cache.cache_keys()
53+
cache = Cache._ensure_cache_initialized()
54+
return keys(cache)
55+
end
56+
57+
"""
58+
cache_values()
59+
60+
Return a list of all values in the cache.
61+
"""
62+
function Cache.cache_values()
63+
cache = Cache._ensure_cache_initialized()
64+
return values(cache)
65+
end
66+
67+
# function Cache.to ensure cache is initialized
68+
function Cache._ensure_cache_initialized()
69+
if isnothing(Cache._cache_ref[])
70+
Cache._cache_ref[] = Dict{Any, Any}()
71+
end
72+
return Cache._cache_ref[]
73+
end
74+
75+
function Cache.get_db_entry_key_for_cache(dbconn::LibPQ.Connection)
76+
77+
if !isopen(dbconn)
78+
error("PostgreSQL connection (closed)")
79+
end
80+
81+
keywords_of_interest = [
82+
"host", "port", "dbname"
83+
]
84+
85+
conninfo = LibPQ.conninfo(dbconn)
86+
87+
result_elts = String[]
88+
for keyword in keywords_of_interest
89+
for ci_opt in conninfo
90+
if ci_opt.keyword == keyword
91+
push!(result_elts, string(ci_opt.val))
92+
end
93+
end
94+
end
95+
96+
return join(result_elts, "/")
97+
98+
end
99+
100+
function Cache.generate_fk2pk_mapping_cache(dbconn::LibPQ.Connection)
101+
102+
fkinfos = FKInfo[]
103+
104+
for referencing_schema in SchemaInfo.get_schemas(dbconn)
105+
for referencing_table in SchemaInfo.get_tables(referencing_schema, dbconn)
106+
for (constraint_name,v) in SchemaInfo.get_fks(referencing_table, referencing_schema, dbconn)
107+
108+
referenced_table = v[:referenced_table][:table]
109+
referenced_schema = v[:referenced_table][:schema]
110+
for (referencing_col,referenced_col) in zip(v[:referencing_cols],v[:referenced_cols])
111+
push!(
112+
fkinfos,
113+
FKInfo(
114+
constraint_name = constraint_name,
115+
referencing_table = referencing_table,
116+
referencing_schema = referencing_schema,
117+
referencing_col = referencing_col,
118+
referenced_table = referenced_table,
119+
referenced_schema = referenced_schema,
120+
referenced_col = referenced_col
121+
)
122+
)
123+
end
124+
125+
end
126+
end
127+
end
128+
129+
#
130+
cache = Cache._ensure_cache_initialized()
131+
132+
db_key = Cache.get_db_entry_key_for_cache(dbconn::LibPQ.Connection)
133+
if ismissing(Cache.get_from_cache(db_key, missing))
134+
Cache.add_to_cache(db_key, Dict{Symbol, Any}())
135+
end
136+
137+
if !haskey(cache[db_key], :fkcol2pkcol)
138+
cache[db_key][:fkcol2pkcol] = Dict{
139+
String, # schema
140+
Dict{
141+
String, # table
142+
Dict{
143+
String, # fk col in referencing table
144+
Dict{
145+
String, # referenced table (a fk col can be involved in several FKs)
146+
NamedTuple
147+
}
148+
}
149+
}
150+
}()
151+
end
152+
153+
for fkinfo in fkinfos
154+
155+
156+
if fkinfo.referencing_table == "variable_value"
157+
@info fkinfo.referencing_col
158+
end
159+
160+
# Check that entry for referencing shema exists
161+
if !haskey(cache[db_key][:fkcol2pkcol], fkinfo.referencing_schema)
162+
cache[db_key][:fkcol2pkcol][fkinfo.referencing_schema] = Dict{
163+
String, # table
164+
Dict{
165+
String, # fk col in referencing table
166+
Dict{
167+
String, # referenced table (a fk col can be involved in several FKs)
168+
NamedTuple
169+
}
170+
}
171+
}()
172+
end
173+
174+
# Check that entry for referencing table exists
175+
if !haskey(cache[db_key][:fkcol2pkcol][fkinfo.referencing_schema], fkinfo.referencing_table)
176+
cache[db_key][:fkcol2pkcol][fkinfo.referencing_schema][fkinfo.referencing_table] = Dict{
177+
String, # fk col in referencing table
178+
Dict{
179+
String, # referenced table (a fk col can be involved in several FKs)
180+
NamedTuple
181+
}
182+
}()
183+
end
184+
185+
# Check that entry for referencing column exists
186+
if !haskey(cache[db_key][:fkcol2pkcol][fkinfo.referencing_schema], fkinfo.referencing_table)
187+
cache[db_key][:fkcol2pkcol][fkinfo.referencing_schema][fkinfo.referencing_table][fkinfo.referencing_col] = Dict{
188+
String, # fk col in referencing table
189+
Dict{
190+
String, # referenced table (a fk col can be involved in several FKs)
191+
NamedTuple
192+
}
193+
}()
194+
end
195+
196+
cache[db_key][:fkcol2pkcol][fkinfo.referencing_schema][fkinfo.referencing_table][fkinfo.referencing_col] =
197+
(schema=fkinfo.referenced_schema, table=fkinfo.referenced_table, col=fkinfo.referenced_col)
198+
end
199+
200+
end
201+
202+
function Cache.__init__()
203+
@info("########### init Cache module")
204+
# Initialize the cache when the module is loaded
205+
Cache._cache_ref[] = Dict{Any, Any}()
206+
end
207+
208+
export add_to_cache, get_from_cache, remove_from_cache, clear_cache, cache_keys, cache_values
209+
210+
# end # module

src/Controller/coreORM.create.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ function create_entity!(new_object::IEntity,
120120
# @info "query is prepared"
121121
# laptimer()
122122

123-
# @info query_string
124-
# @info properties_values
123+
@info query_string
124+
@info properties_values
125125

126126
query_result = execute(prepared_query,
127127
properties_values
@@ -149,7 +149,9 @@ function create_entity!(new_object::IEntity,
149149
# @info "dataframe is transformed to vector of named tuple"
150150
# laptimer()
151151

152-
result =
152+
Serialization.serialize("/home/orfead/CODE/ORFEAD.jl/tmp/result.jls", result)
153+
154+
result =
153155
util_convert_namedtuple_to_object.(result,data_type,
154156
true, # retrieve_complex_props
155157
dbconn)

0 commit comments

Comments
 (0)