Skip to content

Commit

Permalink
Merge pull request #154 from erizocosmico/feature/find-all
Browse files Browse the repository at this point in the history
FindAll added to all typed stores
  • Loading branch information
Miguel Molina committed May 9, 2017
2 parents 71244f0 + 2628007 commit 26b6929
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 24 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -438,6 +438,21 @@ q := NewUserQuery().
user, err := store.FindOne(q)
```

You can also get all of the rows in a result without having to manually iterate the result set with `FindAll`.

```go
q := NewUserQuery().
Where(kallax.Like(Schema.User.Username, "joe%")).
Order(kallax.Asc(Schema.User.Username)).
Limit(20).
Offset(2)

users, err := store.FindAll(q)
if err != nil {
// handle error
}
```

By default, all columns in a row are retrieved. To not retrieve all of them, you can specify the columns to include/exclude. Take into account that partial records retrieved from the database will not be writable. To make them writable you will need to [`Reload`](#reloading-a-model) the object.

```go
Expand Down
14 changes: 2 additions & 12 deletions benchmarks/bench_test.go
Expand Up @@ -245,12 +245,7 @@ func BenchmarkKallaxQueryRelationships(b *testing.B) {

b.Run("query", func(b *testing.B) {
for i := 0; i < b.N; i++ {
rs, err := store.Find(NewPersonQuery().WithPets(nil).Limit(100))
if err != nil {
b.Fatalf("error retrieving persons: %s", err)
}

_, err = rs.All()
_, err := store.FindAll(NewPersonQuery().WithPets(nil).Limit(100))
if err != nil {
b.Fatalf("error retrieving persons: %s", err)
}
Expand Down Expand Up @@ -374,12 +369,7 @@ func BenchmarkKallaxQuery(b *testing.B) {

b.Run("query", func(b *testing.B) {
for i := 0; i < b.N; i++ {
rs, err := store.Find(NewPersonQuery())
if err != nil {
b.Fatalf("error retrieving persons: %s", err)
}

_, err = rs.All()
_, err := store.FindAll(NewPersonQuery())
if err != nil {
b.Fatalf("error retrieving persons: %s", err)
}
Expand Down
87 changes: 78 additions & 9 deletions benchmarks/kallax.go
Expand Up @@ -97,16 +97,37 @@ func NewPersonStore(db *sql.DB) *PersonStore {
return &PersonStore{kallax.NewStore(db)}
}

// GenericStore returns the generic store of this store.
func (s *PersonStore) GenericStore() *kallax.Store {
return s.Store
}

// SetGenericStore changes the generic store of this store.
func (s *PersonStore) SetGenericStore(store *kallax.Store) {
s.Store = store
}

// Debug returns a new store that will print all SQL statements to stdout using
// the log.Printf function.
func (s *PersonStore) Debug() *PersonStore {
return &PersonStore{s.Store.Debug()}
}

// DebugWith returns a new store that will print all SQL statements using the
// given logger function.
func (s *PersonStore) DebugWith(logger kallax.LoggerFunc) *PersonStore {
return &PersonStore{s.Store.DebugWith(logger)}
}

func (s *PersonStore) relationshipRecords(record *Person) []kallax.RecordWithSchema {
record.ClearVirtualColumns()
var records []kallax.RecordWithSchema

for _, rec := range record.Pets {
rec.ClearVirtualColumns()
rec.AddVirtualColumn("person_id", record.GetID())
records = append(records, kallax.RecordWithSchema{
Schema.Pet.BaseSchema,
rec,
Schema: Schema.Pet.BaseSchema,
Record: rec,
})
}

Expand All @@ -118,8 +139,10 @@ func (s *PersonStore) relationshipRecords(record *Person) []kallax.RecordWithSch
func (s *PersonStore) Insert(record *Person) error {

records := s.relationshipRecords(record)

if len(records) > 0 {
return s.Store.Transaction(func(s *kallax.Store) error {

if err := s.Insert(Schema.Person.BaseSchema, record); err != nil {
return err
}
Expand Down Expand Up @@ -156,8 +179,10 @@ func (s *PersonStore) Insert(record *Person) error {
func (s *PersonStore) Update(record *Person, cols ...kallax.SchemaField) (updated int64, err error) {

records := s.relationshipRecords(record)

if len(records) > 0 {
err = s.Store.Transaction(func(s *kallax.Store) error {

updated, err = s.Update(Schema.Person.BaseSchema, record, cols...)
if err != nil {
return err
Expand Down Expand Up @@ -267,6 +292,16 @@ func (s *PersonStore) FindOne(q *PersonQuery) (*Person, error) {
return record, nil
}

// FindAll returns a list of all the rows returned by the given query.
func (s *PersonStore) FindAll(q *PersonQuery) ([]*Person, error) {
rs, err := s.Find(q)
if err != nil {
return nil, err
}

return rs.All()
}

// MustFindOne returns the first row retrieved by the given query. It panics
// if there is an error or if there are no rows.
func (s *PersonStore) MustFindOne(q *PersonQuery) *Person {
Expand Down Expand Up @@ -460,7 +495,8 @@ func (q *PersonQuery) WithPets(cond kallax.Condition) *PersonQuery {
}

// FindByID adds a new filter to the query that will require that
// the ID property is equal to one of the passed values; if no passed values, it will do nothing
// the ID property is equal to one of the passed values; if no passed values,
// it will do nothing.
func (q *PersonQuery) FindByID(v ...int64) *PersonQuery {
if len(v) == 0 {
return q
Expand All @@ -473,7 +509,7 @@ func (q *PersonQuery) FindByID(v ...int64) *PersonQuery {
}

// FindByName adds a new filter to the query that will require that
// the Name property is equal to the passed value
// the Name property is equal to the passed value.
func (q *PersonQuery) FindByName(v string) *PersonQuery {
return q.Where(kallax.Eq(Schema.Person.Name, v))
}
Expand Down Expand Up @@ -604,7 +640,7 @@ func (r *Pet) ColumnAddress(col string) (interface{}, error) {
case "name":
return &r.Name, nil
case "kind":
return &r.Kind, nil
return (*string)(&r.Kind), nil

default:
return nil, fmt.Errorf("kallax: invalid column in Pet: %s", col)
Expand Down Expand Up @@ -649,6 +685,28 @@ func NewPetStore(db *sql.DB) *PetStore {
return &PetStore{kallax.NewStore(db)}
}

// GenericStore returns the generic store of this store.
func (s *PetStore) GenericStore() *kallax.Store {
return s.Store
}

// SetGenericStore changes the generic store of this store.
func (s *PetStore) SetGenericStore(store *kallax.Store) {
s.Store = store
}

// Debug returns a new store that will print all SQL statements to stdout using
// the log.Printf function.
func (s *PetStore) Debug() *PetStore {
return &PetStore{s.Store.Debug()}
}

// DebugWith returns a new store that will print all SQL statements using the
// given logger function.
func (s *PetStore) DebugWith(logger kallax.LoggerFunc) *PetStore {
return &PetStore{s.Store.DebugWith(logger)}
}

// Insert inserts a Pet in the database. A non-persisted object is
// required for this operation.
func (s *PetStore) Insert(record *Pet) error {
Expand Down Expand Up @@ -745,6 +803,16 @@ func (s *PetStore) FindOne(q *PetQuery) (*Pet, error) {
return record, nil
}

// FindAll returns a list of all the rows returned by the given query.
func (s *PetStore) FindAll(q *PetQuery) ([]*Pet, error) {
rs, err := s.Find(q)
if err != nil {
return nil, err
}

return rs.All()
}

// MustFindOne returns the first row retrieved by the given query. It panics
// if there is an error or if there are no rows.
func (s *PetStore) MustFindOne(q *PetQuery) *Pet {
Expand Down Expand Up @@ -844,7 +912,8 @@ func (q *PetQuery) Where(cond kallax.Condition) *PetQuery {
}

// FindByID adds a new filter to the query that will require that
// the ID property is equal to one of the passed values; if no passed values, it will do nothing
// the ID property is equal to one of the passed values; if no passed values,
// it will do nothing.
func (q *PetQuery) FindByID(v ...int64) *PetQuery {
if len(v) == 0 {
return q
Expand All @@ -857,13 +926,13 @@ func (q *PetQuery) FindByID(v ...int64) *PetQuery {
}

// FindByName adds a new filter to the query that will require that
// the Name property is equal to the passed value
// the Name property is equal to the passed value.
func (q *PetQuery) FindByName(v string) *PetQuery {
return q.Where(kallax.Eq(Schema.Pet.Name, v))
}

// FindByKind adds a new filter to the query that will require that
// the Kind property is equal to the passed value
// the Kind property is equal to the passed value.
func (q *PetQuery) FindByKind(v PetKind) *PetQuery {
return q.Where(kallax.Eq(Schema.Pet.Kind, v))
}
Expand Down
10 changes: 10 additions & 0 deletions generator/templates/model.tgo
Expand Up @@ -463,6 +463,16 @@ func (s *{{.StoreName}}) FindOne(q *{{.QueryName}}) (*{{.Name}}, error) {
return record, nil
}

// FindAll returns a list of all the rows returned by the given query.
func (s *{{.StoreName}}) FindAll(q *{{.QueryName}}) ([]*{{.Name}}, error) {
rs, err := s.Find(q)
if err != nil {
return nil, err
}

return rs.All()
}

// MustFindOne returns the first row retrieved by the given query. It panics
// if there is an error or if there are no rows.
func (s *{{.StoreName}}) MustFindOne(q *{{.QueryName}}) *{{.Name}} {
Expand Down

0 comments on commit 26b6929

Please sign in to comment.