Browse Source

Merge pull request #257 from edudutra/fix-186

Index in descending order
revert-array-changes
Brian C 9 years ago
parent
commit
af7ea56e08
  1. 2
      lib/dialect/mssql.js
  2. 13
      lib/dialect/postgres.js
  3. 4
      lib/dialect/sqlite.js
  4. 3
      lib/node/createIndex.js
  5. 26
      test/dialects/alias-tests.js
  6. 155
      test/dialects/cast-tests.js
  7. 51
      test/dialects/indexes-tests.js
  8. 15
      test/function-tests.js

2
lib/dialect/mssql.js

@ -235,10 +235,12 @@ 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 txt = name + '(' + functionCall.nodes.map(this.visit.bind(this)).join(', ') + ')';
this._visitingFunctionCall = false;
return [txt];
};

13
lib/dialect/postgres.js

@ -343,7 +343,9 @@ Postgres.prototype.visitAlter = function(alter) {
};
Postgres.prototype.visitCast = function(cast) {
this._visitingCast = true;
var result = ['CAST(' + this.visit(cast.value) + ' AS ' + cast.dataType + ')'];
this._visitingCast = false;
return result;
};
@ -702,6 +704,8 @@ Postgres.prototype.visitColumn = function(columnNode) {
&& !this.visitingCase
&& !this._visitingJoin
);
var inFunctionCall = this._visitingFunctionCall;
var inCast = this._visitingCast;
var txt = [];
var closeParen = 0;
if(inSelectClause && (!table.alias || !!columnNode.alias)) {
@ -762,7 +766,7 @@ Postgres.prototype.visitColumn = function(columnNode) {
txt.push(')');
}
}
if(inSelectClause && (columnNode.alias || columnNode.property !== columnNode.name)) {
if(inSelectClause && !inFunctionCall && !inCast && (columnNode.alias || columnNode.property !== columnNode.name)) {
txt.push(this._aliasText + this.quote(columnNode.alias || columnNode.property));
}
if(this._visitingCreate || this._visitingAddColumn) {
@ -815,7 +819,9 @@ Postgres.prototype.visitColumn = function(columnNode) {
};
Postgres.prototype.visitFunctionCall = function(functionCall) {
this._visitingFunctionCall = true;
var txt = functionCall.name + '(' + functionCall.nodes.map(this.visit.bind(this)).join(', ') + ')';
this._visitingFunctionCall = false;
return [txt];
};
@ -952,7 +958,10 @@ Postgres.prototype.visitCreateIndex = function(node) {
"ON",
tableName,
"(" + node.options.columns.reduce(function(result, col) {
return result.concat(this.quote(col.name));
var column = col.name ? col.name : col.value.name;
var direction = col.direction ? ' ' + col.direction.text : '';
var res = result.concat(this.quote(column) + direction);
return res;
}.bind(this), []) + ")"
]);

4
lib/dialect/sqlite.js

@ -37,6 +37,8 @@ Sqlite.prototype.visitDropColumn = function() {
Sqlite.prototype.visitFunctionCall = function(functionCall) {
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))
@ -59,6 +61,8 @@ Sqlite.prototype.visitFunctionCall = function(functionCall) {
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];
};

3
lib/node/createIndex.js

