diff --git a/lib/index.js b/lib/index.js index 3a1cdfdc..5c6d7f2d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -80,11 +80,10 @@ Sql.prototype.constant = function(value) { // back compat shim for the Sql class constructor -var create = function(dialect, config) { +Sql.create = Sql.prototype.create = function(dialect, config) { return new Sql(dialect, {}); }; -module.exports = new Sql(DEFAULT_DIALECT, {}); -module.exports.create = create; +module.exports = Sql; module.exports.Sql = Sql; module.exports.Table = Table; diff --git a/lib/node/index.js b/lib/node/index.js index 66c0e481..883cdce3 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -44,7 +44,9 @@ var determineDialect = function(query, dialect) { Dialect = sql.dialect; } else { // dialect is not specified, use the default dialect - Dialect = require('../').dialect; + var Sql = require('../'); + var defaultSql = new Sql(); + Dialect = defaultSql.dialect; } return Dialect; }; diff --git a/lib/table.js b/lib/table.js index 23e335a8..6d1ecf60 100644 --- a/lib/table.js +++ b/lib/table.js @@ -22,7 +22,8 @@ var Table = function(config) { this.foreignKeys = []; this.table = this; if (!config.sql) { - config.sql = require('./index'); + var Sql = require('./index'); + config.sql = new Sql(); } this.sql = config.sql; }; diff --git a/test/column-tests.js b/test/column-tests.js index d0b95a14..5e6e8b17 100644 --- a/test/column-tests.js +++ b/test/column-tests.js @@ -1,7 +1,8 @@ 'use strict'; var assert = require('assert'); -var sql = require(__dirname + '/../lib'); +var Sql = require(__dirname + '/../lib'); +var sql = new Sql(); describe('column', function() { var table = sql.define({ diff --git a/test/dialects/alias-tests.js b/test/dialects/alias-tests.js index 32db9308..62f48246 100644 --- a/test/dialects/alias-tests.js +++ b/test/dialects/alias-tests.js @@ -2,7 +2,9 @@ var Harness = require('./support'); var customer = Harness.defineCustomerTable(); -var Sql = require('../../lib').setDialect('postgres'); +var Sql = require('../../lib'); +var sql = new Sql(); +sql.setDialect('postgres'); Harness.test({ query: customer.select(customer.name.isNull().as('nameIsNull')), @@ -80,7 +82,7 @@ Harness.test({ }); Harness.test({ - query: customer.select(Sql.functions.ROUND(customer.age.as('ageBetween'), 2)), + 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"' diff --git a/test/dialects/array-tests.js b/test/dialects/array-tests.js index f4191400..da5bcd9e 100644 --- a/test/dialects/array-tests.js +++ b/test/dialects/array-tests.js @@ -3,11 +3,12 @@ var Harness = require('./support'); var post = Harness.definePostTable(); var Sql = require('../../lib'); +var sql = new Sql(); // Array columns Harness.test({ query: post.update({ - tags: post.tags.concat(Sql.array('nodejs')) + tags: post.tags.concat(sql.array('nodejs')) }), pg: { text : 'UPDATE "post" SET "tags" = ("post"."tags" || ARRAY[$1])', @@ -18,7 +19,7 @@ Harness.test({ Harness.test({ query: post.select( - post.tags.contains(Sql.array('nodejs', 'js')) + post.tags.contains(sql.array('nodejs', 'js')) ), pg: { text : 'SELECT ("post"."tags" @> ARRAY[$1, $2]) FROM "post"', @@ -29,7 +30,7 @@ Harness.test({ Harness.test({ query: post.select( - post.tags.containedBy(Sql.array('nodejs', 'js')) + post.tags.containedBy(sql.array('nodejs', 'js')) ), pg: { text : 'SELECT ("post"."tags" <@ ARRAY[$1, $2]) FROM "post"', @@ -40,7 +41,7 @@ Harness.test({ Harness.test({ query: post.select( - post.tags.overlap(Sql.array('nodejs', 'js')) + post.tags.overlap(sql.array('nodejs', 'js')) ), pg: { text : 'SELECT ("post"."tags" && ARRAY[$1, $2]) FROM "post"', @@ -69,7 +70,7 @@ Harness.test({ // Array literals Harness.test({ - query: post.select(Sql.array(1,2,3)), + query: post.select(sql.array(1,2,3)), pg: { text : 'SELECT ARRAY[$1, $2, $3] FROM "post"', string: 'SELECT ARRAY[1, 2, 3] FROM "post"' @@ -78,7 +79,7 @@ Harness.test({ }); Harness.test({ - query: post.select(Sql.array(1,2,3).slice(2,3)), + query: post.select(sql.array(1,2,3).slice(2,3)), pg: { text : 'SELECT (ARRAY[$1, $2, $3])[$4:$5] FROM "post"', string: 'SELECT (ARRAY[1, 2, 3])[2:3] FROM "post"' @@ -87,7 +88,7 @@ Harness.test({ }); Harness.test({ - query: post.select(Sql.array(1,2,3).at(2)), + query: post.select(sql.array(1,2,3).at(2)), pg: { text : 'SELECT (ARRAY[$1, $2, $3])[$4] FROM "post"', string: 'SELECT (ARRAY[1, 2, 3])[2] FROM "post"' diff --git a/test/dialects/date-tests.js b/test/dialects/date-tests.js index dd2c4d64..9924e6a0 100644 --- a/test/dialects/date-tests.js +++ b/test/dialects/date-tests.js @@ -3,9 +3,10 @@ var Harness = require('./support'); var customer = Harness.defineCustomerTable(); var Sql = require('../../lib'); +var sql = new Sql(); Harness.test({ - query: customer.select(Sql.functions.YEAR(customer.metadata)), + query: customer.select(sql.functions.YEAR(customer.metadata)), pg: { text : 'SELECT EXTRACT(YEAR FROM "customer"."metadata") FROM "customer"', string: 'SELECT EXTRACT(YEAR FROM "customer"."metadata") FROM "customer"' @@ -30,7 +31,7 @@ Harness.test({ }); Harness.test({ - query: customer.select(Sql.functions.MONTH(customer.metadata)), + query: customer.select(sql.functions.MONTH(customer.metadata)), pg: { text : 'SELECT EXTRACT(MONTH FROM "customer"."metadata") FROM "customer"', string: 'SELECT EXTRACT(MONTH FROM "customer"."metadata") FROM "customer"' @@ -58,7 +59,7 @@ Harness.test({ }); Harness.test({ - query: customer.select(Sql.functions.DAY(customer.metadata)), + query: customer.select(sql.functions.DAY(customer.metadata)), pg: { text : 'SELECT EXTRACT(DAY FROM "customer"."metadata") FROM "customer"', string: 'SELECT EXTRACT(DAY FROM "customer"."metadata") FROM "customer"' @@ -83,7 +84,7 @@ Harness.test({ }); Harness.test({ - query: customer.select(Sql.functions.HOUR(customer.metadata)), + query: customer.select(sql.functions.HOUR(customer.metadata)), pg: { text : 'SELECT EXTRACT(HOUR FROM "customer"."metadata") FROM "customer"', string: 'SELECT EXTRACT(HOUR FROM "customer"."metadata") FROM "customer"' @@ -111,7 +112,7 @@ Harness.test({ }); Harness.test({ - query: customer.select(Sql.functions.CURRENT_TIMESTAMP()), + query: customer.select(sql.functions.CURRENT_TIMESTAMP()), pg: { text : 'SELECT CURRENT_TIMESTAMP FROM "customer"', string: 'SELECT CURRENT_TIMESTAMP FROM "customer"' @@ -136,7 +137,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().plus(Sql.interval({hours:1}))), + query: sql.select(sql.functions.CURRENT_TIMESTAMP().plus(sql.interval({hours:1}))), pg: { text : 'SELECT (CURRENT_TIMESTAMP + INTERVAL \'1 HOUR\')', string: 'SELECT (CURRENT_TIMESTAMP + INTERVAL \'1 HOUR\')' @@ -149,7 +150,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().minus(Sql.interval({years:3}))), + query: sql.select(sql.functions.CURRENT_TIMESTAMP().minus(sql.interval({years:3}))), pg: { text : 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'3 YEAR\')', string: 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'3 YEAR\')' @@ -162,7 +163,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().minus(Sql.interval({years:3, months:2}))), + query: sql.select(sql.functions.CURRENT_TIMESTAMP().minus(sql.interval({years:3, months:2}))), pg: { text : 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'3 YEAR 2 MONTH\')', string: 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'3 YEAR 2 MONTH\')' @@ -175,7 +176,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().plus(Sql.interval({hours:1, minutes:20}))), + query: sql.select(sql.functions.CURRENT_TIMESTAMP().plus(sql.interval({hours:1, minutes:20}))), pg: { text : 'SELECT (CURRENT_TIMESTAMP + INTERVAL \'1 HOUR 20 MINUTE\')', string: 'SELECT (CURRENT_TIMESTAMP + INTERVAL \'1 HOUR 20 MINUTE\')' @@ -188,7 +189,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().plus(Sql.interval({hours:'sql\'injection', minutes:20}))), + query: sql.select(sql.functions.CURRENT_TIMESTAMP().plus(sql.interval({hours:'sql\'injection', minutes:20}))), pg: { text : 'SELECT (CURRENT_TIMESTAMP + INTERVAL \'20 MINUTE\')', string: 'SELECT (CURRENT_TIMESTAMP + INTERVAL \'20 MINUTE\')' @@ -201,7 +202,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().minus(Sql.interval({days: 1, hours:5, minutes: 'sql\'injection'}))), + query: sql.select(sql.functions.CURRENT_TIMESTAMP().minus(sql.interval({days: 1, hours:5, minutes: 'sql\'injection'}))), pg: { text : 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'1 DAY 5 HOUR\')', string: 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'1 DAY 5 HOUR\')' @@ -214,7 +215,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select(Sql.functions.CURRENT_TIMESTAMP().minus(Sql.interval({years: 2, months: 5}))), + query: sql.select(sql.functions.CURRENT_TIMESTAMP().minus(sql.interval({years: 2, months: 5}))), pg: { text : 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'2 YEAR 5 MONTH\')', string: 'SELECT (CURRENT_TIMESTAMP - INTERVAL \'2 YEAR 5 MONTH\')' @@ -225,4 +226,3 @@ Harness.test({ }, params: [] }); - diff --git a/test/dialects/distinct-on-tests.js b/test/dialects/distinct-on-tests.js index d238f928..350904b4 100644 --- a/test/dialects/distinct-on-tests.js +++ b/test/dialects/distinct-on-tests.js @@ -2,7 +2,6 @@ var Harness = require('./support'); var user = Harness.defineUserTable(); -var Sql = require('../../lib').setDialect('postgres'); Harness.test({ query: user.select().distinctOn(user.id), @@ -21,4 +20,3 @@ Harness.test({ }, params: [] }); - diff --git a/test/dialects/function-tests.js b/test/dialects/function-tests.js index 702d8c63..a5034b31 100644 --- a/test/dialects/function-tests.js +++ b/test/dialects/function-tests.js @@ -2,11 +2,13 @@ var Harness = require('./support'); var Sql = require('../../lib'); +var sql = new Sql(); + var post = Harness.definePostTable(); Harness.test({ - query: post.select(Sql.functions.LENGTH(post.content)), + query: post.select(sql.functions.LENGTH(post.content)), pg: { text : 'SELECT LENGTH("post"."content") FROM "post"', string: 'SELECT LENGTH("post"."content") FROM "post"' @@ -31,7 +33,7 @@ Harness.test({ }); Harness.test({ - query: post.select(Sql.functions.LEFT(post.content,4)), + query: post.select(sql.functions.LEFT(post.content,4)), pg: { text : 'SELECT LEFT("post"."content", $1) FROM "post"', string: 'SELECT LEFT("post"."content", 4) FROM "post"' @@ -56,7 +58,7 @@ Harness.test({ }); Harness.test({ - query: post.select(Sql.functions.RIGHT(post.content,4)), + query: post.select(sql.functions.RIGHT(post.content,4)), pg: { text : 'SELECT RIGHT("post"."content", $1) FROM "post"', string: 'SELECT RIGHT("post"."content", 4) FROM "post"' @@ -79,4 +81,3 @@ Harness.test({ }, params: [4] }); - diff --git a/test/dialects/hstore-tests.js b/test/dialects/hstore-tests.js index 54ba2d22..472e3945 100644 --- a/test/dialects/hstore-tests.js +++ b/test/dialects/hstore-tests.js @@ -2,11 +2,13 @@ var Harness = require('./support'); var customer = Harness.defineCustomerTable(); -var Sql = require('../../lib').setDialect('postgres'); +var Sql = require('../../lib'); +var sql = new Sql(); +sql.setDialect('postgres'); Harness.test({ query: customer.update({ - metadata: customer.metadata.concat(Sql.functions.HSTORE('age', 20)) + metadata: customer.metadata.concat(sql.functions.HSTORE('age', 20)) }), pg: { text : 'UPDATE "customer" SET "metadata" = ("customer"."metadata" || HSTORE($1, $2))', diff --git a/test/dialects/ilike-tests.js b/test/dialects/ilike-tests.js index aba487ee..b4be0e92 100644 --- a/test/dialects/ilike-tests.js +++ b/test/dialects/ilike-tests.js @@ -3,7 +3,6 @@ var Harness = require('./support'); var post = Harness.definePostTable(); var user = Harness.defineUserTable(); -var Sql = require('../../lib').setDialect('postgres'); Harness.test({ query: post.select(post.content, post.userId).where(post.content.ilike('A%')), diff --git a/test/dialects/join-to-tests.js b/test/dialects/join-to-tests.js index d50f8817..72aa8727 100644 --- a/test/dialects/join-to-tests.js +++ b/test/dialects/join-to-tests.js @@ -1,5 +1,6 @@ 'use strict'; -var sql = require(__dirname + '/../../lib'); +var Sql = require(__dirname + '/../../lib'); +var sql = new Sql(); var Harness = require('./support'); diff --git a/test/dialects/json-tests.js b/test/dialects/json-tests.js index b610b96f..38e68f9c 100644 --- a/test/dialects/json-tests.js +++ b/test/dialects/json-tests.js @@ -2,7 +2,6 @@ var Harness = require('./support'); var customer = Harness.defineCustomerTable(); -var Sql = require('../../lib').setDialect('postgres'); Harness.test({ query: customer.select(customer.metadata.key('age')), diff --git a/test/dialects/matches-test.js b/test/dialects/matches-test.js index c385641c..9c987745 100644 --- a/test/dialects/matches-test.js +++ b/test/dialects/matches-test.js @@ -3,7 +3,9 @@ var Harness = require('./support'); var post = Harness.definePostTable(); var customerAlias = Harness.defineCustomerAliasTable(); -var sql = require(__dirname + '/../../lib').setDialect('postgres'); +var Sql = require(__dirname + '/../../lib'); +var sql = new Sql(); +sql.setDialect('postgres'); //Postgres needs the to_tsquery function to use with @@ operator Harness.test({ diff --git a/test/dialects/order-tests.js b/test/dialects/order-tests.js index 2c4803f5..ab91a9e4 100644 --- a/test/dialects/order-tests.js +++ b/test/dialects/order-tests.js @@ -2,7 +2,8 @@ var Harness = require('./support'); var post = Harness.definePostTable(); -var sql = require('../../lib'); +var Sql = require('../../lib'); +var sql = new Sql(); Harness.test({ query: post.select(post.content).order(post.content), @@ -336,4 +337,4 @@ Harness.test({ string: 'SELECT "post"."content" FROM "post" ORDER BY "post"."content"' }, params: [] -}); \ No newline at end of file +}); diff --git a/test/dialects/regex-tests.js b/test/dialects/regex-tests.js index 40dde618..a464ca37 100644 --- a/test/dialects/regex-tests.js +++ b/test/dialects/regex-tests.js @@ -2,7 +2,6 @@ var Harness = require('./support'); var customer = Harness.defineCustomerTable(); -var Sql = require('../../lib').setDialect('postgres'); Harness.test({ query: customer.select(customer.metadata.regex('age')), diff --git a/test/dialects/select-tests.js b/test/dialects/select-tests.js index 2892c8ae..125500a6 100644 --- a/test/dialects/select-tests.js +++ b/test/dialects/select-tests.js @@ -5,6 +5,7 @@ var post = Harness.definePostTable(); var user = Harness.defineUserTable(); var customerAlias = Harness.defineCustomerAliasTable(); var Sql = require('../../lib'); +var sql = new Sql(); Harness.test({ query: post.select(post.id).select(post.content), @@ -58,7 +59,7 @@ Harness.test({ // Test that we can generate a SELECT claus without a FROM clause Harness.test({ - query: Sql.select(), + query: sql.select(), pg: { text : 'SELECT ', string: 'SELECT ' @@ -84,7 +85,7 @@ Harness.test({ // Test that we can generate a SELECT claus without a FROM clause Harness.test({ - query: Sql.select("1"), + query: sql.select("1"), pg: { text : 'SELECT 1', string: 'SELECT 1' @@ -109,7 +110,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select("1").where("1=1"), + query: sql.select("1").where("1=1"), pg: { text : 'SELECT 1 WHERE (1=1)', string: 'SELECT 1 WHERE (1=1)' @@ -134,7 +135,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select(post.select(post.id)), + query: sql.select(post.select(post.id)), pg: { text : 'SELECT (SELECT "post"."id" FROM "post")', string: 'SELECT (SELECT "post"."id" FROM "post")' @@ -159,7 +160,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select(post.select(post.content).plus(post.select(post.content))), + query: sql.select(post.select(post.content).plus(post.select(post.content))), pg: { text : 'SELECT ((SELECT "post"."content" FROM "post") + (SELECT "post"."content" FROM "post"))', string: 'SELECT ((SELECT "post"."content" FROM "post") + (SELECT "post"."content" FROM "post"))' @@ -209,7 +210,7 @@ Harness.test({ }); Harness.test({ - query: post.select(Sql.constant(4)), + query: post.select(sql.constant(4)), pg: { text : 'SELECT $1 FROM "post"', string: 'SELECT 4 FROM "post"' @@ -234,7 +235,7 @@ Harness.test({ }); Harness.test({ - query: post.select(post.id,Sql.constant(4)), + query: post.select(post.id,sql.constant(4)), pg: { text : 'SELECT "post"."id", $1 FROM "post"', string: 'SELECT "post"."id", 4 FROM "post"' @@ -259,7 +260,7 @@ Harness.test({ }); Harness.test({ - query: post.select(Sql.constant(4).as('col1')), + query: post.select(sql.constant(4).as('col1')), pg: { text : 'SELECT $1 AS "col1" FROM "post"', string: 'SELECT 4 AS "col1" FROM "post"' @@ -284,7 +285,7 @@ Harness.test({ }); Harness.test({ - query: post.select(Sql.constant(4).plus(5)), + query: post.select(sql.constant(4).plus(5)), pg: { text : 'SELECT ($1 + $2) FROM "post"', string: 'SELECT (4 + 5) FROM "post"' @@ -309,7 +310,7 @@ Harness.test({ }); Harness.test({ - query: post.select(Sql.constant(4).plus(5).as('col1')), + query: post.select(sql.constant(4).plus(5).as('col1')), pg: { text : 'SELECT ($1 + $2) AS "col1" FROM "post"', string: 'SELECT (4 + 5) AS "col1" FROM "post"' @@ -334,7 +335,7 @@ Harness.test({ }); Harness.test({ - query: post.select(Sql.constant(4),Sql.constant("abc"),Sql.constant(true)), + query: post.select(sql.constant(4),sql.constant("abc"),sql.constant(true)), pg: { text : 'SELECT $1, $2, $3 FROM "post"', string: 'SELECT 4, \'abc\', TRUE FROM "post"' @@ -359,7 +360,7 @@ Harness.test({ }); Harness.test({ - query: post.select(Sql.constant(1).sum()), + query: post.select(sql.constant(1).sum()), pg: { text : 'SELECT SUM($1) AS "constant_sum" FROM "post"', string: 'SELECT SUM(1) AS "constant_sum" FROM "post"' @@ -384,7 +385,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select(post.select(post.id).as("column1")), + query: sql.select(post.select(post.id).as("column1")), pg: { text : 'SELECT (SELECT "post"."id" FROM "post") AS "column1"', string: 'SELECT (SELECT "post"."id" FROM "post") AS "column1"' @@ -409,7 +410,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select(post.select(post.count()).as("column1")), + query: sql.select(post.select(post.count()).as("column1")), pg: { text : 'SELECT (SELECT COUNT("post".*) AS "post_count" FROM "post") AS "column1"', string: 'SELECT (SELECT COUNT("post".*) AS "post_count" FROM "post") AS "column1"' @@ -434,7 +435,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select(post.select(post.id).as("column1"),user.select(user.id).as("column2")), + query: sql.select(post.select(post.id).as("column1"),user.select(user.id).as("column2")), pg: { text : 'SELECT (SELECT "post"."id" FROM "post") AS "column1", (SELECT "user"."id" FROM "user") AS "column2"', string: 'SELECT (SELECT "post"."id" FROM "post") AS "column1", (SELECT "user"."id" FROM "user") AS "column2"' @@ -457,5 +458,3 @@ Harness.test({ }, params: [] }); - - diff --git a/test/dialects/subfield-tests.js b/test/dialects/subfield-tests.js index aeaab51d..5b7bf52c 100644 --- a/test/dialects/subfield-tests.js +++ b/test/dialects/subfield-tests.js @@ -2,7 +2,6 @@ var Harness = require('./support'); var customer = Harness.defineCustomerCompositeTable(); -var Sql = require('../../lib').setDialect('postgres'); Harness.test({ query: customer.select(customer.info.subfields.age), diff --git a/test/dialects/subquery-tests.js b/test/dialects/subquery-tests.js index fe5b2aad..3fe046b3 100644 --- a/test/dialects/subquery-tests.js +++ b/test/dialects/subquery-tests.js @@ -5,6 +5,7 @@ var customer = Harness.defineCustomerTable(); var user = Harness.defineUserTable(); var post = Harness.definePostTable(); var Sql = require('../../lib'); +var sql = new Sql(); Harness.test({ query: user.select(user.name).where(user.id.in(post.select(post.userId))), @@ -61,7 +62,7 @@ Harness.test({ }); Harness.test({ - query: Sql.select('*').from(user.subQuery()), + query: sql.select('*').from(user.subQuery()), pg: { text : 'SELECT * FROM (SELECT * FROM "user")', string: 'SELECT * FROM (SELECT * FROM "user")' @@ -87,7 +88,7 @@ Harness.test({ // Subquery with a date Harness.test({ - query: Sql.select('*').from(post.subQuery().where(post.content.equals(new Date('Sat, 01 Jan 2000 00:00:00 GMT')))), + query: sql.select('*').from(post.subQuery().where(post.content.equals(new Date('Sat, 01 Jan 2000 00:00:00 GMT')))), pg: { text : 'SELECT * FROM (SELECT * FROM "post" WHERE ("post"."content" = $1))', string: 'SELECT * FROM (SELECT * FROM "post" WHERE ("post"."content" = \'2000-01-01T00:00:00.000Z\'))' @@ -116,7 +117,7 @@ Harness.test({ Harness.test({ - query: Sql.select('*').from(customer.subQuery('T1')).from(user.subQuery('T2')), + query: sql.select('*').from(customer.subQuery('T1')).from(user.subQuery('T2')), pg: { text : 'SELECT * FROM (SELECT * FROM "customer") "T1" , (SELECT * FROM "user") "T2"', string: 'SELECT * FROM (SELECT * FROM "customer") "T1" , (SELECT * FROM "user") "T2"' @@ -142,8 +143,8 @@ Harness.test({ Harness.test({ query: customer.name.between( - customer.subQuery().select(Sql.functions.MIN(customer.name)), - customer.subQuery().select(Sql.functions.MAX(customer.name)) + customer.subQuery().select(sql.functions.MIN(customer.name)), + customer.subQuery().select(sql.functions.MAX(customer.name)) ), pg: { text : '("customer"."name" BETWEEN (SELECT MIN("customer"."name") FROM "customer") AND (SELECT MAX("customer"."name") FROM "customer"))', @@ -195,7 +196,7 @@ Harness.test({ var limitUsers = user.subQuery('limit-users').select(user.id, user.name).from(user).order(user.name).limit(10).offset(10); Harness.test({ - query: Sql.select(limitUsers.name, post.tags).from(limitUsers.leftJoin(post).on(post.userId.equals(limitUsers.id))), + query: sql.select(limitUsers.name, post.tags).from(limitUsers.leftJoin(post).on(post.userId.equals(limitUsers.id))), pg: { text : 'SELECT "limit-users"."name", "post"."tags" FROM (SELECT "user"."id", "user"."name" FROM "user" ORDER BY "user"."name" LIMIT 10 OFFSET 10) "limit-users" LEFT JOIN "post" ON ("post"."userId" = "limit-users"."id")', string: 'SELECT "limit-users"."name", "post"."tags" FROM (SELECT "user"."id", "user"."name" FROM "user" ORDER BY "user"."name" LIMIT 10 OFFSET 10) "limit-users" LEFT JOIN "post" ON ("post"."userId" = "limit-users"."id")' @@ -218,4 +219,3 @@ Harness.test({ }, params: [] }); - diff --git a/test/function-tests.js b/test/function-tests.js index 77ef0087..94095d4f 100644 --- a/test/function-tests.js +++ b/test/function-tests.js @@ -1,12 +1,14 @@ 'use strict'; var assert = require('assert'); -var sql = require(__dirname + '/../lib').setDialect('postgres'); +var Sql = require(__dirname + '/../lib'); +var sql = new Sql(); +sql.setDialect('postgres'); var user = sql.define({ name: 'user', columns: [ - {name: 'id'}, + {name: 'id'}, {name:'email'}, {name: 'name'}, {name: 'age', property: 'howOld'} diff --git a/test/index-tests.js b/test/index-tests.js index f25839e1..99adeaab 100644 --- a/test/index-tests.js +++ b/test/index-tests.js @@ -1,7 +1,8 @@ 'use strict'; var assert = require('assert'); -var sql = require('../lib'); +var Sql = require('../lib'); +var sql = new Sql(); var user = sql.define({ name: 'user', @@ -30,7 +31,7 @@ suite('index', function() { test('stores the mssql dialect', function() { assert.equal(sql.create('mssql').dialectName, 'mssql'); }); - + test('stores the oracle dialect', function() { assert.equal(sql.create('oracle').dialectName, 'oracle'); }); @@ -77,7 +78,6 @@ suite('index', function() { }); test('using Sql as a class', function() { - var Sql = sql.Sql; var mssql = new Sql('mssql'); var mysql = new Sql('mysql'); var postgres = new Sql('postgres'); @@ -92,7 +92,6 @@ suite('index', function() { }); test('override dialect for toQuery using dialect name', function() { - var Sql = sql.Sql; var mssql = new Sql('mssql'); var mysql = new Sql('mysql'); var postgres = new Sql('postgres'); @@ -117,7 +116,7 @@ suite('index', function() { assert.equal(mssqlQuery.text, 'SELECT [user].[id] FROM [user] WHERE ([user].[email] = @1)'); assert.deepEqual(mssqlQuery.values, values); - + assert.equal(oracleQuery.text, 'SELECT "user"."id" FROM "user" WHERE ("user"."email" = :1)'); assert.deepEqual(oracleQuery.values, values); }); @@ -151,7 +150,6 @@ suite('index', function() { }); test('override dialect for toNamedQuery using dialect name', function() { - var Sql = sql.Sql; var mysql = new Sql('mysql'); var postgres = new Sql('postgres'); var sqlite = new Sql('sqlite'); @@ -177,11 +175,11 @@ suite('index', function() { assert.equal(mysqlQuery.text, 'SELECT `user`.`id` FROM `user` WHERE (`user`.`email` = ?)'); assert.deepEqual(mysqlQuery.values, values); assert.equal('user.select_brian', mysqlQuery.name); - + assert.equal(mssqlQuery.text, 'SELECT [user].[id] FROM [user] WHERE ([user].[email] = @1)'); assert.deepEqual(mssqlQuery.values, values); assert.equal('user.select_brian', mssqlQuery.name); - + assert.equal(oracleQuery.text, 'SELECT "user"."id" FROM "user" WHERE ("user"."email" = :1)'); assert.deepEqual(oracleQuery.values, values); assert.equal('user.select_brian', oracleQuery.name); @@ -196,7 +194,6 @@ suite('index', function() { }); test('mssql default parameter place holder is @index', function() { - var Sql = sql.Sql; var mssql = new Sql('mssql'); var query = mssql.select(user.id).from(user).where(user.email.equals('x@y.com')).toQuery(); assert.equal(query.text, 'SELECT [user].[id] FROM [user] WHERE ([user].[email] = @1)'); @@ -204,7 +201,6 @@ suite('index', function() { }); test('mssql override default parameter placeholder with ?', function() { - var Sql = sql.Sql; var mssql = new Sql('mssql',{questionMarkParameterPlaceholder:true}); var query = mssql.select(user.id).from(user).where(user.email.equals('x@y.com')).toQuery(); assert.equal(query.text, 'SELECT [user].[id] FROM [user] WHERE ([user].[email] = ?)');