Skip to content

Commit c37c119

Browse files
committed
Fix library list bug with caching
Signed-off-by: worksofliam <[email protected]>
1 parent eedcba4 commit c37c119

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/database/schemas.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,30 @@ const BASE_RESOLVE_SELECT = [
7575
`end as sqlType`,
7676
].join(` `);
7777

78-
let ReferenceCache: Map<string, ResolvedSqlObject> = new Map<string, ResolvedSqlObject>();
78+
7979
export default class Schemas {
80+
private static ReferenceCache: Map<string, ResolvedSqlObject> = new Map<string, ResolvedSqlObject>();
81+
8082
private static buildReferenceCacheKey(obj: ObjectReference): string {
8183
return `${obj.schema}.${obj.name}`;
8284
}
85+
86+
87+
static storeCachedReference(obj: ObjectReference, resolvedTo: ResolvedSqlObject): void {
88+
if (obj.name && obj.schema) {
89+
const key = Schemas.buildReferenceCacheKey(obj);
90+
this.ReferenceCache.set(key, resolvedTo);
91+
}
92+
}
93+
94+
static getCachedReference(obj: ObjectReference): ResolvedSqlObject | undefined {
95+
if (obj.name && obj.schema) {
96+
const key = Schemas.buildReferenceCacheKey(obj);
97+
return this.ReferenceCache.get(key);
98+
}
99+
return undefined;
100+
}
101+
83102
/**
84103
* Resolves to the following SQL types: SCHEMA, TABLE, VIEW, ALIAS, INDEX, FUNCTION and PROCEDURE
85104
*/
@@ -96,12 +115,12 @@ export default class Schemas {
96115
// First, we use OBJECT_STATISTICS to resolve the object based on the library list.
97116
// But, if the object is qualified with a schema, we need to use that schema to get the correct object.
98117
for (const obj of sqlObjects) {
99-
const key = this.buildReferenceCacheKey(obj);
100-
// check if we have already resolved this object
101-
if (ReferenceCache.has(key)) {
102-
resolvedObjects.push(ReferenceCache.get(key!));
118+
const cached = this.getCachedReference(obj);
119+
if (cached) {
120+
resolvedObjects.push(cached);
103121
continue;
104122
}
123+
105124
if (obj.schema) {
106125
statements.push(
107126
`${BASE_RESOLVE_SELECT} from table(qsys2.object_statistics(?, '*ALL', object_name => ?))`
@@ -169,10 +188,7 @@ export default class Schemas {
169188

170189
// add reslved objects to to ReferenceCache
171190
resolvedObjects.forEach((obj) => {
172-
const key = this.buildReferenceCacheKey(obj);
173-
if (!ReferenceCache.has(key)) {
174-
ReferenceCache.set(key, obj);
175-
}
191+
this.storeCachedReference(obj, obj);
176192
});
177193

178194
return resolvedObjects;

0 commit comments

Comments
 (0)