Skip to content

Commit

Permalink
Merge pull request #30 from jirfag/exported-db-schema-field
Browse files Browse the repository at this point in the history
#21: generate DBSchemaField as exported type
  • Loading branch information
jirfag authored Feb 13, 2018
2 parents 628a8ad + a45a964 commit e76f651
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 154 deletions.
115 changes: 58 additions & 57 deletions examples/comparison/gorm4/autogenerated_gorm4.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,37 @@ func (o *User) Create(db *gorm.DB) error {
// CreatedAtEq is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) CreatedAtEq(createdAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("created_at = ?", createdAt))
return qs.w(qs.db.Where("`created_at` = ?", createdAt))
}

// CreatedAtGt is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) CreatedAtGt(createdAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("created_at > ?", createdAt))
return qs.w(qs.db.Where("`created_at` > ?", createdAt))
}

// CreatedAtGte is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) CreatedAtGte(createdAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("created_at >= ?", createdAt))
return qs.w(qs.db.Where("`created_at` >= ?", createdAt))
}

// CreatedAtLt is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) CreatedAtLt(createdAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("created_at < ?", createdAt))
return qs.w(qs.db.Where("`created_at` < ?", createdAt))
}

// CreatedAtLte is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) CreatedAtLte(createdAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("created_at <= ?", createdAt))
return qs.w(qs.db.Where("`created_at` <= ?", createdAt))
}

// CreatedAtNe is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) CreatedAtNe(createdAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("created_at != ?", createdAt))
return qs.w(qs.db.Where("`created_at` != ?", createdAt))
}

// Delete is an autogenerated method
Expand All @@ -98,19 +98,19 @@ func (qs UserQuerySet) Delete() error {
// DeletedAtEq is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) DeletedAtEq(deletedAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("deleted_at = ?", deletedAt))
return qs.w(qs.db.Where("`deleted_at` = ?", deletedAt))
}

// DeletedAtGt is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) DeletedAtGt(deletedAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("deleted_at > ?", deletedAt))
return qs.w(qs.db.Where("`deleted_at` > ?", deletedAt))
}

// DeletedAtGte is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) DeletedAtGte(deletedAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("deleted_at >= ?", deletedAt))
return qs.w(qs.db.Where("`deleted_at` >= ?", deletedAt))
}

// DeletedAtIsNotNull is an autogenerated method
Expand All @@ -128,19 +128,19 @@ func (qs UserQuerySet) DeletedAtIsNull() UserQuerySet {
// DeletedAtLt is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) DeletedAtLt(deletedAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("deleted_at < ?", deletedAt))
return qs.w(qs.db.Where("`deleted_at` < ?", deletedAt))
}

// DeletedAtLte is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) DeletedAtLte(deletedAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("deleted_at <= ?", deletedAt))
return qs.w(qs.db.Where("`deleted_at` <= ?", deletedAt))
}

// DeletedAtNe is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) DeletedAtNe(deletedAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("deleted_at != ?", deletedAt))
return qs.w(qs.db.Where("`deleted_at` != ?", deletedAt))
}

// GetUpdater is an autogenerated method
Expand All @@ -152,19 +152,19 @@ func (qs UserQuerySet) GetUpdater() UserUpdater {
// IDEq is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) IDEq(ID uint) UserQuerySet {
return qs.w(qs.db.Where("id = ?", ID))
return qs.w(qs.db.Where("`id` = ?", ID))
}

// IDGt is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) IDGt(ID uint) UserQuerySet {
return qs.w(qs.db.Where("id > ?", ID))
return qs.w(qs.db.Where("`id` > ?", ID))
}

// IDGte is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) IDGte(ID uint) UserQuerySet {
return qs.w(qs.db.Where("id >= ?", ID))
return qs.w(qs.db.Where("`id` >= ?", ID))
}

// IDIn is an autogenerated method
Expand All @@ -174,25 +174,25 @@ func (qs UserQuerySet) IDIn(ID uint, IDRest ...uint) UserQuerySet {
for _, arg := range IDRest {
iArgs = append(iArgs, arg)
}
return qs.w(qs.db.Where("id IN (?)", iArgs))
return qs.w(qs.db.Where("`id` IN (?)", iArgs))
}

// IDLt is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) IDLt(ID uint) UserQuerySet {
return qs.w(qs.db.Where("id < ?", ID))
return qs.w(qs.db.Where("`id` < ?", ID))
}

// IDLte is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) IDLte(ID uint) UserQuerySet {
return qs.w(qs.db.Where("id <= ?", ID))
return qs.w(qs.db.Where("`id` <= ?", ID))
}

// IDNe is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) IDNe(ID uint) UserQuerySet {
return qs.w(qs.db.Where("id != ?", ID))
return qs.w(qs.db.Where("`id` != ?", ID))
}

