jinaga-server 2.1.7 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -53,7 +53,7 @@ class QueryDescription {
53
53
  private readonly notExistsConditions: NotExistsConditionDescription[] = []
54
54
  ) {}
55
55
 
56
- generateSqlQuery(bookmark: string, limit: number): SpecificationSqlQuery {
56
+ generateSqlQuery(schema: string, bookmark: string, limit: number): SpecificationSqlQuery {
57
57
  const hashes = this.outputs
58
58
  .map(output => `f${output.factIndex}.hash as hash${output.factIndex}`)
59
59
  .join(", ");
@@ -65,17 +65,17 @@ class QueryDescription {
65
65
  const successorFact = this.inputs.find(i => i.factIndex === firstEdge.successorFactIndex);
66
66
  const firstFactIndex = predecessorFact ? predecessorFact.factIndex : successorFact!.factIndex;
67
67
  const writtenFactIndexes = new Set<number>().add(firstFactIndex);
68
- const joins: string[] = generateJoins(this.edges, writtenFactIndexes);
68
+ const joins: string[] = generateJoins(schema, this.edges, writtenFactIndexes);
69
69
  const inputWhereClauses = this.inputs
70
70
  .filter(input => input.factTypeParameter !== 0)
71
71
  .map(input => `f${input.factIndex}.fact_type_id = $${input.factTypeParameter} AND f${input.factIndex}.hash = $${input.factHashParameter}`)
72
72
  .join(" AND ");
73
73
  const notExistsWhereClauses = this.notExistsConditions
74
- .map(notExistsWhereClause => ` AND NOT EXISTS (${generateNotExistsWhereClause(notExistsWhereClause, writtenFactIndexes)})`)
74
+ .map(notExistsWhereClause => ` AND NOT EXISTS (${generateNotExistsWhereClause(schema, notExistsWhereClause, writtenFactIndexes)})`)
75
75
  .join("");
76
76
  const bookmarkParameter = this.parameters.length + 1;
77
77
  const limitParameter = bookmarkParameter + 1;
78
- const sql = `SELECT ${hashes}, sort(array[${factIds}], 'desc') as bookmark FROM public.fact f${firstFactIndex}${joins.join("")} WHERE ${inputWhereClauses}${notExistsWhereClauses} AND sort(array[${factIds}], 'desc') > $${bookmarkParameter} ORDER BY bookmark ASC LIMIT $${limitParameter}`;
78
+ const sql = `SELECT ${hashes}, sort(array[${factIds}], 'desc') as bookmark FROM ${schema}.fact f${firstFactIndex}${joins.join("")} WHERE ${inputWhereClauses}${notExistsWhereClauses} AND sort(array[${factIds}], 'desc') > $${bookmarkParameter} ORDER BY bookmark ASC LIMIT $${limitParameter}`;
79
79
  const bookmarkValue: number[] = parseBookmark(bookmark);
80
80
  return {
81
81
  sql,
@@ -89,13 +89,13 @@ class QueryDescription {
89
89
  }
90
90
  }
91
91
 
92
- function generateJoins(edges: EdgeDescription[], writtenFactIndexes: Set<number>) {
92
+ function generateJoins(schema: string, edges: EdgeDescription[], writtenFactIndexes: Set<number>) {
93
93
  const joins: string[] = [];
94
94
  edges.forEach(edge => {
95
95
  if (writtenFactIndexes.has(edge.predecessorFactIndex)) {
96
96
  if (writtenFactIndexes.has(edge.successorFactIndex)) {
97
97
  joins.push(
98
- ` JOIN public.edge e${edge.edgeIndex}` +
98
+ ` JOIN ${schema}.edge e${edge.edgeIndex}` +
99
99
  ` ON e${edge.edgeIndex}.predecessor_fact_id = f${edge.predecessorFactIndex}.fact_id` +
100
100
  ` AND e${edge.edgeIndex}.successor_fact_id = f${edge.successorFactIndex}.fact_id` +
101
101
  ` AND e${edge.edgeIndex}.role_id = $${edge.roleParameter}`
@@ -103,12 +103,12 @@ function generateJoins(edges: EdgeDescription[], writtenFactIndexes: Set<number>
103
103
  }
104
104
  else {
105
105
  joins.push(
106
- ` JOIN public.edge e${edge.edgeIndex}` +
106
+ ` JOIN ${schema}.edge e${edge.edgeIndex}` +
107
107
  ` ON e${edge.edgeIndex}.predecessor_fact_id = f${edge.predecessorFactIndex}.fact_id` +
108
108
  ` AND e${edge.edgeIndex}.role_id = $${edge.roleParameter}`
109
109
  );
110
110
  joins.push(
111
- ` JOIN public.fact f${edge.successorFactIndex}` +
111
+ ` JOIN ${schema}.fact f${edge.successorFactIndex}` +
112
112
  ` ON f${edge.successorFactIndex}.fact_id = e${edge.edgeIndex}.successor_fact_id`
113
113
  );
114
114
  writtenFactIndexes.add(edge.successorFactIndex);
@@ -116,12 +116,12 @@ function generateJoins(edges: EdgeDescription[], writtenFactIndexes: Set<number>
116
116
  }
117
117
  else if (writtenFactIndexes.has(edge.successorFactIndex)) {
118
118
  joins.push(
119
- ` JOIN public.edge e${edge.edgeIndex}` +
119
+ ` JOIN ${schema}.edge e${edge.edgeIndex}` +
120
120
  ` ON e${edge.edgeIndex}.successor_fact_id = f${edge.successorFactIndex}.fact_id` +
121
121
  ` AND e${edge.edgeIndex}.role_id = $${edge.roleParameter}`
122
122
  );
123
123
  joins.push(
124
- ` JOIN public.fact f${edge.predecessorFactIndex}` +
124
+ ` JOIN ${schema}.fact f${edge.predecessorFactIndex}` +
125
125
  ` ON f${edge.predecessorFactIndex}.fact_id = e${edge.edgeIndex}.predecessor_fact_id`
126
126
  );
127
127
  writtenFactIndexes.add(edge.predecessorFactIndex);
@@ -133,7 +133,7 @@ function generateJoins(edges: EdgeDescription[], writtenFactIndexes: Set<number>
133
133
  return joins;
134
134
  }
135
135
 
136
- function generateNotExistsWhereClause(notExistsWhereClause: NotExistsConditionDescription, outerFactIndexes: Set<number>): string {
136
+ function generateNotExistsWhereClause(schema: string, notExistsWhereClause: NotExistsConditionDescription, outerFactIndexes: Set<number>): string {
137
137
  const firstEdge = notExistsWhereClause.edges[0];
138
138
  const writtenFactIndexes = new Set<number>(outerFactIndexes);
139
139
  const firstJoin: string[] = [];
@@ -148,7 +148,7 @@ function generateNotExistsWhereClause(notExistsWhereClause: NotExistsConditionDe
148
148
  ` AND e${firstEdge.edgeIndex}.role_id = $${firstEdge.roleParameter}`
149
149
  );
150
150
  firstJoin.push(
151
- ` JOIN public.fact f${firstEdge.successorFactIndex}` +
151
+ ` JOIN ${schema}.fact f${firstEdge.successorFactIndex}` +
152
152
  ` ON f${firstEdge.successorFactIndex}.fact_id = e${firstEdge.edgeIndex}.successor_fact_id`
153
153
  );
154
154
  writtenFactIndexes.add(firstEdge.successorFactIndex);
@@ -160,7 +160,7 @@ function generateNotExistsWhereClause(notExistsWhereClause: NotExistsConditionDe
160
160
  ` AND e${firstEdge.edgeIndex}.role_id = $${firstEdge.roleParameter}`
161
161
  );
162
162
  firstJoin.push(
163
- ` JOIN public.fact f${firstEdge.predecessorFactIndex}` +
163
+ ` JOIN ${schema}.fact f${firstEdge.predecessorFactIndex}` +
164
164
  ` ON f${firstEdge.predecessorFactIndex}.fact_id = e${firstEdge.edgeIndex}.predecessor_fact_id`
165
165
  );
166
166
  writtenFactIndexes.add(firstEdge.predecessorFactIndex);
@@ -168,9 +168,9 @@ function generateNotExistsWhereClause(notExistsWhereClause: NotExistsConditionDe
168
168
  else {
169
169
  throw new Error("Neither predecessor nor successor fact has been written");
170
170
  }
171
- const tailJoins: string[] = generateJoins(notExistsWhereClause.edges.slice(1), writtenFactIndexes);
171
+ const tailJoins: string[] = generateJoins(schema, notExistsWhereClause.edges.slice(1), writtenFactIndexes);
172
172
  const joins = firstJoin.concat(tailJoins);
173
- return `SELECT 1 FROM public.edge e${firstEdge.edgeIndex}${joins.join("")} WHERE ${whereClause.join(" AND ")}`;
173
+ return `SELECT 1 FROM ${schema}.edge e${firstEdge.edgeIndex}${joins.join("")} WHERE ${whereClause.join(" AND ")}`;
174
174
  }
175
175
 
176
176
  function parseBookmark(bookmark: string): number[] {
@@ -330,7 +330,7 @@ class DescriptionBuilder {
330
330
  }
331
331
  }
332
332
 
333
- export function sqlFromSpecification(start: FactReference[], bookmarks: string[], limit: number, specification: Specification, factTypes: Map<string, number>, roleMap: Map<number, Map<string, number>>): SpecificationSqlQuery[] {
333
+ export function sqlFromSpecification(start: FactReference[], schema: string, bookmarks: string[], limit: number, specification: Specification, factTypes: Map<string, number>, roleMap: Map<number, Map<string, number>>): SpecificationSqlQuery[] {
334
334
  const feeds = buildFeeds(start, specification);
335
335
  const descriptionBuilder = new DescriptionBuilder(factTypes, roleMap);
336
336
  const feedAndBookmark = feeds.map((feed, index) => ({
@@ -341,18 +341,18 @@ export function sqlFromSpecification(start: FactReference[], bookmarks: string[]
341
341
  descriptionBuilder.isSatisfiable(fb.feed, fb.feed.edges));
342
342
  const sqlQueries = satisfiableFeedsAndBookmarks.map(fb => {
343
343
  const description = descriptionBuilder.buildDescription(fb.feed);
344
- const sql = description.generateSqlQuery(fb.bookmark, limit);
344
+ const sql = description.generateSqlQuery(schema, fb.bookmark, limit);
345
345
  return sql;
346
346
  });
347
347
  return sqlQueries;
348
348
  }
349
349
 
350
- export function sqlFromFeed(feed: Feed, bookmark: string, limit: number, factTypes: Map<string, number>, roleMap: Map<number, Map<string, number>>): SpecificationSqlQuery | null {
350
+ export function sqlFromFeed(feed: Feed, schema: string, bookmark: string, limit: number, factTypes: Map<string, number>, roleMap: Map<number, Map<string, number>>): SpecificationSqlQuery | null {
351
351
  const descriptionBuilder = new DescriptionBuilder(factTypes, roleMap);
352
352
  if (!descriptionBuilder.isSatisfiable(feed, feed.edges)) {
353
353
  return null;
354
354
  }
355
355
  const description = descriptionBuilder.buildDescription(feed);
356
- const sql = description.generateSqlQuery(bookmark, limit);
356
+ const sql = description.generateSqlQuery(schema, bookmark, limit);
357
357
  return sql;
358
358
  }
@@ -10,10 +10,10 @@ export type SqlQuery = {
10
10
  factTypeNames: string[]
11
11
  };
12
12
 
13
- export function sqlFromSteps(start: FactReference, steps: Step[], factTypes: FactTypeMap, roleMap: RoleMap) : SqlQuery | null {
13
+ export function sqlFromSteps(start: FactReference, steps: Step[], factTypes: FactTypeMap, roleMap: RoleMap, schema: string) : SqlQuery | null {
14
14
  const builder = new QueryBuilder(factTypes, roleMap);
15
15
 
16
- return builder.buildQuery(start, steps);
16
+ return builder.buildQuery(start, steps, schema);
17
17
  }
18
18
 
19
19
  interface QueryJoinEdge {
@@ -90,7 +90,7 @@ class QueryBuilder {
90
90
  constructor(private factTypes: FactTypeMap, private roleMap: RoleMap) {
91
91
  }
92
92
 
93
- buildQuery(start: FactReference, steps: Step[]): SqlQuery | null {
93
+ buildQuery(start: FactReference, steps: Step[], schema: string): SqlQuery | null {
94
94
  if (steps.length === 0) {
95
95
  return null;
96
96
  }
@@ -120,9 +120,9 @@ class QueryBuilder {
120
120
  .filter(j => j.table === 'fact')
121
121
  .map(j => (j as QueryJoinFact).factAlias);
122
122
  const hashes = factAliases.map(a => `f${a}.hash as hash${a}`).join(', ');
123
- const joins = this.buildJoins(this.queryParts.joins, 'f1.fact_id');
124
- const whereClause = this.buildWhereClause(this.queryParts.existentialClauses);
125
- const sql = `SELECT ${hashes} FROM public.fact f1${joins} WHERE f1.fact_type_id = $1 AND f1.hash = $2${whereClause}`;
123
+ const joins = this.buildJoins(schema, this.queryParts.joins, 'f1.fact_id');
124
+ const whereClause = this.buildWhereClause(schema, this.queryParts.existentialClauses);
125
+ const sql = `SELECT ${hashes} FROM ${schema}.fact f1${joins} WHERE f1.fact_type_id = $1 AND f1.hash = $2${whereClause}`;
126
126
 
127
127
  return {
128
128
  empty: false,
@@ -133,7 +133,7 @@ class QueryBuilder {
133
133
  };
134
134
  }
135
135
 
136
- private buildJoins(joins: QueryJoin[], priorFactId: string) {
136
+ private buildJoins(schema: string, joins: QueryJoin[], priorFactId: string) {
137
137
  interface Joins {
138
138
  priorFactId: string,
139
139
  clauses: string[]
@@ -145,7 +145,7 @@ class QueryBuilder {
145
145
  const clauses = joins.reduce((joins, join) => {
146
146
  if (join.table === 'edge') {
147
147
  if (join.direction === 'successor') {
148
- const clause = ` JOIN public.edge e${join.edgeAlias} ` +
148
+ const clause = ` JOIN ${schema}.edge e${join.edgeAlias} ` +
149
149
  `ON e${join.edgeAlias}.predecessor_fact_id = ${joins.priorFactId} ` +
150
150
  `AND e${join.edgeAlias}.role_id = $${join.roleParameter}`;
151
151
  return {
@@ -154,7 +154,7 @@ class QueryBuilder {
154
154
  };
155
155
  }
156
156
  else {
157
- const clause = ` JOIN public.edge e${join.edgeAlias} ` +
157
+ const clause = ` JOIN ${schema}.edge e${join.edgeAlias} ` +
158
158
  `ON e${join.edgeAlias}.successor_fact_id = ${joins.priorFactId} ` +
159
159
  `AND e${join.edgeAlias}.role_id = $${join.roleParameter}`;
160
160
  return {
@@ -163,7 +163,7 @@ class QueryBuilder {
163
163
  };
164
164
  }
165
165
  } else {
166
- const clause = ` JOIN public.fact f${join.factAlias} ON f${join.factAlias}.fact_id = ${joins.priorFactId}`;
166
+ const clause = ` JOIN ${schema}.fact f${join.factAlias} ON f${join.factAlias}.fact_id = ${joins.priorFactId}`;
167
167
  return {
168
168
  priorFactId: joins.priorFactId,
169
169
  clauses: [...joins.clauses, clause]
@@ -173,7 +173,7 @@ class QueryBuilder {
173
173
  return clauses.join('');
174
174
  }
175
175
 
176
- buildWhereClause(existentialClauses: ExistentialClause[]): string {
176
+ buildWhereClause(schema: string, existentialClauses: ExistentialClause[]): string {
177
177
  if (existentialClauses.length === 0) {
178
178
  return '';
179
179
  }
@@ -187,12 +187,12 @@ class QueryBuilder {
187
187
  const firstTailFactId = first.direction === 'predecessor'
188
188
  ? `e${first.edgeAlias}.successor_fact_id`
189
189
  : `e${first.edgeAlias}.predecessor_fact_id`;
190
- const joins = this.buildJoins(rest, firstHeadFactId);
190
+ const joins = this.buildJoins(schema, rest, firstHeadFactId);
191
191
  const priorFactId = clause.priorEdgeJoin.direction === 'predecessor'
192
192
  ? `e${clause.priorEdgeJoin.edgeAlias}.predecessor_fact_id`
193
193
  : `e${clause.priorEdgeJoin.edgeAlias}.successor_fact_id`;
194
- const whereClause = this.buildWhereClause(clause.query.existentialClauses);
195
- return ` AND ${quantifierSql} (SELECT 1 FROM public.edge e${first.edgeAlias}${joins} ` +
194
+ const whereClause = this.buildWhereClause(schema, clause.query.existentialClauses);
195
+ return ` AND ${quantifierSql} (SELECT 1 FROM ${schema}.edge e${first.edgeAlias}${joins} ` +
196
196
  `WHERE ${firstTailFactId} = ${priorFactId} ` +
197
197
  `AND e${first.edgeAlias}.role_id = $${first.roleParameter}${whereClause})`;
198
198
  });
@@ -43,7 +43,7 @@ function sqlFor(descriptiveString: string, bookmarks: string[] = []) {
43
43
  }
44
44
  throw new Error(`Unknown input type ${input.type}`);
45
45
  });
46
- const composer = resultSqlFromSpecification(start, specification, factTypes, roleMap);
46
+ const composer = resultSqlFromSpecification(start, specification, factTypes, roleMap, "public");
47
47
  if (!composer) {
48
48
  throw new Error("The specification is not satisfiable.");
49
49
  }
@@ -27,7 +27,7 @@ describe('Postgres', () => {
27
27
  let roleMap = allRoles(query.steps, 'Root').filter(r => r.type !== 'Unknown' && r.role !== 'unknown').reduce(
28
28
  (r, role, i) => addRole(r, ensureGetFactTypeId(factTypes, role.type), role.role, i + 1),
29
29
  emptyRoleMap());
30
- const sqlQuery = sqlFromSteps(start, query.steps, factTypes, roleMap);
30
+ const sqlQuery = sqlFromSteps(start, query.steps, factTypes, roleMap, "public");
31
31
  return sqlQuery ? { sql: sqlQuery.sql, parameters: sqlQuery.parameters, pathLength: sqlQuery.pathLength, empty: sqlQuery.empty, factTypes, roleMap } : null;
32
32
  }
33
33
 
@@ -44,7 +44,7 @@ function sqlFor(descriptiveString: string, bookmarks: string[] = []) {
44
44
  }
45
45
  throw new Error(`Unknown input type ${input.type}`);
46
46
  });
47
- const sqlQueries = sqlFromSpecification(start, bookmarks, 100, specification, factTypes, roleMap);
47
+ const sqlQueries = sqlFromSpecification(start, "public", bookmarks, 100, specification, factTypes, roleMap);
48
48
  return { sqlQueries, factTypes, roleMap };
49
49
  }
50
50