Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add more correct session support and use production protobufs
this starts to get to a more correct/usable Session
- Loading branch information
1 parent
341b722
commit a2701f4
Showing
8 changed files
with
165 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package session | ||
|
||
import ( | ||
"math/rand" | ||
|
||
gonanoid "github.com/matoous/go-nanoid/v2" | ||
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" | ||
) | ||
|
||
func UUID(session *psdbv1alpha1.Session) string { | ||
if session != nil && session.VitessSession != nil { | ||
return session.VitessSession.SessionUUID | ||
} | ||
return "" | ||
} | ||
|
||
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 | ||
var signature [32]byte | ||
rand.Read(signature[:]) | ||
|
||
session := &psdbv1alpha1.Session{ | ||
Signature: signature[:], | ||
VitessSession: &vtgatepb.Session{ | ||
Options: &querypb.ExecuteOptions{ | ||
IncludedFields: querypb.ExecuteOptions_ALL, | ||
Workload: querypb.ExecuteOptions_UNSPECIFIED, | ||
ClientFoundRows: true, | ||
}, | ||
Autocommit: true, | ||
DDLStrategy: "direct", | ||
SessionUUID: gonanoid.Must(), | ||
EnableSystemSettings: true, | ||
}, | ||
} | ||
return session | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package vitess | ||
|
||
import ( | ||
"context" | ||
"unsafe" | ||
|
||
querypb "github.com/planetscale/vitess-types/gen/vitess/query/v16" | ||
vtrpcpb "github.com/planetscale/vitess-types/gen/vitess/vtrpc/v16" | ||
"vitess.io/vitess/go/sqltypes" | ||
vitessquerypb "vitess.io/vitess/go/vt/proto/query" | ||
) | ||
|
||
// Code returns the error code if it's a vtError. | ||
// If err is nil, it returns ok. | ||
func Code(err error) vtrpcpb.Code { | ||
if err == nil { | ||
return vtrpcpb.Code_OK | ||
} | ||
|
||
// Handle some special cases. | ||
switch err { | ||
case context.Canceled: | ||
return vtrpcpb.Code_CANCELED | ||
case context.DeadlineExceeded: | ||
return vtrpcpb.Code_DEADLINE_EXCEEDED | ||
} | ||
return vtrpcpb.Code_UNKNOWN | ||
} | ||
|
||
func ToVTRPC(err error) *vtrpcpb.RPCError { | ||
if err == nil { | ||
return nil | ||
} | ||
return &vtrpcpb.RPCError{ | ||
Code: Code(err), | ||
Message: err.Error(), | ||
} | ||
} | ||
|
||
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 unsafeCastQueryResult(qr *vitessquerypb.QueryResult) *querypb.QueryResult { | ||
return castTo[*querypb.QueryResult](qr) | ||
} |
Oops, something went wrong.