/
viewcompiler.js
138 lines (118 loc) · 3.44 KB
/
viewcompiler.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
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
/* eslint max-len:0 */
// View Compiler
// -------
const { pushQuery } = require('./internal/helpers');
const groupBy = require('lodash/groupBy');
const { columnize: columnize_ } = require('../formatter/wrappingFormatter');
class ViewCompiler {
constructor(client, viewBuilder) {
this.client = client;
this.viewBuilder = viewBuilder;
this._commonBuilder = this.viewBuilder;
this.method = viewBuilder._method;
this.schemaNameRaw = viewBuilder._schemaName;
this.viewNameRaw = viewBuilder._viewName;
this.single = viewBuilder._single;
this.selectQuery = viewBuilder._selectQuery;
this.columns = viewBuilder._columns;
this.grouped = groupBy(viewBuilder._statements, 'grouping');
this.formatter = client.formatter(viewBuilder);
this.bindings = [];
this.formatter.bindings = this.bindings;
this.bindingsHolder = this;
this.sequence = [];
}
// Convert the tableCompiler toSQL
toSQL() {
this[this.method]();
return this.sequence;
}
// Column Compilation
// -------
create() {
this.createQuery(this.columns, this.selectQuery);
}
createOrReplace() {
throw new Error('replace views is not supported by this dialect.');
}
createMaterializedView() {
throw new Error('materialized views are not supported by this dialect.');
}
createQuery(columns, selectQuery, materialized, replace) {
const createStatement =
'create ' +
(materialized ? 'materialized ' : '') +
'view ' +
(replace ? 'or replace ' : '');
const columnList = columns
? ' (' +
columnize_(
columns,
this.viewBuilder,
this.client,
this.bindingsHolder
) +
')'
: '';
let sql = createStatement + this.viewName() + columnList;
sql += ' as ';
sql += selectQuery.toString();
switch (this.single.checkOption) {
case 'default_option':
sql += ' with check option';
break;
case 'local':
sql += ' with local check option';
break;
case 'cascaded':
sql += ' with cascaded check option';
break;
default:
break;
}
this.pushQuery({
sql,
});
}
renameView(from, to) {
throw new Error(
'rename view is not supported by this dialect (instead drop, then create another view).'
);
}
refreshMaterializedView() {
throw new Error('materialized views are not supported by this dialect.');
}
alter() {
this.alterView();
}
alterView() {
const alterView = this.grouped.alterView || [];
for (let i = 0, l = alterView.length; i < l; i++) {
const statement = alterView[i];
if (this[statement.method]) {
this[statement.method].apply(this, statement.args);
} else {
this.client.logger.error(`Debug: ${statement.method} does not exist`);
}
}
for (const item in this.single) {
if (typeof this[item] === 'function') this[item](this.single[item]);
}
}
renameColumn(from, to) {
throw new Error('rename column of views is not supported by this dialect.');
}
defaultTo(column, defaultValue) {
throw new Error(
'change default values of views is not supported by this dialect.'
);
}
viewName() {
const name = this.schemaNameRaw
? `${this.schemaNameRaw}.${this.viewNameRaw}`
: this.viewNameRaw;
return this.formatter.wrap(name);
}
}
ViewCompiler.prototype.pushQuery = pushQuery;
module.exports = ViewCompiler;