Skip to content

Commit 1007235

Browse files
committed
Add support for CASCADE in DROP TABLE.
1 parent dc4b97c commit 1007235

8 files changed

Lines changed: 93 additions & 5 deletions

File tree

lib/dialect/postgres.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ Postgres.prototype.visit = function(node) {
125125
case 'DEFAULT' : return this.visitDefault(node);
126126
case 'IF EXISTS' : return this.visitIfExists();
127127
case 'IF NOT EXISTS' : return this.visitIfNotExists();
128+
case 'CASCADE' : return this.visitCascade();
129+
case 'RESTRICT' : return this.visitRestrict();
128130
case 'RENAME' : return this.visitRename(node);
129131
case 'ADD COLUMN' : return this.visitAddColumn(node);
130132
case 'DROP COLUMN' : return this.visitDropColumn(node);
@@ -248,7 +250,6 @@ Postgres.prototype.visitDrop = function(drop) {
248250
// don't auto-generate from clause
249251
var result = ['DROP TABLE'];
250252
result = result.concat(drop.nodes.map(this.visit.bind(this)));
251-
result.push(this.visit(this._queryNode.table.toNode()));
252253
return result;
253254
};
254255

@@ -611,6 +612,14 @@ Postgres.prototype.visitIfNotExists = function() {
611612
return ['IF NOT EXISTS'];
612613
};
613614

615+
Postgres.prototype.visitCascade = function() {
616+
return ['CASCADE'];
617+
};
618+
619+
Postgres.prototype.visitRestrict = function() {
620+
return ['RESTRICT'];
621+
};
622+
614623
Postgres.prototype.visitJoin = function(join) {
615624
var result = [];
616625
result = result.concat(this.visit(join.from));

lib/dialect/sqlite.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,12 @@ Sqlite.prototype.visitIndexes = function(node) {
5454
return "PRAGMA INDEX_LIST(" + tableName + ")";
5555
};
5656

57+
Sqlite.prototype.visitCascade = function() {
58+
throw new Error('Sqlite do not support CASCADE in DROP TABLE');
59+
};
60+
61+
Sqlite.prototype.visitRestrict = function() {
62+
throw new Error('Sqlite do not support RESTRICT in DROP TABLE');
63+
};
64+
5765
module.exports = Sqlite;

lib/node/cascade.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
var Node = require(__dirname);
4+
5+
module.exports = Node.define({
6+
type: 'CASCADE'
7+
});

lib/node/drop.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33
var Node = require(__dirname);
44

55
module.exports = Node.define({
6-
type: 'DROP'
6+
type: 'DROP',
7+
8+
constructor: function(table) {
9+
Node.call(this);
10+
this.add(table);
11+
}
712
});

lib/node/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ Node.prototype.add = function(node) {
1919
return this;
2020
};
2121

22+
Node.prototype.unshift = function(node) {
23+
assert(node, 'Error while trying to add a non-existant node to a query');
24+
this.nodes.unshift(typeof node === 'string' ? new TextNode(node) : node.toNode());
25+
return this;
26+
};
27+
2228
// Before the change that introduced parallel dialects, every node could be turned
2329
// into a query. The parallel dialects change made it impossible to change some nodes
2430
// into a query because not all nodes are constructed with the sql instance.

lib/node/query.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var ParameterNode = require('./parameter');
2727
var PrefixUnaryNode = require('./prefixUnary');
2828
var IfExists = require('./ifExists');
2929
var IfNotExists = require('./ifNotExists');
30+
var Cascade = require('./cascade');
31+
var Restrict = require('./restrict');
3032
var Indexes = require('./indexes');
3133
var CreateIndex = require('./createIndex');
3234
var DropIndex = require('./dropIndex');
@@ -245,7 +247,7 @@ var Query = Node.define({
245247
this.add(dropIndex);
246248
return dropIndex;
247249
} else {
248-
return this.add(new Drop());
250+
return this.add(new Drop(this.table));
249251
}
250252
},
251253

@@ -338,12 +340,22 @@ var Query = Node.define({
338340
},
339341

340342
ifExists: function() {
341-
this.nodes[0].add(new IfExists());
343+
this.nodes[0].unshift(new IfExists());
342344
return this;
343345
},
344346

345347
ifNotExists: function() {
346-
this.nodes[0].add(new IfNotExists());
348+
this.nodes[0].unshift(new IfNotExists());
349+
return this;
350+
},
351+
352+
cascade: function() {
353+
this.nodes[0].add(new Cascade());
354+
return this;
355+
},
356+
357+
restrict: function() {
358+
this.nodes[0].add(new Restrict());
347359
return this;
348360
},
349361

lib/node/restrict.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
var Node = require(__dirname);
4+
5+
module.exports = Node.define({
6+
type: 'RESTRICT'
7+
});

test/dialects/drop-table-tests.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,37 @@ Harness.test({
3636
},
3737
params: []
3838
});
39+
40+
Harness.test({
41+
query: post.drop().cascade(),
42+
pg: {
43+
text : 'DROP TABLE "post" CASCADE',
44+
string: 'DROP TABLE "post" CASCADE'
45+
},
46+
sqlite: {
47+
text : 'Sqlite do not support CASCADE in DROP TABLE',
48+
throws: true
49+
},
50+
mysql: {
51+
text : 'DROP TABLE `post` CASCADE',
52+
string: 'DROP TABLE `post` CASCADE'
53+
},
54+
params: []
55+
});
56+
57+
Harness.test({
58+
query: post.drop().restrict(),
59+
pg: {
60+
text : 'DROP TABLE "post" RESTRICT',
61+
string: 'DROP TABLE "post" RESTRICT'
62+
},
63+
sqlite: {
64+
text : 'Sqlite do not support RESTRICT in DROP TABLE',
65+
throws: true
66+
},
67+
mysql: {
68+
text : 'DROP TABLE `post` RESTRICT',
69+
string: 'DROP TABLE `post` RESTRICT'
70+
},
71+
params: []
72+
});

0 commit comments

Comments
 (0)