diff --git a/generator/templates/model.tgo b/generator/templates/model.tgo index 1d30659..9e9bee8 100644 --- a/generator/templates/model.tgo +++ b/generator/templates/model.tgo @@ -408,6 +408,25 @@ func (s *{{.StoreName}}) MustCount(q *{{.QueryName}}) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *{{.StoreName}}) Exists(q *{{.QueryName}}) (bool, error) { + q.Limit(1) + q.Select(Schema.{{.Name}}.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *{{.StoreName}}) FindOne(q *{{.QueryName}}) (*{{.Name}}, error) { diff --git a/tests/kallax.go b/tests/kallax.go index 180357e..4545c3a 100644 --- a/tests/kallax.go +++ b/tests/kallax.go @@ -246,6 +246,24 @@ func (s *AStore) MustCount(q *AQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *AStore) Exists(q *AQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.A.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *AStore) FindOne(q *AQuery) (*A, error) { @@ -820,6 +838,24 @@ func (s *BStore) MustCount(q *BQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *BStore) Exists(q *BQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.B.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *BStore) FindOne(q *BQuery) (*B, error) { @@ -1280,6 +1316,24 @@ func (s *BrandStore) MustCount(q *BrandQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *BrandStore) Exists(q *BrandQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.Brand.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *BrandStore) FindOne(q *BrandQuery) (*Brand, error) { @@ -1775,6 +1829,24 @@ func (s *CStore) MustCount(q *CQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *CStore) Exists(q *CQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.C.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *CStore) FindOne(q *CQuery) (*C, error) { @@ -2362,6 +2434,24 @@ func (s *CarStore) MustCount(q *CarQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *CarStore) Exists(q *CarQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.Car.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *CarStore) FindOne(q *CarQuery) (*Car, error) { @@ -2805,6 +2895,24 @@ func (s *ChildStore) MustCount(q *ChildQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *ChildStore) Exists(q *ChildQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.Child.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *ChildStore) FindOne(q *ChildQuery) (*Child, error) { @@ -3276,6 +3384,24 @@ func (s *EventsAllFixtureStore) MustCount(q *EventsAllFixtureQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *EventsAllFixtureStore) Exists(q *EventsAllFixtureQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.EventsAllFixture.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *EventsAllFixtureStore) FindOne(q *EventsAllFixtureQuery) (*EventsAllFixture, error) { @@ -3725,6 +3851,24 @@ func (s *EventsFixtureStore) MustCount(q *EventsFixtureQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *EventsFixtureStore) Exists(q *EventsFixtureQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.EventsFixture.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *EventsFixtureStore) FindOne(q *EventsFixtureQuery) (*EventsFixture, error) { @@ -4174,6 +4318,24 @@ func (s *EventsSaveFixtureStore) MustCount(q *EventsSaveFixtureQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *EventsSaveFixtureStore) Exists(q *EventsSaveFixtureQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.EventsSaveFixture.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *EventsSaveFixtureStore) FindOne(q *EventsSaveFixtureQuery) (*EventsSaveFixture, error) { @@ -4599,6 +4761,24 @@ func (s *JSONModelStore) MustCount(q *JSONModelQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *JSONModelStore) Exists(q *JSONModelQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.JSONModel.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *JSONModelStore) FindOne(q *JSONModelQuery) (*JSONModel, error) { @@ -5026,6 +5206,24 @@ func (s *MultiKeySortFixtureStore) MustCount(q *MultiKeySortFixtureQuery) int64 return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *MultiKeySortFixtureStore) Exists(q *MultiKeySortFixtureQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.MultiKeySortFixture.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *MultiKeySortFixtureStore) FindOne(q *MultiKeySortFixtureQuery) (*MultiKeySortFixture, error) { @@ -5482,6 +5680,24 @@ func (s *NullableStore) MustCount(q *NullableQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *NullableStore) Exists(q *NullableQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.Nullable.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *NullableStore) FindOne(q *NullableQuery) (*Nullable, error) { @@ -5983,6 +6199,24 @@ func (s *ParentStore) MustCount(q *ParentQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *ParentStore) Exists(q *ParentQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.Parent.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *ParentStore) FindOne(q *ParentQuery) (*Parent, error) { @@ -6575,6 +6809,24 @@ func (s *ParentNoPtrStore) MustCount(q *ParentNoPtrQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *ParentNoPtrStore) Exists(q *ParentNoPtrQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.ParentNoPtr.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *ParentNoPtrStore) FindOne(q *ParentNoPtrQuery) (*ParentNoPtr, error) { @@ -7241,6 +7493,24 @@ func (s *PersonStore) MustCount(q *PersonQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *PersonStore) Exists(q *PersonQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.Person.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *PersonStore) FindOne(q *PersonQuery) (*Person, error) { @@ -7926,6 +8196,24 @@ func (s *PetStore) MustCount(q *PetQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *PetStore) Exists(q *PetQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.Pet.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *PetStore) FindOne(q *PetQuery) (*Pet, error) { @@ -8621,6 +8909,24 @@ func (s *QueryFixtureStore) MustCount(q *QueryFixtureQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *QueryFixtureStore) Exists(q *QueryFixtureQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.QueryFixture.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *QueryFixtureStore) FindOne(q *QueryFixtureQuery) (*QueryFixture, error) { @@ -9436,6 +9742,24 @@ func (s *QueryRelationFixtureStore) MustCount(q *QueryRelationFixtureQuery) int6 return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *QueryRelationFixtureStore) Exists(q *QueryRelationFixtureQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.QueryRelationFixture.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *QueryRelationFixtureStore) FindOne(q *QueryRelationFixtureQuery) (*QueryRelationFixture, error) { @@ -9860,6 +10184,24 @@ func (s *ResultSetFixtureStore) MustCount(q *ResultSetFixtureQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *ResultSetFixtureStore) Exists(q *ResultSetFixtureQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.ResultSetFixture.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *ResultSetFixtureStore) FindOne(q *ResultSetFixtureQuery) (*ResultSetFixture, error) { @@ -10418,6 +10760,24 @@ func (s *SchemaFixtureStore) MustCount(q *SchemaFixtureQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *SchemaFixtureStore) Exists(q *SchemaFixtureQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.SchemaFixture.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *SchemaFixtureStore) FindOne(q *SchemaFixtureQuery) (*SchemaFixture, error) { @@ -10886,6 +11246,24 @@ func (s *SchemaRelationshipFixtureStore) MustCount(q *SchemaRelationshipFixtureQ return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *SchemaRelationshipFixtureStore) Exists(q *SchemaRelationshipFixtureQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.SchemaRelationshipFixture.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *SchemaRelationshipFixtureStore) FindOne(q *SchemaRelationshipFixtureQuery) (*SchemaRelationshipFixture, error) { @@ -11301,6 +11679,24 @@ func (s *StoreFixtureStore) MustCount(q *StoreFixtureQuery) int64 { return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *StoreFixtureStore) Exists(q *StoreFixtureQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.StoreFixture.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *StoreFixtureStore) FindOne(q *StoreFixtureQuery) (*StoreFixture, error) { @@ -11742,6 +12138,24 @@ func (s *StoreWithConstructFixtureStore) MustCount(q *StoreWithConstructFixtureQ return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *StoreWithConstructFixtureStore) Exists(q *StoreWithConstructFixtureQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.StoreWithConstructFixture.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *StoreWithConstructFixtureStore) FindOne(q *StoreWithConstructFixtureQuery) (*StoreWithConstructFixture, error) { @@ -12159,6 +12573,24 @@ func (s *StoreWithNewFixtureStore) MustCount(q *StoreWithNewFixtureQuery) int64 return s.Store.MustCount(q) } +// Exists returns true if there is at least one record by given query. +func (s *StoreWithNewFixtureStore) Exists(q *StoreWithNewFixtureQuery) (bool, error) { + q.Limit(1) + q.Select(Schema.StoreWithNewFixture.ID) + q.Offset(0) + rs, err := s.Find(q) + if err != nil { + return false, err + } + + if !rs.Next() { + return false, nil + } + + err = rs.Close() + return true, err +} + // FindOne returns the first row returned by the given query. // `ErrNotFound` is returned if there are no results. func (s *StoreWithNewFixtureStore) FindOne(q *StoreWithNewFixtureQuery) (*StoreWithNewFixture, error) {