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