github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/models/account__generated.go (about) 1 // This is a generated source file. DO NOT EDIT 2 // Source: models/account__generated.go 3 4 package models 5 6 import ( 7 "fmt" 8 "time" 9 10 "github.com/machinefi/w3bstream/pkg/depends/base/types" 11 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx" 12 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder" 13 ) 14 15 var AccountTable *builder.Table 16 17 func init() { 18 AccountTable = DB.Register(&Account{}) 19 } 20 21 type AccountIterator struct { 22 } 23 24 func (*AccountIterator) New() interface{} { 25 return &Account{} 26 } 27 28 func (*AccountIterator) Resolve(v interface{}) *Account { 29 return v.(*Account) 30 } 31 32 func (*Account) TableName() string { 33 return "t_account" 34 } 35 36 func (*Account) TableDesc() []string { 37 return []string{ 38 "Account w3bstream account", 39 } 40 } 41 42 func (*Account) Comments() map[string]string { 43 return map[string]string{ 44 "AccountID": "AccountID account id", 45 "OperatorPrivateKey": "Deprecated: pls use pkg/modules/operator", 46 } 47 } 48 49 func (*Account) ColDesc() map[string][]string { 50 return map[string][]string{ 51 "AccountID": []string{ 52 "AccountID account id", 53 }, 54 "OperatorPrivateKey": []string{ 55 "Deprecated: pls use pkg/modules/operator", 56 }, 57 } 58 } 59 60 func (*Account) ColRel() map[string][]string { 61 return map[string][]string{ 62 "AccountID": []string{ 63 "Account", 64 "AccountID", 65 }, 66 } 67 } 68 69 func (*Account) PrimaryKey() []string { 70 return []string{ 71 "ID", 72 } 73 } 74 75 func (m *Account) IndexFieldNames() []string { 76 return []string{ 77 "AccountID", 78 "ID", 79 } 80 } 81 82 func (*Account) UniqueIndexes() builder.Indexes { 83 return builder.Indexes{ 84 "ui_account_id": []string{ 85 "AccountID", 86 "DeletedAt", 87 }, 88 } 89 } 90 91 func (*Account) UniqueIndexUIAccountID() string { 92 return "ui_account_id" 93 } 94 95 func (m *Account) ColID() *builder.Column { 96 return AccountTable.ColByFieldName(m.FieldID()) 97 } 98 99 func (*Account) FieldID() string { 100 return "ID" 101 } 102 103 func (m *Account) ColAccountID() *builder.Column { 104 return AccountTable.ColByFieldName(m.FieldAccountID()) 105 } 106 107 func (*Account) FieldAccountID() string { 108 return "AccountID" 109 } 110 111 func (m *Account) ColRole() *builder.Column { 112 return AccountTable.ColByFieldName(m.FieldRole()) 113 } 114 115 func (*Account) FieldRole() string { 116 return "Role" 117 } 118 119 func (m *Account) ColState() *builder.Column { 120 return AccountTable.ColByFieldName(m.FieldState()) 121 } 122 123 func (*Account) FieldState() string { 124 return "State" 125 } 126 127 func (m *Account) ColAvatar() *builder.Column { 128 return AccountTable.ColByFieldName(m.FieldAvatar()) 129 } 130 131 func (*Account) FieldAvatar() string { 132 return "Avatar" 133 } 134 135 func (m *Account) ColMeta() *builder.Column { 136 return AccountTable.ColByFieldName(m.FieldMeta()) 137 } 138 139 func (*Account) FieldMeta() string { 140 return "Meta" 141 } 142 143 func (m *Account) ColOperatorPrivateKey() *builder.Column { 144 return AccountTable.ColByFieldName(m.FieldOperatorPrivateKey()) 145 } 146 147 func (*Account) FieldOperatorPrivateKey() string { 148 return "OperatorPrivateKey" 149 } 150 151 func (m *Account) ColCreatedAt() *builder.Column { 152 return AccountTable.ColByFieldName(m.FieldCreatedAt()) 153 } 154 155 func (*Account) FieldCreatedAt() string { 156 return "CreatedAt" 157 } 158 159 func (m *Account) ColUpdatedAt() *builder.Column { 160 return AccountTable.ColByFieldName(m.FieldUpdatedAt()) 161 } 162 163 func (*Account) FieldUpdatedAt() string { 164 return "UpdatedAt" 165 } 166 167 func (m *Account) ColDeletedAt() *builder.Column { 168 return AccountTable.ColByFieldName(m.FieldDeletedAt()) 169 } 170 171 func (*Account) FieldDeletedAt() string { 172 return "DeletedAt" 173 } 174 175 func (m *Account) CondByValue(db sqlx.DBExecutor) builder.SqlCondition { 176 var ( 177 tbl = db.T(m) 178 fvs = builder.FieldValueFromStructByNoneZero(m) 179 cond = []builder.SqlCondition{tbl.ColByFieldName("DeletedAt").Eq(0)} 180 ) 181 182 for _, fn := range m.IndexFieldNames() { 183 if v, ok := fvs[fn]; ok { 184 cond = append(cond, tbl.ColByFieldName(fn).Eq(v)) 185 delete(fvs, fn) 186 } 187 } 188 if len(cond) == 0 { 189 panic(fmt.Errorf("no field for indexes has value")) 190 } 191 for fn, v := range fvs { 192 cond = append(cond, tbl.ColByFieldName(fn).Eq(v)) 193 } 194 return builder.And(cond...) 195 } 196 197 func (m *Account) Create(db sqlx.DBExecutor) error { 198 199 if m.CreatedAt.IsZero() { 200 m.CreatedAt.Set(time.Now()) 201 } 202 203 if m.UpdatedAt.IsZero() { 204 m.UpdatedAt.Set(time.Now()) 205 } 206 207 _, err := db.Exec(sqlx.InsertToDB(db, m, nil)) 208 return err 209 } 210 211 func (m *Account) List(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) ([]Account, error) { 212 var ( 213 tbl = db.T(m) 214 lst = make([]Account, 0) 215 ) 216 cond = builder.And(tbl.ColByFieldName("DeletedAt").Eq(0), cond) 217 adds = append([]builder.Addition{builder.Where(cond), builder.Comment("Account.List")}, adds...) 218 err := db.QueryAndScan(builder.Select(nil).From(tbl, adds...), &lst) 219 return lst, err 220 } 221 222 func (m *Account) Count(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) (cnt int64, err error) { 223 tbl := db.T(m) 224 cond = builder.And(tbl.ColByFieldName("DeletedAt").Eq(0), cond) 225 adds = append([]builder.Addition{builder.Where(cond), builder.Comment("Account.List")}, adds...) 226 err = db.QueryAndScan(builder.Select(builder.Count()).From(tbl, adds...), &cnt) 227 return 228 } 229 230 func (m *Account) FetchByID(db sqlx.DBExecutor) error { 231 tbl := db.T(m) 232 err := db.QueryAndScan( 233 builder.Select(nil). 234 From( 235 tbl, 236 builder.Where( 237 builder.And( 238 tbl.ColByFieldName("ID").Eq(m.ID), 239 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 240 ), 241 ), 242 builder.Comment("Account.FetchByID"), 243 ), 244 m, 245 ) 246 return err 247 } 248 249 func (m *Account) FetchByAccountID(db sqlx.DBExecutor) error { 250 tbl := db.T(m) 251 err := db.QueryAndScan( 252 builder.Select(nil). 253 From( 254 tbl, 255 builder.Where( 256 builder.And( 257 tbl.ColByFieldName("AccountID").Eq(m.AccountID), 258 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 259 ), 260 ), 261 builder.Comment("Account.FetchByAccountID"), 262 ), 263 m, 264 ) 265 return err 266 } 267 268 func (m *Account) UpdateByIDWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error { 269 270 if _, ok := fvs["UpdatedAt"]; !ok { 271 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 272 } 273 tbl := db.T(m) 274 res, err := db.Exec( 275 builder.Update(tbl). 276 Where( 277 builder.And( 278 tbl.ColByFieldName("ID").Eq(m.ID), 279 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 280 ), 281 builder.Comment("Account.UpdateByIDWithFVs"), 282 ). 283 Set(tbl.AssignmentsByFieldValues(fvs)...), 284 ) 285 if err != nil { 286 return err 287 } 288 if affected, _ := res.RowsAffected(); affected == 0 { 289 return m.FetchByID(db) 290 } 291 return nil 292 } 293 294 func (m *Account) UpdateByID(db sqlx.DBExecutor, zeros ...string) error { 295 fvs := builder.FieldValueFromStructByNoneZero(m, zeros...) 296 return m.UpdateByIDWithFVs(db, fvs) 297 } 298 299 func (m *Account) UpdateByAccountIDWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error { 300 301 if _, ok := fvs["UpdatedAt"]; !ok { 302 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 303 } 304 tbl := db.T(m) 305 res, err := db.Exec( 306 builder.Update(tbl). 307 Where( 308 builder.And( 309 tbl.ColByFieldName("AccountID").Eq(m.AccountID), 310 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 311 ), 312 builder.Comment("Account.UpdateByAccountIDWithFVs"), 313 ). 314 Set(tbl.AssignmentsByFieldValues(fvs)...), 315 ) 316 if err != nil { 317 return err 318 } 319 if affected, _ := res.RowsAffected(); affected == 0 { 320 return m.FetchByAccountID(db) 321 } 322 return nil 323 } 324 325 func (m *Account) UpdateByAccountID(db sqlx.DBExecutor, zeros ...string) error { 326 fvs := builder.FieldValueFromStructByNoneZero(m, zeros...) 327 return m.UpdateByAccountIDWithFVs(db, fvs) 328 } 329 330 func (m *Account) Delete(db sqlx.DBExecutor) error { 331 _, err := db.Exec( 332 builder.Delete(). 333 From( 334 db.T(m), 335 builder.Where(m.CondByValue(db)), 336 builder.Comment("Account.Delete"), 337 ), 338 ) 339 return err 340 } 341 342 func (m *Account) DeleteByID(db sqlx.DBExecutor) error { 343 tbl := db.T(m) 344 _, err := db.Exec( 345 builder.Delete(). 346 From( 347 tbl, 348 builder.Where( 349 builder.And( 350 tbl.ColByFieldName("ID").Eq(m.ID), 351 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 352 ), 353 ), 354 builder.Comment("Account.DeleteByID"), 355 ), 356 ) 357 return err 358 } 359 360 func (m *Account) SoftDeleteByID(db sqlx.DBExecutor) error { 361 tbl := db.T(m) 362 fvs := builder.FieldValues{} 363 364 if _, ok := fvs["DeletedAt"]; !ok { 365 fvs["DeletedAt"] = types.Timestamp{Time: time.Now()} 366 } 367 368 if _, ok := fvs["UpdatedAt"]; !ok { 369 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 370 } 371 _, err := db.Exec( 372 builder.Update(db.T(m)). 373 Where( 374 builder.And( 375 tbl.ColByFieldName("ID").Eq(m.ID), 376 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 377 ), 378 builder.Comment("Account.SoftDeleteByID"), 379 ). 380 Set(tbl.AssignmentsByFieldValues(fvs)...), 381 ) 382 return err 383 } 384 385 func (m *Account) DeleteByAccountID(db sqlx.DBExecutor) error { 386 tbl := db.T(m) 387 _, err := db.Exec( 388 builder.Delete(). 389 From( 390 tbl, 391 builder.Where( 392 builder.And( 393 tbl.ColByFieldName("AccountID").Eq(m.AccountID), 394 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 395 ), 396 ), 397 builder.Comment("Account.DeleteByAccountID"), 398 ), 399 ) 400 return err 401 } 402 403 func (m *Account) SoftDeleteByAccountID(db sqlx.DBExecutor) error { 404 tbl := db.T(m) 405 fvs := builder.FieldValues{} 406 407 if _, ok := fvs["DeletedAt"]; !ok { 408 fvs["DeletedAt"] = types.Timestamp{Time: time.Now()} 409 } 410 411 if _, ok := fvs["UpdatedAt"]; !ok { 412 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 413 } 414 _, err := db.Exec( 415 builder.Update(db.T(m)). 416 Where( 417 builder.And( 418 tbl.ColByFieldName("AccountID").Eq(m.AccountID), 419 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 420 ), 421 builder.Comment("Account.SoftDeleteByAccountID"), 422 ). 423 Set(tbl.AssignmentsByFieldValues(fvs)...), 424 ) 425 return err 426 }