github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/models/resource__generated.go (about)

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