// IDNotIn is an autogenerated method
Expand All @@ -202,7 +202,7 @@ func (qs UserQuerySet) IDNotIn(ID uint, IDRest ...uint) UserQuerySet {
for _, arg := range IDRest {
iArgs = append(iArgs, arg)
}
return qs.w(qs.db.Where("id NOT IN (?)", iArgs))
return qs.w(qs.db.Where("`id` NOT IN (?)", iArgs))
}

// Limit is an autogenerated method
Expand Down Expand Up @@ -292,19 +292,19 @@ func (qs UserQuerySet) OrderDescByUpdatedAt() UserQuerySet {
// RatingEq is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) RatingEq(rating int) UserQuerySet {
return qs.w(qs.db.Where("rating = ?", rating))
return qs.w(qs.db.Where("`rating` = ?", rating))
}

// RatingGt is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) RatingGt(rating int) UserQuerySet {
return qs.w(qs.db.Where("rating > ?", rating))
return qs.w(qs.db.Where("`rating` > ?", rating))
}

// RatingGte is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) RatingGte(rating int) UserQuerySet {
return qs.w(qs.db.Where("rating >= ?", rating))
return qs.w(qs.db.Where("`rating` >= ?", rating))
}

// RatingIn is an autogenerated method
Expand All @@ -314,37 +314,37 @@ func (qs UserQuerySet) RatingIn(rating int, ratingRest ...int) UserQuerySet {
for _, arg := range ratingRest {
iArgs = append(iArgs, arg)
}
return qs.w(qs.db.Where("rating IN (?)", iArgs))
return qs.w(qs.db.Where("`rating` IN (?)", iArgs))
}

// RatingLt is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) RatingLt(rating int) UserQuerySet {
return qs.w(qs.db.Where("rating < ?", rating))
return qs.w(qs.db.Where("`rating` < ?", rating))
}

// RatingLte is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) RatingLte(rating int) UserQuerySet {
return qs.w(qs.db.Where("rating <= ?", rating))
return qs.w(qs.db.Where("`rating` <= ?", rating))
}

// RatingMarksEq is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) RatingMarksEq(ratingMarks int) UserQuerySet {
return qs.w(qs.db.Where("rating_marks = ?", ratingMarks))
return qs.w(qs.db.Where("`rating_marks` = ?", ratingMarks))
}

// RatingMarksGt is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) RatingMarksGt(ratingMarks int) UserQuerySet {
return qs.w(qs.db.Where("rating_marks > ?", ratingMarks))
return qs.w(qs.db.Where("`rating_marks` > ?", ratingMarks))
}

// RatingMarksGte is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) RatingMarksGte(ratingMarks int) UserQuerySet {
return qs.w(qs.db.Where("rating_marks >= ?", ratingMarks))
return qs.w(qs.db.Where("`rating_marks` >= ?", ratingMarks))
}

// RatingMarksIn is an autogenerated method
Expand All @@ -354,25 +354,25 @@ func (qs UserQuerySet) RatingMarksIn(ratingMarks int, ratingMarksRest ...int) Us
for _, arg := range ratingMarksRest {
iArgs = append(iArgs, arg)
}
return qs.w(qs.db.Where("rating_marks IN (?)", iArgs))
return qs.w(qs.db.Where("`rating_marks` IN (?)", iArgs))
}

// RatingMarksLt is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) RatingMarksLt(ratingMarks int) UserQuerySet {
return qs.w(qs.db.Where("rating_marks < ?", ratingMarks))
return qs.w(qs.db.Where("`rating_marks` < ?", ratingMarks))
}

// RatingMarksLte is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) RatingMarksLte(ratingMarks int) UserQuerySet {
return qs.w(qs.db.Where("rating_marks <= ?", ratingMarks))
return qs.w(qs.db.Where("`rating_marks` <= ?", ratingMarks))
}

// RatingMarksNe is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) RatingMarksNe(ratingMarks int) UserQuerySet {
return qs.w(qs.db.Where("rating_marks != ?", ratingMarks))
return qs.w(qs.db.Where("`rating_marks` != ?", ratingMarks))
}

// RatingMarksNotIn is an autogenerated method
Expand All @@ -382,13 +382,13 @@ func (qs UserQuerySet) RatingMarksNotIn(ratingMarks int, ratingMarksRest ...int)
for _, arg := range ratingMarksRest {
iArgs = append(iArgs, arg)
}
return qs.w(qs.db.Where("rating_marks NOT IN (?)", iArgs))
return qs.w(qs.db.Where("`rating_marks` NOT IN (?)", iArgs))
}

