Browse Source

jshint: tabs to spaces

revert-array-changes
iamcharliegoddard 9 years ago
parent
commit
c7a36a39cf
  1. 154
      lib/dialect/mssql.js
  2. 2
      lib/dialect/oracle.js
  3. 44
      lib/dialect/postgres.js
  4. 46
      lib/dialect/sqlite.js
  5. 4
      lib/node/drop.js
  6. 2
      lib/node/parameter.js
  7. 8
      lib/node/query.js
  8. 4
      lib/node/truncate.js
  9. 46
      test/column-tests.js

154
lib/dialect/mssql.js

@ -31,7 +31,7 @@ Mssql.prototype._quoteCharacter = '[';
Mssql.prototype._arrayAggFunctionName = '';
Mssql.prototype._getParameterPlaceholder = function(index, value) {
if (this.config.questionMarkParameterPlaceholder) return '?';
if (this.config.questionMarkParameterPlaceholder) return '?';
return '@' + index;
};
@ -246,24 +246,24 @@ Mssql.prototype.visitDrop = function(drop) {
Mssql.prototype.visitFunctionCall = function(functionCall) {
this._visitingFunctionCall = true;
var name=functionCall.name;
// override the LENGTH function since mssql calls it LEN
if (name=="LENGTH") name="LEN";
var name=functionCall.name;
// override the LENGTH function since mssql calls it LEN
if (name=="LENGTH") name="LEN";
var txt = name + '(' + functionCall.nodes.map(this.visit.bind(this)).join(', ') + ')';
this._visitingFunctionCall = false;
return [txt];
};
Mssql.prototype.visitOrderBy = function(orderBy) {
var result=Mssql.super_.prototype.visitOrderBy.call(this, orderBy);
var offsetNode=orderBy.msSQLOffsetNode;
var limitNode=orderBy.msSQLLimitNode;
if (!offsetNode && !limitNode) return result;
assert(offsetNode,"Something bad happened, should have had an msSQLOffsetNode here.");
result.push("OFFSET "+getModifierValue(this,offsetNode)+" ROWS");
if (!limitNode) return result;
result.push("FETCH NEXT "+getModifierValue(this,limitNode)+" ROWS ONLY");
return result;
var result=Mssql.super_.prototype.visitOrderBy.call(this, orderBy);
var offsetNode=orderBy.msSQLOffsetNode;
var limitNode=orderBy.msSQLLimitNode;
if (!offsetNode && !limitNode) return result;
assert(offsetNode,"Something bad happened, should have had an msSQLOffsetNode here.");
result.push("OFFSET "+getModifierValue(this,offsetNode)+" ROWS");
if (!limitNode) return result;
result.push("FETCH NEXT "+getModifierValue(this,limitNode)+" ROWS ONLY");
return result;
};
/**
@ -281,63 +281,63 @@ Mssql.prototype.visitOrderBy = function(orderBy) {
* @returns {String[]}
*/
Mssql.prototype.visitQueryHelper=function(actions,targets,filters){
function _handleLimitAndOffset(){
var limitInfo=Mssql.super_.prototype.findNode.call(this, filters, "LIMIT"); // jshint ignore:line
var offsetInfo=Mssql.super_.prototype.findNode.call(this, filters, "OFFSET"); // jshint ignore:line
var orderByInfo=Mssql.super_.prototype.findNode.call(this, filters, "ORDER BY"); // jshint ignore:line
// no OFFSET or LIMIT then there's nothing special to do
if (!offsetInfo && !limitInfo) return;
// ORDER BY with OFFSET we have work to do, may consume LIMIT as well
if (orderByInfo && offsetInfo) _processOrderByOffsetLimit(orderByInfo,offsetInfo,limitInfo);
else if (offsetInfo) throw new Error("MS SQL Server does not allow OFFSET without ORDER BY");
else if (limitInfo) _processLimit(limitInfo);
}
/**
* We need to turn LIMIT into a TOP clause on the SELECT STATEMENT
*
* @param limitInfo
* @private
*/
function _processLimit(limitInfo){
var selectInfo=Mssql.super_.prototype.findNode.call(this, actions, "SELECT"); // jshint ignore:line
assert(selectInfo!==undefined,"MS SQL Server requires a SELECT clause when using LIMIT");
// save the LIMIT node with the SELECT node
selectInfo.node.msSQLLimitNode=limitInfo.node;
// remove the LIMIT node from the filters so it doesn't get processed later.
filters.splice(limitInfo.index,1);
}
/**
* We need to turn LIMIT into a TOP clause on the SELECT STATEMENT
*
* @param orderByInfo
* @param offsetInfo
* @param limitInfo
* @private
*/
function _processOrderByOffsetLimit(orderByInfo,offsetInfo,limitInfo){
// save the OFFSET AND LIMIT nodes with the ORDER BY node
orderByInfo.node.msSQLOffsetNode=offsetInfo.node;
if (limitInfo) orderByInfo.node.msSQLLimitNode=limitInfo.node;
// remove the OFFSET and LIMIT nodes from the filters so they don't get processed later.
filters.splice(offsetInfo.index,1);
if (limitInfo) filters.splice(limitInfo.index,1);
}
// MAIN
function _handleLimitAndOffset(){
var limitInfo=Mssql.super_.prototype.findNode.call(this, filters, "LIMIT"); // jshint ignore:line
var offsetInfo=Mssql.super_.prototype.findNode.call(this, filters, "OFFSET"); // jshint ignore:line
var orderByInfo=Mssql.super_.prototype.findNode.call(this, filters, "ORDER BY"); // jshint ignore:line
// no OFFSET or LIMIT then there's nothing special to do
if (!offsetInfo && !limitInfo) return;
// ORDER BY with OFFSET we have work to do, may consume LIMIT as well
if (orderByInfo && offsetInfo) _processOrderByOffsetLimit(orderByInfo,offsetInfo,limitInfo);
else if (offsetInfo) throw new Error("MS SQL Server does not allow OFFSET without ORDER BY");
else if (limitInfo) _processLimit(limitInfo);
}
/**
* We need to turn LIMIT into a TOP clause on the SELECT STATEMENT
*
* @param limitInfo
* @private
*/
function _processLimit(limitInfo){
var selectInfo=Mssql.super_.prototype.findNode.call(this, actions, "SELECT"); // jshint ignore:line
assert(selectInfo!==undefined,"MS SQL Server requires a SELECT clause when using LIMIT");
// save the LIMIT node with the SELECT node
selectInfo.node.msSQLLimitNode=limitInfo.node;
// remove the LIMIT node from the filters so it doesn't get processed later.
filters.splice(limitInfo.index,1);
}
/**
* We need to turn LIMIT into a TOP clause on the SELECT STATEMENT
*
* @param orderByInfo
* @param offsetInfo
* @param limitInfo
* @private
*/
function _processOrderByOffsetLimit(orderByInfo,offsetInfo,limitInfo){
// save the OFFSET AND LIMIT nodes with the ORDER BY node
orderByInfo.node.msSQLOffsetNode=offsetInfo.node;
if (limitInfo) orderByInfo.node.msSQLLimitNode=limitInfo.node;
// remove the OFFSET and LIMIT nodes from the filters so they don't get processed later.
filters.splice(offsetInfo.index,1);
if (limitInfo) filters.splice(limitInfo.index,1);
}
// MAIN
Mssql.super_.prototype.handleDistinct.call(this, actions, filters);
_handleLimitAndOffset();
// lazy-man sorting
var sortedNodes = actions.concat(targets).concat(filters);
for(var i = 0; i < sortedNodes.length; i++) {
var res = this.visit(sortedNodes[i]);
this.output = this.output.concat(res);
}
return this.output;
_handleLimitAndOffset();
// lazy-man sorting
var sortedNodes = actions.concat(targets).concat(filters);
for(var i = 0; i < sortedNodes.length; i++) {
var res = this.visit(sortedNodes[i]);
this.output = this.output.concat(res);
}
return this.output;
};
//Mysql.prototype.visitRenameColumn = function(renameColumn) {
@ -373,19 +373,19 @@ Mssql.prototype.visitReturning = function() {
// We deal with SELECT specially so we can add the TOP clause if needed
Mssql.prototype.visitSelect = function(select) {
if (!select.msSQLLimitNode) return Mssql.super_.prototype.visitSelect.call(this, select);
var result=[
'SELECT',
'TOP('+getModifierValue(this,select.msSQLLimitNode)+')',
select.nodes.map(this.visit.bind(this)).join(', ')
];
this._selectOrDeleteEndIndex = this.output.length + result.length;
return result;
if (!select.msSQLLimitNode) return Mssql.super_.prototype.visitSelect.call(this, select);
var result=[
'SELECT',
'TOP('+getModifierValue(this,select.msSQLLimitNode)+')',
select.nodes.map(this.visit.bind(this)).join(', ')
];
this._selectOrDeleteEndIndex = this.output.length + result.length;
return result;
};
// Node is either an OFFSET or LIMIT node
function getModifierValue(dialect,node){
return node.count.type ? dialect.visit(node.count) : node.count;
return node.count.type ? dialect.visit(node.count) : node.count;
}
function isAlterAddColumn(alter){

2
lib/dialect/oracle.js

@ -144,7 +144,7 @@ Oracle.prototype.visitQueryHelper=function(actions,targets,filters){
output[limit+2] = temp[2];
}
return this.output;
return this.output;
};
Oracle.prototype.visitColumn = function(columnNode) {

44
lib/dialect/postgres.js

@ -649,15 +649,15 @@ Postgres.prototype.visitQuery = function(queryNode) {
* @returns {String[]}
*/
Postgres.prototype.visitQueryHelper=function(actions,targets,filters){
this.handleDistinct(actions, filters);
// lazy-man sorting
var sortedNodes = actions.concat(targets).concat(filters);
for(var i = 0; i < sortedNodes.length; i++) {
var res = this.visit(sortedNodes[i]);
this.output = this.output.concat(res);
}
// implicit 'from'
return this.output;
this.handleDistinct(actions, filters);
// lazy-man sorting
var sortedNodes = actions.concat(targets).concat(filters);
for(var i = 0; i < sortedNodes.length; i++) {
var res = this.visit(sortedNodes[i]);
this.output = this.output.concat(res);
}
// implicit 'from'
return this.output;
};
Postgres.prototype.visitSubquery = function(queryNode) {
@ -1002,11 +1002,11 @@ Postgres.prototype.visitCreateView = function(createView) {
* @returns {Object|undefined} {index:number, node:Node}
*/
Postgres.prototype.findNode=function(list,type) {
for (var i= 0, len=list.length; i<len; i++) {
var n=list[i];
if (n.type==type) return {index:i,node:n};
}
return undefined;
for (var i= 0, len=list.length; i<len; i++) {
var n=list[i];
if (n.type==type) return {index:i,node:n};
}
return undefined;
};
/**
@ -1014,14 +1014,14 @@ Postgres.prototype.findNode=function(list,type) {
* Broken out as a separate function so that dialects that derive from this class can still use this functionality.
*/
Postgres.prototype.handleDistinct = function(actions,filters) {
var distinctNode = this.findNode(filters,"DISTINCT");
//if (!distinctNode) distinctNode = _findNode(targets,"DISTINCT");
//if (!distinctNode) distinctNode = _findNode(actions,"DISTINCT");
if (!distinctNode) return;
var selectInfo = this.findNode(actions,"SELECT");
if (!selectInfo) return; // there should be one by now, I think
// mark the SELECT node that it's distinct
selectInfo.node.isDistinct = true;
var distinctNode = this.findNode(filters,"DISTINCT");
//if (!distinctNode) distinctNode = _findNode(targets,"DISTINCT");
//if (!distinctNode) distinctNode = _findNode(actions,"DISTINCT");
if (!distinctNode) return;
var selectInfo = this.findNode(actions,"SELECT");
if (!selectInfo) return; // there should be one by now, I think
// mark the SELECT node that it's distinct
selectInfo.node.isDistinct = true;
};
module.exports = Postgres;

46
lib/dialect/sqlite.js

@ -38,32 +38,32 @@ Sqlite.prototype.visitDropColumn = function() {
};
Sqlite.prototype.visitFunctionCall = function(functionCall) {
var _this=this;
var _this=this;
this._visitingFunctionCall = true;
function _left() {
// convert LEFT(column,4) to SUBSTR(column,1,4)
var nodes = functionCall.nodes.map(_this.visit.bind(_this));
if (nodes.length != 2) throw new Error('Not enough parameters passed to LEFT function.');
var txt = "SUBSTR(" + (nodes[0]+'') + ', 1, ' + (nodes[1]+'') + ')';
return txt;
}
function _right() {
// convert RIGHT(column,4) to SUBSTR(column,-4)
var nodes = functionCall.nodes.map(_this.visit.bind(_this));
if (nodes.length != 2) throw new Error('Not enough parameters passed to RIGHT function.');
var txt = "SUBSTR(" + (nodes[0]+'') + ', -' + (nodes[1]+'') + ')';
return txt;
}
var txt="";
var name=functionCall.name;
// Override LEFT and RIGHT and convert to SUBSTR
if (name == "LEFT") txt = _left();
else if (name == "RIGHT") txt = _right();
else txt = name + '(' + functionCall.nodes.map(this.visit.bind(this)).join(', ') + ')';
function _left() {
// convert LEFT(column,4) to SUBSTR(column,1,4)
var nodes = functionCall.nodes.map(_this.visit.bind(_this));
if (nodes.length != 2) throw new Error('Not enough parameters passed to LEFT function.');
var txt = "SUBSTR(" + (nodes[0]+'') + ', 1, ' + (nodes[1]+'') + ')';
return txt;
}
function _right() {
// convert RIGHT(column,4) to SUBSTR(column,-4)
var nodes = functionCall.nodes.map(_this.visit.bind(_this));
if (nodes.length != 2) throw new Error('Not enough parameters passed to RIGHT function.');
var txt = "SUBSTR(" + (nodes[0]+'') + ', -' + (nodes[1]+'') + ')';
return txt;
}
var txt="";
var name=functionCall.name;
// Override LEFT and RIGHT and convert to SUBSTR
if (name == "LEFT") txt = _left();
else if (name == "RIGHT") txt = _right();
else txt = name + '(' + functionCall.nodes.map(this.visit.bind(this)).join(', ') + ')';
this._visitingFunctionCall = false;
return [txt];

4
lib/node/drop.js

@ -6,7 +6,7 @@ module.exports = Node.define({
type: 'DROP',
constructor: function(table) {
Node.call(this);
this.add(table);
Node.call(this);
this.add(table);
}
});

2
lib/node/parameter.js

@ -7,7 +7,7 @@ var ParameterNode = module.exports = Node.define({
constructor: function(val) {
Node.call(this);
this._val = val;
this.isExplicit = false;
this.isExplicit = false;
},
value: function() {
return this._val;

8
lib/node/query.js

@ -233,10 +233,10 @@ var Query = Node.define({
},
parameter: function(v) {
var param = ParameterNode.getNodeOrParameterNode(v);
param.isExplicit = true;
return this.add(param);
},
var param = ParameterNode.getNodeOrParameterNode(v);
param.isExplicit = true;
return this.add(param);
},
delete: function(params) {
var result;

4
lib/node/truncate.js

@ -6,7 +6,7 @@ module.exports = Node.define({
type: 'TRUNCATE',
constructor: function(table) {
Node.call(this);
this.add(table);
Node.call(this);
this.add(table);
}
});

46
test/column-tests.js

@ -36,29 +36,29 @@ describe('column', function() {
assert.equal(col, '"subTable"."subId"');
});
describe('property', function() {
var table = sql.define({
name: 'roundtrip',
columns: {
column_name: { property: 'propertyName' }
}
});
it('used as alias when !== column name', function() {
assert.equal(table.propertyName.toQuery().text, '"roundtrip"."column_name" AS "propertyName"');
});
it('uses explicit alias when !== column name', function() {
assert.equal(table.propertyName.as('alias').toQuery().text, '"roundtrip"."column_name" AS "alias"');
});
it('maps to column name in insert', function() {
assert.equal(table.insert({propertyName:'propVal'}).toQuery().text, 'INSERT INTO "roundtrip" ("column_name") VALUES ($1)');
});
it('maps to column name in update', function() {
assert.equal(table.update({propertyName:'propVal'}).toQuery().text, 'UPDATE "roundtrip" SET "column_name" = $1');
});
it('explicitly selected by *', function() {
assert.equal(table.select(table.star()).from(table).toQuery().text, 'SELECT "roundtrip"."column_name" AS "propertyName" FROM "roundtrip"');
});
});
describe('property', function() {
var table = sql.define({
name: 'roundtrip',
columns: {
column_name: { property: 'propertyName' }
}
});
it('used as alias when !== column name', function() {
assert.equal(table.propertyName.toQuery().text, '"roundtrip"."column_name" AS "propertyName"');
});
it('uses explicit alias when !== column name', function() {
assert.equal(table.propertyName.as('alias').toQuery().text, '"roundtrip"."column_name" AS "alias"');
});
it('maps to column name in insert', function() {
assert.equal(table.insert({propertyName:'propVal'}).toQuery().text, 'INSERT INTO "roundtrip" ("column_name") VALUES ($1)');
});
it('maps to column name in update', function() {
assert.equal(table.update({propertyName:'propVal'}).toQuery().text, 'UPDATE "roundtrip" SET "column_name" = $1');
});
it('explicitly selected by *', function() {
assert.equal(table.select(table.star()).from(table).toQuery().text, 'SELECT "roundtrip"."column_name" AS "propertyName" FROM "roundtrip"');
});
});
describe('autoGenerate', function() {
var table = sql.define({

Loading…
Cancel
Save