Brian C
12 years ago
24 changed files with 207 additions and 68 deletions
@ -1,8 +1,10 @@ |
|||||
'use strict'; |
'use strict'; |
||||
|
|
||||
module.exports = require(__dirname).define({ |
var Node = require(__dirname); |
||||
|
module.exports = Node.define({ |
||||
type: 'TABLE', |
type: 'TABLE', |
||||
constructor: function(table) { |
constructor: function(table) { |
||||
|
Node.call(this); |
||||
this.table = table; |
this.table = table; |
||||
} |
} |
||||
}); |
}); |
||||
|
@ -1,24 +1,54 @@ |
|||||
'use strict'; |
'use strict'; |
||||
|
|
||||
|
var Node = require(__dirname); |
||||
|
var Column = require(__dirname + '/../column'); |
||||
var BinaryNode = require(__dirname + '/binary'); |
var BinaryNode = require(__dirname + '/binary'); |
||||
var TextNode = require(__dirname + '/text'); |
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', |
type: 'WHERE', |
||||
expr: function(other) { |
add: function(node) { |
||||
return typeof other === 'string' ? new TextNode('('+other+')') : other; |
var node = normalizeNode(this.table, node); |
||||
|
return Node.prototype.add.call(this, node); |
||||
}, |
}, |
||||
or: function(other) { |
or: function(other) { |
||||
|
var right = normalizeNode(this.table, other); |
||||
return this.nodes.push(new BinaryNode({ |
return this.nodes.push(new BinaryNode({ |
||||
left: this.nodes.pop(), |
left: this.nodes.pop(), |
||||
operator: 'OR', |
operator: 'OR', |
||||
right: this.expr(other) |
right: right |
||||
})); |
})); |
||||
}, |
}, |
||||
and: function(other) { |
and: function(other) { |
||||
|
var right = normalizeNode(this.table, other); |
||||
return this.nodes.push(new BinaryNode({ |
return this.nodes.push(new BinaryNode({ |
||||
left: this.nodes.pop(), |
left: this.nodes.pop(), |
||||
operator: 'AND', |
operator: 'AND', |
||||
right: this.expr(other) |
right: right |
||||
})); |
})); |
||||
} |
} |
||||
}); |
}); |
||||
|
Loading…
Reference in new issue