-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_imp.jl
More file actions
205 lines (167 loc) · 5.89 KB
/
_imp.jl
File metadata and controls
205 lines (167 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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