File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+
614623Postgres . prototype . visitJoin = function ( join ) {
615624 var result = [ ] ;
616625 result = result . concat ( this . visit ( join . from ) ) ;
Original file line number Diff line number Diff 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+
5765module . exports = Sqlite ;
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ var Node = require ( __dirname ) ;
4+
5+ module . exports = Node . define ( {
6+ type : 'CASCADE'
7+ } ) ;
Original file line number Diff line number Diff line change 33var Node = require ( __dirname ) ;
44
55module . exports = Node . define ( {
6- type : 'DROP'
6+ type : 'DROP' ,
7+
8+ constructor : function ( table ) {
9+ Node . call ( this ) ;
10+ this . add ( table ) ;
11+ }
712} ) ;
Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff line change @@ -27,6 +27,8 @@ var ParameterNode = require('./parameter');
2727var PrefixUnaryNode = require ( './prefixUnary' ) ;
2828var IfExists = require ( './ifExists' ) ;
2929var IfNotExists = require ( './ifNotExists' ) ;
30+ var Cascade = require ( './cascade' ) ;
31+ var Restrict = require ( './restrict' ) ;
3032var Indexes = require ( './indexes' ) ;
3133var CreateIndex = require ( './createIndex' ) ;
3234var 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
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ var Node = require ( __dirname ) ;
4+
5+ module . exports = Node . define ( {
6+ type : 'RESTRICT'
7+ } ) ;
Original file line number Diff line number Diff 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+ } ) ;
You can’t perform that action at this time.
0 commit comments