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

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