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

     1  package generator
     2  
     3  import (
     4  	"sort"
     5  
     6  	"github.com/go-courier/codegen"
     7  	"github.com/kunlun-qilian/sqlx/v3/builder"
     8  )
     9  
    10  func (m *Model) IndexFieldNames() []string {
    11  	indexedFields := make([]string, 0)
    12  
    13  	m.Table.Keys.Range(func(key *builder.Key, idx int) {
    14  		fieldNames := key.Def.FieldNames
    15  		indexedFields = append(indexedFields, fieldNames...)
    16  	})
    17  
    18  	indexedFields = stringUniq(indexedFields)
    19  
    20  	indexedFields = stringFilter(indexedFields, func(item string, i int) bool {
    21  		if m.HasDeletedAt {
    22  			return item != m.FieldKeyDeletedAt
    23  		}
    24  		return true
    25  	})
    26  
    27  	sort.Strings(indexedFields)
    28  
    29  	return indexedFields
    30  }
    31  
    32  func (m *Model) WriteTableName(file *codegen.File) {
    33  	file.WriteBlock(
    34  		codegen.DeclVar(codegen.Var(codegen.Star(
    35  			codegen.Type(file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Table"))),
    36  			m.VarTable(),
    37  		)),
    38  
    39  		codegen.Func().
    40  			Named("init").
    41  			Do(
    42  				codegen.Expr("? = ?.Register(&?{})",
    43  					codegen.Id(m.VarTable()),
    44  					codegen.Id(m.Database),
    45  					codegen.Id(m.StructName),
    46  				),
    47  			),
    48  	)
    49  
    50  	file.WriteBlock(
    51  		codegen.DeclType(
    52  			codegen.Var(codegen.Struct(), string(m.IteratorType().Bytes())),
    53  		),
    54  		codegen.Func().
    55  			Named("New").
    56  			MethodOf(codegen.Var(m.IteratorType())).
    57  			Return(codegen.Var(codegen.Interface())).
    58  			Do(
    59  				codegen.Return(codegen.Expr("&?{}", m.Type())),
    60  			),
    61  		codegen.Func(codegen.Var(codegen.Interface(), "v")).
    62  			Named("Resolve").
    63  			MethodOf(codegen.Var(m.IteratorType())).
    64  			Return(codegen.Var(m.PtrType())).
    65  			Do(
    66  				codegen.Return(codegen.Expr("v.(?)", m.PtrType())),
    67  			),
    68  	)
    69  
    70  	file.WriteBlock(
    71  		codegen.Func().
    72  			Named("TableName").
    73  			MethodOf(codegen.Var(m.Type())).
    74  			Return(codegen.Var(codegen.String)).
    75  			Do(
    76  				codegen.Return(file.Val(m.Config.TableName)),
    77  			),
    78  	)
    79  }
    80  
    81  func (m *Model) WriteTableInterfaces(file *codegen.File) {
    82  	if m.Description != nil {
    83  		file.WriteBlock(
    84  			codegen.Func().
    85  				Named("TableDescription").
    86  				MethodOf(codegen.Var(m.Type())).
    87  				Return(codegen.Var(codegen.Slice(codegen.String))).
    88  				Do(
    89  					codegen.Return(file.Val(m.Description)),
    90  				),
    91  		)
    92  	}
    93  
    94  	file.WriteBlock(
    95  		codegen.Func().
    96  			Named("ColDescriptions").
    97  			MethodOf(codegen.Var(m.Type())).
    98  			Return(codegen.Var(codegen.Map(codegen.String, codegen.Slice(codegen.String)))).
    99  			Do(
   100  				codegen.Return(file.Val(m.GetColDescriptions())),
   101  			),
   102  	)
   103  
   104  	m.Columns.Range(func(col *builder.Column, idx int) {
   105  		if col.DeprecatedActions != nil {
   106  			return
   107  		}
   108  
   109  		file.WriteBlock(
   110  			codegen.Func().
   111  				Named("FieldKey" + col.FieldName).
   112  				MethodOf(codegen.Var(m.Type())).
   113  				Return(codegen.Var(codegen.String)).
   114  				Do(
   115  					codegen.Return(file.Val(col.FieldName)),
   116  				),
   117  		)
   118  
   119  		file.WriteBlock(
   120  			codegen.Func().
   121  				Named("Field" + col.FieldName).
   122  				MethodOf(codegen.Var(m.PtrType(), "m")).
   123  				Return(codegen.Var(codegen.Star(codegen.Type(file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Column"))))).
   124  				Do(
   125  					codegen.Return(codegen.Expr("?.F(m.FieldKey"+col.FieldName+"())", codegen.Id(m.VarTable()))),
   126  				),
   127  		)
   128  	})
   129  
   130  	file.WriteBlock(
   131  		codegen.Func().
   132  			Named("ColRelations").
   133  			MethodOf(codegen.Var(m.Type())).
   134  			Return(codegen.Var(codegen.Map(codegen.String, codegen.Slice(codegen.String)))).
   135  			Do(
   136  				codegen.Return(file.Val(m.GetRelations())),
   137  			),
   138  	)
   139  
   140  	file.WriteBlock(
   141  		codegen.Func().
   142  			Named("IndexFieldNames").
   143  			MethodOf(codegen.Var(m.PtrType(), "m")).
   144  			Return(codegen.Var(codegen.Slice(codegen.String))).
   145  			Do(
   146  				codegen.Return(file.Val(m.IndexFieldNames())),
   147  			),
   148  	)
   149  
   150  	if !m.Config.WithMethods {
   151  		return
   152  	}
   153  
   154  	file.WriteBlock(
   155  		codegen.Func(
   156  			codegen.Var(codegen.Type(file.Use("github.com/kunlun-qilian/sqlx/v3", "DBExecutor")), "db"),
   157  		).
   158  			Named("ConditionByStruct").
   159  			MethodOf(codegen.Var(m.PtrType(), "m")).
   160  			Return(codegen.Var(codegen.Type(file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "SqlCondition")))).
   161  			Do(
   162  				codegen.Expr(`table := db.T(m)`),
   163  				codegen.Expr(`fieldValues := `+file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "FieldValuesFromStructByNonZero")+`(m)
   164  
   165  conditions := make([]`+file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "SqlCondition")+`, 0)
   166  
   167  for _, fieldName := range m.IndexFieldNames() {
   168  	if v, exists := fieldValues[fieldName]; exists {
   169  		conditions = append(conditions, table.F(fieldName).Eq(v))
   170  		delete(fieldValues, fieldName)
   171  	}
   172  }
   173  
   174  if len(conditions) == 0 {
   175  	panic(`+file.Use("fmt", "Errorf")+`("at least one of field for indexes has value"))
   176  }
   177  	
   178  for fieldName, v := range fieldValues {
   179  	conditions = append(conditions, table.F(fieldName).Eq(v))
   180  }
   181  	
   182  condition := `+file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "And")+`(conditions...)
   183  `),
   184  
   185  				func() codegen.Snippet {
   186  					if m.HasDeletedAt {
   187  						return codegen.Expr(
   188  							`condition = `+file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "And")+`(condition, table.F(?).Eq(0))`,
   189  							file.Val(m.FieldKeyDeletedAt),
   190  						)
   191  					}
   192  					return codegen.Expr("")
   193  				}(),
   194  
   195  				codegen.Return(codegen.Id("condition")),
   196  			),
   197  	)
   198  }
   199  
   200  func (m *Model) WriteTableKeyInterfaces(file *codegen.File) {
   201  	if len(m.Keys.Primary) > 0 {
   202  		file.WriteBlock(
   203  			codegen.Func().
   204  				Named("PrimaryKey").
   205  				MethodOf(codegen.Var(m.Type())).
   206  				Return(codegen.Var(codegen.Slice(codegen.String))).
   207  				Do(
   208  					codegen.Return(file.Val(m.Keys.Primary)),
   209  				),
   210  		)
   211  	}
   212  
   213  	// 增加分区表支持
   214  	if len(m.Keys.Partition) > 0 {
   215  		file.WriteBlock(
   216  			codegen.Func().
   217  				Named("Partition").
   218  				MethodOf(codegen.Var(m.Type())).
   219  				Return(codegen.Var(codegen.Slice(codegen.String))).
   220  				Do(
   221  					codegen.Return(file.Val(m.Keys.Partition)),
   222  				),
   223  		)
   224  	}
   225  
   226  	if len(m.Keys.Indexes) > 0 {
   227  
   228  		file.WriteBlock(
   229  			codegen.Func().
   230  				Named("Indexes").
   231  				MethodOf(codegen.Var(m.Type())).
   232  				Return(codegen.Var(codegen.Type(file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Indexes")))).
   233  				Do(
   234  					codegen.Return(file.Val(m.Keys.Indexes)),
   235  				),
   236  		)
   237  	}
   238  
   239  	if len(m.Keys.UniqueIndexes) > 0 {
   240  		indexKeys := make([]string, 0)
   241  
   242  		for k := range m.Keys.UniqueIndexes {
   243  			indexKeys = append(indexKeys, k)
   244  		}
   245  
   246  		sort.Strings(indexKeys)
   247  
   248  		for _, k := range indexKeys {
   249  			file.WriteBlock(
   250  				codegen.Func().
   251  					Named("UniqueIndex" + codegen.UpperCamelCase(k)).
   252  					MethodOf(codegen.Var(m.Type())).
   253  					Return(codegen.Var(codegen.String)).
   254  					Do(
   255  						codegen.Return(file.Val(k)),
   256  					),
   257  			)
   258  		}
   259  
   260  		file.WriteBlock(
   261  			codegen.Func().
   262  				Named("UniqueIndexes").
   263  				MethodOf(codegen.Var(m.Type())).
   264  				Return(codegen.Var(codegen.Type(file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Indexes")))).
   265  				Do(
   266  					codegen.Return(file.Val(m.Keys.UniqueIndexes)),
   267  				),
   268  		)
   269  	}
   270  
   271  	if m.WithComments {
   272  		file.WriteBlock(
   273  			codegen.Func().
   274  				Named("Comments").
   275  				MethodOf(codegen.Var(m.Type())).
   276  				Return(codegen.Var(codegen.Map(codegen.String, codegen.String))).
   277  				Do(
   278  					codegen.Return(file.Val(m.GetComments())),
   279  				),
   280  		)
   281  	}
   282  }
   283  
   284  func (m *Model) GetRelations() map[string][]string {
   285  	rels := map[string][]string{}
   286  	m.Columns.Range(func(col *builder.Column, idx int) {
   287  		if len(col.Relation) == 2 {
   288  			rels[col.FieldName] = col.Relation
   289  		}
   290  	})
   291  	return rels
   292  }
   293  
   294  func (m *Model) GetComments() map[string]string {
   295  	comments := map[string]string{}
   296  	m.Columns.Range(func(col *builder.Column, idx int) {
   297  		if col.Comment != "" {
   298  			comments[col.FieldName] = col.Comment
   299  		}
   300  	})
   301  	return comments
   302  }
   303  
   304  func (m *Model) GetColDescriptions() map[string][]string {
   305  	descriptions := map[string][]string{}
   306  	m.Columns.Range(func(col *builder.Column, idx int) {
   307  		if col.Description != nil {
   308  			descriptions[col.FieldName] = col.Description
   309  		}
   310  	})
   311  	return descriptions
   312  }