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

     1  // This is a generated source file. DO NOT EDIT
     2  // Source: models/blockchain__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 BlockchainTable *builder.Table
    16  
    17  func init() {
    18  	BlockchainTable = MonitorDB.Register(&Blockchain{})
    19  }
    20  
    21  type BlockchainIterator struct {
    22  }
    23  
    24  func (*BlockchainIterator) New() interface{} {
    25  	return &Blockchain{}
    26  }
    27  
    28  func (*BlockchainIterator) Resolve(v interface{}) *Blockchain {
    29  	return v.(*Blockchain)
    30  }
    31  
    32  func (*Blockchain) TableName() string {
    33  	return "t_blockchain"
    34  }
    35  
    36  func (*Blockchain) TableDesc() []string {
    37  	return []string{
    38  		"TODO will delete after caller migrate to http api",
    39  		"Blockchain database model blockchain",
    40  	}
    41  }
    42  
    43  func (*Blockchain) Comments() map[string]string {
    44  	return map[string]string{}
    45  }
    46  
    47  func (*Blockchain) ColDesc() map[string][]string {
    48  	return map[string][]string{}
    49  }
    50  
    51  func (*Blockchain) ColRel() map[string][]string {
    52  	return map[string][]string{}
    53  }
    54  
    55  func (*Blockchain) PrimaryKey() []string {
    56  	return []string{
    57  		"ID",
    58  	}
    59  }
    60  
    61  func (m *Blockchain) IndexFieldNames() []string {
    62  	return []string{
    63  		"ChainID",
    64  		"ID",
    65  	}
    66  }
    67  
    68  func (*Blockchain) UniqueIndexes() builder.Indexes {
    69  	return builder.Indexes{
    70  		"ui_chain_id": []string{
    71  			"ChainID",
    72  		},
    73  	}
    74  }
    75  
    76  func (*Blockchain) UniqueIndexUIChainID() string {
    77  	return "ui_chain_id"
    78  }
    79  
    80  func (m *Blockchain) ColID() *builder.Column {
    81  	return BlockchainTable.ColByFieldName(m.FieldID())
    82  }
    83  
    84  func (*Blockchain) FieldID() string {
    85  	return "ID"
    86  }
    87  
    88  func (m *Blockchain) ColChainID() *builder.Column {
    89  	return BlockchainTable.ColByFieldName(m.FieldChainID())
    90  }
    91  
    92  func (*Blockchain) FieldChainID() string {
    93  	return "ChainID"
    94  }
    95  
    96  func (m *Blockchain) ColAddress() *builder.Column {
    97  	return BlockchainTable.ColByFieldName(m.FieldAddress())
    98  }
    99  
   100  func (*Blockchain) FieldAddress() string {
   101  	return "Address"
   102  }
   103  
   104  func (m *Blockchain) ColCreatedAt() *builder.Column {
   105  	return BlockchainTable.ColByFieldName(m.FieldCreatedAt())
   106  }
   107  
   108  func (*Blockchain) FieldCreatedAt() string {
   109  	return "CreatedAt"
   110  }
   111  
   112  func (m *Blockchain) ColUpdatedAt() *builder.Column {
   113  	return BlockchainTable.ColByFieldName(m.FieldUpdatedAt())
   114  }
   115  
   116  func (*Blockchain) FieldUpdatedAt() string {
   117  	return "UpdatedAt"
   118  }
   119  
   120  func (m *Blockchain) CondByValue(db sqlx.DBExecutor) builder.SqlCondition {
   121  	var (
   122  		tbl  = db.T(m)
   123  		fvs  = builder.FieldValueFromStructByNoneZero(m)
   124  		cond = make([]builder.SqlCondition, 0)
   125  	)
   126  
   127  	for _, fn := range m.IndexFieldNames() {
   128  		if v, ok := fvs[fn]; ok {
   129  			cond = append(cond, tbl.ColByFieldName(fn).Eq(v))
   130  			delete(fvs, fn)
   131  		}
   132  	}
   133  	if len(cond) == 0 {
   134  		panic(fmt.Errorf("no field for indexes has value"))
   135  	}
   136  	for fn, v := range fvs {
   137  		cond = append(cond, tbl.ColByFieldName(fn).Eq(v))
   138  	}
   139  	return builder.And(cond...)
   140  }
   141  
   142  func (m *Blockchain) Create(db sqlx.DBExecutor) error {
   143  
   144  	if m.CreatedAt.IsZero() {
   145  		m.CreatedAt.Set(time.Now())
   146  	}
   147  
   148  	if m.UpdatedAt.IsZero() {
   149  		m.UpdatedAt.Set(time.Now())
   150  	}
   151  
   152  	_, err := db.Exec(sqlx.InsertToDB(db, m, nil))
   153  	return err
   154  }
   155  
   156  func (m *Blockchain) List(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) ([]Blockchain, error) {
   157  	var (
   158  		tbl = db.T(m)
   159  		lst = make([]Blockchain, 0)
   160  	)
   161  	adds = append([]builder.Addition{builder.Where(cond), builder.Comment("Blockchain.List")}, adds...)
   162  	err := db.QueryAndScan(builder.Select(nil).From(tbl, adds...), &lst)
   163  	return lst, err
   164  }
   165  
   166  func (m *Blockchain) Count(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) (cnt int64, err error) {
   167  	tbl := db.T(m)
   168  	adds = append([]builder.Addition{builder.Where(cond), builder.Comment("Blockchain.List")}, adds...)
   169  	err = db.QueryAndScan(builder.Select(builder.Count()).From(tbl, adds...), &cnt)
   170  	return
   171  }
   172  
   173  func (m *Blockchain) FetchByID(db sqlx.DBExecutor) error {
   174  	tbl := db.T(m)
   175  	err := db.QueryAndScan(
   176  		builder.Select(nil).
   177  			From(
   178  				tbl,
   179  				builder.Where(
   180  					builder.And(
   181  						tbl.ColByFieldName("ID").Eq(m.ID),
   182  					),
   183  				),
   184  				builder.Comment("Blockchain.FetchByID"),
   185  			),
   186  		m,
   187  	)
   188  	return err
   189  }
   190  
   191  func (m *Blockchain) FetchByChainID(db sqlx.DBExecutor) error {
   192  	tbl := db.T(m)
   193  	err := db.QueryAndScan(
   194  		builder.Select(nil).
   195  			From(
   196  				tbl,
   197  				builder.Where(
   198  					builder.And(
   199  						tbl.ColByFieldName("ChainID").Eq(m.ChainID),
   200  					),
   201  				),
   202  				builder.Comment("Blockchain.FetchByChainID"),
   203  			),
   204  		m,
   205  	)
   206  	return err
   207  }
   208  
   209  func (m *Blockchain) UpdateByIDWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error {
   210  
   211  	if _, ok := fvs["UpdatedAt"]; !ok {
   212  		fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()}
   213  	}
   214  	tbl := db.T(m)
   215  	res, err := db.Exec(
   216  		builder.Update(tbl).
   217  			Where(
   218  				builder.And(
   219  					tbl.ColByFieldName("ID").Eq(m.ID),
   220  				),
   221  				builder.Comment("Blockchain.UpdateByIDWithFVs"),
   222  			).
   223  			Set(tbl.AssignmentsByFieldValues(fvs)...),
   224  	)
   225  	if err != nil {
   226  		return err
   227  	}
   228  	if affected, _ := res.RowsAffected(); affected == 0 {
   229  		return m.FetchByID(db)
   230  	}
   231  	return nil
   232  }
   233  
   234  func (m *Blockchain) UpdateByID(db sqlx.DBExecutor, zeros ...string) error {
   235  	fvs := builder.FieldValueFromStructByNoneZero(m, zeros...)
   236  	return m.UpdateByIDWithFVs(db, fvs)
   237  }
   238  
   239  func (m *Blockchain) UpdateByChainIDWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error {
   240  
   241  	if _, ok := fvs["UpdatedAt"]; !ok {
   242  		fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()}
   243  	}
   244  	tbl := db.T(m)
   245  	res, err := db.Exec(
   246  		builder.Update(tbl).
   247  			Where(
   248  				builder.And(
   249  					tbl.ColByFieldName("ChainID").Eq(m.ChainID),
   250  				),
   251  				builder.Comment("Blockchain.UpdateByChainIDWithFVs"),
   252  			).
   253  			Set(tbl.AssignmentsByFieldValues(fvs)...),
   254  	)
   255  	if err != nil {
   256  		return err
   257  	}
   258  	if affected, _ := res.RowsAffected(); affected == 0 {
   259  		return m.FetchByChainID(db)
   260  	}
   261  	return nil
   262  }
   263  
   264  func (m *Blockchain) UpdateByChainID(db sqlx.DBExecutor, zeros ...string) error {
   265  	fvs := builder.FieldValueFromStructByNoneZero(m, zeros...)
   266  	return m.UpdateByChainIDWithFVs(db, fvs)
   267  }
   268  
   269  func (m *Blockchain) Delete(db sqlx.DBExecutor) error {
   270  	_, err := db.Exec(
   271  		builder.Delete().
   272  			From(
   273  				db.T(m),
   274  				builder.Where(m.CondByValue(db)),
   275  				builder.Comment("Blockchain.Delete"),
   276  			),
   277  	)
   278  	return err
   279  }
   280  
   281  func (m *Blockchain) DeleteByID(db sqlx.DBExecutor) error {
   282  	tbl := db.T(m)
   283  	_, err := db.Exec(
   284  		builder.Delete().
   285  			From(
   286  				tbl,
   287  				builder.Where(
   288  					builder.And(
   289  						tbl.ColByFieldName("ID").Eq(m.ID),
   290  					),
   291  				),
   292  				builder.Comment("Blockchain.DeleteByID"),
   293  			),
   294  	)
   295  	return err
   296  }
   297  
   298  func (m *Blockchain) DeleteByChainID(db sqlx.DBExecutor) error {
   299  	tbl := db.T(m)
   300  	_, err := db.Exec(
   301  		builder.Delete().
   302  			From(
   303  				tbl,
   304  				builder.Where(
   305  					builder.And(
   306  						tbl.ColByFieldName("ChainID").Eq(m.ChainID),
   307  					),
   308  				),
   309  				builder.Comment("Blockchain.DeleteByChainID"),
   310  			),
   311  	)
   312  	return err
   313  }