|
| 1 | +package proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + q "github.com/core-go/sql" |
| 6 | + "reflect" |
| 7 | +) |
| 8 | + |
| 9 | +const txs = "tx" |
| 10 | + |
| 11 | +func GetTx(ctx context.Context) *string { |
| 12 | + txi := ctx.Value(txs) |
| 13 | + if txi != nil { |
| 14 | + txx, ok := txi.(*string) |
| 15 | + if ok { |
| 16 | + return txx |
| 17 | + } |
| 18 | + } |
| 19 | + return nil |
| 20 | +} |
| 21 | + |
| 22 | +type Proxy interface { |
| 23 | + BeginTransaction(ctx context.Context, timeout int64) (string, error) |
| 24 | + CommitTransaction(ctx context.Context, tx string) error |
| 25 | + RollbackTransaction(ctx context.Context, tx string) error |
| 26 | + Exec(ctx context.Context, query string, values ...interface{}) (int64, error) |
| 27 | + ExecBatch(ctx context.Context, master bool, stm ...q.Statement) (int64, error) |
| 28 | + Query(ctx context.Context, result interface{}, query string, values ...interface{}) error |
| 29 | + ExecTx(ctx context.Context, tx string, commit bool, query string, values ...interface{}) (int64, error) |
| 30 | + ExecBatchTx(ctx context.Context, tx string, commit bool, master bool, stm ...q.Statement) (int64, error) |
| 31 | + QueryTx(ctx context.Context, tx string, commit bool, result interface{}, query string, values ...interface{}) error |
| 32 | + |
| 33 | + Insert(ctx context.Context, table string, model interface{}, buildParam func(int) string, boolSupport bool, options ...*q.Schema) (int64, error) |
| 34 | + Update(ctx context.Context, table string, model interface{}, buildParam func(int) string, boolSupport bool, options ...*q.Schema) (int64, error) |
| 35 | + Save(ctx context.Context, table string, model interface{}, driver string, options ...*q.Schema) (int64, error) |
| 36 | + InsertBatch(ctx context.Context, table string, models interface{}, driver string, options ...*q.Schema) (int64, error) |
| 37 | + UpdateBatch(ctx context.Context, table string, models interface{}, buildParam func(int) string, boolSupport bool, options ...*q.Schema) (int64, error) |
| 38 | + SaveBatch(ctx context.Context, table string, models interface{}, driver string, options ...*q.Schema) (int64, error) |
| 39 | + |
| 40 | + InsertTx(ctx context.Context, tx string, commit bool, table string, model interface{}, buildParam func(int) string, boolSupport bool, options ...*q.Schema) (int64, error) |
| 41 | + UpdateTx(ctx context.Context, tx string, commit bool, table string, model interface{}, buildParam func(int) string, boolSupport bool, options ...*q.Schema) (int64, error) |
| 42 | + SaveTx(ctx context.Context, tx string, commit bool, table string, model interface{}, driver string, options ...*q.Schema) (int64, error) |
| 43 | + InsertBatchTx(ctx context.Context, tx string, commit bool, table string, models interface{}, driver string, options ...*q.Schema) (int64, error) |
| 44 | + UpdateBatchTx(ctx context.Context, tx string, commit bool, table string, models interface{}, buildParam func(int) string, boolSupport bool, options ...*q.Schema) (int64, error) |
| 45 | + SaveBatchTx(ctx context.Context, tx string, commit bool, table string, models interface{}, driver string, options ...*q.Schema) (int64, error) |
| 46 | + |
| 47 | + InsertAndCommit(ctx context.Context, tx string, table string, model interface{}, buildParam func(int) string, boolSupport bool, options ...*q.Schema) (int64, error) |
| 48 | + UpdateAndCommit(ctx context.Context, tx string, table string, model interface{}, driver string, buildParam func(int) string, boolSupport bool, options ...*q.Schema) (int64, error) |
| 49 | + SaveAndCommit(ctx context.Context, tx string, table string, model interface{}, driver string, options ...*q.Schema) (int64, error) |
| 50 | + InsertBatchAndCommit(ctx context.Context, tx string, table string, models interface{}, driver string, options ...*q.Schema) (int64, error) |
| 51 | + UpdateBatchAndCommit(ctx context.Context, tx string, table string, models interface{}, buildParam func(int) string, boolSupport bool, options ...*q.Schema) (int64, error) |
| 52 | + SaveBatchAndCommit(ctx context.Context, tx string, table string, models interface{}, driver string, options ...*q.Schema) (int64, error) |
| 53 | +} |
| 54 | + |
| 55 | +type Loader struct { |
| 56 | + Proxy Proxy |
| 57 | + BuildParam func(i int) string |
| 58 | + Map func(ctx context.Context, model interface{}) (interface{}, error) |
| 59 | + modelType reflect.Type |
| 60 | + modelsType reflect.Type |
| 61 | + keys []string |
| 62 | + mapJsonColumnKeys map[string]string |
| 63 | + fieldsIndex map[string]int |
| 64 | + table string |
| 65 | + IsRollback bool |
| 66 | +} |
| 67 | + |
| 68 | +func NewLoader(proxy Proxy, tableName string, modelType reflect.Type, buildParam func(i int) string, options ...func(context.Context, interface{}) (interface{}, error)) (*Loader, error) { |
| 69 | + _, idNames := q.FindPrimaryKeys(modelType) |
| 70 | + mapJsonColumnKeys := q.MapJsonColumn(modelType) |
| 71 | + modelsType := reflect.Zero(reflect.SliceOf(modelType)).Type() |
| 72 | + |
| 73 | + fieldsIndex, er0 := q.GetColumnIndexes(modelType) |
| 74 | + if er0 != nil { |
| 75 | + return nil, er0 |
| 76 | + } |
| 77 | + var mp func(context.Context, interface{}) (interface{}, error) |
| 78 | + if len(options) > 0 { |
| 79 | + mp = options[0] |
| 80 | + } |
| 81 | + return &Loader{Proxy: proxy, IsRollback: true, BuildParam: buildParam, Map: mp, modelType: modelType, modelsType: modelsType, keys: idNames, mapJsonColumnKeys: mapJsonColumnKeys, fieldsIndex: fieldsIndex, table: tableName}, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (s *Loader) Keys() []string { |
| 85 | + return s.keys |
| 86 | +} |
| 87 | + |
| 88 | +func (s *Loader) All(ctx context.Context) (interface{}, error) { |
| 89 | + query := q.BuildSelectAllQuery(s.table) |
| 90 | + result := reflect.New(s.modelsType).Interface() |
| 91 | + var err error |
| 92 | + tx := GetTx(ctx) |
| 93 | + if tx == nil { |
| 94 | + err = s.Proxy.Query(ctx, result, query) |
| 95 | + } else { |
| 96 | + err = s.Proxy.QueryTx(ctx, *tx, false, result, query) |
| 97 | + if err != nil { |
| 98 | + if s.IsRollback { |
| 99 | + s.Proxy.RollbackTransaction(ctx, *tx) |
| 100 | + } |
| 101 | + return result, err |
| 102 | + } |
| 103 | + } |
| 104 | + if err == nil { |
| 105 | + if s.Map != nil { |
| 106 | + return q.MapModels(ctx, result, s.Map) |
| 107 | + } |
| 108 | + return result, err |
| 109 | + } |
| 110 | + return result, err |
| 111 | +} |
| 112 | + |
| 113 | +func (s *Loader) Load(ctx context.Context, id interface{}) (interface{}, error) { |
| 114 | + queryFindById, values := q.BuildFindById(s.table, s.BuildParam, id, s.mapJsonColumnKeys, s.keys) |
| 115 | + tx := GetTx(ctx) |
| 116 | + result := reflect.New(s.modelsType).Interface() |
| 117 | + var r interface{} |
| 118 | + var er1 error |
| 119 | + if tx == nil { |
| 120 | + er1 = s.Proxy.Query(ctx, result, queryFindById, values...) |
| 121 | + } else { |
| 122 | + er1 = s.Proxy.QueryTx(ctx, *tx, false, result, queryFindById, values...) |
| 123 | + } |
| 124 | + if er1 != nil { |
| 125 | + if s.IsRollback && tx != nil { |
| 126 | + s.Proxy.RollbackTransaction(ctx, *tx) |
| 127 | + } |
| 128 | + return r, er1 |
| 129 | + } |
| 130 | + if s.Map != nil { |
| 131 | + _, er2 := s.Map(ctx, &r) |
| 132 | + if er2 != nil { |
| 133 | + return nil, er2 |
| 134 | + } |
| 135 | + vo := reflect.Indirect(reflect.ValueOf(r)) |
| 136 | + if vo.Kind() == reflect.Slice { |
| 137 | + if vo.Len() > 0 { |
| 138 | + return vo.Index(0).Addr(), nil |
| 139 | + } |
| 140 | + return nil, nil |
| 141 | + } |
| 142 | + return r, er2 |
| 143 | + } |
| 144 | + vo := reflect.Indirect(reflect.ValueOf(r)) |
| 145 | + if vo.Kind() == reflect.Slice { |
| 146 | + if vo.Len() > 0 { |
| 147 | + return vo.Index(0).Addr(), nil |
| 148 | + } |
| 149 | + return nil, nil |
| 150 | + } |
| 151 | + return r, er1 |
| 152 | +} |
0 commit comments