github.com/eden-framework/sqlx@v0.0.2/generator/model_interfaces.go (about)

     1  package generator
     2  
     3  import (
     4  	"github.com/eden-framework/codegen"
     5  	"sort"
     6  
     7  	"github.com/eden-framework/sqlx/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.Columns.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/eden-framework/sqlx/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/eden-framework/sqlx/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/eden-framework/sqlx", "DBExecutor")), "db"),
   157  		).
   158  			Named("ConditionByStruct").
   159  			MethodOf(codegen.Var(m.PtrType(), "m")).
   160  			Return(codegen.Var(codegen.Type(file.Use("github.com/eden-framework/sqlx/builder", "SqlCondition")))).
   161  			Do(
   162  				codegen.Expr(`table := db.T(m)`),
   163  				codegen.Expr(`fieldValues := `+file.Use("github.com/eden-framework/sqlx/builder", "FieldValuesFromStructByNonZero")+`(m)
   164  
   165  conditions := make([]`+file.Use("github.com/eden-framework/sqlx/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/eden-framework/sqlx/builder", "And")+`(conditions...)
   183  `),
   184  
   185  				func() codegen.Snippet {
   186  					if m.HasDeletedAt {
   187  						return codegen.Expr(
   188  							`condition = `+file.Use("github.com/eden-framework/sqlx/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  	if len(m.Keys.Indexes) > 0 {
   214  
   215  		file.WriteBlock(
   216  			codegen.Func().
   217  				Named("Indexes").
   218  				MethodOf(codegen.Var(m.Type())).
   219  				Return(codegen.Var(codegen.Type(file.Use("github.com/eden-framework/sqlx/builder", "Indexes")))).
   220  				Do(
   221  					codegen.Return(file.Val(m.Keys.Indexes)),
   222  				),
   223  		)
   224  	}
   225  
   226  	if len(m.Keys.UniqueIndexes) > 0 {
   227  		indexKeys := make([]string, 0)
   228  
   229  		for k := range m.Keys.UniqueIndexes {
   230  			indexKeys = append(indexKeys, k)
   231  		}
   232  
   233  		sort.Strings(indexKeys)
   234  
   235  		for _, k := range indexKeys {
   236  			file.WriteBlock(
   237  				codegen.Func().
   238  					Named("UniqueIndex" + codegen.UpperCamelCase(k)).
   239  					MethodOf(codegen.Var(m.Type())).
   240  					Return(codegen.Var(codegen.String)).
   241  					Do(
   242  						codegen.Return(file.Val(k)),
   243  					),
   244  			)
   245  		}
   246  
   247  		file.WriteBlock(
   248  			codegen.Func().
   249  				Named("UniqueIndexes").
   250  				MethodOf(codegen.Var(m.Type())).
   251  				Return(codegen.Var(codegen.Type(file.Use("github.com/eden-framework/sqlx/builder", "Indexes")))).
   252  				Do(
   253  					codegen.Return(file.Val(m.Keys.UniqueIndexes)),
   254  				),
   255  		)
   256  	}
   257  
   258  	if m.WithComments {
   259  		file.WriteBlock(
   260  			codegen.Func().
   261  				Named("Comments").
   262  				MethodOf(codegen.Var(m.Type())).
   263  				Return(codegen.Var(codegen.Map(codegen.String, codegen.String))).
   264  				Do(
   265  					codegen.Return(file.Val(m.GetComments())),
   266  				),
   267  		)
   268  	}
   269  }
   270  
   271  func (m *Model) GetRelations() map[string][]string {
   272  	rels := map[string][]string{}
   273  	m.Columns.Range(func(col *builder.Column, idx int) {
   274  		if len(col.Relation) == 2 {
   275  			rels[col.FieldName] = col.Relation
   276  		}
   277  	})
   278  	return rels
   279  }
   280  
   281  func (m *Model) GetComments() map[string]string {
   282  	comments := map[string]string{}
   283  	m.Columns.Range(func(col *builder.Column, idx int) {
   284  		if col.Comment != "" {
   285  			comments[col.FieldName] = col.Comment
   286  		}
   287  	})
   288  	return comments
   289  }
   290  
   291  func (m *Model) GetColDescriptions() map[string][]string {
   292  	descriptions := map[string][]string{}
   293  	m.Columns.Range(func(col *builder.Column, idx int) {
   294  		if col.Description != nil {
   295  			descriptions[col.FieldName] = col.Description
   296  		}
   297  	})
   298  	return descriptions
   299  }