@incdevco/framework
Version:
node.js lambda framework
647 lines (346 loc) • 9.81 kB
JavaScript
var Util = require('util');
function Query(config) {
'use strict';
config = config || {};
this._connection = config.connection;
this._limit = null;
this._offset = null;
this._table = null;
this._where = null;
}
Query.prototype._buildLimit = function (inserts) {
'use strict';
var sql = '';
if (this._limit) {
inserts.push(this._limit);
sql += ' LIMIT $' + inserts.length;
}
return sql;
};
Query.prototype._buildOffset = function (inserts) {
'use strict';
var sql = '';
if (this._offset) {
inserts.push(this._offset);
sql += ' OFFSET $' + inserts.length;
}
return sql;
};
Query.prototype._buildWhere = function (inserts) {
'use strict';
var where, sql = '';
where = this._where;
if (where) {
sql += ' WHERE ';
if (typeof where === 'object') {
if (Array.isArray(where)) {
where.forEach(function (where, index) {
if (index !== 0) {
sql += ' AND ';
}
if (typeof where === 'object') {
sql += '"' + where.key + '"';
sql += ' ' + (where.comparator || '=') + ' ';
inserts.push(where.value);
sql += '$' + inserts.length;
} else {
sql += where;
}
});
} else {
Object.keys(where).forEach(function(key, index) {
if (index !== 0) {
sql += ' AND ';
}
sql += '"' + key + '"';
if (typeof where[key] === 'object') {
sql += ' ' + (where[key].comparator || '=') + ' ';
inserts.push(where[key].value);
sql += '$' + inserts.length;
} else {
inserts.push(where[key]);
sql += ' = $' + inserts.length;
}
});
}
} else {
sql += where;
}
}
return sql;
};
Query.prototype.build = function (inserts) {
'use strict';
return this._build(inserts);
};
Query.prototype.execute = function () {
'use strict';
var inserts = [];
var sql = this.build(inserts);
return this._connection.query(sql, inserts);
};
Query.prototype.from = function (table) {
'use strict';
this._table = table;
return this;
};
Query.prototype.limit = function (expression) {
'use strict';
this._limit = parseInt(expression);
return this;
};
Query.prototype.offset = function (expression) {
'use strict';
this._offset = parseInt(expression);
return this;
};
Query.prototype.where = function (expression) {
'use strict';
this._where = expression;
return this;
};
function Delete(config) {
'use strict';
Query.call(this, config);
this._table = null;
this._where = null;
}
Util.inherits(Delete, Query);
Delete.prototype._build = function (inserts) {
'use strict';
var sql = 'DELETE FROM "' + this._table + '"';
sql += this._buildWhere(inserts);
sql += this._buildLimit(inserts);
sql += this._buildOffset(inserts);
return sql;
};
function Insert(config) {
'use strict';
Query.call(this, config);
this._columns = null;
this._data = null;
this._returning = null;
this._table = null;
}
Util.inherits(Insert, Query);
Insert.prototype._build = function (inserts) {
'use strict';
var sql = 'INSERT INTO "' + this._table + '"';
sql += this._buildColumns();
sql += this._buildRows(inserts);
sql += this._buildReturning();
return sql;
};
Insert.prototype._buildColumns = function () {
'use strict';
var row, sql = '';
if (this._columns) {
if (Array.isArray(this._columns)) {
this._columns.forEach(function (column, index) {
if (index !== 0) {
sql += ',';
}
sql += '"' + column + '"';
});
} else {
sql += this._columns;
}
} else if (this._data) {
if (Array.isArray(this._data)) {
row = this._data[0];
} else {
row = this._data;
}
Object.keys(row).forEach(function (key, index) {
if (index !== 0) {
sql += ',';
}
sql += '"' + key + '"';
});
}
return ' (' + sql + ') VALUES ';
};
Insert.prototype._buildRow = function (row, inserts) {
'use strict';
var sql = '';
if (row) {
Object.keys(row).forEach(function(key, index) {
if (index !== 0) {
sql += ',';
}
inserts.push(row[key]);
sql += '$' + inserts.length;
});
}
return '(' + sql + ')';
};
Insert.prototype._buildRows = function (inserts) {
'use strict';
var self = this, sql = '';
if (this._data) {
if (Array.isArray(this._data)) {
this._data.forEach(function (row, index) {
if (index !== 0) {
sql += ',';
}
sql += self._buildRow(row, inserts);
});
} else {
sql += this._buildRow(this._data, inserts);
}
}
return sql;
};
Insert.prototype._buildReturning = function () {
'use strict';
var sql = '';
if (this._returning) {
sql += ' RETURNING "' + this._returning + '"';
}
return sql;
};
Insert.prototype.insert = function (data) {
'use strict';
this._data = data;
return this;
};
Insert.prototype.into = function (table) {
'use strict';
this._table = table;
return this;
};
Insert.prototype.returning = function (expression) {
'use strict';
this._returning = expression;
return this;
};
function Select(config) {
'use strict';
Query.call(this, config);
this._forUpdate = false;
this._orderBy = null;
this._select = '*';
this._table = null;
this._where = null;
}
Util.inherits(Select, Query);
Select.prototype._build = function (inserts) {
'use strict';
var sql = 'SELECT ';
sql += this._buildSelect();
sql += ' FROM "' + this._table + '"';
sql += this._buildWhere(inserts);
sql += this._buildOrderBy();
sql += this._buildLimit(inserts);
sql += this._buildOffset(inserts);
sql += this._buildForUpdate();
return sql;
};
Select.prototype._buildForUpdate = function () {
'use strict';
var sql = '';
if (this._forUpdate) {
sql += ' FOR UPDATE';
}
return sql;
};
Select.prototype._buildOrderBy = function () {
'use strict';
var sql = '';
if (this._orderBy) {
sql += ' ORDER BY ' + this._orderBy;
}
return sql;
};
Select.prototype._buildSelect = function () {
'use strict';
var select, sql = '';
select = this._select;
if (this._select) {
if (typeof this._select === 'string') {
sql += this._select;
} else {
if (Array.isArray(this._select)) {
this._select.forEach(function (select, index) {
if (index !== 0) {
sql += ',';
}
sql += '"' + select + '"';
});
} else {
Object.keys(this._select).forEach(function(key, index) {
if (index !== 0) {
sql += ',';
}
sql += '"' + select[key] + '" AS "' + key + '"';
});
}
}
}
return sql;
};
Select.prototype.forUpdate = function () {
'use strict';
this._forUpdate = true;
return this;
};
Select.prototype.orderBy = function (expression) {
'use strict';
this._orderBy = expression;
return this;
};
Select.prototype.select = function (expression) {
'use strict';
this._select = expression;
return this;
};
function Update(config) {
'use strict';
Query.call(this, config);
this._set = null;
}
Util.inherits(Update, Query);
Update.prototype._build = function (inserts) {
'use strict';
var sql = 'UPDATE "' + this._table + '"';
sql += this._buildSet(inserts);
sql += this._buildWhere(inserts);
sql += this._buildLimit(inserts);
sql += this._buildOffset(inserts);
return sql;
};
Update.prototype._buildSet = function (inserts) {
'use strict';
var _set, sql = '';
_set = this._set;
if (_set) {
sql += ' SET ';
if (typeof _set === 'object') {
Object.keys(_set).forEach(function(key, index) {
if (index !== 0) {
sql += ',';
}
inserts.push(_set[key]);
sql += '"' + key + '" = $' + inserts.length;
});
} else {
sql += _set;
}
}
return sql;
};
Update.prototype.set = function (expression) {
'use strict';
this._set = expression;
return this;
};
Update.prototype.update = function (table) {
'use strict';
this._table = table;
return this;
};
module.exports.Delete = Delete;
module.exports.Insert = Insert;
module.exports.Query = Query;
module.exports.Select = Select;
module.exports.Update = Update;