// RatingNe is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) RatingNe(rating int) UserQuerySet {
return qs.w(qs.db.Where("rating != ?", rating))
return qs.w(qs.db.Where("`rating` != ?", rating))
}

// RatingNotIn is an autogenerated method
Expand All @@ -398,7 +398,7 @@ func (qs UserQuerySet) RatingNotIn(rating int, ratingRest ...int) UserQuerySet {
for _, arg := range ratingRest {
iArgs = append(iArgs, arg)
}
return qs.w(qs.db.Where("rating NOT IN (?)", iArgs))
return qs.w(qs.db.Where("`rating` NOT IN (?)", iArgs))
}

// SetCreatedAt is an autogenerated method
Expand Down Expand Up @@ -459,69 +459,70 @@ func (u UserUpdater) UpdateNum() (int64, error) {
// UpdatedAtEq is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) UpdatedAtEq(updatedAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("updated_at = ?", updatedAt))
return qs.w(qs.db.Where("`updated_at` = ?", updatedAt))
}

// UpdatedAtGt is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) UpdatedAtGt(updatedAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("updated_at > ?", updatedAt))
return qs.w(qs.db.Where("`updated_at` > ?", updatedAt))
}

// UpdatedAtGte is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) UpdatedAtGte(updatedAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("updated_at >= ?", updatedAt))
return qs.w(qs.db.Where("`updated_at` >= ?", updatedAt))
}

// UpdatedAtLt is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) UpdatedAtLt(updatedAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("updated_at < ?", updatedAt))
return qs.w(qs.db.Where("`updated_at` < ?", updatedAt))
}

// UpdatedAtLte is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) UpdatedAtLte(updatedAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("updated_at <= ?", updatedAt))
return qs.w(qs.db.Where("`updated_at` <= ?", updatedAt))
}

// UpdatedAtNe is an autogenerated method
// nolint: dupl
func (qs UserQuerySet) UpdatedAtNe(updatedAt time.Time) UserQuerySet {
return qs.w(qs.db.Where("updated_at != ?", updatedAt))
return qs.w(qs.db.Where("`updated_at` != ?", updatedAt))
}

// ===== END of query set UserQuerySet

// ===== BEGIN of User modifiers

type userDBSchemaField string
// UserDBSchemaField describes database schema field. It requires for method 'Update'
type UserDBSchemaField string

func (f userDBSchemaField) String() string {
func (f UserDBSchemaField) String() string {
return string(f)
}

// UserDBSchema stores db field names of User
var UserDBSchema = struct {
ID userDBSchemaField
CreatedAt userDBSchemaField
UpdatedAt userDBSchemaField
DeletedAt userDBSchemaField
Rating userDBSchemaField
RatingMarks userDBSchemaField
ID UserDBSchemaField
CreatedAt UserDBSchemaField
UpdatedAt UserDBSchemaField
DeletedAt UserDBSchemaField
Rating UserDBSchemaField
RatingMarks UserDBSchemaField
}{

ID: userDBSchemaField("id"),
CreatedAt: userDBSchemaField("created_at"),
UpdatedAt: userDBSchemaField("updated_at"),
DeletedAt: userDBSchemaField("deleted_at"),
Rating: userDBSchemaField("rating"),
RatingMarks: userDBSchemaField("rating_marks"),
ID: UserDBSchemaField("id"),
CreatedAt: UserDBSchemaField("created_at"),
UpdatedAt: UserDBSchemaField("updated_at"),
DeletedAt: UserDBSchemaField("deleted_at"),
Rating: UserDBSchemaField("rating"),
RatingMarks: UserDBSchemaField("rating_marks"),
}

// Update updates User fields by primary key
func (o *User) Update(db *gorm.DB, fields ...userDBSchemaField) error {
func (o *User) Update(db *gorm.DB, fields ...UserDBSchemaField) error {
dbNameToFieldName := map[string]interface{}{
"id": o.ID,
"created_at": o.CreatedAt,
Expand Down
8 changes: 2 additions & 6 deletions queryset/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ package queryset

import (
"text/template"

"github.com/jirfag/go-queryset/queryset/methods"
)

var qsTmpl = template.Must(
template.New("generator").
Funcs(template.FuncMap{
"lcf": methods.LowercaseFirstRune,
}).
Parse(qsCode),
)

Expand Down Expand Up @@ -48,7 +43,8 @@ const qsCode = `
// ===== BEGIN of {{ .StructName }} modifiers
{{ $ft := printf "%s%s" .StructName "DBSchemaField" | lcf }}
{{ $ft := printf "%s%s" .StructName "DBSchemaField" }}
// {{ $ft }} describes database schema field. It requires for method 'Update'
type {{ $ft }} string
func (f {{ $ft }}) String() string {
Expand Down
Loading

0 comments on commit e76f651

Please sign in to comment.