@@ -20,6 +20,7 @@ import (
2020 "bytes"
2121 "encoding/json"
2222 "reflect"
23+ "strings"
2324 "testing"
2425 "unsafe"
2526
@@ -441,3 +442,48 @@ func TestColumns_FindColumn(t *testing.T) {
441442 }
442443 }
443444}
445+
446+ func TestSplitStatementToPieces (t * testing.T ) {
447+ testcases := []struct {
448+ input string
449+ output string
450+ }{{
451+ input : "select * from table" ,
452+ }, {
453+ input : "select * from table1; select * from table2;" ,
454+ output : "select * from table1; select * from table2" ,
455+ }, {
456+ input : "select * from /* comment ; */ table;" ,
457+ output : "select * from /* comment ; */ table" ,
458+ }, {
459+ input : "select * from table where semi = ';';" ,
460+ output : "select * from table where semi = ';'" ,
461+ }, {
462+ input : "select * from table1;--comment;\n select * from table2;" ,
463+ output : "select * from table1;--comment;\n select * from table2" ,
464+ }, {
465+ input : "CREATE TABLE `total_data` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id', " +
466+ "`region` varchar(32) NOT NULL COMMENT 'region name, like zh; th; kepler'," +
467+ "`data_size` bigint NOT NULL DEFAULT '0' COMMENT 'data size;'," +
468+ "`createtime` datetime NOT NULL DEFAULT NOW() COMMENT 'create time;'," +
469+ "`comment` varchar(100) NOT NULL DEFAULT '' COMMENT 'comment'," +
470+ "PRIMARY KEY (`id`))" ,
471+ }}
472+
473+ for _ , tcase := range testcases {
474+ if tcase .output == "" {
475+ tcase .output = tcase .input
476+ }
477+
478+ stmtPieces , err := SplitStatementToPieces (tcase .input )
479+ if err != nil {
480+ t .Errorf ("input: %s, err: %v" , tcase .input , err )
481+ continue
482+ }
483+
484+ out := strings .Join (stmtPieces , ";" )
485+ if out != tcase .output {
486+ t .Errorf ("out: %s, want %s" , out , tcase .output )
487+ }
488+ }
489+ }
0 commit comments