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