github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/sharding/types.go (about) 1 // Copyright 2021 ecodeclub 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package sharding 16 17 import ( 18 "context" 19 20 operator "github.com/ecodeclub/eorm/internal/operator" 21 "github.com/ecodeclub/eorm/internal/query" 22 ) 23 24 var EmptyResp = Response{} 25 var EmptyQuery = Query{} 26 27 type Algorithm interface { 28 // Sharding 返回分库分表之后目标库和目标表信息 29 Sharding(ctx context.Context, req Request) (Response, error) 30 // Broadcast 返回所有的目标库、目标表 31 Broadcast(ctx context.Context) []Dst 32 // ShardingKeys 返回所有的 sharding key 33 // 这部分不包含任何放在 context.Context 中的部分,例如 shadow 标记位等 34 // 或者说,它只是指数据库中用于分库分表的列 35 ShardingKeys() []string 36 } 37 38 // Executor sql 语句执行器 39 type Executor interface { 40 Exec(ctx context.Context) Result 41 } 42 43 // QueryBuilder sharding sql 构造抽象 44 type QueryBuilder interface { 45 Build(ctx context.Context) ([]Query, error) 46 } 47 48 type Query = query.Query 49 50 type Dst struct { 51 // Name 数据源的逻辑名字 52 Name string 53 DB string 54 Table string 55 } 56 57 func (r Dst) Equals(l Dst) bool { 58 return r.Name == l.Name && r.DB == l.DB && r.Table == l.Table 59 } 60 61 func (r Dst) NotEquals(l Dst) bool { 62 return r.Name != l.Name || r.DB != l.DB || r.Table != l.Table 63 } 64 65 type Request struct { 66 Op operator.Op 67 SkValues map[string]any 68 } 69 70 type Response struct { 71 Dsts []Dst 72 }