Skip to content

Commit 86c88af

Browse files
author
Scott Tadman
committed
Adding support for Postgres <@, @>, && ARRAY operations
1 parent a1f19a6 commit 86c88af

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

lib/dialect/postgres.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ Postgres.prototype.visit = function(node) {
142142
case 'CASE' : return this.visitCase(node);
143143
case 'AT' : return this.visitAt(node);
144144
case 'SLICE' : return this.visitSlice(node);
145+
case 'CONTAINS' : return this.visitContains(node);
146+
case 'CONTAINED BY' : return this.visitContainedBy(node);
147+
case 'OVERLAP' : return this.visitOverlap(node);
145148

146149
case 'LIMIT' :
147150
case 'OFFSET':
@@ -399,6 +402,24 @@ Postgres.prototype.visitSlice = function(slice) {
399402
return [text];
400403
};
401404

405+
Postgres.prototype.visitContains = function(contains) {
406+
var text = this.visit(contains.value);
407+
text += ' @> ' + this.visit(contains.set);
408+
return [text];
409+
};
410+
411+
Postgres.prototype.visitContainedBy = function(containedBy) {
412+
var text = this.visit(containedBy.value);
413+
text += ' <@ ' + this.visit(containedBy.set);
414+
return [text];
415+
};
416+
417+
Postgres.prototype.visitOverlap = function(overlap) {
418+
var text = this.visit(overlap.value);
419+
text += ' && ' + this.visit(overlap.set);
420+
return [text];
421+
};
422+
402423
Postgres.prototype.visitQuery = function(queryNode) {
403424
this._queryNode = queryNode;
404425
// need to sort the top level query nodes on visitation priority

lib/node/valueExpression.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ var ValueExpressionMixin = function() {
6161
return new SliceNode(this.toNode(), processParams(start), processParams(end));
6262
};
6363

64+
var containsMethod = function(set) {
65+
return new ContainsNode(this.toNode(), processParams(set));
66+
};
67+
68+
var containedByMethod = function(set) {
69+
return new ContainedByNode(this.toNode(), processParams(set));
70+
};
71+
72+
var overlapMethod = function(set) {
73+
return new OverlapNode(this.toNode(), processParams(set));
74+
};
75+
6476
var castMethod = function(dataType) {
6577
return new CastNode(this.toNode(), dataType);
6678
};
@@ -120,6 +132,9 @@ var ValueExpressionMixin = function() {
120132
notIn : binaryMethod('NOT IN'),
121133
between : ternaryMethod('BETWEEN', 'AND'),
122134
at : atMethod,
135+
contains : binaryMethod('@>'),
136+
containedBy : binaryMethod('<@'),
137+
overlap : binaryMethod('&&'),
123138
slice : sliceMethod,
124139
cast : castMethod,
125140
descending : orderMethod('DESC'),

test/dialects/array-tests.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,39 @@ Harness.test({
1616
params: ['nodejs']
1717
});
1818

19+
Harness.test({
20+
query: post.select(
21+
post.tags.contains(Sql.array('nodejs', 'js'))
22+
),
23+
pg: {
24+
text : 'SELECT ("post"."tags" @> ARRAY[$1, $2]) FROM "post"',
25+
string: 'SELECT ("post"."tags" @> ARRAY[\'nodejs\', \'js\']) FROM "post"'
26+
},
27+
params: ['nodejs', 'js']
28+
});
29+
30+
Harness.test({
31+
query: post.select(
32+
post.tags.containedBy(Sql.array('nodejs', 'js'))
33+
),
34+
pg: {
35+
text : 'SELECT ("post"."tags" <@ ARRAY[$1, $2]) FROM "post"',
36+
string: 'SELECT ("post"."tags" <@ ARRAY[\'nodejs\', \'js\']) FROM "post"'
37+
},
38+
params: ['nodejs', 'js']
39+
});
40+
41+
Harness.test({
42+
query: post.select(
43+
post.tags.overlap(Sql.array('nodejs', 'js'))
44+
),
45+
pg: {
46+
text : 'SELECT ("post"."tags" && ARRAY[$1, $2]) FROM "post"',
47+
string: 'SELECT ("post"."tags" && ARRAY[\'nodejs\', \'js\']) FROM "post"'
48+
},
49+
params: ['nodejs', 'js']
50+
});
51+
1952
Harness.test({
2053
query: post.select(post.tags.slice(2,3)),
2154
pg: {

0 commit comments

Comments
 (0)