Skip to content

Commit

Permalink
Merge pull request #14 from Stone-afk/dev-new
Browse files Browse the repository at this point in the history
Dev new
  • Loading branch information
Stone-afk authored Sep 7, 2023
2 parents ae91a74 + 043bd0d commit 4b72380
Show file tree
Hide file tree
Showing 28 changed files with 362 additions and 620 deletions.
4 changes: 3 additions & 1 deletion .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
- [eorm: 补充 NULL 语义和基本类型之间转化的测试用例](https://github.com/ecodeclub/eorm/pull/198)
- [eorm: 分库分表: ShardingSelector GetMulti 使用 merge](https://github.com/ecodeclub/eorm/pull/199)
- [eorm: 分库分表:Inserter 支持分库分表](https://github.com/ecodeclub/eorm/pull/200)
- [eorm: ShardingInserter 修改为表维度执行](https://github.com/ecodeclub/eorm/pull/211)
- [eorm: 分库分表:ShardingUpdater 实现](https://github.com/ecodeclub/eorm/pull/201)
- [eorm: 分库分表:datasource-简单的分布式事务方案支持](https://github.com/ecodeclub/eorm/pull/204)
- [eorm: ShardingInserter 修改为表维度执行](https://github.com/ecodeclub/eorm/pull/211)
- [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:
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
.idea
*.iml
fuzz
go.work
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)
}
2 changes: 1 addition & 1 deletion internal/integration/sharding_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"testing"

"github.com/ecodeclub/eorm/internal/datasource"
"github.com/ecodeclub/eorm"
"github.com/ecodeclub/eorm/internal/datasource/masterslave"

"github.com/ecodeclub/eorm"
Expand Down
51 changes: 24 additions & 27 deletions internal/merger/aggregatemerger/merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ package aggregatemerger
import (
"context"
"database/sql"
"errors"
"sync"
_ "unsafe"

"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 @@ -46,17 +47,16 @@ 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()
}
if len(results) == 0 {
return nil, errs.ErrMergerEmptyRows
}
for _, res := range results {
err := m.checkColumns(res)
if err != nil {
return nil, err
if res == nil {
return nil, errs.ErrMergerRowsIsNull
}
}

Expand All @@ -70,15 +70,9 @@ func (m *Merger) Merge(ctx context.Context, results []*sql.Rows) (merger.Rows, e
}, nil

}
func (m *Merger) checkColumns(rows *sql.Rows) error {
if rows == nil {
return errs.ErrMergerRowsIsNull
}
return nil
}

type Rows struct {
rowsList []*sql.Rows
rowsList []rows.Rows
aggregators []aggregator.Aggregator
closed bool
mu *sync.RWMutex
Expand All @@ -88,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 @@ -149,23 +151,18 @@ func (r *Rows) getSqlRowsData() ([][]any, error) {
}
return rowsData, nil
}
func (r *Rows) getSqlRowData(row *sql.Rows) ([]any, error) {

func (*Rows) getSqlRowData(row rows.Rows) ([]any, error) {
var colsData []any
var err error
if row.Next() {
colsData, err = utils.Scan(row)
if err != nil {
return nil, err
}
} else {
// sql.Rows迭代过程中发生报错,返回报错
if row.Err() != nil {
return nil, row.Err()
}
scanner, err := sqlx.NewSQLRowsScanner(row)
if err != nil {
return nil, err
}
colsData, err = scanner.Scan()
if errors.Is(err, sqlx.ErrNoMoreRows) {
return nil, errs.ErrMergerAggregateHasEmptyRows
}
return colsData, nil
return colsData, err
}

func (r *Rows) Scan(dest ...any) error {
Expand All @@ -182,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 4b72380

Please sign in to comment.