@@ -57,7 +57,6 @@ pub struct Session {
5757
5858 settings : Settings ,
5959 query : String ,
60- in_comment_block : bool ,
6160
6261 keywords : Arc < Vec < String > > ,
6362}
@@ -108,7 +107,6 @@ impl Session {
108107 is_repl,
109108 settings,
110109 query : String :: new ( ) ,
111- in_comment_block : false ,
112110 keywords : Arc :: new ( keywords) ,
113111 } )
114112 }
@@ -316,7 +314,6 @@ impl Session {
316314 }
317315
318316 pub fn append_query ( & mut self , line : & str ) -> Vec < String > {
319- let line = line. trim ( ) ;
320317 if line. is_empty ( ) {
321318 return vec ! [ ] ;
322319 }
@@ -338,63 +335,56 @@ impl Session {
338335 }
339336 }
340337
341- self . query . push ( ' ' ) ;
342-
338+ // consume self.query and get the result
343339 let mut queries = Vec :: new ( ) ;
344- let mut tokenizer = Tokenizer :: new ( line) ;
345- let mut in_comment = false ;
346- let mut start = 0 ;
347- let mut comment_block_start = 0 ;
348-
349- while let Some ( Ok ( token) ) = tokenizer. next ( ) {
350- match token. kind {
351- TokenKind :: SemiColon => {
352- if in_comment || self . in_comment_block {
353- continue ;
354- } else {
355- let mut sql = self . query . trim ( ) . to_owned ( ) ;
356- if sql. is_empty ( ) {
340+
341+ if !self . query . is_empty ( ) {
342+ self . query . push ( '\n' ) ;
343+ }
344+ self . query . push_str ( line) ;
345+
346+ ' Parser : loop {
347+ let mut tokenizer = Tokenizer :: new ( & self . query ) ;
348+
349+ let mut in_comment = false ;
350+ let mut in_comment_block = false ;
351+
352+ while let Some ( Ok ( token) ) = tokenizer. next ( ) {
353+ match token. kind {
354+ TokenKind :: SemiColon => {
355+ if in_comment_block || in_comment {
357356 continue ;
358357 }
359- sql. push ( ';' ) ;
360358
361- queries. push ( sql) ;
362- self . query . clear ( ) ;
359+ // push to current and continue the tokenizer
360+ let ( sql, remain) = self . query . split_at ( token. span . end ) ;
361+ if !sql. is_empty ( ) {
362+ queries. push ( sql. to_string ( ) ) ;
363+ }
364+ self . query = remain. to_string ( ) ;
365+ continue ' Parser ;
363366 }
364- }
365- TokenKind :: Comment => {
366- in_comment = true ;
367- }
368- TokenKind :: EOI => {
369- in_comment = false ;
370- }
371- TokenKind :: Newline => {
372- in_comment = false ;
373- self . query . push ( '\n' ) ;
374- }
375- TokenKind :: CommentBlockStart => {
376- if !self . in_comment_block {
377- comment_block_start = token. span . start ;
367+ TokenKind :: Comment => {
368+ if in_comment_block {
369+ continue ;
370+ }
371+ in_comment = true ;
378372 }
379- self . in_comment_block = true ;
380- }
381- TokenKind :: CommentBlockEnd => {
382- self . in_comment_block = false ;
383- self . query
384- . push_str ( & line[ comment_block_start..token. span . end ] ) ;
385- }
386- _ => {
387- if !in_comment && !self . in_comment_block {
388- self . query . push_str ( & line[ start..token. span . end ] ) ;
373+ TokenKind :: Newline => {
374+ in_comment = false ;
375+ }
376+ TokenKind :: CommentBlockStart => {
377+ in_comment_block = true ;
378+ }
379+ TokenKind :: CommentBlockEnd => {
380+ in_comment_block = false ;
389381 }
382+ _ => { }
390383 }
391384 }
392- start = token . span . end ;
385+ break ;
393386 }
394387
395- if self . in_comment_block {
396- self . query . push_str ( & line[ comment_block_start..] ) ;
397- }
398388 queries
399389 }
400390
0 commit comments