|
| 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 |
0 commit comments