Skip to content

Commit

Permalink
rows, merger: 使用 sqlx.Rows 作为接口,并重构 merger 包 (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash authored Sep 5, 2023
1 parent d10f1b7 commit 07dc416
Show file tree
Hide file tree
Showing 24 changed files with 335 additions and 270 deletions.
1 change: 1 addition & 0 deletions .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- [eorm: 分库分表:ShardingUpdater 实现](https://github.com/ecodeclub/eorm/pull/201)
- [eorm: 分库分表:datasource-简单的分布式事务方案支持](https://github.com/ecodeclub/eorm/pull/204)
- [merger: 使用 sqlx.Scanner 来读取数据](https://github.com/ecodeclub/eorm/pull/216)
- [rows, merger: 使用 sqlx.Rows 作为接口,并重构 merger 包 ](https://github.com/ecodeclub/eorm/pull/217)

## v0.0.1:
- [Init Project](https://github.com/ecodeclub/eorm/pull/1)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go-fmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ">=1.18.0"
go-version: ">=1.20.0"

- name: Install goimports
run: go install golang.org/x/tools/cmd/goimports@latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: '1.20'

- name: Build
run: go build -v ./...
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.18'
go-version: '1.20'
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/integration_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: '1.20'

- name: Test
run: sudo sh ./script/integrate_test.sh
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

### Go 版本

请使用 Go 1.18 以上版本。
请使用 Go 1.20 以上版本。

### SQL 2003 标准
理论上来说,我们计划支持 [SQL 2003 standard](https://ronsavage.github.io/SQL/sql-2003-2.bnf.html#query%20specification). 不过据我们所知,并不是所有的数据库都支持全部的 SQL 2003 标准,所以用户还是需要进一步检查目标数据库的语法。
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/ecodeclub/eorm

go 1.18
go 1.20

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/ecodeclub/ekit v0.0.4-0.20230530053225-e671c5fdd2d1
github.com/ecodeclub/ekit v0.0.4-0.20230904153403-e76aae064994
github.com/go-sql-driver/mysql v1.6.0
github.com/gotomicro/ekit v0.0.0-20230224040531-869798da3c4d
github.com/mattn/go-sqlite3 v1.14.15
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ecodeclub/ekit v0.0.4-0.20230530053225-e671c5fdd2d1 h1:a1Dbg0zZOQPfG3pgFqZjkQM2ty1ZABewjzRK970OQ8w=
github.com/ecodeclub/ekit v0.0.4-0.20230530053225-e671c5fdd2d1/go.mod h1:OqTojKeKFTxeeAAUwNIPKu339SRkX6KAuoK/8A5BCEs=
github.com/ecodeclub/ekit v0.0.4-0.20230904153403-e76aae064994 h1:4Rp8WrJhISj8GDtnueoD22ygPuppajnCVZuEfRjg6w8=
github.com/ecodeclub/ekit v0.0.4-0.20230904153403-e76aae064994/go.mod h1:OqTojKeKFTxeeAAUwNIPKu339SRkX6KAuoK/8A5BCEs=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/gotomicro/ekit v0.0.0-20230224040531-869798da3c4d h1:kmDgYRZ06UifBqAfew+cj02juQQ3Ko349NzsDIZ0QPw=
Expand Down
4 changes: 4 additions & 0 deletions internal/errs/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,7 @@ func NewInvalidDSNError(dsn string) error {
func NewFailedToGetSlavesFromDNS(err error) error {
return fmt.Errorf("eorm: 从DNS中解析从库失败 %w", err)
}

func NewErrScanWrongDestinationArguments(expect int, actual int) error {
return fmt.Errorf("eorm: Scan 方法收到过多或者过少的参数,预期 %d,实际 %d", expect, actual)
}
22 changes: 14 additions & 8 deletions internal/merger/aggregatemerger/merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import (
"sync"
_ "unsafe"

"github.com/ecodeclub/ekit/sqlx"

"github.com/ecodeclub/eorm/internal/merger"
"github.com/ecodeclub/eorm/internal/rows"

"github.com/ecodeclub/eorm/internal/merger/utils"
"github.com/ecodeclub/ekit/sqlx"

"github.com/ecodeclub/eorm/internal/merger/aggregatemerger/aggregator"
"github.com/ecodeclub/eorm/internal/merger/internal/errs"
Expand All @@ -49,7 +47,7 @@ func NewMerger(aggregators ...aggregator.Aggregator) *Merger {
}
}

func (m *Merger) Merge(ctx context.Context, results []*sql.Rows) (merger.Rows, error) {
func (m *Merger) Merge(ctx context.Context, results []rows.Rows) (rows.Rows, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
Expand All @@ -74,7 +72,7 @@ func (m *Merger) Merge(ctx context.Context, results []*sql.Rows) (merger.Rows, e
}

type Rows struct {
rowsList []*sql.Rows
rowsList []rows.Rows
aggregators []aggregator.Aggregator
closed bool
mu *sync.RWMutex
Expand All @@ -84,6 +82,14 @@ type Rows struct {
nextCalled bool
}

func (r *Rows) ColumnTypes() ([]*sql.ColumnType, error) {
return r.rowsList[0].ColumnTypes()
}

func (*Rows) NextResultSet() bool {
return false
}

func (r *Rows) Next() bool {
r.mu.Lock()
if r.closed || r.lastErr != nil {
Expand Down Expand Up @@ -145,7 +151,7 @@ func (r *Rows) getSqlRowsData() ([][]any, error) {
}
return rowsData, nil
}
func (*Rows) getSqlRowData(row *sql.Rows) ([]any, error) {
func (*Rows) getSqlRowData(row rows.Rows) ([]any, error) {
var colsData []any
var err error
scanner, err := sqlx.NewSQLRowsScanner(row)
Expand Down Expand Up @@ -173,7 +179,7 @@ func (r *Rows) Scan(dest ...any) error {
return errs.ErrMergerScanNotNext
}
for i := 0; i < len(dest); i++ {
err := utils.ConvertAssign(dest[i], r.cur[i])
err := rows.ConvertAssign(dest[i], r.cur[i])
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 07dc416

Please sign in to comment.