forked from planetarydev/json-sql-builder2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.joinhelper.js
More file actions
138 lines (125 loc) · 3.88 KB
/
.joinhelper.js
File metadata and controls
138 lines (125 loc) · 3.88 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict';
class joinHelper extends SQLBuilder.SQLHelper {
constructor(sql, joinType){
super(sql);
let joinSyntax = joinType + '{ LATERAL[$lateral]}-->(PostgreSQL){ [$table]}{ [$select]} AS <key-ident>{ ON [$on]}{ USING [$using]}';
if (!sql._options.useOuterKeywordOnJoin) {
joinSyntax = joinSyntax.split('OUTER ').join('');
}
this.Types({
Object: { syntax: this.Syntax(joinSyntax) },
});
this.Keyword('LATERAL');
this.registerPrivateHelper('on', '../');
this.$table = new SQLBuilder.SQLPredefined.StringIdentifier(sql);
this.$lateral = new SQLBuilder.SQLPredefined.AcceptIfTrue(sql);
let self = this;
// calling conventions are (example using leftJoin):
// sql.leftJoin(<lateral>, <string:table>, [options])
// sql.leftJoin(<lateral>, <object:$select>, [options])
// sql.leftJoin(<string:table>, [options])
// sql.leftJoin(<object:$select>, [options])
this.callee = function(lateral, tableOrSelect, options) {
let __identifier__ = arguments[arguments.length-1];
let query = {};
if (lateral === this.LATERAL) {
query.$lateral = true;
if (this.isString(tableOrSelect)) {
query.$table = tableOrSelect;
} else if(this.isPlainObject(tableOrSelect)) {
query.$select = tableOrSelect;
}
} else if (this.isString(lateral)) {
query.$table = lateral;
options = tableOrSelect;
} else if (this.isPlainObject(lateral)) {
query.$select = lateral;
options = tableOrSelect;
}
if (options && options.$on) query.$on = options.$on;
if (options && options.$using) query.$using = options.$using;
return self.__build__(query, __identifier__);
}
}
}
module.exports = {
definition: joinHelper,
description: 'Generic Helper for left, right, full outer and cross join operators.',
supportedBy: {
MySQL: 'https://dev.mysql.com/doc/refman/5.7/en/select.html',
MariaDB: 'https://mariadb.com/kb/en/library/select/',
PostgreSQL: 'https://www.postgresql.org/docs/9.5/static/sql-select.html',
SQLite: 'https://sqlite.org/lang_select.html',
Oracle: 'https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10002.htm',
SQLServer: 'https://docs.microsoft.com/en-us/sql/t-sql/queries/from-transact-sql'
},
examples: {
Object: {
'Basic Usage': function(sql) {
return {
test: function(){
return sql.build({
$select: {
$columns: {
'people.first_name': true,
'people.last_name': true,
'skills.description': true,
'skills.rate': true
},
$from: 'people',
$join: {
skills: {
$leftJoin: {
$table: 'people_skills',
$on: { 'skills.people_id': { $eq: '~~people.people_id' } },
}
}
},
$where: {
'skills.rate': { $gt: 50 }
}
}
});
},
expectedResults: {
sql: 'SELECT people.first_name, people.last_name, skills.description, skills.rate FROM people LEFT JOIN people_skills AS skills ON skills.people_id = people.people_id WHERE skills.rate > $1',
values:{
$1: 50
}
}
}
},
'Usage as Function': function(sql) {
return {
test: function(){
return sql.build({
$select: {
$columns: {
'people.first_name': true,
'people.last_name': true,
'skills.description': true,
'skills.rate': true
},
$from: 'people',
$join: {
skills: sql.leftJoin('people_skills', {
$on: { 'skills.people_id': { $eq: '~~people.people_id' } }
})
},
$where: {
'skills.rate': { $gt: 50 }
}
}
});
},
expectedResults: {
sql: 'SELECT people.first_name, people.last_name, skills.description, skills.rate FROM people LEFT JOIN people_skills AS skills ON skills.people_id = people.people_id WHERE skills.rate > $1',
values:{
$1: 50
}
}
}
}
}
}
}