33var util = require ( 'util' ) ;
44var assert = require ( 'assert' ) ;
55var From = require ( __dirname + '/../node/from' ) ;
6+ var Select = require ( __dirname + '/../node/select' ) ;
67var Parameter = require ( __dirname + '/../node/parameter' ) ;
78var Postgres = function ( ) {
89 this . output = [ ] ;
@@ -70,7 +71,6 @@ Postgres.prototype.visitSelect = function(select) {
7071
7172Postgres . prototype . visitInsert = function ( insert ) {
7273 var self = this ;
73- this . _visitedFrom = true ;
7474 //don't use table.column for inserts
7575 this . _visitedInsert = true ;
7676
@@ -98,7 +98,6 @@ Postgres.prototype.visitInsert = function(insert) {
9898
9999Postgres . prototype . visitUpdate = function ( update ) {
100100 //don't auto-generate from clause
101- this . _visitedFrom = true ;
102101 var params = [ ] ;
103102 /*jshint boss: true */
104103 for ( var i = 0 , node ; node = update . nodes [ i ] ; i ++ ) {
@@ -124,7 +123,6 @@ Postgres.prototype.visitDelete = function() {
124123Postgres . prototype . visitCreate = function ( create ) {
125124 this . _visitingCreate = true ;
126125 //don't auto-generate from clause
127- this . _visitedFrom = true ;
128126 var table = this . _queryNode . table ;
129127 var col_nodes = table . columns . map ( function ( col ) { return col . toNode ( ) ; } ) ;
130128
@@ -138,7 +136,6 @@ Postgres.prototype.visitCreate = function(create) {
138136
139137Postgres . prototype . visitDrop = function ( drop ) {
140138 //don't auto-generate from clause
141- this . _visitedFrom = true ;
142139 var result = [ 'DROP TABLE' ] ;
143140 result = result . concat ( drop . nodes . map ( this . visit . bind ( this ) ) ) ;
144141 result . push ( this . visit ( this . _queryNode . table . toNode ( ) ) ) ;
@@ -148,7 +145,6 @@ Postgres.prototype.visitDrop = function(drop) {
148145Postgres . prototype . visitAlter = function ( alter ) {
149146 this . _visitingAlter = true ;
150147 //don't auto-generate from clause
151- this . _visitedFrom = true ;
152148 var table = this . _queryNode . table ;
153149 var col_nodes = table . columns . map ( function ( col ) { return col . toNode ( ) ; } ) ;
154150 var result = [
@@ -161,7 +157,6 @@ Postgres.prototype.visitAlter = function(alter) {
161157} ;
162158
163159Postgres . prototype . visitFrom = function ( from ) {
164- this . _visitedFrom = true ;
165160 var result = [ ] ;
166161 result . push ( 'FROM' ) ;
167162 for ( var i = 0 ; i < from . nodes . length ; i ++ ) {
@@ -214,17 +209,53 @@ Postgres.prototype.visitUnary = function(unary) {
214209
215210Postgres . prototype . visitQuery = function ( queryNode ) {
216211 this . _queryNode = queryNode ;
217- for ( var i = 0 ; i < queryNode . nodes . length ; i ++ ) {
218- var res = this . visit ( queryNode . nodes [ i ] ) ;
212+ //need to sort the top level query nodes on visitation priority
213+ //so select/insert/update/delete comes before from comes before where
214+ var sortedNodes = [ ] ;
215+ var missingFrom = true ;
216+ var actions = [ ] ;
217+ var targets = [ ] ;
218+ var filters = [ ] ;
219+ for ( var i = 0 ; i < queryNode . nodes . length ; i ++ ) {
220+ var node = queryNode . nodes [ i ] ;
221+ switch ( node . type ) {
222+ case "SELECT" :
223+ case "DELETE" :
224+ actions . push ( node ) ;
225+ break ;
226+ case "INSERT" :
227+ case "UPDATE" :
228+ case "CREATE" :
229+ case "DROP" :
230+ case "ALTER" :
231+ actions . push ( node ) ;
232+ missingFrom = false ;
233+ break ;
234+ case "FROM" :
235+ missingFrom = false ;
236+ targets . push ( node ) ;
237+ break ;
238+ default :
239+ filters . push ( node ) ;
240+ break ;
241+ }
242+ }
243+ if ( ! actions . length ) {
244+ console . log ( 'missing select' )
245+ //if no actions are given, guess it's a select
246+ actions . push ( new Select ( ) . add ( '*' ) ) ;
247+ }
248+ if ( missingFrom ) {
249+ console . log ( 'missing from' )
250+ targets . push ( new From ( ) . add ( queryNode . table ) ) ;
251+ }
252+ //lazy-man sorting
253+ var sortedNodes = actions . concat ( targets ) . concat ( filters ) ;
254+ for ( var i = 0 ; i < sortedNodes . length ; i ++ ) {
255+ var res = this . visit ( sortedNodes [ i ] ) ;
219256 this . output = this . output . concat ( res ) ;
220257 }
221258 //implicit 'from'
222- if ( ! this . _visitedFrom ) {
223- var select = this . output . slice ( 0 , this . _selectOrDeleteEndIndex ) ;
224- var from = this . visitFrom ( new From ( ) . add ( queryNode . table . toNode ( ) ) ) ;
225- var rest = this . output . slice ( this . _selectOrDeleteEndIndex ) ;
226- this . output = select . concat ( from ) . concat ( rest ) ;
227- }
228259 return this ;
229260} ;
230261
0 commit comments