compiler.js
2.69 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
// PostgreSQL Query Builder & Compiler
// ------
import inherits from 'inherits';
import QueryCompiler from '../../../query/compiler';
import { assign, reduce } from 'lodash'
function QueryCompiler_PG(client, builder) {
QueryCompiler.call(this, client, builder);
}
inherits(QueryCompiler_PG, QueryCompiler);
assign(QueryCompiler_PG.prototype, {
// Compiles a truncate query.
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() {
const sql = QueryCompiler.prototype.insert.call(this)
if (sql === '') return sql;
const { returning } = this.single;
return {
sql: sql + this._returning(returning),
returning
};
},
// Compiles an `update` query, allowing for a return value.
update() {
const updateData = this._prepUpdate(this.single.update);
const wheres = this.where();
const { returning } = this.single;
return {
sql: this.with() +
`update ${this.single.only ? 'only ' : ''}${this.tableName} ` +
`set ${updateData.join(', ')}` +
(wheres ? ` ${wheres}` : '') +
this._returning(returning),
returning
};
},
// Compiles an `update` query, allowing for a return value.
del() {
const sql = QueryCompiler.prototype.del.apply(this, arguments);
const { returning } = this.single;
return {
sql: sql + this._returning(returning),
returning
};
},
_returning(value) {
return value ? ` returning ${this.formatter.columnize(value)}` : '';
},
forUpdate() {
return 'for update';
},
forShare() {
return 'for share';
},
// Compiles a columnInfo query
columnInfo() {
const column = this.single.columnInfo;
let sql = 'select * from information_schema.columns where table_name = ? and table_catalog = ?';
const 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,
bindings,
output(resp) {
const out = reduce(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;
}
};
}
})
export default QueryCompiler_PG;