Skip to content

Commit

Permalink
fix: parse multiple statements with embedded semicolon
Browse files Browse the repository at this point in the history
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.

Fixes: #50
  • Loading branch information
piotrkira committed Apr 28, 2023
1 parent dfa5dd1 commit 29ee493
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 // indirect
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/leodido/go-urn v1.2.2 h1:7z68G0FCGvDk646jz1AelTYNYWrTNm0bEcFAo147wt4=
github.com/leodido/go-urn v1.2.2/go.mod h1:kUaIbLZWttglzwNuG0pgsh5vuV6u2YcGBYz1hIPjtOQ=
github.com/libsql/libsql-client-go v0.0.0-20230417135653-4b3a4f626bc0 h1:k7m1LHwZfTC3jV8hwC9GhgEUFWx2LsTtODN8DNvpROQ=
github.com/libsql/libsql-client-go v0.0.0-20230417135653-4b3a4f626bc0/go.mod h1:w1KCoxf6c2eACi0Rpape7cIooejVqeqYiVr1E/tGcLk=
github.com/libsql/libsql-client-go v0.0.0-20230425122822-72eff623c460 h1:dG1TyWCzFX2KiL6MSyqfzt0XjJq5BP5x4ZD8sY2xahE=
github.com/libsql/libsql-client-go v0.0.0-20230425122822-72eff623c460/go.mod h1:w1KCoxf6c2eACi0Rpape7cIooejVqeqYiVr1E/tGcLk=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down
25 changes: 23 additions & 2 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

_ "github.com/libsql/libsql-client-go/libsql"
_ "github.com/mattn/go-sqlite3"
"github.com/xwb1989/sqlparser"

"github.com/libsql/libsql-shell-go/pkg/shell/enums"
"github.com/libsql/libsql-shell-go/pkg/shell/shellerrors"
Expand Down Expand Up @@ -61,9 +60,31 @@ func (db *Db) Close() {
db.sqlDb.Close()
}

func splitStatementToPieces(statementsString string) (pieces []string, err error) {
pieces = make([]string, 0, 16)
embedded := map[rune]bool{'\'': false, '"': false}
stmtBegin := 0
var stmt string
for i, char := range statementsString {
if status, present := embedded[char]; present {
embedded[char] = !status
}
if embedded['\''] || embedded['"'] || char != ';' {
continue
}
stmt = statementsString[stmtBegin : i]
pieces = append(pieces, stmt)
stmtBegin = i+1
}
if stmtBegin < len(statementsString) {
pieces = append(pieces, statementsString[stmtBegin :])
}
return pieces, nil
}

func (db *Db) ExecuteStatements(statementsString string) (StatementsResult, error) {

statements, err := sqlparser.SplitStatementToPieces(statementsString)
statements, err := splitStatementToPieces(statementsString)
if err != nil {
return StatementsResult{}, err
}
Expand Down

0 comments on commit 29ee493

Please sign in to comment.