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

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