forked from goodybag/mongo-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.js
224 lines (182 loc) · 6.74 KB
/
actions.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* Actions
*/
// ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
// action [, ... ]
// ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
// RENAME [ COLUMN ] column_name TO new_column_name
// ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
// RENAME CONSTRAINT constraint_name TO new_constraint_name
// ALTER TABLE [ IF EXISTS ] name
// RENAME TO new_name
// ALTER TABLE [ IF EXISTS ] name
// SET SCHEMA new_schema
// where action is one of:
// ADD [ COLUMN ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
// DROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ]
// ALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]
// ALTER [ COLUMN ] column_name SET DEFAULT expression
// ALTER [ COLUMN ] column_name DROP DEFAULT
// ALTER [ COLUMN ] column_name { SET | DROP } NOT NULL
// ALTER [ COLUMN ] column_name SET STATISTICS integer
// ALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] )
// ALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] )
// ALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
// ADD table_constraint [ NOT VALID ]
// ADD table_constraint_using_index
// VALIDATE CONSTRAINT constraint_name
// DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]
// DISABLE TRIGGER [ trigger_name | ALL | USER ]
// ENABLE TRIGGER [ trigger_name | ALL | USER ]
// ENABLE REPLICA TRIGGER trigger_name
// ENABLE ALWAYS TRIGGER trigger_name
// DISABLE RULE rewrite_rule_name
// ENABLE RULE rewrite_rule_name
// ENABLE REPLICA RULE rewrite_rule_name
// ENABLE ALWAYS RULE rewrite_rule_name
// CLUSTER ON index_name
// SET WITHOUT CLUSTER
// SET WITH OIDS
// SET WITHOUT OIDS
// SET ( storage_parameter = value [, ... ] )
// RESET ( storage_parameter [, ... ] )
// INHERIT parent_table
// NO INHERIT parent_table
// OF type_name
// NOT OF
// OWNER TO new_owner
// SET TABLESPACE new_tablespace
// and table_constraint_using_index is:
// [ CONSTRAINT constraint_name ]
// { UNIQUE | PRIMARY KEY } USING INDEX index_name
// [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
var actions = require('../lib/action-helpers');
var queryHelpers = require('../lib/query-helpers');
var utils = require('../lib/utils');
actions.add('renameTable', function(value, values, query){
return 'rename to "' + value + '"';
});
actions.add('rename', function(value, values, query){
return actions.get('renameTable').fn(value, values, query);
});
actions.add('renameConstraint', function(value, values, query){
return (
"rename constraint " +
utils.quoteObject(value.from) +
" to " +
utils.quoteObject(value.to)
);
});
actions.add('renameColumn', function(value, values, query){
return (
"rename column " +
utils.quoteObject(value.from) +
" to " +
utils.quoteObject(value.to)
);
});
actions.add('setSchema', function(value, values, query){
return 'set schema "' + value + '"';
});
actions.add('addColumn', function(value, values, query){
var output = ["add column"];
output.push( utils.quoteObject(value.name) );
output.push( value.type );
output.push( queryHelpers.get('columnConstraint').fn(value, values, query) );
return output.join(' ');
});
actions.add('dropColumn', function(value, values, query){
if ( Array.isArray(value) ){
return value.map( function( v ){
return actions.get('dropColumn').fn( v, values, query );
}).join(', ');
}
var output = ["drop column"];
if (value.ifExists)
output.push( 'if exists' );
output.push( utils.quoteObject(value.name) );
if (value.restrict)
output.push( 'restrict' );
else if (value.cascade)
output.push( 'cascade' );
return output.join(' ');
});
actions.add('alterColumn', function(value, values, query){
if ( Array.isArray(value) ){
return value.map( function( v ){
return actions.get('alterColumn').fn( v, values, query );
}).join(', ');
}
var output = ["alter column"];
output.push( utils.quoteObject(value.name) );
if (value.type)
output.push( 'type ' + value.type );
if (value.collation)
output.push( 'collate ' + value.collation );
if (value.using)
output.push( 'using (' + value.using + ')' );
if (value.default)
output.push( 'set default ' + value.default );
if (value.dropDefault)
output.push( 'drop default' );
if (value.notNull === true)
output.push( 'set not null' );
if (value.notNull === false)
output.push( 'drop not null' );
if (value.statistics)
output.push( 'set statistics $' + values.push(value.statistics) );
if (value.storage)
output.push( 'set storage ' + value.storage );
return output.join(' ');
});
actions.add( 'dropConstraint', function( value, values, query ){
if ( !value ) return;
var out = ['drop constraint'];
if ( typeof value === 'object' ){
if ( value.ifExists ) out.push('if exists');
if ( value.name ) out.push('"' + value.name + '"');
if ( value.cascade ) out.push('cascade');
if ( value.restrict ) out.push('restrict');
} else if ( typeof value === 'string' ){
out.push('"' + value + '"');
} else return;
return out.join(' ');
});
actions.add( 'addConstraint', function( constraint, values, query ){
return [
'add constraint'
, utils.quoteObject( constraint.name )
, queryHelpers.get('columnConstraint').fn( constraint, values, query )
,
].join(' ');
});
// Single Parameter actions
[
{ name: 'enableReplicaTrigger', text: 'enable replica trigger' }
, { name: 'enableAlwaysTrigger', text: 'enable always trigger' }
, { name: 'disableRule', text: 'disable rule' }
, { name: 'enableRule', text: 'enable rule' }
, { name: 'enableReplicaRule', text: 'enable replica rule' }
, { name: 'enableAlwaysRule', text: 'enable always rule' }
, { name: 'clusterOn', text: 'cluster on' }
, { name: 'inherit', text: 'inherit' }
, { name: 'noInherit', text: 'no inherit' }
, { name: 'of', text: 'of' }
, { name: 'notOf', text: 'not of' }
, { name: 'ownerTo', text: 'owner to' }
, { name: 'setTableSpace', text: 'set tablespace' }
].forEach(function(action){
actions.add( action.name, function(value, values, query){
return action.text + " " + utils.quoteObject(value);
});
});
// Same text booleans
[
{ name: 'setWithoutCluster', text: 'set without cluster' }
, { name: 'setWithOids', text: 'set with oids' }
, { name: 'setWithoutOids', text: 'set without oids' }
].forEach(function(action){
actions.add( action.name, function(value, values, query){
return value ? action.text : '';
});
});