Skip to content

Commit b67aecc

Browse files
committed
basic INSERT working
1 parent a34d43f commit b67aecc

6 files changed

Lines changed: 27 additions & 5 deletions

File tree

lib/column.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ var unaryMethod = function(name, operator) {
3939
}
4040
}
4141

42-
Column.prototype.value = function() {
43-
this.value = value;
42+
Column.prototype.value = function(value) {
43+
this._value = value;
4444
return this;
4545
}
4646

47+
Column.prototype.getValue = function() {
48+
return this._value;
49+
}
50+
4751
Column.prototype.toNode = function() {
4852
//creates a query node from this column
4953
return new ColumnNode(this);

lib/dialect/postgres.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var From = require(__dirname + '/../node/from');
2+
var Parameter = require(__dirname + '/../node/parameter');
23
var Postgres = function() {
34
this.output = [];
45
this.params = [];
@@ -13,6 +14,7 @@ Postgres.prototype.visit = function(node) {
1314
switch(node.type) {
1415
case 'QUERY': return this.visitQuery(node);
1516
case 'SELECT': return this.visitSelect(node);
17+
case 'INSERT': return this.visitInsert(node);
1618
case 'FROM': return this.visitFrom(node);
1719
case 'WHERE': return this.visitWhere(node);
1820
case 'ORDER BY': return this.visitOrderBy(node);
@@ -37,6 +39,21 @@ Postgres.prototype.visitSelect = function(select) {
3739
return result;
3840
}
3941

42+
Postgres.prototype.visitInsert = function(insert) {
43+
var self = this;
44+
this._visitedFrom = true;
45+
var paramNodes = insert.nodes.map(function(node) {
46+
return self.visit(new Parameter(node.value));
47+
}).join(', ');
48+
var result = [
49+
'INSERT INTO',
50+
this.visit(this._queryNode.table.toNode()),
51+
'(' + insert.nodes.map(this.visit.bind(this)).join(', ') + ')',
52+
'VALUES', '(' + paramNodes + ')'
53+
];
54+
return result;
55+
}
56+
4057
Postgres.prototype.visitFrom = function(from) {
4158
this._visitedFrom = true;
4259
var result = [];

lib/node/column.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var Column = Node.define({
66
this.name = config.name;
77
this.table = config.table;
88
this.quote = config.quote;
9+
this.value = config.getValue();
910
}
1011
});
1112

lib/node/insert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var Node = require(__dirname);
22

33
var Insert = Node.define({
4-
type: 'ORDER BY'
4+
type: 'INSERT'
55
});
66

77
module.exports = Insert;

lib/table.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Table.prototype.select = function() {
5050
}
5151

5252
Table.prototype.insert = function() {
53-
var insert = new Query(this);
53+
var query = new Query(this);
5454
query.insert.apply(query, arguments);
5555
return query;
5656
}

test/dialect-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ console.log('insert');
136136

137137
test({
138138
query : post.insert(post.content.value('test'), post.userId.value(1)),
139-
pg : 'INSERT INTO post(post.content, post."userId") VALUES($1, $2)',
139+
pg : 'INSERT INTO post (post.content, post."userId") VALUES ($1, $2)',
140140
params: ['test', 1]
141141
});
142142

0 commit comments

Comments
 (0)