Skip to content

Commit 5329b03

Browse files
committed
Split Unary into PrefixUnary and PostfixUnary
Separate unary nodes into separate prefix- and postfix- unary nodes in preparation for adding prefix unary operators to the supported grammar. Signed-off-by: Kevin Locke <klocke@quantpost.com>
1 parent 0c3649c commit 5329b03

4 files changed

Lines changed: 46 additions & 12 deletions

File tree

lib/dialect/postgres.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ Postgres.prototype.visit = function(node) {
130130
case 'DROP INDEX' : return this.visitDropIndex(node);
131131
case 'FUNCTION CALL' : return this.visitFunctionCall(node);
132132

133-
case 'UNARY' : return this.visitUnary(node);
133+
case 'POSTFIX UNARY' : return this.visitPostfixUnary(node);
134+
case 'PREFIX UNARY' : return this.visitPrefixUnary(node);
134135
case 'BINARY' : return this.visitBinary(node);
135136
case 'TERNARY' : return this.visitTernary(node);
136137

@@ -295,7 +296,12 @@ Postgres.prototype.visitHaving = function(having) {
295296
return result;
296297
};
297298

298-
Postgres.prototype.visitUnary = function(unary) {
299+
Postgres.prototype.visitPrefixUnary = function(unary) {
300+
var text = '(' + unary.operator + ' ' + this.visit(unary.left) + ')';
301+
return [text];
302+
};
303+
304+
Postgres.prototype.visitPostfixUnary = function(unary) {
299305
var text = '(' + this.visit(unary.left) + ' ' + unary.operator + ')';
300306
return [text];
301307
};

lib/node/postfixUnary.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
var _ = require('lodash');
4+
var Node = require(__dirname);
5+
var valueExpressionMixin = require(__dirname + '/valueExpression');
6+
7+
var valueExpressionMixed = false;
8+
var PostfixUnaryNode = Node.define({
9+
type: 'POSTFIX UNARY',
10+
constructor: function(config) {
11+
Node.call(this);
12+
this.left = config.left;
13+
this.operator = config.operator;
14+
15+
// Delay mixin to runtime, when all nodes have been defined, and
16+
// mixin only once. ValueExpressionMixin has circular dependencies.
17+
if (!valueExpressionMixed) {
18+
valueExpressionMixed = true;
19+
_.extend(PostfixUnaryNode.prototype, valueExpressionMixin());
20+
}
21+
}
22+
});
23+
24+
// allow aliasing
25+
var AliasNode = require(__dirname + '/alias');
26+
_.extend(PostfixUnaryNode.prototype, AliasNode.AliasMixin);
27+
28+
module.exports = PostfixUnaryNode;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ var Node = require(__dirname);
55
var valueExpressionMixin = require(__dirname + '/valueExpression');
66

77
var valueExpressionMixed = false;
8-
var UnaryNode = Node.define({
9-
type: 'UNARY',
8+
var PrefixUnaryNode = Node.define({
9+
type: 'PREFIX UNARY',
1010
constructor: function(config) {
1111
Node.call(this);
1212
this.left = config.left;
@@ -16,13 +16,13 @@ var UnaryNode = Node.define({
1616
// mixin only once. ValueExpressionMixin has circular dependencies.
1717
if (!valueExpressionMixed) {
1818
valueExpressionMixed = true;
19-
_.extend(UnaryNode.prototype, valueExpressionMixin());
19+
_.extend(PrefixUnaryNode.prototype, valueExpressionMixin());
2020
}
2121
}
2222
});
2323

2424
// allow aliasing
2525
var AliasNode = require(__dirname + '/alias');
26-
_.extend(UnaryNode.prototype, AliasNode.AliasMixin);
26+
_.extend(PrefixUnaryNode.prototype, AliasNode.AliasMixin);
2727

28-
module.exports = UnaryNode;
28+
module.exports = PrefixUnaryNode;

lib/node/valueExpression.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ var processParams = function(val) {
1313
// ValueExpressionMixin is evaluated at runtime, hence the
1414
// "thunk" around it.
1515
var ValueExpressionMixin = module.exports = function() {
16-
var UnaryNode = require(__dirname + '/unary');
16+
var PostfixUnaryNode = require(__dirname + '/postfixUnary');
1717
var BinaryNode = require(__dirname + '/binary');
1818
var TernaryNode = require(__dirname + '/ternary');
1919

20-
var unaryMethod = function(operator) {
20+
var postfixUnaryMethod = function(operator) {
2121
/*jshint unused: false */
2222
return function(val) {
23-
return new UnaryNode({
23+
return new PostfixUnaryNode({
2424
left: this.toNode(),
2525
operator: operator
2626
});
@@ -50,8 +50,8 @@ var ValueExpressionMixin = module.exports = function() {
5050
};
5151

5252
return {
53-
isNull : unaryMethod('IS NULL'),
54-
isNotNull : unaryMethod('IS NOT NULL'),
53+
isNull : postfixUnaryMethod('IS NULL'),
54+
isNotNull : postfixUnaryMethod('IS NOT NULL'),
5555
or : binaryMethod('OR'),
5656
and : binaryMethod('AND'),
5757
equals : binaryMethod('='),

0 commit comments

Comments
 (0)