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

use MySQL parser instead of QuerySplitter #29

Merged
merged 2 commits into from
Jun 26, 2024
Merged
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
30 changes: 17 additions & 13 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"database/sql"
"database/sql/driver"
"fmt"
"github.com/dolthub/dolt/go/cmd/dolt/commands/engine"
gms "github.com/dolthub/go-mysql-server/sql"
"io"
"time"

"github.com/dolthub/dolt/go/cmd/dolt/commands/engine"

gms "github.com/dolthub/go-mysql-server/sql"
)

var _ driver.Conn = (*DoltConn)(nil)
Expand All @@ -25,20 +27,21 @@ func (d *DoltConn) Prepare(query string) (driver.Stmt, error) {
multiStatements := d.DataSource.ParamIsTrue(MultiStatementsParam)

if multiStatements {
qs := NewQuerySplitter(query)
current, err := qs.Next()
if err != io.EOF && err != nil {
scanner := gms.NewMysqlParser()
parsed, prequery, remainder, err := scanner.Parse(d.gmsCtx, query, true)
if err != nil {
return nil, translateError(err)
}

for len(current) > 0 {
if !qs.HasMore() {
for {
if len(remainder) == 0 {
query = prequery
break
}
d.se.GetUnderlyingEngine()

err = func() error {
_, rowIter, err := d.se.Query(d.gmsCtx, current)
var rowIter gms.RowIter
_, rowIter, err = d.se.GetUnderlyingEngine().QueryWithBindings(d.gmsCtx, prequery, parsed, nil)
if err != nil {
return translateError(err)
}
Expand All @@ -59,13 +62,14 @@ func (d *DoltConn) Prepare(query string) (driver.Stmt, error) {
return nil, err
}

current, err = qs.Next()
parsed, prequery, remainder, err = scanner.Parse(d.gmsCtx, remainder, true)
if err != nil {
return nil, err
return nil, translateError(err)
}
}

query = current
if prequery != "" {
query = prequery
}
}

if len(query) > 0 {
Expand Down
40 changes: 40 additions & 0 deletions smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,46 @@ func TestMultiStatements(t *testing.T) {
require.NoError(t, conn.Close())
}

func TestMultiStatementsStoredProc(t *testing.T) {
conn, cleanupFunc := initializeTestDatabaseConnection(t, false)
defer cleanupFunc()

ctx := context.Background()
rows, err := conn.QueryContext(ctx, "create procedure p() begin select 1; end; call p(); call p(); call p();")
require.NoError(t, err)
for rows.Next() {
var i int
err = rows.Scan(&i)
require.NoError(t, err)
require.Equal(t, 1, i)
}
require.NoError(t, rows.Err())
require.NoError(t, rows.Close())
}

func TestMultiStatementsTrigger(t *testing.T) {
conn, cleanupFunc := initializeTestDatabaseConnection(t, false)
defer cleanupFunc()

ctx := context.Background()
res, err := conn.ExecContext(ctx, "create table t (i int primary key, j int);")
require.NoError(t, err)
_, err = res.RowsAffected()
require.NoError(t, err)

rows, err := conn.QueryContext(ctx, "create trigger trig before insert on t for each row begin set new.j = new.j * 100; end; insert into t values (1, 2); select * from t;")
require.NoError(t, err)
for rows.Next() {
var i, j int
err = rows.Scan(&i, &j)
require.NoError(t, err)
require.Equal(t, 1, i)
require.Equal(t, 200, j)
}
require.NoError(t, rows.Err())
require.NoError(t, rows.Close())
}

// TestClientFoundRows asserts that the number of affected rows reported for a query
// correctly reflects whether the CLIENT_FOUND_ROWS capability is set or not.
func TestClientFoundRows(t *testing.T) {
Expand Down
Loading