|
| 1 | +package dao |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "database/sql/driver" |
| 7 | + "encoding/json" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "reflect" |
| 11 | + |
| 12 | + q "github.com/core-go/sql" |
| 13 | +) |
| 14 | + |
| 15 | +type Dao[T any, K any] struct { |
| 16 | + *Writer[*T] |
| 17 | + Map map[string]int |
| 18 | + Fields string |
| 19 | + IdMap bool |
| 20 | +} |
| 21 | + |
| 22 | +func NewDao[T any, K any](db *sql.DB, tableName string, opts ...func(int) string) (*Dao[T, K], error) { |
| 23 | + return NewSqlDaoWithVersionAndArray[T, K](db, tableName, "", nil, opts...) |
| 24 | +} |
| 25 | +func NewDaoWithVersion[T any, K any](db *sql.DB, tableName string, versionField string, opts ...func(int) string) (*Dao[T, K], error) { |
| 26 | + return NewSqlDaoWithVersionAndArray[T, K](db, tableName, versionField, nil, opts...) |
| 27 | +} |
| 28 | +func NewSqlDaoWithVersionAndArray[T any, K any](db *sql.DB, tableName string, versionField string, toArray func(interface{}) interface { |
| 29 | + driver.Valuer |
| 30 | + sql.Scanner |
| 31 | +}, opts ...func(int) string) (*Dao[T, K], error) { |
| 32 | + adapter, err := NewSqlWriterWithVersionAndArray[*T](db, tableName, versionField, toArray, opts...) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + |
| 37 | + var t T |
| 38 | + modelType := reflect.TypeOf(t) |
| 39 | + if modelType.Kind() != reflect.Struct { |
| 40 | + return nil, errors.New("T must be a struct") |
| 41 | + } |
| 42 | + |
| 43 | + var k K |
| 44 | + kType := reflect.TypeOf(k) |
| 45 | + idMap := false |
| 46 | + if len(adapter.Keys) > 1 { |
| 47 | + if kType.Kind() == reflect.Map { |
| 48 | + idMap = true |
| 49 | + } else if kType.Kind() != reflect.Struct { |
| 50 | + return nil, errors.New("for composite keys, K must be a struct or a map") |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + fieldsIndex, err := q.GetColumnIndexes(modelType) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + fields := q.BuildFieldsBySchema(adapter.Schema) |
| 59 | + return &Dao[T, K]{adapter, fieldsIndex, fields, idMap}, nil |
| 60 | +} |
| 61 | +func (a *Dao[T, K]) All(ctx context.Context) ([]T, error) { |
| 62 | + var objs []T |
| 63 | + query := fmt.Sprintf("select %s from %s", a.Fields, a.Table) |
| 64 | + tx := q.GetExec(ctx, a.DB, a.TxKey) |
| 65 | + err := q.Query(ctx, tx, a.Map, &objs, query) |
| 66 | + return objs, err |
| 67 | +} |
| 68 | +func toMap(obj interface{}) (map[string]interface{}, error) { |
| 69 | + b, err := json.Marshal(obj) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + im := make(map[string]interface{}) |
| 74 | + er2 := json.Unmarshal(b, &im) |
| 75 | + return im, er2 |
| 76 | +} |
| 77 | +func (a *Dao[T, K]) getId(k K) (interface{}, error) { |
| 78 | + if len(a.Keys) >= 2 && !a.IdMap { |
| 79 | + ri, err := toMap(k) |
| 80 | + return ri, err |
| 81 | + } else { |
| 82 | + return k, nil |
| 83 | + } |
| 84 | +} |
| 85 | +func (a *Dao[T, K]) Load(ctx context.Context, id K) (*T, error) { |
| 86 | + ip, er0 := a.getId(id) |
| 87 | + if er0 != nil { |
| 88 | + return nil, er0 |
| 89 | + } |
| 90 | + var objs []T |
| 91 | + query := fmt.Sprintf("select %s from %s ", a.Fields, a.Table) |
| 92 | + query1, args := q.BuildFindByIdWithDB(a.DB, query, ip, a.JsonColumnMap, a.Keys, a.BuildParam) |
| 93 | + tx := q.GetExec(ctx, a.DB, a.TxKey) |
| 94 | + err := q.Query(ctx, tx, a.Map, &objs, query1, args...) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + if len(objs) > 0 { |
| 99 | + return &objs[0], nil |
| 100 | + } |
| 101 | + return nil, nil |
| 102 | +} |
| 103 | +func (a *Dao[T, K]) Exist(ctx context.Context, id K) (bool, error) { |
| 104 | + ip, er0 := a.getId(id) |
| 105 | + if er0 != nil { |
| 106 | + return false, er0 |
| 107 | + } |
| 108 | + query := fmt.Sprintf("select %s from %s ", a.Schema.SColumns[0], a.Table) |
| 109 | + query1, args := q.BuildFindByIdWithDB(a.DB, query, ip, a.JsonColumnMap, a.Keys, a.BuildParam) |
| 110 | + tx := q.GetExec(ctx, a.DB, a.TxKey) |
| 111 | + rows, err := tx.QueryContext(ctx, query1, args...) |
| 112 | + if err != nil { |
| 113 | + return false, err |
| 114 | + } |
| 115 | + defer rows.Close() |
| 116 | + for rows.Next() { |
| 117 | + return true, nil |
| 118 | + } |
| 119 | + return false, nil |
| 120 | +} |
| 121 | +func (a *Dao[T, K]) Delete(ctx context.Context, id K) (int64, error) { |
| 122 | + ip, er0 := a.getId(id) |
| 123 | + if er0 != nil { |
| 124 | + return -1, er0 |
| 125 | + } |
| 126 | + query := fmt.Sprintf("delete from %s ", a.Table) |
| 127 | + query1, args := q.BuildFindByIdWithDB(a.DB, query, ip, a.JsonColumnMap, a.Keys, a.BuildParam) |
| 128 | + tx := q.GetExec(ctx, a.DB, a.TxKey) |
| 129 | + res, err := tx.ExecContext(ctx, query1, args...) |
| 130 | + if err != nil { |
| 131 | + return -1, err |
| 132 | + } |
| 133 | + return res.RowsAffected() |
| 134 | +} |
0 commit comments