Skip to content

Commit

Permalink
Merge pull request go-gorm#167 from go-gorm/dev
Browse files Browse the repository at this point in the history
add method GetFieldByName
  • Loading branch information
tr1v3r authored Oct 26, 2021
2 parents 2fce38b + ad3d06a commit ea149b7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
18 changes: 12 additions & 6 deletions do.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ func (d *DO) Select(columns ...field.Expr) Dao {
if len(columns) == 0 {
return d.getInstance(d.db.Clauses(clause.Select{}))
}
return d.getInstance(d.db.Select(buildExpr(d.db.Statement, columns...)))
query, args := buildExpr(d.db.Statement, columns...)
return d.getInstance(d.db.Select(query, args...))
}

func (d *DO) Where(conds ...Condition) Dao {
Expand Down Expand Up @@ -580,12 +581,17 @@ func buildColumn(stmt *gorm.Statement, cols []field.Expr, opts ...field.BuildOpt
return results
}

func buildExpr(stmt *gorm.Statement, exprs ...field.Expr) []string {
results := make([]string, len(exprs))
for i, e := range exprs {
results[i] = e.Build(stmt).String()
func buildExpr(stmt *gorm.Statement, exprs ...field.Expr) (query string, args []interface{}) {
for _, e := range exprs {
sql, vars := e.BuildWithArgs(stmt)
if query == "" {
query = sql.String()
} else {
query += "," + sql.String()
}
args = append(args, vars...)
}
return results
return query, args
}

func toExpression(exprs ...field.Expr) []clause.Expression {
Expand Down
10 changes: 10 additions & 0 deletions field/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Expr interface {
ColumnName() sql
BuildColumn(*gorm.Statement, ...BuildOpt) sql
Build(*gorm.Statement) sql
BuildWithArgs(*gorm.Statement) (query sql, args []interface{})
RawExpr() expression

// implement Condition
Expand Down Expand Up @@ -103,6 +104,15 @@ func (e expr) Build(stmt *gorm.Statement) sql {
return sql(newStmt.SQL.String())
}

func (e expr) BuildWithArgs(stmt *gorm.Statement) (sql, []interface{}) {
if e.e == nil {
return sql(e.BuildColumn(stmt, WithAll)), nil
}
newStmt := &gorm.Statement{DB: stmt.DB, Table: stmt.Table, Schema: stmt.Schema}
e.e.Build(newStmt)
return sql(newStmt.SQL.String()), newStmt.Vars
}

func (e expr) RawExpr() expression {
if e.e == nil {
return e.col
Expand Down
5 changes: 5 additions & 0 deletions field/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ func TestExpr_Build(t *testing.T) {
ExpectedVars: []interface{}{time.Duration(24 * time.Hour).Microseconds()},
Result: "DATE_SUB(`creatAt`, INTERVAL ? MICROSECOND)",
},
{
Expr: field.NewTime("", "updateAt").DateFormat("%W %M %Y"),
ExpectedVars: []interface{}{"%W %M %Y"},
Result: "DATE_FORMAT(`updateAt`,?)",
},
// ======================== bool ========================
{
Expr: field.NewBool("", "male").Not(),
Expand Down
23 changes: 19 additions & 4 deletions internal/template/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const (
` + members + `
}
` + cloneMethod + relationship + defineMethodStruct
` + getFieldMethod + cloneMethod + relationship + defineMethodStruct

BaseStructWithContext = createMethod + `
type {{.NewStructName}} struct {
Expand All @@ -17,9 +17,11 @@ const (
func ({{.S}} *{{.NewStructName}}) WithContext(ctx context.Context) *{{.NewStructName}}Do { return {{.S}}.{{.NewStructName}}Do.WithContext(ctx)}
func ({{.S}} {{.NewStructName}}) TableName() string { return {{.S}}.{{.NewStructName}}Do.TableName()}
func ({{.S}} {{.NewStructName}}) TableName() string { return {{.S}}.{{.NewStructName}}Do.TableName()}
` + cloneMethod + relationship + defineMethodStruct
` + getFieldMethod + cloneMethod + relationship + defineMethodStruct
)

const (
Expand All @@ -43,7 +45,12 @@ const (
}
{{end}}
{{end}}
{{range .Members -}}
{{if not .IsRelation -}}
_{{$.NewStructName}}.fieldMap["{{.ColumnName}}"] = _{{$.NewStructName}}.{{.Name}}
{{end -}}
{{end}}
return _{{.NewStructName}}
}
`
Expand All @@ -57,12 +64,20 @@ const (
{{.Relation.Name}} {{$.NewStructName}}{{.Relation.RelationshipName}}{{.Relation.Name}}
{{end}}
{{end}}
fieldMap map[string]field.Expr
`
cloneMethod = `
func ({{.S}} {{.NewStructName}}) clone(db *gorm.DB) {{.NewStructName}} {
{{.S}}.{{.NewStructName}}Do.ReplaceDB(db)
return {{.S}}
}
`
getFieldMethod = `
func ({{.S}} *{{.NewStructName}}) GetFieldByName(fieldName string) (field.Expr, bool) {
field, ok := {{.S}}.fieldMap[fieldName]
return field, ok
}
`
relationship = `{{range .Members}}{{if .IsRelation}}` +
`{{- $relation := .Relation }}{{- $relationship := $relation.RelationshipName}}` +
Expand Down

0 comments on commit ea149b7

Please sign in to comment.