jinaga-server 1.0.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.
- package/LICENSE +22 -0
- package/dist/authentication/authentication-device.d.ts +20 -0
- package/dist/authentication/authentication-device.js +61 -0
- package/dist/authentication/authentication-device.js.map +1 -0
- package/dist/authentication/authentication-impl.d.ts +21 -0
- package/dist/authentication/authentication-impl.js +61 -0
- package/dist/authentication/authentication-impl.js.map +1 -0
- package/dist/authentication/authentication-session.d.ts +23 -0
- package/dist/authentication/authentication-session.js +85 -0
- package/dist/authentication/authentication-session.js.map +1 -0
- package/dist/authentication/authentication.d.ts +5 -0
- package/dist/authentication/authentication.js +3 -0
- package/dist/authentication/authentication.js.map +1 -0
- package/dist/authentication.d.ts +17 -0
- package/dist/authentication.js +52 -0
- package/dist/authentication.js.map +1 -0
- package/dist/authorization/authorization-keystore.d.ts +13 -0
- package/dist/authorization/authorization-keystore.js +61 -0
- package/dist/authorization/authorization-keystore.js.map +1 -0
- package/dist/http/node-http.d.ts +7 -0
- package/dist/http/node-http.js +126 -0
- package/dist/http/node-http.js.map +1 -0
- package/dist/http/router.d.ts +20 -0
- package/dist/http/router.js +223 -0
- package/dist/http/router.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/jinaga-server.d.ts +18 -0
- package/dist/jinaga-server.js +101 -0
- package/dist/jinaga-server.js.map +1 -0
- package/dist/keystore.d.ts +9 -0
- package/dist/keystore.js +3 -0
- package/dist/keystore.js.map +1 -0
- package/dist/memory/memory-keystore.d.ts +16 -0
- package/dist/memory/memory-keystore.js +86 -0
- package/dist/memory/memory-keystore.js.map +1 -0
- package/dist/postgres/connection.d.ts +12 -0
- package/dist/postgres/connection.js +83 -0
- package/dist/postgres/connection.js.map +1 -0
- package/dist/postgres/edge-record.d.ts +9 -0
- package/dist/postgres/edge-record.js +30 -0
- package/dist/postgres/edge-record.js.map +1 -0
- package/dist/postgres/maps.d.ts +21 -0
- package/dist/postgres/maps.js +86 -0
- package/dist/postgres/maps.js.map +1 -0
- package/dist/postgres/postgres-keystore.d.ts +20 -0
- package/dist/postgres/postgres-keystore.js +168 -0
- package/dist/postgres/postgres-keystore.js.map +1 -0
- package/dist/postgres/postgres-store.d.ts +20 -0
- package/dist/postgres/postgres-store.js +717 -0
- package/dist/postgres/postgres-store.js.map +1 -0
- package/dist/postgres/query-description-builder.d.ts +13 -0
- package/dist/postgres/query-description-builder.js +104 -0
- package/dist/postgres/query-description-builder.js.map +1 -0
- package/dist/postgres/query-description.d.ts +71 -0
- package/dist/postgres/query-description.js +256 -0
- package/dist/postgres/query-description.js.map +1 -0
- package/dist/postgres/specification-result-sql.d.ts +41 -0
- package/dist/postgres/specification-result-sql.js +260 -0
- package/dist/postgres/specification-result-sql.js.map +1 -0
- package/dist/postgres/specification-sql.d.ts +3 -0
- package/dist/postgres/specification-sql.js +119 -0
- package/dist/postgres/specification-sql.js.map +1 -0
- package/dist/postgres/sql.d.ts +10 -0
- package/dist/postgres/sql.js +396 -0
- package/dist/postgres/sql.js.map +1 -0
- package/dist/util/fn.d.ts +6 -0
- package/dist/util/fn.js +81 -0
- package/dist/util/fn.js.map +1 -0
- package/dist/util/promise.d.ts +1 -0
- package/dist/util/promise.js +10 -0
- package/dist/util/promise.js.map +1 -0
- package/docs/registrations.sql +22 -0
- package/docs/specification.txt +41 -0
- package/docs/sql-generator.gv +25 -0
- package/integration-test/docker-compose.yaml +13 -0
- package/integration-test/jinaga-test/Dockerfile +12 -0
- package/integration-test/jinaga-test/jinaga.test.js +425 -0
- package/integration-test/jinaga-test/package-lock.json +6767 -0
- package/integration-test/jinaga-test/package.json +25 -0
- package/integration-test/postgres/0-init-app-db.sh +16 -0
- package/integration-test/postgres/1-setup.sql +219 -0
- package/integration-test/postgres/2-grant.sh +17 -0
- package/integration-test/postgres/Dockerfile +5 -0
- package/integration-test/run.sh +17 -0
- package/integration-test/start_db.sh +2 -0
- package/integration-test/stop_db.sh +3 -0
- package/integration-test/test.sh +8 -0
- package/interactive/index.js +56 -0
- package/jest.config.js +12 -0
- package/package.json +51 -0
- package/scripts/combined.sql +46 -0
- package/scripts/cte.sql +55 -0
- package/scripts/separate.sql +27 -0
- package/setup.sh +34 -0
- package/setup.sql +329 -0
- package/src/authentication/authentication-device.ts +68 -0
- package/src/authentication/authentication-impl.ts +59 -0
- package/src/authentication/authentication-session.ts +95 -0
- package/src/authentication/authentication.ts +6 -0
- package/src/authentication.ts +45 -0
- package/src/authorization/authorization-keystore.ts +67 -0
- package/src/http/node-http.ts +106 -0
- package/src/http/router.ts +237 -0
- package/src/index.ts +1 -0
- package/src/jinaga-server.ts +125 -0
- package/src/keystore.ts +10 -0
- package/src/memory/memory-keystore.ts +93 -0
- package/src/postgres/connection.ts +73 -0
- package/src/postgres/edge-record.ts +36 -0
- package/src/postgres/maps.ts +89 -0
- package/src/postgres/postgres-keystore.ts +167 -0
- package/src/postgres/postgres-store.ts +874 -0
- package/src/postgres/query-description-builder.ts +116 -0
- package/src/postgres/query-description.ts +385 -0
- package/src/postgres/specification-result-sql.ts +362 -0
- package/src/postgres/specification-sql.ts +138 -0
- package/src/postgres/sql.ts +482 -0
- package/src/util/fn.ts +63 -0
- package/src/util/promise.ts +5 -0
- package/test/authorization/authorizationSpec.ts +209 -0
- package/test/postgres/postgresSpec.ts +385 -0
- package/test/postgres/postgresSpecificationSpec.ts +742 -0
- package/test/reporter.js +25 -0
- package/tsconfig.json +25 -0
- package/upgrade.sh +21 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resultSqlFromSpecification = exports.ResultComposer = void 0;
|
|
4
|
+
const maps_1 = require("./maps");
|
|
5
|
+
const query_description_1 = require("./query-description");
|
|
6
|
+
const query_description_builder_1 = require("./query-description-builder");
|
|
7
|
+
class ResultComposer {
|
|
8
|
+
constructor(sqlQuery, resultProjection, parentFactIdLength, childResultComposers) {
|
|
9
|
+
this.sqlQuery = sqlQuery;
|
|
10
|
+
this.resultProjection = resultProjection;
|
|
11
|
+
this.parentFactIdLength = parentFactIdLength;
|
|
12
|
+
this.childResultComposers = childResultComposers;
|
|
13
|
+
}
|
|
14
|
+
getSqlQueries() {
|
|
15
|
+
const childQueries = [];
|
|
16
|
+
for (const childResultComposer of this.childResultComposers) {
|
|
17
|
+
childQueries.push((Object.assign({ name: childResultComposer.name }, childResultComposer.resultComposer.getSqlQueries())));
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
sqlQuery: this.sqlQuery,
|
|
21
|
+
childQueries
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
compose(resultSets) {
|
|
25
|
+
const childResults = this.composeInternal(resultSets);
|
|
26
|
+
if (childResults.length === 0) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
return childResults[0].results;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
composeInternal(resultSets) {
|
|
34
|
+
const rows = resultSets.resultSet;
|
|
35
|
+
if (rows.length === 0) {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
// Project all rows and their identifiers
|
|
39
|
+
const identifiedResults = rows.map(row => ({
|
|
40
|
+
factIds: this.identifierOf(row),
|
|
41
|
+
result: this.projectionOf(row)
|
|
42
|
+
}));
|
|
43
|
+
// Compose child results
|
|
44
|
+
for (const childResultComposer of this.childResultComposers) {
|
|
45
|
+
const childResultSet = resultSets.childResultSets.find(childResultSet => childResultSet.name === childResultComposer.name);
|
|
46
|
+
if (!childResultSet) {
|
|
47
|
+
const availableNames = resultSets.childResultSets.map(childResultSet => childResultSet.name);
|
|
48
|
+
throw new Error(`Child result set ${childResultComposer.name} not found in (${availableNames.join(", ")})`);
|
|
49
|
+
}
|
|
50
|
+
const composedResults = childResultComposer.resultComposer.composeInternal(childResultSet);
|
|
51
|
+
// Add the child results
|
|
52
|
+
let index = 0;
|
|
53
|
+
for (const identifiedResult of identifiedResults) {
|
|
54
|
+
let results = [];
|
|
55
|
+
if (index < composedResults.length && idsEqual(identifiedResult.factIds, composedResults[index].parentFactIds)) {
|
|
56
|
+
results = composedResults[index].results;
|
|
57
|
+
index++;
|
|
58
|
+
}
|
|
59
|
+
identifiedResult.result = Object.assign(Object.assign({}, identifiedResult.result), { [childResultComposer.name]: results });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// Group the results by their parent identifiers
|
|
63
|
+
const childResults = [];
|
|
64
|
+
let parentFactIds = identifiedResults[0].factIds.slice(0, this.parentFactIdLength);
|
|
65
|
+
let results = [identifiedResults[0].result];
|
|
66
|
+
for (const identifiedResult of identifiedResults.slice(1)) {
|
|
67
|
+
const nextParentFactIds = identifiedResult.factIds.slice(0, this.parentFactIdLength);
|
|
68
|
+
if (idsEqual(nextParentFactIds, parentFactIds)) {
|
|
69
|
+
results.push(identifiedResult.result);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
childResults.push({
|
|
73
|
+
parentFactIds,
|
|
74
|
+
results
|
|
75
|
+
});
|
|
76
|
+
parentFactIds = nextParentFactIds;
|
|
77
|
+
results = [identifiedResult.result];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
childResults.push({
|
|
81
|
+
parentFactIds,
|
|
82
|
+
results
|
|
83
|
+
});
|
|
84
|
+
return childResults;
|
|
85
|
+
}
|
|
86
|
+
identifierOf(row) {
|
|
87
|
+
return this.sqlQuery.labels.map(label => row[`id${label.index}`]);
|
|
88
|
+
}
|
|
89
|
+
projectionOf(row) {
|
|
90
|
+
if (!Array.isArray(this.resultProjection)) {
|
|
91
|
+
return this.singularValue(this.resultProjection, row);
|
|
92
|
+
}
|
|
93
|
+
else if (this.resultProjection.length === 0 && this.childResultComposers.length === 0) {
|
|
94
|
+
return this.sqlQuery.labels
|
|
95
|
+
.slice(this.parentFactIdLength)
|
|
96
|
+
.reduce((acc, label) => (Object.assign(Object.assign({}, acc), { [label.name]: row[`data${label.index}`].fields })), {});
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
return this.resultProjection.reduce((acc, elementProjection) => (Object.assign(Object.assign({}, acc), { [elementProjection.name]: this.elementValue(elementProjection, row) })), {});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
elementValue(projection, row) {
|
|
103
|
+
const label = this.getLabel(projection.label);
|
|
104
|
+
if (projection.type === "field") {
|
|
105
|
+
return row[`data${label.index}`].fields[projection.field];
|
|
106
|
+
}
|
|
107
|
+
else if (projection.type === "hash") {
|
|
108
|
+
return row[`hash${label.index}`];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
singularValue(projection, row) {
|
|
112
|
+
const label = this.getLabel(projection.label);
|
|
113
|
+
return row[`data${label.index}`].fields[projection.field];
|
|
114
|
+
}
|
|
115
|
+
getLabel(name) {
|
|
116
|
+
const label = this.sqlQuery.labels.find(label => label.name === name);
|
|
117
|
+
if (!label) {
|
|
118
|
+
throw new Error(`Label ${name} not found. Known labels: ${this.sqlQuery.labels.map(label => label.name).join(", ")}`);
|
|
119
|
+
}
|
|
120
|
+
return label;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
exports.ResultComposer = ResultComposer;
|
|
124
|
+
class ResultDescriptionBuilder extends query_description_builder_1.QueryDescriptionBuilder {
|
|
125
|
+
constructor(factTypes, roleMap) {
|
|
126
|
+
super(factTypes, roleMap);
|
|
127
|
+
}
|
|
128
|
+
buildDescription(start, specification) {
|
|
129
|
+
// Verify that the number of start facts equals the number of inputs
|
|
130
|
+
if (start.length !== specification.given.length) {
|
|
131
|
+
throw new Error(`The number of start facts (${start.length}) does not equal the number of inputs (${specification.given.length})`);
|
|
132
|
+
}
|
|
133
|
+
// Verify that the input type matches the start fact type
|
|
134
|
+
for (let i = 0; i < start.length; i++) {
|
|
135
|
+
if (start[i].type !== specification.given[i].type) {
|
|
136
|
+
throw new Error(`The type of start fact ${i} (${start[i].type}) does not match the type of input ${i} (${specification.given[i].type})`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
// Allocate a fact table for each given.
|
|
140
|
+
// While the fact type and hash parameters are zero, the join will not be written.
|
|
141
|
+
const inputs = specification.given
|
|
142
|
+
.map((label, i) => ({
|
|
143
|
+
label: label.name,
|
|
144
|
+
factIndex: i + 1,
|
|
145
|
+
factTypeId: (0, maps_1.getFactTypeId)(this.factTypes, label.type),
|
|
146
|
+
factHash: start[i].hash,
|
|
147
|
+
factTypeParameter: 0,
|
|
148
|
+
factHashParameter: 0
|
|
149
|
+
}));
|
|
150
|
+
const facts = specification.given
|
|
151
|
+
.map((label, i) => ({
|
|
152
|
+
factIndex: i + 1,
|
|
153
|
+
type: label.type
|
|
154
|
+
}));
|
|
155
|
+
const givenFacts = specification.given.reduce((knownFacts, label, i) => (Object.assign(Object.assign({}, knownFacts), { [label.name]: facts[i] })), {});
|
|
156
|
+
// The QueryDescription is an immutable data type.
|
|
157
|
+
// Initialize it with the inputs and facts.
|
|
158
|
+
// The DescriptionBuilder will branch at various points, and
|
|
159
|
+
// build on the current query description along each branch.
|
|
160
|
+
const initialQueryDescription = new query_description_1.QueryDescription(inputs, [], [], facts, [], []);
|
|
161
|
+
return this.createResultDescription(initialQueryDescription, specification.matches, specification.childProjections, givenFacts, []);
|
|
162
|
+
}
|
|
163
|
+
createResultDescription(queryDescription, matches, childProjections, knownFacts, path) {
|
|
164
|
+
({ queryDescription, knownFacts } = this.addEdges(queryDescription, knownFacts, path, matches));
|
|
165
|
+
if (!queryDescription.isSatisfiable()) {
|
|
166
|
+
// Abort the branch if the query is not satisfiable
|
|
167
|
+
return {
|
|
168
|
+
queryDescription,
|
|
169
|
+
resultProjection: [],
|
|
170
|
+
childResultDescriptions: []
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
const childResultDescriptions = [];
|
|
174
|
+
if (Array.isArray(childProjections)) {
|
|
175
|
+
const specificationProjections = childProjections
|
|
176
|
+
.filter(projection => projection.type === "specification");
|
|
177
|
+
const elementProjections = childProjections
|
|
178
|
+
.filter(projection => projection.type === "field" || projection.type === "hash");
|
|
179
|
+
for (const child of specificationProjections) {
|
|
180
|
+
const childResultDescription = this.createResultDescription(queryDescription, child.matches, child.childProjections, knownFacts, []);
|
|
181
|
+
childResultDescriptions.push(Object.assign({ name: child.name }, childResultDescription));
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
queryDescription,
|
|
185
|
+
resultProjection: elementProjections,
|
|
186
|
+
childResultDescriptions
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
return {
|
|
191
|
+
queryDescription,
|
|
192
|
+
resultProjection: childProjections,
|
|
193
|
+
childResultDescriptions: []
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
addEdges(queryDescription, knownFacts, path, matches) {
|
|
198
|
+
for (const match of matches) {
|
|
199
|
+
for (const condition of match.conditions) {
|
|
200
|
+
if (condition.type === "path") {
|
|
201
|
+
({ queryDescription, knownFacts } = this.addPathCondition(queryDescription, knownFacts, path, match.unknown, "", condition));
|
|
202
|
+
}
|
|
203
|
+
else if (condition.type === "existential") {
|
|
204
|
+
if (condition.exists) {
|
|
205
|
+
// Include the edges of the existential condition into the current
|
|
206
|
+
// query description.
|
|
207
|
+
({ queryDescription } = this.addEdges(queryDescription, knownFacts, path, condition.matches));
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
// Apply the where clause and continue with the tuple where it is true.
|
|
211
|
+
// The path describes which not-exists condition we are currently building on.
|
|
212
|
+
// Because the path is not empty, labeled facts will be included in the output.
|
|
213
|
+
const { query: queryDescriptionWithNotExist, path: conditionalPath } = queryDescription.withNotExistsCondition(path);
|
|
214
|
+
const { queryDescription: queryDescriptionConditional } = this.addEdges(queryDescriptionWithNotExist, knownFacts, conditionalPath, condition.matches);
|
|
215
|
+
// If the negative existential condition is not satisfiable, then
|
|
216
|
+
// that means that the condition will always be true.
|
|
217
|
+
// We can therefore skip the branch for the negative existential condition.
|
|
218
|
+
if (queryDescriptionConditional.isSatisfiable()) {
|
|
219
|
+
queryDescription = queryDescriptionConditional;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (!queryDescription.isSatisfiable()) {
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (!queryDescription.isSatisfiable()) {
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
queryDescription,
|
|
233
|
+
knownFacts
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function idsEqual(a, b) {
|
|
238
|
+
return a.every((value, index) => value === b[index]);
|
|
239
|
+
}
|
|
240
|
+
function resultSqlFromSpecification(start, specification, factTypes, roleMap) {
|
|
241
|
+
const descriptionBuilder = new ResultDescriptionBuilder(factTypes, roleMap);
|
|
242
|
+
const description = descriptionBuilder.buildDescription(start, specification);
|
|
243
|
+
if (!description.queryDescription.isSatisfiable()) {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
return createResultComposer(description, 0);
|
|
247
|
+
}
|
|
248
|
+
exports.resultSqlFromSpecification = resultSqlFromSpecification;
|
|
249
|
+
function createResultComposer(description, parentFactIdLength) {
|
|
250
|
+
const sqlQuery = description.queryDescription.generateResultSqlQuery();
|
|
251
|
+
const resultProjection = description.resultProjection;
|
|
252
|
+
const childResultComposers = description.childResultDescriptions
|
|
253
|
+
.filter(child => child.queryDescription.isSatisfiable())
|
|
254
|
+
.map(child => ({
|
|
255
|
+
name: child.name,
|
|
256
|
+
resultComposer: createResultComposer(child, description.queryDescription.outputLength())
|
|
257
|
+
}));
|
|
258
|
+
return new ResultComposer(sqlQuery, resultProjection, parentFactIdLength, childResultComposers);
|
|
259
|
+
}
|
|
260
|
+
//# sourceMappingURL=specification-result-sql.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"specification-result-sql.js","sourceRoot":"","sources":["../../src/postgres/specification-result-sql.ts"],"names":[],"mappings":";;;AAWA,iCAA6D;AAC7D,2DAAiH;AACjH,2EAAsE;AA4CtE,MAAa,cAAc;IACvB,YACqB,QAA+B,EAC/B,gBAAkC,EAClC,kBAA0B,EAC1B,oBAA2C;QAH3C,aAAQ,GAAR,QAAQ,CAAuB;QAC/B,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,uBAAkB,GAAlB,kBAAkB,CAAQ;QAC1B,yBAAoB,GAApB,oBAAoB,CAAuB;IAC5D,CAAC;IAEE,aAAa;QAChB,MAAM,YAAY,GAAwB,EAAE,CAAC;QAC7C,KAAK,MAAM,mBAAmB,IAAI,IAAI,CAAC,oBAAoB,EAAE;YACzD,YAAY,CAAC,IAAI,CAAC,iBACd,IAAI,EAAE,mBAAmB,CAAC,IAAI,IAC3B,mBAAmB,CAAC,cAAc,CAAC,aAAa,EAAE,EACvD,CAAC,CAAC;SACP;QACD,OAAO;YACH,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,YAAY;SACf,CAAC;IACN,CAAC;IAEM,OAAO,CACV,UAAyB;QAEzB,MAAM,YAAY,GAAG,IAAI,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;QACtD,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE;YAC3B,OAAO,EAAE,CAAC;SACb;aACI;YACD,OAAO,YAAY,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;SAClC;IACL,CAAC;IAEO,eAAe,CACnB,UAAyB;QAEzB,MAAM,IAAI,GAAG,UAAU,CAAC,SAAS,CAAC;QAClC,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;YACnB,OAAO,EAAE,CAAC;SACb;QAED,yCAAyC;QACzC,MAAM,iBAAiB,GAAwB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YAC5D,OAAO,EAAE,IA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AC;IACR,OAAO,IAAI,cAAc,CAAC,QAAQ,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,oBAAoB,CAAC,CAAC;AACpG,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { FactBookmark, FactReference, Specification } from "jinaga";
|
|
2
|
+
import { SpecificationSqlQuery } from "./query-description";
|
|
3
|
+
export declare function sqlFromSpecification(start: FactReference[], bookmarks: FactBookmark[], limit: number, specification: Specification, factTypes: Map<string, number>, roleMap: Map<number, Map<string, number>>): SpecificationSqlQuery[];
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sqlFromSpecification = void 0;
|
|
4
|
+
const maps_1 = require("./maps");
|
|
5
|
+
const query_description_1 = require("./query-description");
|
|
6
|
+
const query_description_builder_1 = require("./query-description-builder");
|
|
7
|
+
class DescriptionBuilder extends query_description_builder_1.QueryDescriptionBuilder {
|
|
8
|
+
constructor(factTypes, roleMap) { super(factTypes, roleMap); }
|
|
9
|
+
buildDescriptions(start, specification) {
|
|
10
|
+
// Verify that the number of start facts equals the number of inputs
|
|
11
|
+
if (start.length !== specification.given.length) {
|
|
12
|
+
throw new Error(`The number of start facts (${start.length}) does not equal the number of inputs (${specification.given.length})`);
|
|
13
|
+
}
|
|
14
|
+
// Verify that the input type matches the start fact type
|
|
15
|
+
for (let i = 0; i < start.length; i++) {
|
|
16
|
+
if (start[i].type !== specification.given[i].type) {
|
|
17
|
+
throw new Error(`The type of start fact ${i} (${start[i].type}) does not match the type of input ${i} (${specification.given[i].type})`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// Allocate a fact table for each given.
|
|
21
|
+
// While the fact type and hash parameters are zero, the join will not be written.
|
|
22
|
+
const inputs = specification.given
|
|
23
|
+
.map((label, i) => ({
|
|
24
|
+
label: label.name,
|
|
25
|
+
factIndex: i + 1,
|
|
26
|
+
factTypeId: (0, maps_1.getFactTypeId)(this.factTypes, label.type),
|
|
27
|
+
factHash: start[i].hash,
|
|
28
|
+
factTypeParameter: 0,
|
|
29
|
+
factHashParameter: 0
|
|
30
|
+
}));
|
|
31
|
+
const facts = specification.given
|
|
32
|
+
.map((label, i) => ({
|
|
33
|
+
factIndex: i + 1,
|
|
34
|
+
type: label.type
|
|
35
|
+
}));
|
|
36
|
+
const givenFacts = specification.given.reduce((knownFacts, label, i) => (Object.assign(Object.assign({}, knownFacts), { [label.name]: facts[i] })), {});
|
|
37
|
+
// The QueryDescription is an immutable data type.
|
|
38
|
+
// Initialize it with the inputs and facts.
|
|
39
|
+
// The DescriptionBuilder will branch at various points, and
|
|
40
|
+
// build on the current query description along each branch.
|
|
41
|
+
const initialQueryDescription = new query_description_1.QueryDescription(inputs, [], [], facts, [], []);
|
|
42
|
+
const { queryDescriptions, knownFacts } = this.addEdges(initialQueryDescription, givenFacts, [], "", specification.matches);
|
|
43
|
+
// The final query description represents the complete tuple.
|
|
44
|
+
// Build projections onto that one.
|
|
45
|
+
const finalQueryDescription = queryDescriptions[queryDescriptions.length - 1];
|
|
46
|
+
if (Array.isArray(specification.childProjections)) {
|
|
47
|
+
const queryDescriptionsWithProjections = this.addProjections(finalQueryDescription, knownFacts, specification.childProjections);
|
|
48
|
+
return [...queryDescriptions, ...queryDescriptionsWithProjections];
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
return queryDescriptions;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
addEdges(queryDescription, knownFacts, path, prefix, matches) {
|
|
55
|
+
const queryDescriptions = [];
|
|
56
|
+
for (const match of matches) {
|
|
57
|
+
for (const condition of match.conditions) {
|
|
58
|
+
if (condition.type === "path") {
|
|
59
|
+
({ queryDescription, knownFacts } = this.addPathCondition(queryDescription, knownFacts, path, match.unknown, prefix, condition));
|
|
60
|
+
}
|
|
61
|
+
else if (condition.type === "existential") {
|
|
62
|
+
if (condition.exists) {
|
|
63
|
+
// Include the edges of the existential condition into the current
|
|
64
|
+
// query description.
|
|
65
|
+
const { queryDescriptions: newQueryDescriptions } = this.addEdges(queryDescription, knownFacts, path, prefix, condition.matches);
|
|
66
|
+
const last = newQueryDescriptions.length - 1;
|
|
67
|
+
queryDescriptions.push(...newQueryDescriptions.slice(0, last));
|
|
68
|
+
queryDescription = newQueryDescriptions[last];
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
// Branch from the current query description and follow the
|
|
72
|
+
// edges of the existential condition.
|
|
73
|
+
// This will produce tuples that prove the condition false.
|
|
74
|
+
const { queryDescriptions: newQueryDescriptions } = this.addEdges(queryDescription, knownFacts, path, prefix, condition.matches);
|
|
75
|
+
// Then apply the where clause and continue with the tuple where it is true.
|
|
76
|
+
// The path describes which not-exists condition we are currently building on.
|
|
77
|
+
// Because the path is not empty, labeled facts will be included in the output.
|
|
78
|
+
const { query: queryDescriptionWithNotExist, path: conditionalPath } = queryDescription.withNotExistsCondition(path);
|
|
79
|
+
const { queryDescriptions: newQueryDescriptionsWithNotExists } = this.addEdges(queryDescriptionWithNotExist, knownFacts, conditionalPath, prefix, condition.matches);
|
|
80
|
+
const last = newQueryDescriptionsWithNotExists.length - 1;
|
|
81
|
+
const queryDescriptionConditional = newQueryDescriptionsWithNotExists[last];
|
|
82
|
+
// If the negative existential condition is not satisfiable, then
|
|
83
|
+
// that means that the condition will always be true.
|
|
84
|
+
// We can therefore skip the branch for the negative existential condition.
|
|
85
|
+
if (queryDescriptionConditional.isSatisfiable()) {
|
|
86
|
+
queryDescriptions.push(...newQueryDescriptions);
|
|
87
|
+
queryDescriptions.push(...newQueryDescriptionsWithNotExists.slice(0, last));
|
|
88
|
+
queryDescription = queryDescriptionConditional;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
queryDescriptions.push(queryDescription);
|
|
95
|
+
return { queryDescriptions, knownFacts };
|
|
96
|
+
}
|
|
97
|
+
addProjections(queryDescription, knownFacts, projections) {
|
|
98
|
+
const queryDescriptions = [];
|
|
99
|
+
projections.forEach(projection => {
|
|
100
|
+
if (projection.type === "specification") {
|
|
101
|
+
// Produce more facts in the tuple, and prefix the labels with the projection name.
|
|
102
|
+
const prefix = projection.name + ".";
|
|
103
|
+
const { queryDescriptions: queryDescriptionsWithEdges } = this.addEdges(queryDescription, knownFacts, [], prefix, projection.matches);
|
|
104
|
+
queryDescriptions.push(...queryDescriptionsWithEdges);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
return queryDescriptions;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function sqlFromSpecification(start, bookmarks, limit, specification, factTypes, roleMap) {
|
|
111
|
+
const descriptionBuilder = new DescriptionBuilder(factTypes, roleMap);
|
|
112
|
+
const descriptions = descriptionBuilder.buildDescriptions(start, specification);
|
|
113
|
+
// Only generate SQL for satisfiable queries.
|
|
114
|
+
return descriptions
|
|
115
|
+
.filter(description => description.isSatisfiable())
|
|
116
|
+
.map(description => description.generateSqlQuery(bookmarks, limit));
|
|
117
|
+
}
|
|
118
|
+
exports.sqlFromSpecification = sqlFromSpecification;
|
|
119
|
+
//# sourceMappingURL=specification-sql.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"specification-sql.js","sourceRoot":"","sources":["../../src/postgres/specification-sql.ts"],"names":[],"mappings":";;;AAEA,iCAA6D;AAC7D,2DAAiH;AACjH,2EAAsE;AAMtE,MAAM,kBAAmB,SAAQ,mDAAuB;IACpD,YACI,SAAsB,EACtB,OAAgB,IAChB,KAAK,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IAEzB,iBAAiB,CAAC,KAAsB,EAAE,aAA4B;QACzE,oEAAoE;QACpE,IAAI,KAAK,CAAC,MAAM,KAAK,aAAa,CAAC,KAAK,CAAC,MAAM,EAAE;YAC7C,MAAM,IAAI,KAAK,CAAC,8BAA8B,KAAK,CAAC,MAAM,0CAA0C,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AiB,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;IAEhF,6CAA6C;IAC7C,OAAO,YAAY;SACd,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC,WAAW,CAAC,aAAa,EAAE,CAAC;SAClD,GAAG,CAAC,WAAW,CAAC,EAAE,CAAC,WAAW,CAAC,gBAAgB,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC,CAAC;AAC5E,CAAC;AARD,oDAQC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FactReference, Step } from "jinaga";
|
|
2
|
+
import { FactTypeMap, RoleMap } from "./maps";
|
|
3
|
+
export declare type SqlQuery = {
|
|
4
|
+
empty: boolean;
|
|
5
|
+
sql: string;
|
|
6
|
+
parameters: any[];
|
|
7
|
+
pathLength: number;
|
|
8
|
+
factTypeNames: string[];
|
|
9
|
+
};
|
|
10
|
+
export declare function sqlFromSteps(start: FactReference, steps: Step[], factTypes: FactTypeMap, roleMap: RoleMap): SqlQuery;
|