Skip to content

Commit

Permalink
Support query parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
reshke committed Aug 2, 2024
1 parent 4533d31 commit 7de242a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 26 deletions.
38 changes: 25 additions & 13 deletions pkg/core/parser/gram.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pkg/core/parser/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func NewYpParser() YpParser {
// CMDS
%type <node> command

%type<node> say_hello_command

%type<str> reversed_keyword


Expand Down Expand Up @@ -61,5 +63,11 @@ reversed_keyword:


command:
say_hello_command
{
setParseTree(yylex, $1)
} | /* nothing */ { $$ = nil }

say_hello_command:
SAY HELLO { $$ = &SayHelloCommand{} }
;
51 changes: 38 additions & 13 deletions pkg/core/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"

"github.com/jackc/pgx/v5/pgproto3"
"github.com/yezzey-gp/yproxy/pkg/core/parser"
"github.com/yezzey-gp/yproxy/pkg/ylogger"
)

Expand Down Expand Up @@ -56,22 +57,46 @@ init:
case *pgproto3.Query:
ylogger.Zero.Info().Str("query", q.String).Msg("serving request")

conn.Send(&pgproto3.RowDescription{
Fields: []pgproto3.FieldDescription{
{
Name: []byte("row"),
DataTypeOID: 25, /* textoid*/
node, err := parser.Parse(q.String)

if err != nil {
conn.Send(&pgproto3.ErrorResponse{
Message: "failed to parse query",
})
conn.Send(&pgproto3.ReadyForQuery{})
conn.Flush()
continue
}

ylogger.Zero.Info().Interface("node", node).Msg("parsed nodetree")

switch node.(type) {
case *parser.SayHelloCommand:
conn.Send(&pgproto3.RowDescription{
Fields: []pgproto3.FieldDescription{
{
Name: []byte("row"),
DataTypeOID: 25, /* textoid*/
},
},
},
})
})

conn.Send(&pgproto3.DataRow{
Values: [][]byte{[]byte("hi")},
})
conn.Send(&pgproto3.CommandComplete{CommandTag: []byte("YPROXYHELLO")})
conn.Send(&pgproto3.DataRow{
Values: [][]byte{[]byte("hi")},
})
conn.Send(&pgproto3.CommandComplete{CommandTag: []byte("YPROXYHELLO")})

conn.Send(&pgproto3.ReadyForQuery{})
conn.Flush()
default:
conn.Send(&pgproto3.ErrorResponse{
Message: "unknown command",
})

conn.Send(&pgproto3.ReadyForQuery{})
conn.Flush()
}

conn.Send(&pgproto3.ReadyForQuery{})
conn.Flush()
default:
ylogger.Zero.Error().Interface("msg", q).Msg("unssuported message type")
}
Expand Down

0 comments on commit 7de242a

Please sign in to comment.