2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// PostgreSQL Query Builder & Compiler
|
|
|
|
// ------
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import QueryCompiler from '../../../query/compiler';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
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.
|
2016-05-17 01:01:34 +10:00
|
|
|
truncate() {
|
|
|
|
return `truncate ${this.tableName} restart identity`;
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// 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.
|
2016-05-17 01:01:34 +10:00
|
|
|
insert() {
|
|
|
|
const sql = QueryCompiler.prototype.insert.call(this)
|
2016-03-02 17:07:05 +01:00
|
|
|
if (sql === '') return sql;
|
2016-05-17 01:01:34 +10:00
|
|
|
const { returning } = this.single;
|
2016-03-02 17:07:05 +01:00
|
|
|
return {
|
|
|
|
sql: sql + this._returning(returning),
|
2016-05-17 01:01:34 +10:00
|
|
|
returning
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compiles an `update` query, allowing for a return value.
|
2016-05-17 01:01:34 +10:00
|
|
|
update() {
|
|
|
|
const updateData = this._prepUpdate(this.single.update);
|
2016-05-18 19:59:24 +10:00
|
|
|
const wheres = this.where();
|
|
|
|
const { returning } = this.single;
|
2016-03-02 17:07:05 +01:00
|
|
|
return {
|
2016-05-17 01:01:34 +10:00
|
|
|
sql: `update ${this.tableName} set ${updateData.join(', ')}` +
|
|
|
|
(wheres ? ` ${wheres}` : '') +
|
2016-03-02 17:07:05 +01:00
|
|
|
this._returning(returning),
|
2016-05-17 01:01:34 +10:00
|
|
|
returning
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compiles an `update` query, allowing for a return value.
|
2016-05-17 01:01:34 +10:00
|
|
|
del() {
|
|
|
|
const sql = QueryCompiler.prototype.del.apply(this, arguments);
|
2016-05-18 19:59:24 +10:00
|
|
|
const { returning } = this.single;
|
2016-03-02 17:07:05 +01:00
|
|
|
return {
|
|
|
|
sql: sql + this._returning(returning),
|
2016-05-17 01:01:34 +10:00
|
|
|
returning
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
_returning(value) {
|
|
|
|
return value ? ` returning ${this.formatter.columnize(value)}` : '';
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
forUpdate() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return 'for update';
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
forShare() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return 'for share';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compiles a columnInfo query
|
2016-05-17 01:01:34 +10:00
|
|
|
columnInfo() {
|
|
|
|
const column = this.single.columnInfo;
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
let sql = 'select * from information_schema.columns where table_name = ? and table_catalog = ?';
|
|
|
|
const bindings = [this.single.table, this.client.database()];
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
if (this.single.schema) {
|
|
|
|
sql += ' and table_schema = ?';
|
|
|
|
bindings.push(this.single.schema);
|
|
|
|
} else {
|
|
|
|
sql += ' and table_schema = current_schema';
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2016-05-17 01:01:34 +10:00
|
|
|
sql,
|
|
|
|
bindings,
|
|
|
|
output(resp) {
|
|
|
|
const out = reduce(resp.rows, function(columns, val) {
|
2016-03-02 17:07:05 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default QueryCompiler_PG;
|