github.com/kunlun-qilian/sqlx/v3@v3.0.0/generator/__examples__/database/org__generated.go (about)

     1  package database
     2  
     3  import (
     4  	fmt "fmt"
     5  
     6  	github_com_go_courier_sqlx_v2 "github.com/kunlun-qilian/sqlx/v3"
     7  	github_com_go_courier_sqlx_v2_builder "github.com/kunlun-qilian/sqlx/v3/builder"
     8  )
     9  
    10  func (Org) PrimaryKey() []string {
    11  	return []string{
    12  		"ID",
    13  	}
    14  }
    15  
    16  func (Org) Comments() map[string]string {
    17  	return map[string]string{
    18  		"UserID": "关联用户",
    19  	}
    20  }
    21  
    22  var OrgTable *github_com_go_courier_sqlx_v2_builder.Table
    23  
    24  func init() {
    25  	OrgTable = DBTest.Register(&Org{})
    26  }
    27  
    28  type OrgIterator struct {
    29  }
    30  
    31  func (OrgIterator) New() interface{} {
    32  	return &Org{}
    33  }
    34  
    35  func (OrgIterator) Resolve(v interface{}) *Org {
    36  	return v.(*Org)
    37  }
    38  
    39  func (Org) TableName() string {
    40  	return "t_org"
    41  }
    42  
    43  func (Org) TableDescription() []string {
    44  	return []string{
    45  		"organization",
    46  	}
    47  }
    48  
    49  func (Org) ColDescriptions() map[string][]string {
    50  	return map[string][]string{
    51  		"UserID": []string{
    52  			"关联用户",
    53  			"xxxxx",
    54  		},
    55  	}
    56  }
    57  
    58  func (Org) FieldKeyID() string {
    59  	return "ID"
    60  }
    61  
    62  func (m *Org) FieldID() *github_com_go_courier_sqlx_v2_builder.Column {
    63  	return OrgTable.F(m.FieldKeyID())
    64  }
    65  
    66  func (Org) FieldKeyName() string {
    67  	return "Name"
    68  }
    69  
    70  func (m *Org) FieldName() *github_com_go_courier_sqlx_v2_builder.Column {
    71  	return OrgTable.F(m.FieldKeyName())
    72  }
    73  
    74  func (Org) FieldKeyUserID() string {
    75  	return "UserID"
    76  }
    77  
    78  func (m *Org) FieldUserID() *github_com_go_courier_sqlx_v2_builder.Column {
    79  	return OrgTable.F(m.FieldKeyUserID())
    80  }
    81  
    82  func (Org) ColRelations() map[string][]string {
    83  	return map[string][]string{
    84  		"UserID": []string{
    85  			"User",
    86  			"ID",
    87  		},
    88  	}
    89  }
    90  
    91  func (m *Org) IndexFieldNames() []string {
    92  	return []string{
    93  		"ID",
    94  	}
    95  }
    96  
    97  func (m *Org) ConditionByStruct(db github_com_go_courier_sqlx_v2.DBExecutor) github_com_go_courier_sqlx_v2_builder.SqlCondition {
    98  	table := db.T(m)
    99  	fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m)
   100  
   101  	conditions := make([]github_com_go_courier_sqlx_v2_builder.SqlCondition, 0)
   102  
   103  	for _, fieldName := range m.IndexFieldNames() {
   104  		if v, exists := fieldValues[fieldName]; exists {
   105  			conditions = append(conditions, table.F(fieldName).Eq(v))
   106  			delete(fieldValues, fieldName)
   107  		}
   108  	}
   109  
   110  	if len(conditions) == 0 {
   111  		panic(fmt.Errorf("at least one of field for indexes has value"))
   112  	}
   113  
   114  	for fieldName, v := range fieldValues {
   115  		conditions = append(conditions, table.F(fieldName).Eq(v))
   116  	}
   117  
   118  	condition := github_com_go_courier_sqlx_v2_builder.And(conditions...)
   119  
   120  	return condition
   121  }
   122  
   123  func (m *Org) Create(db github_com_go_courier_sqlx_v2.DBExecutor) error {
   124  
   125  	_, err := db.ExecExpr(github_com_go_courier_sqlx_v2.InsertToDB(db, m, nil))
   126  	return err
   127  
   128  }
   129  
   130  func (m *Org) DeleteByStruct(db github_com_go_courier_sqlx_v2.DBExecutor) error {
   131  
   132  	_, err := db.ExecExpr(
   133  		github_com_go_courier_sqlx_v2_builder.Delete().
   134  			From(
   135  				db.T(m),
   136  				github_com_go_courier_sqlx_v2_builder.Where(m.ConditionByStruct(db)),
   137  				github_com_go_courier_sqlx_v2_builder.Comment("Org.DeleteByStruct"),
   138  			),
   139  	)
   140  
   141  	return err
   142  }
   143  
   144  func (m *Org) FetchByID(db github_com_go_courier_sqlx_v2.DBExecutor) error {
   145  
   146  	table := db.T(m)
   147  
   148  	err := db.QueryExprAndScan(
   149  		github_com_go_courier_sqlx_v2_builder.Select(nil).
   150  			From(
   151  				db.T(m),
   152  				github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And(
   153  					table.F("ID").Eq(m.ID),
   154  				)),
   155  				github_com_go_courier_sqlx_v2_builder.Comment("Org.FetchByID"),
   156  			),
   157  		m,
   158  	)
   159  
   160  	return err
   161  }
   162  
   163  func (m *Org) UpdateByIDWithMap(db github_com_go_courier_sqlx_v2.DBExecutor, fieldValues github_com_go_courier_sqlx_v2_builder.FieldValues) error {
   164  
   165  	table := db.T(m)
   166  
   167  	result, err := db.ExecExpr(
   168  		github_com_go_courier_sqlx_v2_builder.Update(db.T(m)).
   169  			Where(
   170  				github_com_go_courier_sqlx_v2_builder.And(
   171  					table.F("ID").Eq(m.ID),
   172  				),
   173  				github_com_go_courier_sqlx_v2_builder.Comment("Org.UpdateByIDWithMap"),
   174  			).
   175  			Set(table.AssignmentsByFieldValues(fieldValues)...),
   176  	)
   177  
   178  	if err != nil {
   179  		return err
   180  	}
   181  
   182  	rowsAffected, _ := result.RowsAffected()
   183  	if rowsAffected == 0 {
   184  		return m.FetchByID(db)
   185  	}
   186  
   187  	return nil
   188  
   189  }
   190  
   191  func (m *Org) UpdateByIDWithStruct(db github_com_go_courier_sqlx_v2.DBExecutor, zeroFields ...string) error {
   192  
   193  	fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m, zeroFields...)
   194  	return m.UpdateByIDWithMap(db, fieldValues)
   195  
   196  }
   197  
   198  func (m *Org) FetchByIDForUpdate(db github_com_go_courier_sqlx_v2.DBExecutor) error {
   199  
   200  	table := db.T(m)
   201  
   202  	err := db.QueryExprAndScan(
   203  		github_com_go_courier_sqlx_v2_builder.Select(nil).
   204  			From(
   205  				db.T(m),
   206  				github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And(
   207  					table.F("ID").Eq(m.ID),
   208  				)),
   209  				github_com_go_courier_sqlx_v2_builder.ForUpdate(),
   210  				github_com_go_courier_sqlx_v2_builder.Comment("Org.FetchByIDForUpdate"),
   211  			),
   212  		m,
   213  	)
   214  
   215  	return err
   216  }
   217  
   218  func (m *Org) DeleteByID(db github_com_go_courier_sqlx_v2.DBExecutor) error {
   219  
   220  	table := db.T(m)
   221  
   222  	_, err := db.ExecExpr(
   223  		github_com_go_courier_sqlx_v2_builder.Delete().
   224  			From(db.T(m),
   225  				github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And(
   226  					table.F("ID").Eq(m.ID),
   227  				)),
   228  				github_com_go_courier_sqlx_v2_builder.Comment("Org.DeleteByID"),
   229  			))
   230  
   231  	return err
   232  }
   233  
   234  func (m *Org) List(db github_com_go_courier_sqlx_v2.DBExecutor, condition github_com_go_courier_sqlx_v2_builder.SqlCondition, additions ...github_com_go_courier_sqlx_v2_builder.Addition) ([]Org, error) {
   235  
   236  	list := make([]Org, 0)
   237  
   238  	table := db.T(m)
   239  	_ = table
   240  
   241  	finalAdditions := []github_com_go_courier_sqlx_v2_builder.Addition{
   242  		github_com_go_courier_sqlx_v2_builder.Where(condition),
   243  		github_com_go_courier_sqlx_v2_builder.Comment("Org.List"),
   244  	}
   245  
   246  	if len(additions) > 0 {
   247  		finalAdditions = append(finalAdditions, additions...)
   248  	}
   249  
   250  	err := db.QueryExprAndScan(
   251  		github_com_go_courier_sqlx_v2_builder.Select(nil).
   252  			From(db.T(m), finalAdditions...),
   253  		&list,
   254  	)
   255  
   256  	return list, err
   257  
   258  }
   259  
   260  func (m *Org) Count(db github_com_go_courier_sqlx_v2.DBExecutor, condition github_com_go_courier_sqlx_v2_builder.SqlCondition, additions ...github_com_go_courier_sqlx_v2_builder.Addition) (int, error) {
   261  
   262  	count := -1
   263  
   264  	table := db.T(m)
   265  	_ = table
   266  
   267  	finalAdditions := []github_com_go_courier_sqlx_v2_builder.Addition{
   268  		github_com_go_courier_sqlx_v2_builder.Where(condition),
   269  		github_com_go_courier_sqlx_v2_builder.Comment("Org.Count"),
   270  	}
   271  
   272  	if len(additions) > 0 {
   273  		finalAdditions = append(finalAdditions, additions...)
   274  	}
   275  
   276  	err := db.QueryExprAndScan(
   277  		github_com_go_courier_sqlx_v2_builder.Select(
   278  			github_com_go_courier_sqlx_v2_builder.Count(),
   279  		).
   280  			From(db.T(m), finalAdditions...),
   281  		&count,
   282  	)
   283  
   284  	return count, err
   285  
   286  }
   287  
   288  func (m *Org) BatchFetchByIDList(db github_com_go_courier_sqlx_v2.DBExecutor, values []uint64) ([]Org, error) {
   289  
   290  	if len(values) == 0 {
   291  		return nil, nil
   292  	}
   293  
   294  	table := db.T(m)
   295  
   296  	condition := table.F("ID").In(values)
   297  
   298  	return m.List(db, condition)
   299  
   300  }