-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathblocks.js
59 lines (48 loc) · 1.53 KB
/
blocks.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
'use strict';
var _ = require('underscore');
module.exports = function(dialect) {
dialect.blocks.set('limit', function(params) {
return (!isNaN(params.offset)) ? '' : 'top(' + dialect.builder._pushValue(params.limit) + ')';
});
dialect.blocks.set('offset', function(params) {
var pre = (!params.sort) ? 'order by 1 ' : '';
if (params.limit) {
var str = pre + 'offset ' + dialect.builder._pushValue(params.offset);
str += ' rows fetch next ' + dialect.builder._pushValue(params.limit) + ' rows only';
return str;
}else {
return pre + 'OFFSET ' + dialect.builder._pushValue(params.offset) + ' rows';
}
});
dialect.blocks.set('returning', function(params) {
var result = dialect.buildBlock('fields', {fields: params.returning});
if (result) result = 'output ' + result;
return result;
});
dialect.blocks.set('insert:values', function(params) {
var values = params.values;
if (!_.isArray(values)) values = [values];
var fields = params.fields || _(values)
.chain()
.map(function(row) {
return _(row).keys();
})
.flatten()
.uniq()
.value();
return dialect.buildTemplate('insertValues', {
fields: fields,
returning: params.returning || undefined,
values: _(values).map(function(row) {
return _(fields).map(function(field) {
return dialect.buildBlock('value', {value: row[field]});
});
})
});
});
dialect.blocks.add('insertValues:values', function(params) {
return _(params.values).map(function(row) {
return '(' + row.join(', ') + ')';
}).join(', ');
});
};