github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/models/resource_ownership__generated.go (about) 1 // This is a generated source file. DO NOT EDIT 2 // Source: models/resource_ownership__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 ResourceOwnershipTable *builder.Table 16 17 func init() { 18 ResourceOwnershipTable = DB.Register(&ResourceOwnership{}) 19 } 20 21 type ResourceOwnershipIterator struct { 22 } 23 24 func (*ResourceOwnershipIterator) New() interface{} { 25 return &ResourceOwnership{} 26 } 27 28 func (*ResourceOwnershipIterator) Resolve(v interface{}) *ResourceOwnership { 29 return v.(*ResourceOwnership) 30 } 31 32 func (*ResourceOwnership) TableName() string { 33 return "t_resource_ownership" 34 } 35 36 func (*ResourceOwnership) TableDesc() []string { 37 return []string{ 38 "ResourceOwnership database model resource ownership", 39 } 40 } 41 42 func (*ResourceOwnership) Comments() map[string]string { 43 return map[string]string{ 44 "AccountID": "AccountID account id", 45 } 46 } 47 48 func (*ResourceOwnership) ColDesc() map[string][]string { 49 return map[string][]string{ 50 "AccountID": []string{ 51 "AccountID account id", 52 }, 53 } 54 } 55 56 func (*ResourceOwnership) ColRel() map[string][]string { 57 return map[string][]string{ 58 "AccountID": []string{ 59 "Account", 60 "AccountID", 61 }, 62 } 63 } 64 65 func (*ResourceOwnership) PrimaryKey() []string { 66 return []string{ 67 "ID", 68 } 69 } 70 71 func (m *ResourceOwnership) IndexFieldNames() []string { 72 return []string{ 73 "AccountID", 74 "ID", 75 "ResourceID", 76 } 77 } 78 79 func (*ResourceOwnership) UniqueIndexes() builder.Indexes { 80 return builder.Indexes{ 81 "ui_resource_account": []string{ 82 "ResourceID", 83 "AccountID", 84 }, 85 } 86 } 87 88 func (*ResourceOwnership) UniqueIndexUIResourceAccount() string { 89 return "ui_resource_account" 90 } 91 92 func (m *ResourceOwnership) ColID() *builder.Column { 93 return ResourceOwnershipTable.ColByFieldName(m.FieldID()) 94 } 95 96 func (*ResourceOwnership) FieldID() string { 97 return "ID" 98 } 99 100 func (m *ResourceOwnership) ColResourceID() *builder.Column { 101 return ResourceOwnershipTable.ColByFieldName(m.FieldResourceID()) 102 } 103 104 func (*ResourceOwnership) FieldResourceID() string { 105 return "ResourceID" 106 } 107 108 func (m *ResourceOwnership) ColAccountID() *builder.Column { 109 return ResourceOwnershipTable.ColByFieldName(m.FieldAccountID()) 110 } 111 112 func (*ResourceOwnership) FieldAccountID() string { 113 return "AccountID" 114 } 115 116 func (m *ResourceOwnership) ColUploadedAt() *builder.Column { 117 return ResourceOwnershipTable.ColByFieldName(m.FieldUploadedAt()) 118 } 119 120 func (*ResourceOwnership) FieldUploadedAt() string { 121 return "UploadedAt" 122 } 123 124 func (m *ResourceOwnership) ColExpireAt() *builder.Column { 125 return ResourceOwnershipTable.ColByFieldName(m.FieldExpireAt()) 126 } 127 128 func (*ResourceOwnership) FieldExpireAt() string { 129 return "ExpireAt" 130 } 131 132 func (m *ResourceOwnership) ColFilename() *builder.Column { 133 return ResourceOwnershipTable.ColByFieldName(m.FieldFilename()) 134 } 135 136 func (*ResourceOwnership) FieldFilename() string { 137 return "Filename" 138 } 139 140 func (m *ResourceOwnership) ColComment() *builder.Column { 141 return ResourceOwnershipTable.ColByFieldName(m.FieldComment()) 142 } 143 144 func (*ResourceOwnership) FieldComment() string { 145 return "Comment" 146 } 147 148 func (m *ResourceOwnership) ColCreatedAt() *builder.Column { 149 return ResourceOwnershipTable.ColByFieldName(m.FieldCreatedAt()) 150 } 151 152 func (*ResourceOwnership) FieldCreatedAt() string { 153 return "CreatedAt" 154 } 155 156 func (m *ResourceOwnership) ColUpdatedAt() *builder.Column { 157 return ResourceOwnershipTable.ColByFieldName(m.FieldUpdatedAt()) 158 } 159 160 func (*ResourceOwnership) FieldUpdatedAt() string { 161 return "UpdatedAt" 162 } 163 164 func (m *ResourceOwnership) CondByValue(db sqlx.DBExecutor) builder.SqlCondition { 165 var ( 166 tbl = db.T(m) 167 fvs = builder.FieldValueFromStructByNoneZero(m) 168 cond = make([]builder.SqlCondition, 0) 169 ) 170 171 for _, fn := range m.IndexFieldNames() { 172 if v, ok := fvs[fn]; ok { 173 cond = append(cond, tbl.ColByFieldName(fn).Eq(v)) 174 delete(fvs, fn) 175 } 176 } 177 if len(cond) == 0 { 178 panic(fmt.Errorf("no field for indexes has value")) 179 } 180 for fn, v := range fvs { 181 cond = append(cond, tbl.ColByFieldName(fn).Eq(v)) 182 } 183 return builder.And(cond...) 184 } 185 186 func (m *ResourceOwnership) Create(db sqlx.DBExecutor) error { 187 188 if m.CreatedAt.IsZero() { 189 m.CreatedAt.Set(time.Now()) 190 } 191 192 if m.UpdatedAt.IsZero() { 193 m.UpdatedAt.Set(time.Now()) 194 } 195 196 _, err := db.Exec(sqlx.InsertToDB(db, m, nil)) 197 return err 198 } 199 200 func (m *ResourceOwnership) List(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) ([]ResourceOwnership, error) { 201 var ( 202 tbl = db.T(m) 203 lst = make([]ResourceOwnership, 0) 204 ) 205 adds = append([]builder.Addition{builder.Where(cond), builder.Comment("ResourceOwnership.List")}, adds...) 206 err := db.QueryAndScan(builder.Select(nil).From(tbl, adds...), &lst) 207 return lst, err 208 } 209 210 func (m *ResourceOwnership) Count(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) (cnt int64, err error) { 211 tbl := db.T(m) 212 adds = append([]builder.Addition{builder.Where(cond), builder.Comment("ResourceOwnership.List")}, adds...) 213 err = db.QueryAndScan(builder.Select(builder.Count()).From(tbl, adds...), &cnt) 214 return 215 } 216 217 func (m *ResourceOwnership) FetchByID(db sqlx.DBExecutor) error { 218 tbl := db.T(m) 219 err := db.QueryAndScan( 220 builder.Select(nil). 221 From( 222 tbl, 223 builder.Where( 224 builder.And( 225 tbl.ColByFieldName("ID").Eq(m.ID), 226 ), 227 ), 228 builder.Comment("ResourceOwnership.FetchByID"), 229 ), 230 m, 231 ) 232 return err 233 } 234 235 func (m *ResourceOwnership) FetchByResourceIDAndAccountID(db sqlx.DBExecutor) error { 236 tbl := db.T(m) 237 err := db.QueryAndScan( 238 builder.Select(nil). 239 From( 240 tbl, 241 builder.Where( 242 builder.And( 243 tbl.ColByFieldName("ResourceID").Eq(m.ResourceID), 244 tbl.ColByFieldName("AccountID").Eq(m.AccountID), 245 ), 246 ), 247 builder.Comment("ResourceOwnership.FetchByResourceIDAndAccountID"), 248 ), 249 m, 250 ) 251 return err 252 } 253 254 func (m *ResourceOwnership) UpdateByIDWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error { 255 256 if _, ok := fvs["UpdatedAt"]; !ok { 257 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 258 } 259 tbl := db.T(m) 260 res, err := db.Exec( 261 builder.Update(tbl). 262 Where( 263 builder.And( 264 tbl.ColByFieldName("ID").Eq(m.ID), 265 ), 266 builder.Comment("ResourceOwnership.UpdateByIDWithFVs"), 267 ). 268 Set(tbl.AssignmentsByFieldValues(fvs)...), 269 ) 270 if err != nil { 271 return err 272 } 273 if affected, _ := res.RowsAffected(); affected == 0 { 274 return m.FetchByID(db) 275 } 276 return nil 277 } 278 279 func (m *ResourceOwnership) UpdateByID(db sqlx.DBExecutor, zeros ...string) error { 280 fvs := builder.FieldValueFromStructByNoneZero(m, zeros...) 281 return m.UpdateByIDWithFVs(db, fvs) 282 } 283 284 func (m *ResourceOwnership) UpdateByResourceIDAndAccountIDWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error { 285 286 if _, ok := fvs["UpdatedAt"]; !ok { 287 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 288 } 289 tbl := db.T(m) 290 res, err := db.Exec( 291 builder.Update(tbl). 292 Where( 293 builder.And( 294 tbl.ColByFieldName("ResourceID").Eq(m.ResourceID), 295 tbl.ColByFieldName("AccountID").Eq(m.AccountID), 296 ), 297 builder.Comment("ResourceOwnership.UpdateByResourceIDAndAccountIDWithFVs"), 298 ). 299 Set(tbl.AssignmentsByFieldValues(fvs)...), 300 ) 301 if err != nil { 302 return err 303 } 304 if affected, _ := res.RowsAffected(); affected == 0 { 305 return m.FetchByResourceIDAndAccountID(db) 306 } 307 return nil 308 } 309 310 func (m *ResourceOwnership) UpdateByResourceIDAndAccountID(db sqlx.DBExecutor, zeros ...string) error { 311 fvs := builder.FieldValueFromStructByNoneZero(m, zeros...) 312 return m.UpdateByResourceIDAndAccountIDWithFVs(db, fvs) 313 } 314 315 func (m *ResourceOwnership) Delete(db sqlx.DBExecutor) error { 316 _, err := db.Exec( 317 builder.Delete(). 318 From( 319 db.T(m), 320 builder.Where(m.CondByValue(db)), 321 builder.Comment("ResourceOwnership.Delete"), 322 ), 323 ) 324 return err 325 } 326 327 func (m *ResourceOwnership) DeleteByID(db sqlx.DBExecutor) error { 328 tbl := db.T(m) 329 _, err := db.Exec( 330 builder.Delete(). 331 From( 332 tbl, 333 builder.Where( 334 builder.And( 335 tbl.ColByFieldName("ID").Eq(m.ID), 336 ), 337 ), 338 builder.Comment("ResourceOwnership.DeleteByID"), 339 ), 340 ) 341 return err 342 } 343 344 func (m *ResourceOwnership) DeleteByResourceIDAndAccountID(db sqlx.DBExecutor) error { 345 tbl := db.T(m) 346 _, err := db.Exec( 347 builder.Delete(). 348 From( 349 tbl, 350 builder.Where( 351 builder.And( 352 tbl.ColByFieldName("ResourceID").Eq(m.ResourceID), 353 tbl.ColByFieldName("AccountID").Eq(m.AccountID), 354 ), 355 ), 356 builder.Comment("ResourceOwnership.DeleteByResourceIDAndAccountID"), 357 ), 358 ) 359 return err 360 }