Skip to content

Commit

Permalink
Added support for supplying the fields to query in the select statement
Browse files Browse the repository at this point in the history
Example usage: 'NewUserQuerySet(db).Select(UserDBSchema.Name, UserDBSchema.Email).All(&users)'
This will fetch only the 'name' and 'email' fields for all users,
instead of fetching all the fields in the table.
  • Loading branch information
Ronnie Lazar committed Oct 17, 2019
1 parent 828af80 commit ec72873
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
Binary file modified cmd/goqueryset/goqueryset
Binary file not shown.
10 changes: 10 additions & 0 deletions examples/comparison/gorm4/autogenerated_gorm4.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/queryset/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (g Generator) writeQuerySetsToOutput(r io.Reader, packageName, outFile stri
import (
"errors"
"fmt"
"strings"
"time"
"github.com/jinzhu/gorm"
Expand Down
22 changes: 22 additions & 0 deletions internal/queryset/generator/queryset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ type testQueryFunc func(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB)
func TestQueries(t *testing.T) {
funcs := []testQueryFunc{
testUserSelectAll,
testUserSelectAllSingleField,
testUserSelectAllMultipleFields,
testUserSelectWithLimitAndOffset,
testUserSelectAllNoRecords,
testUserSelectOne,
Expand Down Expand Up @@ -152,6 +154,26 @@ func testUserSelectAll(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
assert.Equal(t, expUsers, users)
}

func testUserSelectAllSingleField(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
expUsers := getTestUsers(2)
m.ExpectQuery(fixedFullRe("SELECT name FROM `users` WHERE `users`.`deleted_at` IS NULL")).
WillReturnRows(getRowsForUsers(expUsers))

var users []test.User
assert.Nil(t, test.NewUserQuerySet(db).Select(test.UserDBSchema.Name).All(&users))
assert.Equal(t, expUsers, users)
}

func testUserSelectAllMultipleFields(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
expUsers := getTestUsers(2)
m.ExpectQuery(fixedFullRe("SELECT name,email FROM `users` WHERE `users`.`deleted_at` IS NULL")).
WillReturnRows(getRowsForUsers(expUsers))

var users []test.User
assert.Nil(t, test.NewUserQuerySet(db).Select(test.UserDBSchema.Name, test.UserDBSchema.Email).All(&users))
assert.Equal(t, expUsers, users)
}

func testUserSelectWithLimitAndOffset(t *testing.T, m sqlmock.Sqlmock, db *gorm.DB) {
expUsers := getTestUsers(2)
req := "SELECT * FROM `users` WHERE `users`.`deleted_at` IS NULL LIMIT 1 OFFSET 1"
Expand Down
11 changes: 10 additions & 1 deletion internal/queryset/generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const qsCode = `
// ===== BEGIN of all query sets
{{ range .Configs }}
{{ $ft := printf "%s%s" .StructName "DBSchemaField" }}
// ===== BEGIN of query set {{ .Name }}
// {{ .Name }} is an queryset type for {{ .StructName }}
Expand All @@ -31,6 +32,15 @@ const qsCode = `
return New{{ .Name }}(db)
}
func (qs {{ .Name }}) Select(fields ...{{ $ft }}) {{ .Name }} {
names := []string{}
for _, f := range fields {
names = append(names, f.String())
}
return qs.w(qs.db.Select(strings.Join(names, ",")))
}
{{ range .Methods }}
{{ .GetDoc .GetMethodName }}
func ({{ .GetReceiverDeclaration }}) {{ .GetMethodName }}({{ .GetArgsDeclaration }})
Expand All @@ -43,7 +53,6 @@ const qsCode = `
// ===== BEGIN of {{ .StructName }} modifiers
{{ $ft := printf "%s%s" .StructName "DBSchemaField" }}
// {{ $ft }} describes database schema field. It requires for method 'Update'
type {{ $ft }} string
Expand Down
37 changes: 37 additions & 0 deletions internal/queryset/generator/test/autogenerated_models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions internal/queryset/generator/test/pkgimport/autogenerated_models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ec72873

Please sign in to comment.