compiler.js
3.37 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
'use strict';
exports.__esModule = true;
var _reduce2 = require('lodash/reduce');
var _reduce3 = _interopRequireDefault(_reduce2);
var _assign2 = require('lodash/assign');
var _assign3 = _interopRequireDefault(_assign2);
var _inherits = require('inherits');
var _inherits2 = _interopRequireDefault(_inherits);
var _compiler = require('../../../query/compiler');
var _compiler2 = _interopRequireDefault(_compiler);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// PostgreSQL Query Builder & Compiler
// ------
function QueryCompiler_PG(client, builder) {
_compiler2.default.call(this, client, builder);
}
(0, _inherits2.default)(QueryCompiler_PG, _compiler2.default);
(0, _assign3.default)(QueryCompiler_PG.prototype, {
// Compiles a truncate query.
truncate: function truncate() {
return 'truncate ' + this.tableName + ' restart identity';
},
// is used if the an array with multiple empty values supplied
_defaultInsertValue: 'default',
// Compiles an `insert` query, allowing for multiple
// inserts using a single query statement.
insert: function insert() {
var sql = _compiler2.default.prototype.insert.call(this);
if (sql === '') return sql;
var returning = this.single.returning;
return {
sql: sql + this._returning(returning),
returning: returning
};
},
// Compiles an `update` query, allowing for a return value.
update: function update() {
var updateData = this._prepUpdate(this.single.update);
var wheres = this.where();
var returning = this.single.returning;
return {
sql: this.with() + ('update ' + (this.single.only ? 'only ' : '') + this.tableName + ' ') + ('set ' + updateData.join(', ')) + (wheres ? ' ' + wheres : '') + this._returning(returning),
returning: returning
};
},
// Compiles an `update` query, allowing for a return value.
del: function del() {
var sql = _compiler2.default.prototype.del.apply(this, arguments);
var returning = this.single.returning;
return {
sql: sql + this._returning(returning),
returning: returning
};
},
_returning: function _returning(value) {
return value ? ' returning ' + this.formatter.columnize(value) : '';
},
forUpdate: function forUpdate() {
return 'for update';
},
forShare: function forShare() {
return 'for share';
},
// Compiles a columnInfo query
columnInfo: function columnInfo() {
var column = this.single.columnInfo;
var sql = 'select * from information_schema.columns where table_name = ? and table_catalog = ?';
var bindings = [this.single.table, this.client.database()];
if (this.single.schema) {
sql += ' and table_schema = ?';
bindings.push(this.single.schema);
} else {
sql += ' and table_schema = current_schema';
}
return {
sql: sql,
bindings: bindings,
output: function output(resp) {
var out = (0, _reduce3.default)(resp.rows, function (columns, val) {
columns[val.column_name] = {
type: val.data_type,
maxLength: val.character_maximum_length,
nullable: val.is_nullable === 'YES',
defaultValue: val.column_default
};
return columns;
}, {});
return column && out[column] || out;
}
};
}
});
exports.default = QueryCompiler_PG;
module.exports = exports['default'];