Brian C
12 years ago
24 changed files with 207 additions and 68 deletions
@ -1,8 +1,10 @@ |
|||
'use strict'; |
|||
|
|||
module.exports = require(__dirname).define({ |
|||
var Node = require(__dirname); |
|||
module.exports = Node.define({ |
|||
type: 'TABLE', |
|||
constructor: function(table) { |
|||
Node.call(this); |
|||
this.table = table; |
|||
} |
|||
}); |
|||
|
@ -1,24 +1,54 @@ |
|||
'use strict'; |
|||
|
|||
var Node = require(__dirname); |
|||
var Column = require(__dirname + '/../column'); |
|||
var BinaryNode = require(__dirname + '/binary'); |
|||
var TextNode = require(__dirname + '/text'); |
|||
module.exports = require(__dirname).define({ |
|||
|
|||
var normalizeNode = function(table, node) { |
|||
var result = node; |
|||
if(typeof node == 'string') { |
|||
result = new TextNode('(' + node + ')'); |
|||
} |
|||
else if (!node.toNode && typeof node == 'object'){ |
|||
result = false; |
|||
for (var colName in node) { |
|||
var column = new Column({name: colName, table: table}); |
|||
var query = column.equals(node[colName]); |
|||
if (!result) |
|||
result = query; |
|||
else |
|||
result.and(query); |
|||
} |
|||
} |
|||
return result; |
|||
|
|||
}; |
|||
|
|||
module.exports = Node.define({ |
|||
constructor: function(table) { |
|||
Node.call(this); |
|||
this.table = table; |
|||
}, |
|||
type: 'WHERE', |
|||
expr: function(other) { |
|||
return typeof other === 'string' ? new TextNode('('+other+')') : other; |
|||
add: function(node) { |
|||
var node = normalizeNode(this.table, node); |
|||
return Node.prototype.add.call(this, node); |
|||
}, |
|||
or: function(other) { |
|||
var right = normalizeNode(this.table, other); |
|||
return this.nodes.push(new BinaryNode({ |
|||
left: this.nodes.pop(), |
|||
operator: 'OR', |
|||
right: this.expr(other) |
|||
right: right |
|||
})); |
|||
}, |
|||
and: function(other) { |
|||
var right = normalizeNode(this.table, other); |
|||
return this.nodes.push(new BinaryNode({ |
|||
left: this.nodes.pop(), |
|||
operator: 'AND', |
|||
right: this.expr(other) |
|||
right: right |
|||
})); |
|||
} |
|||
}); |
|||
|
Loading…
Reference in new issue