// PostgreSQL Query Builder & Compiler // ------ module.exports = function(client) { var inherits = require('inherits'); var _ = require('lodash'); var QueryBuilder = require('../../query/builder'); var QueryCompiler = require('../../query/compiler'); // Query Builder // ------ function QueryBuilder_PG() { this.client = client; QueryBuilder.apply(this, arguments); } inherits(QueryBuilder_PG, QueryBuilder); // Query Compiler // ------ function QueryCompiler_PG() { this.formatter = new client.Formatter(); QueryCompiler.apply(this, arguments); } inherits(QueryCompiler_PG, QueryCompiler); // Compiles a truncate query. QueryCompiler_PG.prototype.truncate = function() { return 'truncate ' + this.tableName + ' restart identity'; }; // Used when the insert call is empty. QueryCompiler_PG.prototype._emptyInsertValue = 'default values'; // Compiles an `insert` query, allowing for multiple // inserts using a single query statement. QueryCompiler_PG.prototype.insert = function() { var sql = QueryCompiler.prototype.insert.call(this); var returning = this.single.returning; return { sql: sql + this._returning(returning), returning: returning }; }; // Compiles an `update` query, allowing for a return value. QueryCompiler_PG.prototype.update = function() { var updateData = this._prepUpdate(this.single.update); var wheres = this.where(); var returning = this.single.returning; return { sql: 'update ' + this.tableName + ' set ' + updateData.join(', ') + (wheres ? ' ' + wheres : '') + this._returning(returning), returning: returning }; }; QueryCompiler_PG.prototype._returning = function(value) { return value ? ' returning ' + this.formatter.columnize(value) : ''; }; QueryCompiler_PG.prototype.forUpdate = function() { return 'for update'; }; QueryCompiler_PG.prototype.forShare = function() { return 'for share'; }; client.QueryBuilder = QueryBuilder_PG; client.QueryCompiler = QueryCompiler_PG; };