Skip to content

Commit 499573b

Browse files
committed
SQLizer: Allow backing stores to turn off aggregate function handling.
1 parent 490bab0 commit 499573b

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

sqlite_backing.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type SQLiteBacking struct {
1616

1717
// New creates a new SQLiteBacking with the given SQLite database connection
1818
func NewSQLiteBacking(db *gosql.DB, s *SQLizer) *SQLiteBacking {
19+
s.HandleAggregateFunctions = false
1920
return &SQLiteBacking{
2021
sqlizer: s,
2122
db: db,

sqlizer.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ const (
3838
)
3939

4040
type SQLizer struct {
41-
Tables map[string]*Table
42-
Permissions uint
43-
Backing BackingStore
44-
AggregateFunctions []*AggregateFunctionColumn
45-
RawStatement string
41+
Tables map[string]*Table
42+
Permissions uint
43+
Backing BackingStore
44+
AggregateFunctions []*AggregateFunctionColumn
45+
RawStatement string
46+
HandleAggregateFunctions bool
4647
}
4748

4849
func (s *SQLizer) SetPermissions(permissions uint) {
@@ -89,7 +90,7 @@ func (s *SQLizer) Execute(statement string) (ResultRows, error) {
8990

9091
rows := s.Backing.Rows()
9192

92-
if len(rows) > 0 && len(s.AggregateFunctions) > 0 {
93+
if len(rows) > 0 && len(s.AggregateFunctions) > 0 && s.HandleAggregateFunctions {
9394
var result ResultRows
9495
for idx, aggregate := range s.AggregateFunctions {
9596
r := aggregate.Call(rows)
@@ -288,6 +289,8 @@ func Initialize(structs ...any) *SQLizer {
288289
sql.addStructTable(s)
289290
}
290291

292+
sql.HandleAggregateFunctions = true
293+
291294
return &sql
292295
}
293296

test/all_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package test
22

33
import (
4-
"fmt"
5-
"github.com/hexops/gotextdiff/span"
64
"io"
75
"os"
86
"path"
97
"path/filepath"
108
"testing"
119

10+
"github.com/hexops/gotextdiff/span"
11+
1212
"github.com/hexops/gotextdiff"
1313
"github.com/hexops/gotextdiff/myers"
1414
)
@@ -105,7 +105,9 @@ func TestE2E(t *testing.T) {
105105
edits := myers.ComputeEdits(span.URIFromPath("expectations/"+testName), string(b), output)
106106
diff := gotextdiff.ToUnified("expectations/"+testName, "input/"+testName, string(b), edits)
107107

108-
fmt.Println(diff)
108+
if len(diff.Hunks) > 0 {
109+
t.Fatal(diff)
110+
}
109111
})
110112
}
111113
return nil

validate.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ func (v *Validator) Visit(n sql.Node) (sql.Visitor, sql.Node, error) {
5959
f.ResultPosition = len(v.columns)
6060
v.s.AggregateFunctions = append(v.s.AggregateFunctions, f)
6161
s = f.UnderlyingColumn
62-
n = &sql.ResultColumn{
63-
Expr: &sql.Ident{
64-
Name: s,
65-
Quoted: false,
66-
},
62+
if v.s.HandleAggregateFunctions {
63+
n = &sql.ResultColumn{
64+
Expr: &sql.Ident{
65+
Name: s,
66+
Quoted: false,
67+
},
68+
}
6769
}
6870
}
6971

0 commit comments

Comments
 (0)