Skip to content

Commit

Permalink
merge some session state
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrobenolt committed Jan 14, 2024
1 parent a2701f4 commit d9475bf
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Makefile
Expand Up @@ -3,6 +3,8 @@ gomod := github.com/mattrobenolt/$(app)

BIN := bin

src := main.go internal/session/session.go internal/vitess/vitess.go

all: $(BIN)/$(app)

clean: clean-bin clean-dist
Expand All @@ -21,7 +23,7 @@ GO_INSTALL := env GOBIN=$(PWD)/$(BIN) go install -ldflags "-s -w" -trimpath
$(BIN)/goreleaser: Makefile | $(BIN)
$(GO_INSTALL) github.com/goreleaser/goreleaser@v1.22.1

$(BIN)/$(app): main.go go.mod go.sum | $(BIN)
$(BIN)/$(app): main.go go.mod go.sum $(src) | $(BIN)
$(GO_INSTALL) $(gomod)

run: $(BIN)/$(app)
Expand Down
10 changes: 10 additions & 0 deletions internal/session/session.go
Expand Up @@ -7,6 +7,7 @@ import (
psdbv1alpha1 "github.com/planetscale/psdb/types/psdb/v1alpha1"
querypb "github.com/planetscale/vitess-types/gen/vitess/query/v16"
vtgatepb "github.com/planetscale/vitess-types/gen/vitess/vtgate/v16"
"vitess.io/vitess/go/sqltypes"
)

func UUID(session *psdbv1alpha1.Session) string {
Expand All @@ -16,6 +17,15 @@ func UUID(session *psdbv1alpha1.Session) string {
return ""
}

func Update(qr *sqltypes.Result, session *psdbv1alpha1.Session) {
if s := session.VitessSession; s != nil {
s.LastInsertId = qr.InsertID
s.InTransaction = qr.IsInTransaction()
s.FoundRows = uint64(len(qr.Rows))
s.RowCount = int64(qr.RowsAffected)
}
}

func New() *psdbv1alpha1.Session {
// we're not doing anything with the signature, and it's opaque bytes
// to clients, so just generate a random 32 bytes
Expand Down
6 changes: 3 additions & 3 deletions internal/vitess/vitess.go
Expand Up @@ -41,10 +41,10 @@ func ResultToProto(qr *sqltypes.Result) *querypb.QueryResult {
return unsafeCastQueryResult(sqltypes.ResultToProto3(qr))
}

func castTo[RT any, T any](a T) RT {
return (*(*RT)(unsafe.Pointer(&a)))
func castTo[RT any, T any](a T) *RT {
return (*(**RT)(unsafe.Pointer(&a)))
}

func unsafeCastQueryResult(qr *vitessquerypb.QueryResult) *querypb.QueryResult {
return castTo[*querypb.QueryResult](qr)
return castTo[querypb.QueryResult](qr)
}
2 changes: 2 additions & 0 deletions main.go
Expand Up @@ -255,6 +255,7 @@ func (server) Execute(

// This is a gross simplificiation, but is likely sufficient
qr, err := conn.ExecuteFetch(query, int(*flagMySQLMaxRows), true)
session.Update(qr, sess)

return connect.NewResponse(&psdbv1alpha1.ExecuteResponse{
Session: sess,
Expand Down Expand Up @@ -320,6 +321,7 @@ func (server) StreamExecute(
// fake a streaming response by just returning 2 messages of the same payload
// far from reality, but a simple way to exercise the protocol.
qr, err := conn.ExecuteFetch(query, int(*flagMySQLMaxRows), true)
session.Update(qr, sess)

ll.Info("send msg")
if err := stream.Send(&psdbv1alpha1.ExecuteResponse{
Expand Down

0 comments on commit d9475bf

Please sign in to comment.