Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse multiple statements with embedded semicolon #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
34 changes: 32 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,40 @@ func (db *Db) Close() {
db.sqlDb.Close()
}

func splitStatementToPieces(statementsString string) (pieces []string, err error) {
pieces = make([]string, 0, 16)
embeddedChar := ' '
var stmt string
stmtBegin := 0
for i, char := range statementsString {
if char == embeddedChar && char != ' ' {
embeddedChar = ' '
continue
}
if (char == '\'' || char == '"') && embeddedChar == ' ' {
embeddedChar = char
continue
}
if embeddedChar != ' ' || char != ';' {
continue
}
stmt = strings.TrimSpace(statementsString[stmtBegin : i+1])
if len(stmt) < 1 || strings.HasPrefix(stmt, ";") {
stmtBegin = i + 1
continue
}
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