Skip to content

Commit 3f5ff0a

Browse files
committed
Refactor engine and catalog packages.
1 parent 94cceb5 commit 3f5ff0a

File tree

40 files changed

+103
-109
lines changed

40 files changed

+103
-109
lines changed

cmd/repl/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99

1010
"github.com/chzyer/readline"
1111
"github.com/efritz/gostgres/internal/execution/engine"
12-
"github.com/efritz/gostgres/internal/execution/engine/serialization"
12+
"github.com/efritz/gostgres/internal/execution/protocol"
13+
"github.com/efritz/gostgres/internal/execution/serialization"
1314
"github.com/efritz/gostgres/internal/sample"
1415
"github.com/efritz/gostgres/internal/syntax/parsing"
1516
)
@@ -109,7 +110,10 @@ func handleQuery(engine *engine.Engine, opts options, input string) (err error)
109110
}
110111
}()
111112

112-
rows, err := engine.Query(input, opts.debug)
113+
rows, err := engine.QueryRows(protocol.Request{
114+
Query: input,
115+
Debug: opts.debug,
116+
})
113117
if err != nil {
114118
return err
115119
}

internal/catalog/functions/defaults.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func nextval(ctx impls.Context, args []any) (any, error) {
3333
return nil, fmt.Errorf("nextval() takes one argument of type string")
3434
}
3535

36-
sequence, ok := ctx.GetSequence(name)
36+
sequence, ok := ctx.Sequences.Get(name)
3737
if !ok {
3838
return nil, fmt.Errorf("sequence %s does not exist", name)
3939
}
@@ -54,7 +54,7 @@ func setval(ctx impls.Context, args []any) (any, error) {
5454
return nil, fmt.Errorf("setval() takes two arguments of type string, biginteger")
5555
}
5656

57-
sequence, ok := ctx.GetSequence(name)
57+
sequence, ok := ctx.Sequences.Get(name)
5858
if !ok {
5959
return nil, fmt.Errorf("sequence %s does not exist", name)
6060
}
@@ -71,7 +71,7 @@ func currval(ctx impls.Context, args []any) (any, error) {
7171
return nil, fmt.Errorf("currval() takes one argument of type string")
7272
}
7373

74-
sequence, ok := ctx.GetSequence(name)
74+
sequence, ok := ctx.Sequences.Get(name)
7575
if !ok {
7676
return nil, fmt.Errorf("sequence %s does not exist", name)
7777
}

internal/execution/engine/engine.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/efritz/gostgres/internal/catalog"
77
"github.com/efritz/gostgres/internal/catalog/aggregates"
88
"github.com/efritz/gostgres/internal/catalog/functions"
9-
"github.com/efritz/gostgres/internal/execution/engine/protocol"
9+
"github.com/efritz/gostgres/internal/execution/protocol"
1010
"github.com/efritz/gostgres/internal/shared/impls"
1111
"github.com/efritz/gostgres/internal/shared/rows"
1212
"github.com/efritz/gostgres/internal/syntax/lexing"
@@ -43,10 +43,11 @@ func NewEngine(
4343
}
4444
}
4545

46-
func (e *Engine) Query(input string, debug bool) (rows.Rows, error) {
47-
query, err := parsing.Parse(lexing.Lex(input), e.tables)
46+
func (e *Engine) Query(request protocol.Request, responseWriter protocol.ResponseWriter) {
47+
query, err := parsing.Parse(lexing.Lex(request.Query), e.tables)
4848
if err != nil {
49-
return rows.Rows{}, fmt.Errorf("failed to parse query: %s", err)
49+
responseWriter.Error(fmt.Errorf("failed to parse query: %s", err))
50+
return
5051
}
5152

5253
ctx := impls.NewContext(
@@ -55,16 +56,27 @@ func (e *Engine) Query(input string, debug bool) (rows.Rows, error) {
5556
e.functions,
5657
e.aggregates,
5758
)
58-
if debug {
59+
if request.Debug {
5960
ctx = ctx.WithDebug()
6061
}
6162

63+
query.Execute(ctx, responseWriter)
64+
}
65+
66+
func (e *Engine) QueryRows(request protocol.Request) (rows.Rows, error) {
6267
collector := protocol.NewRowCollector()
63-
query.Execute(ctx, collector)
68+
e.Query(request, collector)
6469
collectedRows, err := collector.Rows()
6570
if err != nil {
66-
return rows.Rows{}, fmt.Errorf("failed to execute query %q: %s", input, err)
71+
return rows.Rows{}, fmt.Errorf("failed to execute query %q: %s", request.Query, err)
6772
}
6873

6974
return collectedRows, nil
7075
}
76+
77+
func (e *Engine) QueryError(request protocol.Request) error {
78+
collector := protocol.NewRowCollector()
79+
e.Query(request, collector)
80+
_, err := collector.Rows()
81+
return err
82+
}

internal/execution/expressions/aggregate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func AsAggregate(ctx impls.Context, e impls.Expression) impls.AggregateExpressio
1616
return e
1717
}
1818

19-
aggregate, ok := ctx.GetAggregate(f.name)
19+
aggregate, ok := ctx.Aggregates.Get(f.name)
2020
if !ok {
2121
return e
2222
}

internal/execution/expressions/function.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (e functionExpression) Map(f func(impls.Expression) impls.Expression) impls
7575
}
7676

7777
func (e functionExpression) ValueFrom(ctx impls.Context, row rows.Row) (any, error) {
78-
f, ok := ctx.GetFunction(e.name)
78+
f, ok := ctx.Functions.Get(e.name)
7979
if !ok {
8080
return nil, fmt.Errorf("unknown function %s", e.name)
8181
}

internal/execution/engine/protocol/request.go renamed to internal/execution/protocol/request.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ package protocol
22

33
type Request struct {
44
Query string
5+
Debug bool
56
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package protocol
2+
3+
import (
4+
"github.com/efritz/gostgres/internal/shared/rows"
5+
)
6+
7+
type ResponseWriter interface {
8+
SendRow(row rows.Row)
9+
Done()
10+
Error(err error)
11+
}

internal/execution/engine/protocol/response.go renamed to internal/execution/protocol/row_collector.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ import (
66
"github.com/efritz/gostgres/internal/shared/rows"
77
)
88

9-
type ResponseWriter interface {
10-
SendRow(row rows.Row)
11-
Done()
12-
Error(err error)
13-
}
14-
159
type rowCollector struct {
1610
done bool
1711
rows rows.Rows

internal/execution/queries/access/access.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package access
22

33
import (
4-
"github.com/efritz/gostgres/internal/execution/engine/serialization"
54
"github.com/efritz/gostgres/internal/execution/expressions"
65
"github.com/efritz/gostgres/internal/execution/queries"
76
"github.com/efritz/gostgres/internal/execution/queries/filter"
87
"github.com/efritz/gostgres/internal/execution/scan"
8+
"github.com/efritz/gostgres/internal/execution/serialization"
99
"github.com/efritz/gostgres/internal/shared/fields"
1010
"github.com/efritz/gostgres/internal/shared/impls"
1111
)

internal/execution/queries/access/strategy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package access
22

33
import (
44
"github.com/efritz/gostgres/internal/catalog/table/indexes"
5-
"github.com/efritz/gostgres/internal/execution/engine/serialization"
65
"github.com/efritz/gostgres/internal/execution/expressions"
76
"github.com/efritz/gostgres/internal/execution/scan"
7+
"github.com/efritz/gostgres/internal/execution/serialization"
88
"github.com/efritz/gostgres/internal/shared/impls"
99
)
1010

0 commit comments

Comments
 (0)