Comparing version 0.1.0 to 0.1.2
@@ -10,2 +10,3 @@ var ColumnNode = require(__dirname + '/node/column'); | ||
this.asc = this.ascending = this; | ||
this.alias = null; | ||
this.desc = this.descending = new BinaryNode({ | ||
@@ -51,2 +52,7 @@ left: this.toNode(), | ||
Column.prototype.as = function(alias) { | ||
this.alias = alias; | ||
return new ColumnNode(this); | ||
} | ||
binaryMethod('equals', '='); | ||
@@ -53,0 +59,0 @@ binaryMethod('equal', '='); |
@@ -31,2 +31,5 @@ var From = require(__dirname + '/../node/from'); | ||
case 'PARAMETER': return this.visitParameter(node); | ||
case 'LIMIT': | ||
case 'OFFSET': | ||
return this.visitModifier(node); | ||
default: throw new Error("Unrecognized node type " + node.type); | ||
@@ -160,3 +163,8 @@ } | ||
} | ||
return txt + this.quote(columnNode.name); | ||
txt += this.quote(columnNode.name); | ||
var inSelectClause = !this._selectOrDeleteEndIndex; | ||
if(inSelectClause && columnNode.alias) { | ||
txt += ' as ' + this.quote(columnNode.alias); | ||
} | ||
return txt; | ||
} | ||
@@ -183,2 +191,6 @@ | ||
Postgres.prototype.visitModifier = function(node) { | ||
return [node.type, node.count]; | ||
} | ||
module.exports = Postgres; |
@@ -7,2 +7,3 @@ var Node = require(__dirname); | ||
this.name = config.name; | ||
this.alias = config.alias; | ||
this.table = config.table; | ||
@@ -9,0 +10,0 @@ this.value = config.getValue(); |
@@ -11,2 +11,10 @@ var Node = require(__dirname); | ||
var Modifier = Node.define({ | ||
constructor: function(table, type, count) { | ||
this.table = table; | ||
this.type = type; | ||
this.count = count; | ||
} | ||
}); | ||
var Query = Node.define({ | ||
@@ -86,2 +94,8 @@ type: 'QUERY', | ||
}, | ||
limit: function(count) { | ||
return this.add(new Modifier(this, 'LIMIT', count)); | ||
}, | ||
offset: function(count) { | ||
return this.add(new Modifier(this, 'OFFSET', count)); | ||
}, | ||
toQuery: function() { | ||
@@ -88,0 +102,0 @@ var Dialect = require(__dirname + '/../').dialect; |
@@ -5,3 +5,3 @@ { | ||
"description": "sql builder", | ||
"version": "0.1.0", | ||
"version": "0.1.2", | ||
"homepage": "https://github.com/brianc/node-sql", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -12,3 +12,3 @@ # node-sql | ||
//first we define our table | ||
//first we define our tables | ||
var user = sql.define({ | ||
@@ -19,15 +19,61 @@ name: 'user', | ||
//now we make a query | ||
var usersWhoLoggedInBeforeToday = user.select(user.id, user.email).from(user).where(user.lastLogin.lt(new Date())); | ||
console.log(usersWhoLoggedInBeforeToday.toQuery()); | ||
// { text: 'SELECT user.'id', user.'email' FROM user WHERE user.'lastLogin' > $1', values: ['2011-01-1'] } | ||
var post = sql.define({ | ||
name: 'post', | ||
columns: ['id', 'userId', 'date', 'title', 'body'] | ||
}) | ||
//now let's make a simple query | ||
var query = user.select(user.star()).from(user).toQuery(); | ||
console.log(query.text); //SELECT "user".* FROM "user" | ||
//something more interesting | ||
var query = user | ||
.select(user.id) | ||
.from(user) | ||
.where( | ||
user.name.equals('boom').and(user.id.equals(1)) | ||
).or( | ||
user.name.equals('bang').and(user.id.equals(2)) | ||
).toQuery(); | ||
//query is parameterized by default | ||
console.log(query.text); //SELECT "user"."id" FROM "user" WHERE ((("user"."name" = $1) AND ("user"."id" = $2)) OR (("user"."name" = $3) AND ("user"."id" = $4))) | ||
console.log(query.values); //['boom', 1, 'bang', 2] | ||
//how about a join? | ||
var query = user.select(user.name, post.content) | ||
.from(user.join(post).on(user.id.equals(post.userId))).toQuery(); | ||
console.log(query.text); //'SELECT "user"."name", "post"."content" FROM "user" INNER JOIN "post" ON ("user"."id" = "post"."userId")' | ||
``` | ||
I know this is a cop-out for now, but for more in-depth examples view `test/dialect-tests.js` | ||
There are a __lot__ more examples under `test/dialect-tests.js` | ||
## help! | ||
## contributing | ||
I need help with a mysql and sqlite syntax. Even if you don't want to contribute code, I could still use some failing tests I can work towards. You see, I don't really know mysql or sqlite syntax very well and don't use either database much. If you'd like to contribute, please message me on github. I'll give you commit access, and we'll be off to the races. | ||
I __love__ contributions. If you want to contribute here's what you do: | ||
1. fork the repo | ||
2. `git pull https://github.com/(your_username)/node-sql` | ||
3. `cd node-sql` | ||
4. `npm install` | ||
5. `npm test` | ||
At this point the tests should pass for you. If they don't pass please open an issue with the output or you can even send me an email directly. My email address is on my github profile and also on every commit I contributed in the repo. | ||
Once the tests are passing, modify as you see fit. _Please_ make sure you write tests to cover your modifications. Once you're ready, commit your changes and submit a pull request. | ||
__As long as your pull request doesn't have completely off-the-wall changes and it does have tests I will almost always merge it right away and push it to npm__ | ||
If you think your changes are too off-the-wall, open an issue or a pull-request without code so we can discuss them. | ||
__Seriously:__ | ||
your contributions and ideas >= my contributions and ideas. | ||
I definitely need help with mysql and sqlite syntax. I'm not very familiar...so that's always a good place to start. | ||
##license | ||
MIT |
@@ -71,6 +71,14 @@ var tap = require('tap').test; | ||
test({ | ||
query : user.select(user.id).from(user) | ||
.where(user.name.equals('boom') | ||
.and(user.id.equals(1))).or(user.name.equals('bang').and(user.id.equals(2))), | ||
pg : 'SELECT "user"."id" FROM "user" WHERE ((("user"."name" = $1) AND ("user"."id" = $2)) OR (("user"."name" = $3) AND ("user"."id" = $4)))' | ||
query : user | ||
.select(user.id) | ||
.from(user) | ||
.where( | ||
user.name.equals('boom') | ||
.and(user.id.equals(1)) | ||
).or( | ||
user.name.equals('bang').and(user.id.equals(2)) | ||
), | ||
pg : 'SELECT "user"."id" FROM "user" WHERE ((("user"."name" = $1) AND ("user"."id" = $2)) OR (("user"."name" = $3) AND ("user"."id" = $4)))', | ||
params: ['boom', 1, 'bang', 2] | ||
}); | ||
@@ -88,2 +96,12 @@ | ||
test({ | ||
query : user.select(user.name.as('user name'), user.id.as('user id')).from(user), | ||
pg : 'SELECT "user"."name" as "user name", "user"."id" as "user id" FROM "user"' | ||
}); | ||
test({ | ||
query : user.select(user.name.as('user name')).from(user).where(user.name.equals('brian')), | ||
pg : 'SELECT "user"."name" as "user name" FROM "user" WHERE ("user"."name" = $1)' | ||
}); | ||
var u = user.as('u'); | ||
@@ -121,3 +139,2 @@ test({ | ||
test({ | ||
@@ -182,2 +199,3 @@ query : post.select(post.content).order(post.content), | ||
var ignore = function() { | ||
@@ -245,1 +263,17 @@ var parent = post.select(post.content); | ||
}); | ||
//limit and offset | ||
test({ | ||
query : user.select(user.star()).from(user).order(user.name.asc).limit(1), | ||
pg : 'SELECT "user".* FROM "user" ORDER BY "user"."name" LIMIT 1' | ||
}); | ||
test({ | ||
query : user.select(user.star()).from(user).order(user.name.asc).limit(3).offset(6), | ||
pg : 'SELECT "user".* FROM "user" ORDER BY "user"."name" LIMIT 3 OFFSET 6' | ||
}); | ||
test({ | ||
query : user.select(user.star()).from(user).order(user.name.asc).offset(10), | ||
pg : 'SELECT "user".* FROM "user" ORDER BY "user"."name" OFFSET 10' | ||
}); |
Sorry, the diff of this file is not supported yet
31119
917
78