@ -49,7 +49,8 @@ module.exports = Node.define({
if (!result) {
var columns = this.options.columns.map(function(col) {
return col.name;
var column = col.name ? col.name : col.value.name;
return column;
}).sort();
result = [this.table._name];

26
test/dialects/alias-tests.js

@ -2,6 +2,7 @@
var Harness = require('./support');
var customer = Harness.defineCustomerTable();
var Sql = require('../../lib').setDialect('postgres');
Harness.test({
query: customer.select(customer.name.isNull().as('nameIsNull')),
@ -78,6 +79,31 @@ Harness.test({
params: [10, 20]
});
Harness.test({
query: customer.select(Sql.functions.ROUND(customer.age.as('ageBetween'), 2)),
pg: {
text : 'SELECT ROUND("customer"."age", $1) FROM "customer"',
string: 'SELECT ROUND("customer"."age", 2) FROM "customer"'
},
sqlite: {
text : 'SELECT ROUND("customer"."age", $1) FROM "customer"',
string: 'SELECT ROUND("customer"."age", 2) FROM "customer"'
},
mysql: {
text : 'SELECT ROUND(`customer`.`age`, ?) FROM `customer`',
string: 'SELECT ROUND(`customer`.`age`, 2) FROM `customer`'
},
mssql: {
text : 'SELECT ROUND([customer].[age], @1) FROM [customer]',
string: 'SELECT ROUND([customer].[age], 2) FROM [customer]'
},
oracle: {
text : 'SELECT ROUND("customer"."age", :1) FROM "customer"',
string: 'SELECT ROUND("customer"."age", 2) FROM "customer"'
},
params: [2]
});
Harness.test({
query: customer.select(customer.age.notBetween(10, 20).as('ageNotBetween')),
pg: {

155
test/dialects/cast-tests.js

@ -2,6 +2,7 @@
var Harness = require('./support');
var customer = Harness.defineCustomerTable();
var customerAlias = Harness.defineCustomerAliasTable();
// Cast columns.
Harness.test({
@ -157,3 +158,157 @@ Harness.test({
},
params: []
});
// Cast Aliased columns.
Harness.test({
query: customerAlias.select(customerAlias.age_alias.cast('int')),
pg: {
text : 'SELECT CAST("customer"."age" AS int) FROM "customer"',
string: 'SELECT CAST("customer"."age" AS int) FROM "customer"'
},
sqlite: {
text : 'SELECT CAST("customer"."age" AS int) FROM "customer"',
string: 'SELECT CAST("customer"."age" AS int) FROM "customer"'
},
mysql: {
text : 'SELECT CAST(`customer`.`age` AS int) FROM `customer`',
string: 'SELECT CAST(`customer`.`age` AS int) FROM `customer`'
},
mssql: {
text : 'SELECT CAST([customer].[age] AS int) FROM [customer]',
string: 'SELECT CAST([customer].[age] AS int) FROM [customer]'
},
oracle: {
text : 'SELECT CAST("customer"."age" AS int) FROM "customer"',
string: 'SELECT CAST("customer"."age" AS int) FROM "customer"'
},
params: []
});
Harness.test({
query: customerAlias.select(customerAlias.name_alias.cast('varchar(10)')),
pg: {
text : 'SELECT CAST("customer"."name" AS varchar(10)) FROM "customer"',
string: 'SELECT CAST("customer"."name" AS varchar(10)) FROM "customer"'
},
sqlite: {
text : 'SELECT CAST("customer"."name" AS varchar(10)) FROM "customer"',
string: 'SELECT CAST("customer"."name" AS varchar(10)) FROM "customer"'
},
mysql: {
text : 'SELECT CAST(`customer`.`name` AS varchar(10)) FROM `customer`',
string: 'SELECT CAST(`customer`.`name` AS varchar(10)) FROM `customer`'
},
mssql: {
text : 'SELECT CAST([customer].[name] AS varchar(10)) FROM [customer]',
string: 'SELECT CAST([customer].[name] AS varchar(10)) FROM [customer]'
},
oracle: {
text : 'SELECT CAST("customer"."name" AS varchar(10)) FROM "customer"',
string: 'SELECT CAST("customer"."name" AS varchar(10)) FROM "customer"'
},
params: []
});
// Cast binary expressions for Aliased Column.
Harness.test({
query: customerAlias.select(customerAlias.name_alias.plus(customerAlias.age_alias).cast('varchar(15)')),
pg: {
text : 'SELECT CAST(("customer"."name" + "customer"."age") AS varchar(15)) FROM "customer"',
string: 'SELECT CAST(("customer"."name" + "customer"."age") AS varchar(15)) FROM "customer"'
},
sqlite: {
text : 'SELECT CAST(("customer"."name" + "customer"."age") AS varchar(15)) FROM "customer"',
string: 'SELECT CAST(("customer"."name" + "customer"."age") AS varchar(15)) FROM "customer"'
},
mysql: {
text : 'SELECT CAST((`customer`.`name` + `customer`.`age`) AS varchar(15)) FROM `customer`',
string: 'SELECT CAST((`customer`.`name` + `customer`.`age`) AS varchar(15)) FROM `customer`'
},
mssql: {
text : 'SELECT CAST(([customer].[name] + [customer].[age]) AS varchar(15)) FROM [customer]',
string: 'SELECT CAST(([customer].[name] + [customer].[age]) AS varchar(15)) FROM [customer]'
},
oracle: {
text : 'SELECT CAST(("customer"."name" + "customer"."age") AS varchar(15)) FROM "customer"',
string: 'SELECT CAST(("customer"."name" + "customer"."age") AS varchar(15)) FROM "customer"'
},
params: []
});
// Cast cast expressions for aliased columns.
Harness.test({
query: customerAlias.select(customerAlias.name_alias.cast('varchar(15)').cast('varchar(10)')),
pg: {
text : 'SELECT CAST(CAST("customer"."name" AS varchar(15)) AS varchar(10)) FROM "customer"',
string: 'SELECT CAST(CAST("customer"."name" AS varchar(15)) AS varchar(10)) FROM "customer"'
},
sqlite: {
text : 'SELECT CAST(CAST("customer"."name" AS varchar(15)) AS varchar(10)) FROM "customer"',
string: 'SELECT CAST(CAST("customer"."name" AS varchar(15)) AS varchar(10)) FROM "customer"'
},
mysql: {
text : 'SELECT CAST(CAST(`customer`.`name` AS varchar(15)) AS varchar(10)) FROM `customer`',
string: 'SELECT CAST(CAST(`customer`.`name` AS varchar(15)) AS varchar(10)) FROM `customer`'
},
mssql: {
text : 'SELECT CAST(CAST([customer].[name] AS varchar(15)) AS varchar(10)) FROM [customer]',
string: 'SELECT CAST(CAST([customer].[name] AS varchar(15)) AS varchar(10)) FROM [customer]'
},
oracle: {
text : 'SELECT CAST(CAST("customer"."name" AS varchar(15)) AS varchar(10)) FROM "customer"',
string: 'SELECT CAST(CAST("customer"."name" AS varchar(15)) AS varchar(10)) FROM "customer"'
},
params: []
});
// Cast in WHERE for aliased columns.
Harness.test({
query: customerAlias.select(customerAlias.name_alias).where(customerAlias.age_alias.cast('int').plus(100).equals(150)),
pg: {
text : 'SELECT "customer"."name" AS "name_alias" FROM "customer" WHERE ((CAST("customer"."age" AS int) + $1) = $2)',
string: 'SELECT "customer"."name" AS "name_alias" FROM "customer" WHERE ((CAST("customer"."age" AS int) + 100) = 150)'
},
sqlite: {
text : 'SELECT "customer"."name" AS "name_alias" FROM "customer" WHERE ((CAST("customer"."age" AS int) + $1) = $2)',
string: 'SELECT "customer"."name" AS "name_alias" FROM "customer" WHERE ((CAST("customer"."age" AS int) + 100) = 150)'
},
mysql: {
text : 'SELECT `customer`.`name` AS `name_alias` FROM `customer` WHERE ((CAST(`customer`.`age` AS int) + ?) = ?)',
string: 'SELECT `customer`.`name` AS `name_alias` FROM `customer` WHERE ((CAST(`customer`.`age` AS int) + 100) = 150)'
},
mssql: {
text : 'SELECT [customer].[name] AS [name_alias] FROM [customer] WHERE ((CAST([customer].[age] AS int) + @1) = @2)',
string: 'SELECT [customer].[name] AS [name_alias] FROM [customer] WHERE ((CAST([customer].[age] AS int) + 100) = 150)'
},
oracle: {
text : 'SELECT "customer"."name" "name_alias" FROM "customer" WHERE ((CAST("customer"."age" AS int) + :1) = :2)',
string: 'SELECT "customer"."name" "name_alias" FROM "customer" WHERE ((CAST("customer"."age" AS int) + 100) = 150)'
},
params: [100, 150]
});
// Alias cast.
Harness.test({
query: customerAlias.select(customerAlias.age_alias.cast('int').as('age_int')),
pg: {
text : 'SELECT CAST("customer"."age" AS int) AS "age_int" FROM "customer"',
string: 'SELECT CAST("customer"."age" AS int) AS "age_int" FROM "customer"'
},
sqlite: {
text : 'SELECT CAST("customer"."age" AS int) AS "age_int" FROM "customer"',
string: 'SELECT CAST("customer"."age" AS int) AS "age_int" FROM "customer"'
},
mysql: {
text : 'SELECT CAST(`customer`.`age` AS int) AS `age_int` FROM `customer`',
string: 'SELECT CAST(`customer`.`age` AS int) AS `age_int` FROM `customer`'
},
mssql: {
text : 'SELECT CAST([customer].[age] AS int) AS [age_int] FROM [customer]',
string: 'SELECT CAST([customer].[age] AS int) AS [age_int] FROM [customer]'
},
oracle: {
text : 'SELECT CAST("customer"."age" AS int) "age_int" FROM "customer"',
string: 'SELECT CAST("customer"."age" AS int) "age_int" FROM "customer"'
},
params: []
});

51
test/dialects/indexes-tests.js

@ -121,6 +121,57 @@ Harness.test({
params: []
});
Harness.test({
query: post.indexes().create().on(post.userId, post.id.desc),
pg: {
text : 'CREATE INDEX "post_id_userId" ON "post" ("userId","id" DESC)',
string: 'CREATE INDEX "post_id_userId" ON "post" ("userId","id" DESC)'
},
mysql: {
text : 'CREATE INDEX `post_id_userId` ON `post` (`userId`,`id` DESC)',
string: 'CREATE INDEX `post_id_userId` ON `post` (`userId`,`id` DESC)'
},
sqlite: {
text : 'CREATE INDEX "post_id_userId" ON "post" ("userId","id" DESC)',
string: 'CREATE INDEX "post_id_userId" ON "post" ("userId","id" DESC)'
},
mssql: {
text : 'CREATE INDEX [post_id_userId] ON [post] ([userId],[id] DESC)',
string: 'CREATE INDEX [post_id_userId] ON [post] ([userId],[id] DESC)'
},
oracle: {
text : 'CREATE INDEX "post_id_userId" ON "post" ("userId","id" DESC)',
string: 'CREATE INDEX "post_id_userId" ON "post" ("userId","id" DESC)'
},
params: []
});
Harness.test({
query: post.indexes().create().on(post.userId).on(post.id.descending),
pg: {
text : 'CREATE INDEX "post_id_userId" ON "post" ("userId","id" DESC)',
string: 'CREATE INDEX "post_id_userId" ON "post" ("userId","id" DESC)'
},
mysql: {
text : 'CREATE INDEX `post_id_userId` ON `post` (`userId`,`id` DESC)',
string: 'CREATE INDEX `post_id_userId` ON `post` (`userId`,`id` DESC)'
},
sqlite: {
text : 'CREATE INDEX "post_id_userId" ON "post" ("userId","id" DESC)',
string: 'CREATE INDEX "post_id_userId" ON "post" ("userId","id" DESC)'
},
mssql: {
text : 'CREATE INDEX [post_id_userId] ON [post] ([userId],[id] DESC)',
string: 'CREATE INDEX [post_id_userId] ON [post] ([userId],[id] DESC)'
},
oracle: {
text : 'CREATE INDEX "post_id_userId" ON "post" ("userId","id" DESC)',
string: 'CREATE INDEX "post_id_userId" ON "post" ("userId","id" DESC)'
},
params: []
});
Harness.test({
query: post.indexes().create(),
pg: {

15
test/function-tests.js

@ -5,7 +5,12 @@ var sql = require(__dirname + '/../lib').setDialect('postgres');
var user = sql.define({
name: 'user',
columns: ['id', 'email', 'name']
columns: [
{name: 'id'},
{name:'email'},
{name: 'name'},
{name: 'age', property: 'howOld'}
]
});
suite('function', function() {
@ -16,6 +21,14 @@ suite('function', function() {
assert.equal(aliasedUpper.text, 'UPPER("user"."email") AS "upperAlias"');
});
test('function call on aliased column', function() {
var round = sql.functions.ROUND;
var aliasedRound = round(user.howOld, 2).toQuery();
assert.equal(aliasedRound.text, 'ROUND("user"."age", $1)');
assert.equal(aliasedRound.values[0], 2);
});
test('creating function call works', function() {
var upper = sql.functionCallCreator('UPPER');
var functionCall = upper('hello', 'world').toQuery();

Loading…
Cancel
Save