Browse Source

Merge pull request #85 from yc662/fixCommentSpacing

fix spacing in comments
dev
Brian C 12 years ago
parent
commit
bc167a487e
  1. 6
      lib/column.js
  2. 24
      lib/dialect/postgres.js
  3. 8
      lib/joiner.js
  4. 4
      lib/node/index.js
  5. 6
      lib/node/query.js
  6. 2
      lib/node/where.js
  7. 15
      lib/table.js
  8. 8
      test/dialects/clause-ordering-tests.js
  9. 8
      test/dialects/insert-tests.js
  10. 1
      test/dialects/namespace-tests.js
  11. 4
      test/dialects/shortcut-tests.js
  12. 1
      test/dialects/table-tests.js
  13. 21
      test/readme-examples.js

6
lib/column.js

@ -67,7 +67,7 @@ Column.prototype.getValue = function() {
};
Column.prototype.toNode = function() {
//creates a query node from this column
// creates a query node from this column
return new ColumnNode(contextify(this));
};
@ -103,11 +103,11 @@ Column.prototype.max = function(alias) {
return this.aggregate(alias, 'max');
};
Column.prototype.sum = function(alias) {
Column.prototype.sum = function(alias) {
return this.aggregate(alias, 'sum');
};
Column.prototype.avg = function(alias) {
Column.prototype.avg = function(alias) {
return this.aggregate(alias, 'avg');
};

24
lib/dialect/postgres.js

@ -14,7 +14,7 @@ Postgres.prototype._myClass = Postgres;
Postgres.prototype._arrayAggFunctionName = 'array_agg';
Postgres.prototype.getQuery = function(queryNode) {
//passed in a table, not a query
// passed in a table, not a query
if(queryNode instanceof Table) {
queryNode = queryNode.select(queryNode.star());
}
@ -78,7 +78,7 @@ Postgres.prototype.visitSelect = function(select) {
Postgres.prototype.visitInsert = function(insert) {
var self = this;
//don't use table.column for inserts
// don't use table.column for inserts
this._visitedInsert = true;
var paramNodes = insert.getParameters()
@ -104,9 +104,9 @@ Postgres.prototype.visitInsert = function(insert) {
};
Postgres.prototype.visitUpdate = function(update) {
//don't auto-generate from clause
// don't auto-generate from clause
var params = [];
/*jshint boss: true */
/* jshint boss: true */
for(var i = 0, node; node = update.nodes[i]; i++) {
this._visitingUpdateTargetColumn = true;
var target_col = this.visit(node);
@ -129,7 +129,7 @@ Postgres.prototype.visitDelete = function() {
Postgres.prototype.visitCreate = function(create) {
this._visitingCreate = true;
//don't auto-generate from clause
// don't auto-generate from clause
var table = this._queryNode.table;
var col_nodes = table.columns.map(function(col) { return col.toNode(); });
@ -142,7 +142,7 @@ Postgres.prototype.visitCreate = function(create) {
};
Postgres.prototype.visitDrop = function(drop) {
//don't auto-generate from clause
// don't auto-generate from clause
var result = ['DROP TABLE'];
result = result.concat(drop.nodes.map(this.visit.bind(this)));
result.push(this.visit(this._queryNode.table.toNode()));
@ -151,7 +151,7 @@ Postgres.prototype.visitDrop = function(drop) {
Postgres.prototype.visitAlter = function(alter) {
this._visitingAlter = true;
//don't auto-generate from clause
// don't auto-generate from clause
var table = this._queryNode.table;
// TODO: col_nodes is unused?
var col_nodes = table.columns.map(function(col) { return col.toNode(); });
@ -221,8 +221,8 @@ Postgres.prototype.visitUnary = function(unary) {
Postgres.prototype.visitQuery = function(queryNode) {
this._queryNode = queryNode;
//need to sort the top level query nodes on visitation priority
//so select/insert/update/delete comes before from comes before where
// need to sort the top level query nodes on visitation priority
// so select/insert/update/delete comes before from comes before where
var sortedNodes = [];
var missingFrom = true;
var hasFrom = false;
@ -257,19 +257,19 @@ Postgres.prototype.visitQuery = function(queryNode) {
}
}
if(!actions.length) {
//if no actions are given, guess it's a select
// if no actions are given, guess it's a select
actions.push(new Select().add('*'));
}
if(missingFrom) {
targets.push(new From().add(queryNode.table));
}
//lazy-man sorting
// lazy-man sorting
sortedNodes = actions.concat(targets).concat(filters);
for(i = 0; i < sortedNodes.length; i++) {
var res = this.visit(sortedNodes[i]);
this.output = this.output.concat(res);
}
//implicit 'from'
// implicit 'from'
return this.output;
};

8
lib/joiner.js

@ -8,7 +8,7 @@ var getPrimaryKeyColumn = function(table) {
};
var findReference = function(left, right) {
//find reference
// find reference
for(var i = 0; i < right.columns.length; i++) {
var col = right.columns[i];
if(col.references) {
@ -25,9 +25,9 @@ var findReference = function(left, right) {
};
module.exports = {
//auto-join two tables based on column properties
//requires one column to have { references: {table: 'foreignTableName', column: 'foreignColumnName'}}
//or to have { references: 'foreignTableName'} -- in which case the foreign table's primary key is assumed
// auto-join two tables based on column properties
// requires one column to have { references: {table: 'foreignTableName', column: 'foreignColumnName'}}
// or to have { references: 'foreignTableName'} -- in which case the foreign table's primary key is assumed
leftJoin: function(left, right) {
var leftCol, rightCol;
var ref = findReference(left, right);

4
lib/node/index.js

@ -3,7 +3,7 @@
var util = require('util');
var assert = require('assert');
/*jshint unused: false */
/* jshint unused: false */
var Node = function(type) {
this.nodes = [];
};
@ -34,7 +34,7 @@ Node.define = function(def) {
var c = function() {
Node.call(this);
};
//allow custom sub-class constructor
// allow custom sub-class constructor
if(def.constructor && def.constructor != {}.constructor) {
c = def.constructor;
}

6
lib/node/query.js

@ -81,14 +81,14 @@ var Query = Node.define({
},
where: function(node) {
if(arguments.length > 1) {
//allow multiple where clause arguments
// allow multiple where clause arguments
var args = sliced(arguments);
for(var i = 0; i < args.length; i++) {
this.where(args[i]);
}
return this;
}
//calling #where twice functions like calling #where & then #and
// calling #where twice functions like calling #where & then #and
if(this.whereClause) return this.and(node);
this.whereClause = new Where(this.table);
this.whereClause.add(node);
@ -122,7 +122,7 @@ var Query = Node.define({
var self = this;
var args = sliced(arguments);
//object literal
// object literal
if(arguments.length == 1 && !o.toNode && !o.forEach) {
args = Object.keys(o).map(function(key) {
return self.table.get(key).value(o[key]);

2
lib/node/where.js

@ -35,7 +35,7 @@ module.exports = Node.define({
},
or: function(other) {
var right = normalizeNode(this.table, other);
//calling 'or' without an initial 'where'
// calling 'or' without an initial 'where'
if(!this.nodes.length) {
return this.add(other);
}

15
lib/table.js

@ -20,7 +20,7 @@ var Table = function(config) {
Table.define = function(config) {
var table = new Table(config);
//allow hash of columns as well as array
// allow hash of columns as well as array
if(config.columns && !util.isArray(config.columns)) {
var cols = [];
@ -110,11 +110,12 @@ Table.prototype.star = function() {
Table.prototype.count = function(alias) {
var name = this.alias || this._name,
col = new Column({table: this, star: true});
return col.count(alias || name + '_count'); //ColumnNode
// ColumnNode
return col.count(alias || name + '_count');
};
Table.prototype.select = function() {
//create the query and pass it off
// create the query and pass it off
var query = new Query(this);
if (arguments.length === 0) {
query.select.call(query, this.star());
@ -131,7 +132,7 @@ Table.prototype.from = function() {
};
Table.prototype.subQuery = function(alias) {
//create the query and pass it off
// create the query and pass it off
var query = new Query(this);
query.type = 'SUBQUERY';
query.alias = alias;
@ -186,19 +187,19 @@ Table.prototype.leftJoin = function(other) {
return new JoinNode('LEFT', this.toNode(), other.toNode());
};
//auto-join tables based on column intropsection
// auto-join tables based on column intropsection
Table.prototype.joinTo = function(other) {
return Joiner.leftJoin(this, other);
};
Table.prototype.as = function(alias) {
//TODO could this be cleaner?
// TODO could this be cleaner?
var t = Table.define(this._initialConfig);
t.alias = alias;
return t;
};
//called in shorthand when not calling select
// called in shorthand when not calling select
Table.prototype.__defineGetter__("nodes", function() {
return this.select(this.star()).nodes;
});

8
test/dialects/clause-ordering-tests.js

@ -4,7 +4,7 @@ var Harness = require('./support');
var user = Harness.defineUserTable();
var post = Harness.definePostTable();
//FROM - SELECT
// FROM - SELECT
Harness.test({
query : user.from(user.join(post).on(user.id.equals(post.userId))).select(user.name, post.content),
pg : 'SELECT "user"."name", "post"."content" FROM "user" INNER JOIN "post" ON ("user"."id" = "post"."userId")',
@ -12,7 +12,7 @@ Harness.test({
mysql : 'SELECT `user`.`name`, `post`.`content` FROM `user` INNER JOIN `post` ON (`user`.`id` = `post`.`userId`)'
});
//WHERE - FROM - SELECT
// WHERE - FROM - SELECT
Harness.test({
query : user.where({name: ''}).from(user).select(user.id),
pg : 'SELECT "user"."id" FROM "user" WHERE ("user"."name" = $1)',
@ -21,7 +21,7 @@ Harness.test({
params: ['']
});
//SELECT - FROM - WHERE
// SELECT - FROM - WHERE
Harness.test({
query : user
.select(user.name, post.content)
@ -33,7 +33,7 @@ Harness.test({
params: ['']
});
//SELECT - FROM - WHERE
// SELECT - FROM - WHERE
Harness.test({
query : user.select(user.id).from(user).where({name: ''}),
pg : 'SELECT "user"."id" FROM "user" WHERE ("user"."name" = $1)',

8
test/dialects/insert-tests.js

@ -28,7 +28,7 @@ Harness.test({
params: ['test', 2]
});
//allow bulk insert
// allow bulk insert
Harness.test({
query : post.insert([{content: 'whoah'}, {content: 'hey'}]),
pg : 'INSERT INTO "post" ("content") VALUES ($1), ($2)',
@ -45,8 +45,7 @@ Harness.test({
params: ['whoah', 1, 'hey', 2]
});
//consistent order
// consistent order
Harness.test({
query : post.insert([{content: 'whoah', userId: 1}, {userId: 2, content: 'hey' }]),
pg : 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2), ($3, $4)',
@ -55,8 +54,7 @@ Harness.test({
params: ['whoah', 1, 'hey', 2]
});
//handle missing columns
// handle missing columns
Harness.test({
query : post.insert([{content: 'whoah', userId: 1}, {content: 'hey'}]),
pg : {

1
test/dialects/namespace-tests.js

@ -36,7 +36,6 @@ Harness.test({
mysql : 'SELECT `p`.`content`, `u`.`name` FROM `user` AS `u` INNER JOIN `post` AS `p` ON ((`u`.`id` = `p`.`userId`) AND (`p`.`content` IS NOT NULL))'
});
// the quote property isn't implemented for columns, so all columns are quoted in generated queries
var comment = Table.define({
name: 'comment',

4
test/dialects/shortcut-tests.js

@ -4,7 +4,7 @@ var Harness = require('./support');
var user = Harness.defineUserTable();
var post = Harness.definePostTable();
//shortcut: 'select * from <table>'
// shortcut: 'select * from <table>'
Harness.test({
query : user,
pg : 'SELECT "user".* FROM "user"',
@ -28,7 +28,7 @@ Harness.test({
params: [3,1]
});
//shortcut: no 'from'
// shortcut: no 'from'
Harness.test({
query : post.select(post.content),
pg : 'SELECT "post"."content" FROM "post"',

1
test/dialects/table-tests.js

@ -118,7 +118,6 @@ Harness.test({
params: ['brian']
});
//Fix #10: prevent column state mutation
Harness.test({
query : user.select(user.name).from(user).where(user.name.equals('brian')),
pg : 'SELECT "user"."name" FROM "user" WHERE ("user"."name" = $1)',

21
test/readme-examples.js

@ -4,8 +4,7 @@ var user = sql.define({
columns: ['id', 'lastLogin', 'email']
});
//this also makes parts of your queries composable, which is handy
// this also makes parts of your queries composable, which is handy
var friendship = sql.define({
name: 'friendship',
columns: ['userId', 'friendId']
@ -15,18 +14,18 @@ var friends = user.as('friends');
var userToFriends = user
.leftJoin(friendship).on(user.id.equals(friendship.userId))
.leftJoin(friends).on(friendship.friendId.equals(friends.id));
//and now...compose...
var friendsWhoHaveLoggedInQuery = user.from(userToFriends).where(friends.lastLogin.isNotNull());
console.log(friendsWhoHaveLoggedInQuery.toQuery().text);
//SELECT * FROM "user"
//LEFT JOIN "friendship" ON ("user"."id" = "friendship"."userId")
//LEFT JOIN "user" AS "friends" ON ("friendship"."friendId" = "friends"."id")
//WHERE "friends"."lastLogin" IS NOT NULL
// SELECT * FROM "user"
// LEFT JOIN "friendship" ON ("user"."id" = "friendship"."userId")
// LEFT JOIN "user" AS "friends" ON ("friendship"."friendId" = "friends"."id")
// WHERE "friends"."lastLogin" IS NOT NULL
var friendsWhoUseGmailQuery = user.from(userToFriends).where(friends.email.like('%@gmail.com'));
console.log(friendsWhoUseGmailQuery.toQuery().text);
//SELECT * FROM "user"
//LEFT JOIN "friendship" ON ("user"."id" = "friendship"."userId")
//LEFT JOIN "user" AS "friends" ON ("friendship"."friendId" = "friends"."id")
//WHERE "friends"."email" LIKE %1
// SELECT * FROM "user"
// LEFT JOIN "friendship" ON ("user"."id" = "friendship"."userId")
// LEFT JOIN "user" AS "friends" ON ("friendship"."friendId" = "friends"."id")
// WHERE "friends"."email" LIKE %1

Loading…
Cancel
Save