[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; Id: number;
RowsAffected: number; RowsAffected: number;
LastInsertId: number; LastInsertId: number;
Msg?: string;
} }
interface Row { interface Row {
@ -31,11 +32,12 @@ export function encodeRequest(request: Request): Uint8Array {
} }
export function decodeResponse(input: Uint8Array): ExecResult { 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 { return {
Id: res.i, Id: res.i,
RowsAffected: res.ra, RowsAffected: res.ra,
LastInsertId: res.li, LastInsertId: res.li,
Msg: res.m,
}; };
} }

View file

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

View file

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