@@ -71,10 +71,20 @@ Postgres.prototype.getQuery = function(queryNode) {
7171 queryNode = queryNode . select ( queryNode . star ( ) ) ;
7272 }
7373 this . output = this . visit ( queryNode ) ;
74-
74+
75+ //if is a create view, must replace paramaters with values
76+ if ( this . output . indexOf ( 'CREATE VIEW' ) > - 1 ) {
77+ var previousFlagStatus = this . _disableParameterPlaceholders ;
78+ this . _disableParameterPlaceholders = true ;
79+ this . output = [ ] ;
80+ this . output = this . visit ( queryNode ) ;
81+ this . params = [ ] ;
82+ this . _disableParameterPlaceholders = previousFlagStatus ;
83+ }
84+
7585 // create the query object
7686 var query = { text : this . output . join ( ' ' ) , values : this . params } ;
77-
87+
7888 // reset the internal state of this builder
7989 this . output = [ ] ;
8090 this . params = [ ] ;
@@ -143,7 +153,8 @@ Postgres.prototype.visit = function(node) {
143153 case 'DROP INDEX' : return this . visitDropIndex ( node ) ;
144154 case 'FUNCTION CALL' : return this . visitFunctionCall ( node ) ;
145155 case 'ARRAY CALL' : return this . visitArrayCall ( node ) ;
146-
156+ case 'CREATE VIEW' : return this . visitCreateView ( node ) ;
157+
147158 case 'POSTFIX UNARY' : return this . visitPostfixUnary ( node ) ;
148159 case 'PREFIX UNARY' : return this . visitPrefixUnary ( node ) ;
149160 case 'BINARY' : return this . visitBinary ( node ) ;
@@ -568,13 +579,16 @@ Postgres.prototype.visitQuery = function(queryNode) {
568579 // so select/insert/update/delete comes before from comes before where
569580 var missingFrom = true ;
570581 var hasFrom = false ;
582+ var createView ;
583+ var isSelect = false ;
571584 var actions = [ ] ;
572585 var targets = [ ] ;
573586 var filters = [ ] ;
574587 for ( var i = 0 ; i < queryNode . nodes . length ; i ++ ) {
575588 var node = queryNode . nodes [ i ] ;
576589 switch ( node . type ) {
577590 case "SELECT" :
591+ isSelect = true ;
578592 case "DELETE" :
579593 actions . push ( node ) ;
580594 break ;
@@ -594,6 +608,9 @@ Postgres.prototype.visitQuery = function(queryNode) {
594608 missingFrom = false ;
595609 targets . push ( node ) ;
596610 break ;
611+ case "CREATE VIEW" :
612+ createView = node ;
613+ break ;
597614 default :
598615 filters . push ( node ) ;
599616 break ;
@@ -602,10 +619,18 @@ Postgres.prototype.visitQuery = function(queryNode) {
602619 if ( ! actions . length ) {
603620 // if no actions are given, guess it's a select
604621 actions . push ( new Select ( ) . add ( '*' ) ) ;
622+ isSelect = true ;
605623 }
606624 if ( missingFrom ) {
607625 targets . push ( new From ( ) . add ( queryNode . table ) ) ;
608626 }
627+ if ( createView ) {
628+ if ( isSelect ) {
629+ actions . unshift ( createView ) ;
630+ } else {
631+ throw new Error ( 'Create View requires a Select.' ) ;
632+ }
633+ }
609634 return this . visitQueryHelper ( actions , targets , filters )
610635} ;
611636
@@ -947,6 +972,12 @@ Postgres.prototype.visitDropIndex = function(node) {
947972 return result ;
948973} ;
949974
975+ Postgres . prototype . visitCreateView = function ( createView ) {
976+ //console.log('createView: ' + createView);
977+ var result = [ 'CREATE VIEW' , this . quote ( createView . options . viewName ) , 'AS' ] ;
978+ return result ;
979+ } ;
980+
950981/**
951982 * Broken out as a separate function so that dialects that derive from this class can still use this functionality.
952983 *
0 commit comments