Skip to content

Commit

Permalink
eorm: 支持分库分表最简单的 ShardingSelector (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stone-afk committed Feb 25, 2023
1 parent f57e3af commit 101df98
Show file tree
Hide file tree
Showing 79 changed files with 2,336 additions and 127 deletions.
26 changes: 14 additions & 12 deletions .CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
## 开发中
- [eorm: Distinct功能](https://github.com/ecodeclub/eorm/pull/116)
- [eorm: aop方案的支持](https://github.com/ecodeclub/eorm/pull/117)
- [eorm: 利用 aop 接口打印 SQL](https://github.com/ecodeclub/eorm/pull/123)
- [eorm: 插入忽略主键](https://github.com/ecodeclub/eorm/pull/126)
- [eorm: join查询](https://github.com/ecodeclub/eorm/pull/122)
- [eorm: 重构 JOIN 查询](https://github.com/ecodeclub/eorm/pull/130)
- [eorm: 新增子查詢](https://github.com/ecodeclub/eorm/pull/131)
- [eorm: 取消支持 Having 中使用 alias](https://github.com/ecodeclub/eorm/pull/135)
- [eorm: 删除 IsAutoIncrement 字段](https://github.com/ecodeclub/eorm/pull/136)
- [eorm: 删除 IsHolderType](https://github.com/ecodeclub/eorm/pull/137)
- [eorm: MasterSlaveDB实现](https://github.com/ecodeclub/eorm/pull/146)
- [eorm: 重构 Update 语句忽略零值和Nil](https://github.com/ecodeclub/eorm/pull/138)
- [eorm: Distinct功能](https://github.com/gotomicro/eorm/pull/116)
- [eorm: aop方案的支持](https://github.com/gotomicro/eorm/pull/117)
- [eorm: 利用 aop 接口打印 SQL](https://github.com/gotomicro/eorm/pull/123)
- [eorm: 插入忽略主键](https://github.com/gotomicro/eorm/pull/126)
- [eorm: join查询](https://github.com/gotomicro/eorm/pull/122)
- [eorm: 重构 JOIN 查询](https://github.com/gotomicro/eorm/pull/130)
- [eorm: 新增子查詢](https://github.com/gotomicro/eorm/pull/131)
- [eorm: 取消支持 Having 中使用 alias](https://github.com/gotomicro/eorm/pull/135)
- [eorm: 删除 IsAutoIncrement 字段](https://github.com/gotomicro/eorm/pull/136)
- [eorm: 删除 IsHolderType](https://github.com/gotomicro/eorm/pull/137)
- [eorm: MasterSlaveDB实现](https://github.com/gotomicro/eorm/pull/146)
- [eorm: 重构 Update 语句忽略零值和Nil](https://github.com/gotomicro/eorm/pull/138)
- [eorm: 新增 ShardingSelector 实现](https://github.com/gotomicro/eorm/pull/145)
- [eorm: 基于dns的slave发现](https://github.com/ecodeclub/eorm/pull/152)


## v0.0.1:
- [Init Project](https://github.com/ecodeclub/eorm/pull/1)
- [Selector Definition](https://github.com/ecodeclub/eorm/pull/2)
Expand Down
2 changes: 1 addition & 1 deletion .LICENSE_FILE_HEADER
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2021 ecodehub
Copyright 2021 ecodeclub

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion .licenserc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"**/*.go": "// Copyright 2021 ecodehub",
"**/*.go": "// Copyright 2021 ecodeclub",
"**/*.{yml,toml}": "# Copyright 2021 ecodeclub"
}
2 changes: 1 addition & 1 deletion aggregate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion aggregate_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion assignment.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion assignment_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
24 changes: 13 additions & 11 deletions builder.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -23,20 +23,10 @@ import (
"github.com/valyala/bytebufferpool"
)

// QueryBuilder is used to build a query
type QueryBuilder interface {
Build() (*Query, error)
}

var _ Executor = &Inserter[any]{}
var _ Executor = &Updater[any]{}
var _ Executor = &Deleter[any]{}

// Executor is used to build a query
type Executor interface {
Exec(ctx context.Context) Result
}

// Query 代表一个查询
type Query struct {
SQL string
Expand Down Expand Up @@ -109,6 +99,18 @@ func (q Querier[T]) Get(ctx context.Context) (*T, error) {
return res.Result.(*T), nil
}

type selectorBuilder struct {
builder
columns []Selectable
where []Predicate
distinct bool
having []Predicate
groupBy []string
orderBy []OrderBy
offset int
limit int
}

type builder struct {
core
// 使用 bytebufferpool 以减少内存分配
Expand Down
2 changes: 1 addition & 1 deletion builder_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion column.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion column_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion core.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
15 changes: 14 additions & 1 deletion db_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -142,6 +142,19 @@ func memoryDB() *DB {
return orm
}

// memoryDB 返回一个基于内存的 MasterSlaveDB,它使用的是 sqlite3 内存模式。
func masterSlaveMemoryDB() *MasterSlavesDB {
db, err := sql.Open("sqlite3", "file:test.db?cache=shared&mode=memory")
if err != nil {
panic(err)
}
masterSlaveDB, err := OpenMasterSlaveDB("sqlite3", db)
if err != nil {
panic(err)
}
return masterSlaveDB
}

func memoryDBWithDB(db string) *DB {
orm, err := Open("sqlite3", fmt.Sprintf("file:%s.db?cache=shared&mode=memory", db))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion delete.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion delete_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion expression.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion expression_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.13
github.com/stretchr/testify v1.7.1
github.com/valyala/bytebufferpool v1.0.0
golang.org/x/sync v0.1.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMT
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
2 changes: 1 addition & 1 deletion insert.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion insert_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion internal/dialect/dialect.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion internal/dialect/dialect_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
19 changes: 16 additions & 3 deletions internal/errs/error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 ecodehub
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,8 +28,16 @@ var (
ErrTooManyColumns = errors.New("eorm: 过多列")

// ErrCombinationIsNotStruct 不支持的组合类型,eorm 只支持结构体组合
ErrCombinationIsNotStruct = errors.New("eorm: 不支持的组合类型,eorm 只支持结构体组合")
ErrSlaveNotFound = errors.New("eorm: slave不存在")
ErrCombinationIsNotStruct = errors.New("eorm: 不支持的组合类型,eorm 只支持结构体组合")
ErrMissingShardingKey = errors.New("eorm: sharding key 未设置")
ErrOnlyResultOneQuery = errors.New("eorm: 只能生成一个 SQL")
ErrNotGenShardingQuery = errors.New("eorm: 未生成 sharding query")
ErrUnsupportedTooComplexQuery = errors.New("eorm: 暂未支持太复杂的查询")
ErrExcShardingAlgorithm = errors.New("eorm: 执行 sharding algorithm 出错")
ErrCtxGetDBName = errors.New("eorm: ctx 获取目标 dbName 出错")
ErrNotFoundTargetDB = errors.New("eorm: 未发现目标 DB")
ErrNotFoundTargetTable = errors.New("eorm: 未发现目标 Table")
ErrSlaveNotFound = errors.New("eorm: slave不存在")
)

func NewFieldConflictError(field string) error {
Expand Down Expand Up @@ -72,6 +80,11 @@ func NewMustSpecifyColumnsError() error {
return fmt.Errorf("eorm: 复合查询如 JOIN 查询、子查询必须指定要查找的列,即指定 SELECT xxx 部分")
}

func NewUnsupportedOperatorError(op string) error {
return fmt.Errorf("eorm: 不支持的 operator %v", op)
}

func NewInvalidDSNError(dsn string) error {
return fmt.Errorf("eorm: 不正确的 DSN %s", dsn)

}

0 comments on commit 101df98

Please sign in to comment.