-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql_scope.js
78 lines (62 loc) · 2.12 KB
/
sql_scope.js
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
var currentId = 0;
function getNewId(prefix){
return prefix + ++currentId;
}
function getNot(input) {
return "NOT " + input;
}
function getBooleanExpression(op1, b_e, op2) {
return `${op1} ${b_e} ${op2}`;
}
function getBooleanOperation(op1, operation, op2) {
return op1 + operation + op2;
}
function getUnion(sentence1, sentence2) {
return `(${getTableFromSentence(sentence1)} UNION ${getTableFromSentence(sentence2)}) AS ${getNewId("UNION")}`;
}
function getIntersection(sentence1, sentence2) {
return `(${getTableFromSentence(sentence1)} INTERSECT ${getTableFromSentence(sentence2)}) AS ${getNewId("INTERSECT")}`;
}
function getSubtraction(sentence1, sentence2) {
return `(${getTableFromSentence(sentence1)} EXCEPT ${getTableFromSentence(sentence2)}) AS ${getNewId("SUB")}`;
}
function getProduct(sentence1, sentence2) {
return `(SELECT DISTINCT * FROM ${sentence1}, ${sentence2}) AS ${getNewId("PROD")}`;
}
function getNaturalJoin(sentence1, sentence2) {
return `(${getTableFromSentence(sentence1)} NATURAL JOIN ${sentence2}) AS ${getNewId("NAT")}`;
}
function getTheta(sentence1, sentence2, condition) {
return `(${getTableFromSentence(sentence1)} JOIN ${sentence2} ON ${condition}) AS ${getNewId("NAT")}`;
}
function getSelection(table, alias, condition){
return `(SELECT DISTINCT * FROM ${table} WHERE ${condition}) AS ${getNewId('SEL')}`;
}
function getProjection(table, alias, fieldList){
return `(SELECT DISTINCT ${fieldList} FROM ${table}) AS ${getNewId('PROJ')}`;
}
function getRename(table, alias, fieldList){
return `(SELECT DISTINCT ${fieldList.map(x => `null as ${x}`).join(',')} WHERE 1=2 UNION SELECT DISTINCT * FROM ${table}) AS ${getNewId('REN')}`;
}
function getSingleTable(tableName) {
return `##${tableName}##`;
}
function getTableFromSentence(sentence) {
return `SELECT DISTINCT * FROM ${sentence}`;
}
module.exports = {
getNewId,
getNot,
getBooleanExpression,
getBooleanOperation,
getUnion,
getSelection,
getProjection,
getSingleTable,
getProduct,
getIntersection,
getSubtraction,
getRename,
getNaturalJoin,
getTheta
}