@@ -72,6 +72,7 @@ const (
7272type query struct {
7373 firstWord string
7474 Query string
75+ delimiter string
7576 Line int
7677 tp int
7778}
@@ -148,6 +149,9 @@ type tester struct {
148149
149150 // replace output result through --replace_regex /\.dll/.so/
150151 replaceRegex []* ReplaceRegex
152+
153+ // the delimter for TiDB, default value is ";"
154+ delimiter string
151155}
152156
153157func newTester (name string ) * tester {
@@ -161,6 +165,7 @@ func newTester(name string) *tester {
161165 t .enableWarning = false
162166 t .enableConcurrent = false
163167 t .enableInfo = false
168+ t .delimiter = ";"
164169
165170 return t
166171}
@@ -462,10 +467,7 @@ func (t *tester) Run() error {
462467 t .replaceColumn = append (t .replaceColumn , ReplaceColumn {col : colNr , replace : []byte (cols [i + 1 ])})
463468 }
464469 case Q_CONNECT :
465- q .Query = strings .TrimSpace (q .Query )
466- if q .Query [len (q .Query )- 1 ] == ';' {
467- q .Query = q .Query [:len (q .Query )- 1 ]
468- }
470+ q .Query = strings .TrimSuffix (strings .TrimSpace (q .Query ), q .delimiter )
469471 q .Query = q .Query [1 : len (q .Query )- 1 ]
470472 args := strings .Split (q .Query , "," )
471473 for i := range args {
@@ -476,16 +478,10 @@ func (t *tester) Run() error {
476478 }
477479 t .addConnection (args [0 ], args [1 ], args [2 ], args [3 ], args [4 ])
478480 case Q_CONNECTION :
479- q .Query = strings .TrimSpace (q .Query )
480- if q .Query [len (q .Query )- 1 ] == ';' {
481- q .Query = q .Query [:len (q .Query )- 1 ]
482- }
481+ q .Query = strings .TrimSuffix (strings .TrimSpace (q .Query ), q .delimiter )
483482 t .switchConnection (q .Query )
484483 case Q_DISCONNECT :
485- q .Query = strings .TrimSpace (q .Query )
486- if q .Query [len (q .Query )- 1 ] == ';' {
487- q .Query = q .Query [:len (q .Query )- 1 ]
488- }
484+ q .Query = strings .TrimSuffix (strings .TrimSpace (q .Query ), q .delimiter )
489485 t .disconnect (q .Query )
490486 case Q_LET :
491487 q .Query = strings .TrimSpace (q .Query )
@@ -622,7 +618,7 @@ func (t *tester) concurrentExecute(querys []query, wg *sync.WaitGroup, errOccure
622618 return
623619 }
624620
625- err := tt .stmtExecute (query . Query )
621+ err := tt .stmtExecute (query )
626622 if err != nil && len (t .expectedErrs ) > 0 {
627623 for _ , tStr := range t .expectedErrs {
628624 if strings .Contains (err .Error (), tStr ) {
@@ -650,43 +646,90 @@ func (t *tester) loadQueries() ([]query, error) {
650646
651647 seps := bytes .Split (data , []byte ("\n " ))
652648 queries := make ([]query , 0 , len (seps ))
653- newStmt := true
649+ buffer := ""
654650 for i , v := range seps {
655651 v := bytes .TrimSpace (v )
656652 s := string (v )
657653 // we will skip # comment here
658654 if strings .HasPrefix (s , "#" ) {
659- newStmt = true
655+ if len (buffer ) != 0 {
656+ return nil , errors .Errorf ("Has remained message(%s) before COMMENTS" , buffer )
657+ }
660658 continue
661659 } else if strings .HasPrefix (s , "--" ) {
662- queries = append (queries , query {Query : s , Line : i + 1 })
663- newStmt = true
660+ if len (buffer ) != 0 {
661+ return nil , errors .Errorf ("Has remained message(%s) before COMMANDS" , buffer )
662+ }
663+ q , err := ParseQuery (query {Query : s , Line : i + 1 , delimiter : t .delimiter })
664+ if err != nil {
665+ return nil , err
666+ }
667+ if q == nil {
668+ continue
669+ }
670+ if q .tp == Q_DELIMITER {
671+ tokens := strings .Split (strings .TrimSpace (q .Query ), " " )
672+ if len (tokens ) == 0 {
673+ return nil , errors .Errorf ("DELIMITER must be followed by a 'delimiter' character or string" )
674+ }
675+ t .delimiter = tokens [0 ]
676+ } else {
677+ queries = append (queries , * q )
678+ }
679+ continue
680+ } else if strings .HasPrefix (strings .ToLower (strings .TrimSpace (s )), "delimiter " ) {
681+ if len (buffer ) != 0 {
682+ return nil , errors .Errorf ("Has remained message(%s) before DELIMITER COMMAND" , buffer )
683+ }
684+ tokens := strings .Split (strings .TrimSpace (s ), " " )
685+ if len (tokens ) <= 1 {
686+ return nil , errors .Errorf ("DELIMITER must be followed by a 'delimiter' character or string" )
687+ }
688+ t .delimiter = tokens [1 ]
664689 continue
665690 } else if len (s ) == 0 {
666691 continue
667692 }
668693
669- if newStmt {
670- queries = append (queries , query {Query : s , Line : i + 1 })
671- } else {
672- lastQuery := queries [len (queries )- 1 ]
673- lastQuery = query {Query : fmt .Sprintf ("%s\n %s" , lastQuery .Query , s ), Line : lastQuery .Line }
674- queries [len (queries )- 1 ] = lastQuery
694+ if len (buffer ) != 0 {
695+ buffer += "\n "
675696 }
697+ buffer += s
698+ for {
699+ idx := strings .LastIndex (buffer , t .delimiter )
700+ if idx == - 1 {
701+ break
702+ }
676703
677- // if the line has a ; in the end, we will treat new line as the new statement.
678- newStmt = strings .HasSuffix (s , ";" )
704+ queryStr := buffer [:idx + len (t .delimiter )]
705+ buffer = buffer [idx + len (t .delimiter ):]
706+ q , err := ParseQuery (query {Query : strings .TrimSpace (queryStr ), Line : i + 1 , delimiter : t .delimiter })
707+ if err != nil {
708+ return nil , err
709+ }
710+ if q == nil {
711+ continue
712+ }
713+ queries = append (queries , * q )
714+ }
715+ // If has remained comments, ignore them.
716+ if len (buffer ) != 0 && strings .HasPrefix (strings .TrimSpace (buffer ), "#" ) {
717+ buffer = ""
718+ }
679719 }
680-
681- return ParseQueries (queries ... )
720+ if len (buffer ) != 0 {
721+ return nil , errors .Errorf ("Has remained text(%s) in file" , buffer )
722+ }
723+ return queries , nil
682724}
683725
684- func (t * tester ) stmtExecute (query string ) (err error ) {
726+ func (t * tester ) stmtExecute (query query ) (err error ) {
685727 if t .enableQueryLog {
686- t .buf .WriteString (query )
728+ t .buf .WriteString (query . Query )
687729 t .buf .WriteString ("\n " )
688730 }
689- return t .executeStmt (query )
731+
732+ return t .executeStmt (strings .TrimSuffix (query .Query , query .delimiter ))
690733}
691734
692735// checkExpectedError check if error was expected
@@ -784,7 +827,7 @@ func (t *tester) execute(query query) error {
784827 }
785828
786829 offset := t .buf .Len ()
787- err := t .stmtExecute (query . Query )
830+ err := t .stmtExecute (query )
788831
789832 err = t .checkExpectedError (query , err )
790833 if err != nil {
@@ -967,7 +1010,7 @@ func (t *tester) executeStmt(query string) error {
9671010 }
9681011
9691012 if t .enableWarning {
970- raw , err := t .curr .conn .QueryContext (context .Background (), "show warnings; " )
1013+ raw , err := t .curr .conn .QueryContext (context .Background (), "show warnings" )
9711014 if err != nil {
9721015 return errors .Trace (err )
9731016 }
0 commit comments