Skip to content

Commit 0ad5e0a

Browse files
committed
fix: parse multiple statements with embedded semicolon
Sqlparser is not maintained anymore, as it was used just for this simple thing I've decided to re implement logic of spiting statements into one simple function. Related-to: #50
1 parent dfa5dd1 commit 0ad5e0a

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,5 @@ require (
7070
github.com/olekukonko/tablewriter v0.0.5
7171
github.com/rogpeppe/go-internal v1.9.0 // indirect
7272
github.com/spf13/pflag v1.0.5 // indirect
73-
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
73+
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 // indirect
7474
)

Diff for: go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
8282
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
8383
github.com/leodido/go-urn v1.2.2 h1:7z68G0FCGvDk646jz1AelTYNYWrTNm0bEcFAo147wt4=
8484
github.com/leodido/go-urn v1.2.2/go.mod h1:kUaIbLZWttglzwNuG0pgsh5vuV6u2YcGBYz1hIPjtOQ=
85-
github.com/libsql/libsql-client-go v0.0.0-20230417135653-4b3a4f626bc0 h1:k7m1LHwZfTC3jV8hwC9GhgEUFWx2LsTtODN8DNvpROQ=
86-
github.com/libsql/libsql-client-go v0.0.0-20230417135653-4b3a4f626bc0/go.mod h1:w1KCoxf6c2eACi0Rpape7cIooejVqeqYiVr1E/tGcLk=
8785
github.com/libsql/libsql-client-go v0.0.0-20230425122822-72eff623c460 h1:dG1TyWCzFX2KiL6MSyqfzt0XjJq5BP5x4ZD8sY2xahE=
8886
github.com/libsql/libsql-client-go v0.0.0-20230425122822-72eff623c460/go.mod h1:w1KCoxf6c2eACi0Rpape7cIooejVqeqYiVr1E/tGcLk=
8987
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=

Diff for: internal/db/db.go

+32-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
_ "github.com/libsql/libsql-client-go/libsql"
1010
_ "github.com/mattn/go-sqlite3"
11-
"github.com/xwb1989/sqlparser"
1211

1312
"github.com/libsql/libsql-shell-go/pkg/shell/enums"
1413
"github.com/libsql/libsql-shell-go/pkg/shell/shellerrors"
@@ -61,9 +60,40 @@ func (db *Db) Close() {
6160
db.sqlDb.Close()
6261
}
6362

63+
func splitStatementToPieces(statementsString string) (pieces []string, err error) {
64+
pieces = make([]string, 0, 16)
65+
embeddedChar := ' '
66+
var stmt string
67+
stmtBegin := 0
68+
for i, char := range statementsString {
69+
if char == embeddedChar && char != ' ' {
70+
embeddedChar = ' '
71+
continue
72+
}
73+
if (char == '\'' || char == '"') && embeddedChar == ' ' {
74+
embeddedChar = char
75+
continue
76+
}
77+
if embeddedChar != ' ' || char != ';' {
78+
continue
79+
}
80+
stmt = strings.TrimSpace(statementsString[stmtBegin : i+1])
81+
if len(stmt) < 1 || strings.HasPrefix(stmt, ";") {
82+
stmtBegin = i + 1
83+
continue
84+
}
85+
pieces = append(pieces, stmt)
86+
stmtBegin = i + 1
87+
}
88+
if stmtBegin < len(statementsString) {
89+
pieces = append(pieces, statementsString[stmtBegin:])
90+
}
91+
return pieces, nil
92+
}
93+
6494
func (db *Db) ExecuteStatements(statementsString string) (StatementsResult, error) {
6595

66-
statements, err := sqlparser.SplitStatementToPieces(statementsString)
96+
statements, err := splitStatementToPieces(statementsString)
6797
if err != nil {
6898
return StatementsResult{}, err
6999
}

0 commit comments

Comments
 (0)