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

     1  // This is a generated source file. DO NOT EDIT
     2  // Source: models/traffic_limit__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 TrafficLimitTable *builder.Table
    16  
    17  func init() {
    18  	TrafficLimitTable = DB.Register(&TrafficLimit{})
    19  }
    20  
    21  type TrafficLimitIterator struct {
    22  }
    23  
    24  func (*TrafficLimitIterator) New() interface{} {
    25  	return &TrafficLimit{}
    26  }
    27  
    28  func (*TrafficLimitIterator) Resolve(v interface{}) *TrafficLimit {
    29  	return v.(*TrafficLimit)
    30  }
    31  
    32  func (*TrafficLimit) TableName() string {
    33  	return "t_traffic_limit"
    34  }
    35  
    36  func (*TrafficLimit) TableDesc() []string {
    37  	return []string{
    38  		"TrafficLimit traffic limit for each project",
    39  	}
    40  }
    41  
    42  func (*TrafficLimit) Comments() map[string]string {
    43  	return map[string]string{}
    44  }
    45  
    46  func (*TrafficLimit) ColDesc() map[string][]string {
    47  	return map[string][]string{}
    48  }
    49  
    50  func (*TrafficLimit) ColRel() map[string][]string {
    51  	return map[string][]string{}
    52  }
    53  
    54  func (*TrafficLimit) PrimaryKey() []string {
    55  	return []string{
    56  		"ID",
    57  	}
    58  }
    59  
    60  func (m *TrafficLimit) IndexFieldNames() []string {
    61  	return []string{
    62  		"ApiType",
    63  		"ID",
    64  		"ProjectID",
    65  		"TrafficLimitID",
    66  	}
    67  }
    68  
    69  func (*TrafficLimit) UniqueIndexes() builder.Indexes {
    70  	return builder.Indexes{
    71  		"ui_prj_api_type": []string{
    72  			"ProjectID",
    73  			"ApiType",
    74  			"DeletedAt",
    75  		},
    76  		"ui_traffic_limit_id": []string{
    77  			"TrafficLimitID",
    78  			"DeletedAt",
    79  		},
    80  	}
    81  }
    82  
    83  func (*TrafficLimit) UniqueIndexUIPrjAPIType() string {
    84  	return "ui_prj_api_type"
    85  }
    86  
    87  func (*TrafficLimit) UniqueIndexUITrafficLimitID() string {
    88  	return "ui_traffic_limit_id"
    89  }
    90  
    91  func (m *TrafficLimit) ColID() *builder.Column {
    92  	return TrafficLimitTable.ColByFieldName(m.FieldID())
    93  }
    94  
    95  func (*TrafficLimit) FieldID() string {
    96  	return "ID"
    97  }
    98  
    99  func (m *TrafficLimit) ColTrafficLimitID() *builder.Column {
   100  	return TrafficLimitTable.ColByFieldName(m.FieldTrafficLimitID())
   101  }
   102  
   103  func (*TrafficLimit) FieldTrafficLimitID() string {
   104  	return "TrafficLimitID"
   105  }
   106  
   107  func (m *TrafficLimit) ColProjectID() *builder.Column {
   108  	return TrafficLimitTable.ColByFieldName(m.FieldProjectID())
   109  }
   110  
   111  func (*TrafficLimit) FieldProjectID() string {
   112  	return "ProjectID"
   113  }
   114  
   115  func (m *TrafficLimit) ColThreshold() *builder.Column {
   116  	return TrafficLimitTable.ColByFieldName(m.FieldThreshold())
   117  }
   118  
   119  func (*TrafficLimit) FieldThreshold() string {
   120  	return "Threshold"
   121  }
   122  
   123  func (m *TrafficLimit) ColDuration() *builder.Column {
   124  	return TrafficLimitTable.ColByFieldName(m.FieldDuration())
   125  }
   126  
   127  func (*TrafficLimit) FieldDuration() string {
   128  	return "Duration"
   129  }
   130  
   131  func (m *TrafficLimit) ColApiType() *builder.Column {
   132  	return TrafficLimitTable.ColByFieldName(m.FieldApiType())
   133  }
   134  
   135  func (*TrafficLimit) FieldApiType() string {
   136  	return "ApiType"
   137  }
   138  
   139  func (m *TrafficLimit) ColStartAt() *builder.Column {
   140  	return TrafficLimitTable.ColByFieldName(m.FieldStartAt())
   141  }
   142  
   143  func (*TrafficLimit) FieldStartAt() string {
   144  	return "StartAt"
   145  }
   146  
   147  func (m *TrafficLimit) ColCreatedAt() *builder.Column {
   148  	return TrafficLimitTable.ColByFieldName(m.FieldCreatedAt())
   149  }
   150  
   151  func (*TrafficLimit) FieldCreatedAt() string {
   152  	return "CreatedAt"
   153  }
   154  
   155  func (m *TrafficLimit) ColUpdatedAt() *builder.Column {
   156  	return TrafficLimitTable.ColByFieldName(m.FieldUpdatedAt())
   157  }
   158  
   159  func (*TrafficLimit) FieldUpdatedAt() string {
   160  	return "UpdatedAt"
   161  }
   162  
   163  func (m *TrafficLimit) ColDeletedAt() *builder.Column {
   164  	return TrafficLimitTable.ColByFieldName(m.FieldDeletedAt())
   165  }
   166  
   167  func (*TrafficLimit) FieldDeletedAt() string {
   168  	return "DeletedAt"
   169  }
   170  
   171  func (m *TrafficLimit) CondByValue(db sqlx.DBExecutor) builder.SqlCondition {
   172  	var (
   173  		tbl  = db.T(m)
   174  		fvs  = builder.FieldValueFromStructByNoneZero(m)
   175  		cond = []builder.SqlCondition{tbl.ColByFieldName("DeletedAt").Eq(0)}
   176  	)
   177  
   178  	for _, fn := range m.IndexFieldNames() {
   179  		if v, ok := fvs[fn]; ok {
   180  			cond = append(cond, tbl.ColByFieldName(fn).Eq(v))
   181  			delete(fvs, fn)
   182  		}
   183  	}
   184  	if len(cond) == 0 {
   185  		panic(fmt.Errorf("no field for indexes has value"))
   186  	}
   187  	for fn, v := range fvs {
   188  		cond = append(cond, tbl.ColByFieldName(fn).Eq(v))
   189  	}
   190  	return builder.And(cond...)
   191  }
   192  
   193  func (m *TrafficLimit) Create(db sqlx.DBExecutor) error {
   194  
   195  	if m.CreatedAt.IsZero() {
   196  		m.CreatedAt.Set(time.Now())
   197  	}
   198  
   199  	if m.UpdatedAt.IsZero() {
   200  		m.UpdatedAt.Set(time.Now())
   201  	}
   202  
   203  	_, err := db.Exec(sqlx.InsertToDB(db, m, nil))
   204  	return err
   205  }
   206  
   207  func (m *TrafficLimit) List(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) ([]TrafficLimit, error) {
   208  	var (
   209  		tbl = db.T(m)
   210  		lst = make([]TrafficLimit, 0)
   211  	)
   212  	cond = builder.And(tbl.ColByFieldName("DeletedAt").Eq(0), cond)
   213  	adds = append([]builder.Addition{builder.Where(cond), builder.Comment("TrafficLimit.List")}, adds...)
   214  	err := db.QueryAndScan(builder.Select(nil).From(tbl, adds...), &lst)
   215  	return lst, err
   216  }
   217  
   218  func (m *TrafficLimit) Count(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) (cnt int64, err error) {
   219  	tbl := db.T(m)
   220  	cond = builder.And(tbl.ColByFieldName("DeletedAt").Eq(0), cond)
   221  	adds = append([]builder.Addition{builder.Where(cond), builder.Comment("TrafficLimit.List")}, adds...)
   222  	err = db.QueryAndScan(builder.Select(builder.Count()).From(tbl, adds...), &cnt)
   223  	return
   224  }
   225  
   226  func (m *TrafficLimit) FetchByID(db sqlx.DBExecutor) error {
   227  	tbl := db.T(m)
   228  	err := db.QueryAndScan(
   229  		builder.Select(nil).
   230  			From(
   231  				tbl,
   232  				builder.Where(
   233  					builder.And(
   234  						tbl.ColByFieldName("ID").Eq(m.ID),
   235  						tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt),
   236  					),
   237  				),
   238  				builder.Comment("TrafficLimit.FetchByID"),
   239  			),
   240  		m,
   241  	)
   242  	return err
   243  }
   244  
   245  func (m *TrafficLimit) FetchByProjectIDAndApiType(db sqlx.DBExecutor) error {
   246  	tbl := db.T(m)
   247  	err := db.QueryAndScan(
   248  		builder.Select(nil).
   249  			From(
   250  				tbl,
   251  				builder.Where(
   252  					builder.And(
   253  						tbl.ColByFieldName("ProjectID").Eq(m.ProjectID),
   254  						tbl.ColByFieldName("ApiType").Eq(m.ApiType),
   255  						tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt),
   256  					),
   257  				),
   258  				builder.Comment("TrafficLimit.FetchByProjectIDAndApiType"),
   259  			),
   260  		m,
   261  	)
   262  	return err
   263  }
   264  
   265  func (m *TrafficLimit) FetchByTrafficLimitID(db sqlx.DBExecutor) error {
   266  	tbl := db.T(m)
   267  	err := db.QueryAndScan(
   268  		builder.Select(nil).
   269  			From(
   270  				tbl,
   271  				builder.Where(
   272  					builder.And(
   273  						tbl.ColByFieldName("TrafficLimitID").Eq(m.TrafficLimitID),
   274  						tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt),
   275  					),
   276  				),
   277  				builder.Comment("TrafficLimit.FetchByTrafficLimitID"),
   278  			),
   279  		m,
   280  	)
   281  	return err
   282  }
   283  
   284  func (m *TrafficLimit) UpdateByIDWithFVs(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("ID").Eq(m.ID),
   295  					tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt),
   296  				),
   297  				builder.Comment("TrafficLimit.UpdateByIDWithFVs"),
   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.FetchByID(db)
   306  	}
   307  	return nil
   308  }
   309  
   310  func (m *TrafficLimit) UpdateByID(db sqlx.DBExecutor, zeros ...string) error {
   311  	fvs := builder.FieldValueFromStructByNoneZero(m, zeros...)
   312  	return m.UpdateByIDWithFVs(db, fvs)
   313  }
   314  
   315  func (m *TrafficLimit) UpdateByProjectIDAndApiTypeWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error {
   316  
   317  	if _, ok := fvs["UpdatedAt"]; !ok {
   318  		fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()}
   319  	}
   320  	tbl := db.T(m)
   321  	res, err := db.Exec(
   322  		builder.Update(tbl).
   323  			Where(
   324  				builder.And(
   325  					tbl.ColByFieldName("ProjectID").Eq(m.ProjectID),
   326  					tbl.ColByFieldName("ApiType").Eq(m.ApiType),
   327  					tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt),
   328  				),
   329  				builder.Comment("TrafficLimit.UpdateByProjectIDAndApiTypeWithFVs"),
   330  			).
   331  			Set(tbl.AssignmentsByFieldValues(fvs)...),
   332  	)
   333  	if err != nil {
   334  		return err
   335  	}
   336  	if affected, _ := res.RowsAffected(); affected == 0 {
   337  		return m.FetchByProjectIDAndApiType(db)
   338  	}
   339  	return nil
   340  }
   341  
   342  func (m *TrafficLimit) UpdateByProjectIDAndApiType(db sqlx.DBExecutor, zeros ...string) error {
   343  	fvs := builder.FieldValueFromStructByNoneZero(m, zeros...)
   344  	return m.UpdateByProjectIDAndApiTypeWithFVs(db, fvs)
   345  }
   346  
   347  func (m *TrafficLimit) UpdateByTrafficLimitIDWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error {
   348  
   349  	if _, ok := fvs["UpdatedAt"]; !ok {
   350  		fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()}
   351  	}
   352  	tbl := db.T(m)
   353  	res, err := db.Exec(
   354  		builder.Update(tbl).
   355  			Where(
   356  				builder.And(
   357  					tbl.ColByFieldName("TrafficLimitID").Eq(m.TrafficLimitID),
   358  					tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt),
   359  				),
   360  				builder.Comment("TrafficLimit.UpdateByTrafficLimitIDWithFVs"),
   361  			).
   362  			Set(tbl.AssignmentsByFieldValues(fvs)...),
   363  	)
   364  	if err != nil {
   365  		return err
   366  	}
   367  	if affected, _ := res.RowsAffected(); affected == 0 {
   368  		return m.FetchByTrafficLimitID(db)
   369  	}
   370  	return nil
   371  }
   372  
   373  func (m *TrafficLimit) UpdateByTrafficLimitID(db sqlx.DBExecutor, zeros ...string) error {
   374  	fvs := builder.FieldValueFromStructByNoneZero(m, zeros...)
   375  	return m.UpdateByTrafficLimitIDWithFVs(db, fvs)
   376  }
   377  
   378  func (m *TrafficLimit) Delete(db sqlx.DBExecutor) error {
   379  	_, err := db.Exec(
   380  		builder.Delete().
   381  			From(
   382  				db.T(m),
   383  				builder.Where(m.CondByValue(db)),
   384  				builder.Comment("TrafficLimit.Delete"),
   385  			),
   386  	)
   387  	return err
   388  }
   389  
   390  func (m *TrafficLimit) DeleteByID(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("ID").Eq(m.ID),
   399  						tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt),
   400  					),
   401  				),
   402  				builder.Comment("TrafficLimit.DeleteByID"),
   403  			),
   404  	)
   405  	return err
   406  }
   407  
   408  func (m *TrafficLimit) SoftDeleteByID(db sqlx.DBExecutor) error {
   409  	tbl := db.T(m)
   410  	fvs := builder.FieldValues{}
   411  
   412  	if _, ok := fvs["DeletedAt"]; !ok {
   413  		fvs["DeletedAt"] = types.Timestamp{Time: time.Now()}
   414  	}
   415  
   416  	if _, ok := fvs["UpdatedAt"]; !ok {
   417  		fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()}
   418  	}
   419  	_, err := db.Exec(
   420  		builder.Update(db.T(m)).
   421  			Where(
   422  				builder.And(
   423  					tbl.ColByFieldName("ID").Eq(m.ID),
   424  					tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt),
   425  				),
   426  				builder.Comment("TrafficLimit.SoftDeleteByID"),
   427  			).
   428  			Set(tbl.AssignmentsByFieldValues(fvs)...),
   429  	)
   430  	return err
   431  }
   432  
   433  func (m *TrafficLimit) DeleteByProjectIDAndApiType(db sqlx.DBExecutor) error {
   434  	tbl := db.T(m)
   435  	_, err := db.Exec(
   436  		builder.Delete().
   437  			From(
   438  				tbl,
   439  				builder.Where(
   440  					builder.And(
   441  						tbl.ColByFieldName("ProjectID").Eq(m.ProjectID),
   442  						tbl.ColByFieldName("ApiType").Eq(m.ApiType),
   443  						tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt),
   444  					),
   445  				),
   446  				builder.Comment("TrafficLimit.DeleteByProjectIDAndApiType"),
   447  			),
   448  	)
   449  	return err
   450  }
   451  
   452  func (m *TrafficLimit) SoftDeleteByProjectIDAndApiType(db sqlx.DBExecutor) error {
   453  	tbl := db.T(m)
   454  	fvs := builder.FieldValues{}
   455  
   456  	if _, ok := fvs["DeletedAt"]; !ok {
   457  		fvs["DeletedAt"] = types.Timestamp{Time: time.Now()}
   458  	}
   459  
   460  	if _, ok := fvs["UpdatedAt"]; !ok {
   461  		fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()}
   462  	}
   463  	_, err := db.Exec(
   464  		builder.Update(db.T(m)).
   465  			Where(
   466  				builder.And(
   467  					tbl.ColByFieldName("ProjectID").Eq(m.ProjectID),
   468  					tbl.ColByFieldName("ApiType").Eq(m.ApiType),
   469  					tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt),
   470  				),
   471  				builder.Comment("TrafficLimit.SoftDeleteByProjectIDAndApiType"),
   472  			).
   473  			Set(tbl.AssignmentsByFieldValues(fvs)...),
   474  	)
   475  	return err
   476  }
   477  
   478  func (m *TrafficLimit) DeleteByTrafficLimitID(db sqlx.DBExecutor) error {
   479  	tbl := db.T(m)
   480  	_, err := db.Exec(
   481  		builder.Delete().
   482  			From(
   483  				tbl,
   484  				builder.Where(
   485  					builder.And(
   486  						tbl.ColByFieldName("TrafficLimitID").Eq(m.TrafficLimitID),
   487  						tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt),
   488  					),
   489  				),
   490  				builder.Comment("TrafficLimit.DeleteByTrafficLimitID"),
   491  			),
   492  	)
   493  	return err
   494  }
   495  
   496  func (m *TrafficLimit) SoftDeleteByTrafficLimitID(db sqlx.DBExecutor) error {
   497  	tbl := db.T(m)
   498  	fvs := builder.FieldValues{}
   499  
   500  	if _, ok := fvs["DeletedAt"]; !ok {
   501  		fvs["DeletedAt"] = types.Timestamp{Time: time.Now()}
   502  	}
   503  
   504  	if _, ok := fvs["UpdatedAt"]; !ok {
   505  		fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()}
   506  	}
   507  	_, err := db.Exec(
   508  		builder.Update(db.T(m)).
   509  			Where(
   510  				builder.And(
   511  					tbl.ColByFieldName("TrafficLimitID").Eq(m.TrafficLimitID),
   512  					tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt),
   513  				),
   514  				builder.Comment("TrafficLimit.SoftDeleteByTrafficLimitID"),
   515  			).
   516  			Set(tbl.AssignmentsByFieldValues(fvs)...),
   517  	)
   518  	return err
   519  }