|
| 1 | +package database |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/konveyor/tackle2-hub/model" |
| 10 | + "gorm.io/gorm" |
| 11 | + "gorm.io/gorm/clause" |
| 12 | + "gorm.io/gorm/logger" |
| 13 | +) |
| 14 | + |
| 15 | +// PK singleton pk sequence. |
| 16 | +var PK PkSequence |
| 17 | + |
| 18 | +// PkSequence provides a primary key sequence. |
| 19 | +type PkSequence struct { |
| 20 | + mutex sync.Mutex |
| 21 | +} |
| 22 | + |
| 23 | +// Load highest key for all models. |
| 24 | +func (r *PkSequence) Load(db *gorm.DB, models []any) (err error) { |
| 25 | + r.mutex.Lock() |
| 26 | + defer r.mutex.Unlock() |
| 27 | + for _, m := range models { |
| 28 | + mt := reflect.TypeOf(m) |
| 29 | + if mt.Kind() == reflect.Ptr { |
| 30 | + mt = mt.Elem() |
| 31 | + } |
| 32 | + kind := strings.ToUpper(mt.Name()) |
| 33 | + db = r.session(db) |
| 34 | + q := db.Table(kind) |
| 35 | + q = q.Select("MAX(ID) id") |
| 36 | + cursor, err := q.Rows() |
| 37 | + if err != nil || !cursor.Next() { |
| 38 | + // not a table with id. |
| 39 | + // discarded. |
| 40 | + continue |
| 41 | + } |
| 42 | + id := int64(0) |
| 43 | + err = cursor.Scan(&id) |
| 44 | + _ = cursor.Close() |
| 45 | + if err != nil { |
| 46 | + r.add(db, kind, uint(0)) |
| 47 | + } else { |
| 48 | + r.add(db, kind, uint(id)) |
| 49 | + } |
| 50 | + } |
| 51 | + return |
| 52 | +} |
| 53 | + |
| 54 | +// Next returns the next primary key. |
| 55 | +func (r *PkSequence) Next(db *gorm.DB) (id uint) { |
| 56 | + r.mutex.Lock() |
| 57 | + defer r.mutex.Unlock() |
| 58 | + kind := strings.ToUpper(db.Statement.Table) |
| 59 | + m := &model.PK{} |
| 60 | + db = r.session(db) |
| 61 | + err := db.First(m, "Kind", kind).Error |
| 62 | + if err != nil { |
| 63 | + return |
| 64 | + } |
| 65 | + m.LastID++ |
| 66 | + id = m.LastID |
| 67 | + err = db.Save(m).Error |
| 68 | + if err != nil { |
| 69 | + panic(err) |
| 70 | + } |
| 71 | + return |
| 72 | +} |
| 73 | + |
| 74 | +// session returns a new DB with a new session. |
| 75 | +func (r *PkSequence) session(in *gorm.DB) (out *gorm.DB) { |
| 76 | + out = &gorm.DB{ |
| 77 | + Config: in.Config, |
| 78 | + } |
| 79 | + out.Config.Logger.LogMode(logger.Warn) |
| 80 | + out.Statement = &gorm.Statement{ |
| 81 | + DB: out, |
| 82 | + ConnPool: in.Statement.ConnPool, |
| 83 | + Context: in.Statement.Context, |
| 84 | + Clauses: map[string]clause.Clause{}, |
| 85 | + Vars: make([]interface{}, 0, 8), |
| 86 | + } |
| 87 | + return |
| 88 | +} |
| 89 | + |
| 90 | +// add the last (higher) id for the kind. |
| 91 | +func (r *PkSequence) add(db *gorm.DB, kind string, id uint) { |
| 92 | + m := &model.PK{Kind: kind} |
| 93 | + db = r.session(db) |
| 94 | + err := db.First(m).Error |
| 95 | + if err != nil { |
| 96 | + if !errors.Is(err, gorm.ErrRecordNotFound) { |
| 97 | + panic(err) |
| 98 | + } |
| 99 | + } |
| 100 | + if m.LastID > id { |
| 101 | + return |
| 102 | + } |
| 103 | + m.LastID = id |
| 104 | + db = r.session(db) |
| 105 | + err = db.Save(m).Error |
| 106 | + if err != nil { |
| 107 | + panic(err) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// assignPk assigns PK as needed. |
| 112 | +func assignPk(db *gorm.DB) { |
| 113 | + statement := db.Statement |
| 114 | + schema := statement.Schema |
| 115 | + if schema == nil { |
| 116 | + return |
| 117 | + } |
| 118 | + switch statement.ReflectValue.Kind() { |
| 119 | + case reflect.Slice, |
| 120 | + reflect.Array: |
| 121 | + for i := 0; i < statement.ReflectValue.Len(); i++ { |
| 122 | + for _, f := range schema.Fields { |
| 123 | + if f.Name != "ID" { |
| 124 | + continue |
| 125 | + } |
| 126 | + _, isZero := f.ValueOf( |
| 127 | + statement.Context, |
| 128 | + statement.ReflectValue.Index(i)) |
| 129 | + if isZero { |
| 130 | + id := PK.Next(db) |
| 131 | + _ = f.Set( |
| 132 | + statement.Context, |
| 133 | + statement.ReflectValue.Index(i), |
| 134 | + id) |
| 135 | + |
| 136 | + } |
| 137 | + break |
| 138 | + } |
| 139 | + } |
| 140 | + case reflect.Struct: |
| 141 | + for _, f := range schema.Fields { |
| 142 | + if f.Name != "ID" { |
| 143 | + continue |
| 144 | + } |
| 145 | + _, isZero := f.ValueOf( |
| 146 | + statement.Context, |
| 147 | + statement.ReflectValue) |
| 148 | + if isZero { |
| 149 | + id := PK.Next(db) |
| 150 | + _ = f.Set( |
| 151 | + statement.Context, |
| 152 | + statement.ReflectValue, |
| 153 | + id) |
| 154 | + } |
| 155 | + break |
| 156 | + } |
| 157 | + default: |
| 158 | + log.Info("[WARN] assignPk: unknown kind.") |
| 159 | + } |
| 160 | +} |
0 commit comments