[fix] add msg field for query resut

Signed-off-by: Anton Nesterov <anton@demiurg.io>
This commit is contained in:
Anton Nesterov 2024-08-21 01:03:28 +02:00
parent 97cad8eaac
commit f8e28d2a2e
No known key found for this signature in database
GPG key ID: 59121E8AE2851FB5
4 changed files with 69 additions and 19 deletions

View file

@ -15,6 +15,7 @@ export interface ExecResult {
Id: number;
RowsAffected: number;
LastInsertId: number;
Msg?: string;
}
interface Row {
@ -31,11 +32,12 @@ export function encodeRequest(request: Request): Uint8Array {
}
export function decodeResponse(input: Uint8Array): ExecResult {
const res = decode(input) as {i: number; ra: number; li: number};
const res = decode(input) as {i: number; ra: number; li: number, m?: string};
return {
Id: res.i,
RowsAffected: res.ra,
LastInsertId: res.li,
Msg: res.m,
};
}

View file

@ -1,7 +1,6 @@
package facade
import (
"log"
"reflect"
"github.com/nesterow/dal/pkg/adapter"
@ -31,20 +30,37 @@ func HandleQuery(input *[]byte, output *[]byte) int {
req := proto.Request{}
_, err := req.UnmarshalMsg(*input)
if err != nil {
log.Println(*input)
log.Printf("failed to unmarshal request: %v", err)
return 1
res := proto.Response{
Id: 0,
RowsAffected: -1,
LastInsertId: -1,
Msg: "failed to unmarshal request",
}
*output, _ = res.MarshalMsg(nil)
return 0
}
query, err := req.Parse(adapter.GetDialect(db.Type))
if err != nil {
log.Printf("failed to parse request: %v", err)
return 1
res := proto.Response{
Id: 0,
RowsAffected: -1,
LastInsertId: -1,
Msg: err.Error(),
}
*output, _ = res.MarshalMsg(nil)
return 0
}
if query.Exec {
result, err := db.Exec(query)
if err != nil {
log.Printf("failed to exec query: %v", err)
return 1
res := proto.Response{
Id: 0,
RowsAffected: -1,
LastInsertId: -1,
Msg: err.Error(),
}
*output, _ = res.MarshalMsg(nil)
return 0
}
ra, _ := result.RowsAffected()
la, _ := result.LastInsertId()
@ -58,8 +74,14 @@ func HandleQuery(input *[]byte, output *[]byte) int {
}
rows, err := db.Query(query)
if err != nil {
log.Printf("failed to query: %v", err)
return 1
res := proto.Response{
Id: 0,
RowsAffected: -1,
LastInsertId: -1,
Msg: err.Error(),
}
*output, _ = res.MarshalMsg(nil)
return 0
}
columns, _ := rows.Columns()
types, _ := rows.ColumnTypes()

View file

@ -6,4 +6,5 @@ type Response struct {
Id uint32 `msg:"i"`
RowsAffected int64 `msg:"ra"`
LastInsertId int64 `msg:"li"`
Msg string `msg:"m"`
}

View file

@ -42,6 +42,12 @@ func (z *Response) DecodeMsg(dc *msgp.Reader) (err error) {
err = msgp.WrapError(err, "LastInsertId")
return
}
case "m":
z.Msg, err = dc.ReadString()
if err != nil {
err = msgp.WrapError(err, "Msg")
return
}
default:
err = dc.Skip()
if err != nil {
@ -54,10 +60,10 @@ func (z *Response) DecodeMsg(dc *msgp.Reader) (err error) {
}
// EncodeMsg implements msgp.Encodable
func (z Response) EncodeMsg(en *msgp.Writer) (err error) {
// map header, size 3
func (z *Response) EncodeMsg(en *msgp.Writer) (err error) {
// map header, size 4
// write "i"
err = en.Append(0x83, 0xa1, 0x69)
err = en.Append(0x84, 0xa1, 0x69)
if err != nil {
return
}
@ -86,15 +92,25 @@ func (z Response) EncodeMsg(en *msgp.Writer) (err error) {
err = msgp.WrapError(err, "LastInsertId")
return
}
// write "m"
err = en.Append(0xa1, 0x6d)
if err != nil {
return
}
err = en.WriteString(z.Msg)
if err != nil {
err = msgp.WrapError(err, "Msg")
return
}
return
}
// MarshalMsg implements msgp.Marshaler
func (z Response) MarshalMsg(b []byte) (o []byte, err error) {
func (z *Response) MarshalMsg(b []byte) (o []byte, err error) {
o = msgp.Require(b, z.Msgsize())
// map header, size 3
// map header, size 4
// string "i"
o = append(o, 0x83, 0xa1, 0x69)
o = append(o, 0x84, 0xa1, 0x69)
o = msgp.AppendUint32(o, z.Id)
// string "ra"
o = append(o, 0xa2, 0x72, 0x61)
@ -102,6 +118,9 @@ func (z Response) MarshalMsg(b []byte) (o []byte, err error) {
// string "li"
o = append(o, 0xa2, 0x6c, 0x69)
o = msgp.AppendInt64(o, z.LastInsertId)
// string "m"
o = append(o, 0xa1, 0x6d)
o = msgp.AppendString(o, z.Msg)
return
}
@ -141,6 +160,12 @@ func (z *Response) UnmarshalMsg(bts []byte) (o []byte, err error) {
err = msgp.WrapError(err, "LastInsertId")
return
}
case "m":
z.Msg, bts, err = msgp.ReadStringBytes(bts)
if err != nil {
err = msgp.WrapError(err, "Msg")
return
}
default:
bts, err = msgp.Skip(bts)
if err != nil {
@ -154,7 +179,7 @@ func (z *Response) UnmarshalMsg(bts []byte) (o []byte, err error) {
}
// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
func (z Response) Msgsize() (s int) {
s = 1 + 2 + msgp.Uint32Size + 3 + msgp.Int64Size + 3 + msgp.Int64Size
func (z *Response) Msgsize() (s int) {
s = 1 + 2 + msgp.Uint32Size + 3 + msgp.Int64Size + 3 + msgp.Int64Size + 2 + msgp.StringPrefixSize + len(z.Msg)
return
}