Commit 82b32c0f authored by Amos Wenger's avatar Amos Wenger

Make SelectOne return bool

parent 82eb2f62
Pipeline #10999 passed with stage
in 35 seconds
......@@ -48,7 +48,9 @@ func Test_AutoMigrate(t *testing.T) {
ordie(c.SaveOne(conn, &User{ID: 123, FirstName: "Joanna"}))
u := &User{}
ordie(c.SelectOne(conn, u, builder.Eq{"id": 123}))
foundUser, err := c.SelectOne(conn, u, builder.Eq{"id": 123})
ordie(err)
assert.True(t, foundUser)
assert.EqualValues(t, &User{ID: 123, FirstName: "Joanna"}, u)
t.Logf("first migration (bis)")
......@@ -89,7 +91,13 @@ func Test_AutoMigrate(t *testing.T) {
assert.False(t, pti[2].NotNull)
u := &User{}
ordie(c.SelectOne(conn, u, builder.Eq{"id": 123}))
foundUser, err := c.SelectOne(conn, u, builder.Eq{"id": 83294})
ordie(err)
assert.False(t, foundUser)
foundUser, err = c.SelectOne(conn, u, builder.Eq{"id": 123})
ordie(err)
assert.True(t, foundUser)
assert.EqualValues(t, &User{ID: 123, FirstName: "Joanna", LastName: ""}, u)
t.Logf("second migration (bis)")
......@@ -188,7 +196,9 @@ func Test_AutoMigrateAllValidTypes(t *testing.T) {
ordie(c.SaveOne(conn, h1))
h2 := &Humanoid{}
ordie(c.SelectOne(conn, h2, builder.Eq{"id": 12}))
found, err := c.SelectOne(conn, h2, builder.Eq{"id": 12})
ordie(err)
assert.True(t, found)
assert.EqualValues(t, h1.ID, h2.ID)
assert.EqualValues(t, h1.Alive, h2.Alive)
......
......@@ -70,7 +70,9 @@ func Test_BelongsTo(t *testing.T) {
})
fate := &Fate{}
wtest.Must(t, c.SelectOne(conn, fate, builder.Eq{"id": 421}))
found, err := c.SelectOne(conn, fate, builder.Eq{"id": 421})
wtest.Must(t, err)
assert.True(t, found)
assert.EqualValues(t, "Book authorship", fate.Desc)
})
......
......@@ -53,7 +53,9 @@ func Test_HasMany(t *testing.T) {
assertCount(&Quality{}, 3)
{
q := &Quality{}
wtest.Must(t, c.SelectOne(conn, q, builder.Eq{"id": 11}))
found, err := c.SelectOne(conn, q, builder.Eq{"id": 11})
wtest.Must(t, err)
assert.True(t, found)
assert.EqualValues(t, "Inspiration again", q.Label)
}
......
......@@ -64,7 +64,9 @@ func Test_HasOne(t *testing.T) {
var countries []*Country
for i := 0; i < 4; i++ {
country := &Country{}
wtest.Must(t, c.SelectOne(conn, country, builder.Eq{"id": 324}))
found, err := c.SelectOne(conn, country, builder.Eq{"id": 324})
wtest.Must(t, err)
assert.True(t, found)
countries = append(countries, country)
}
......
......@@ -105,7 +105,9 @@ func Test_ManyToMany(t *testing.T) {
{
w := &Word{}
wtest.Must(t, c.SelectOne(conn, w, builder.Eq{"id": "Wreck"}))
found, err := c.SelectOne(conn, w, builder.Eq{"id": "Wreck"})
wtest.Must(t, err)
assert.True(t, found)
assert.EqualValues(t, "punk band reference", w.Comment)
}
......
......@@ -55,7 +55,9 @@ func Test_Null(t *testing.T) {
ordie(c.SaveOne(conn, d))
{
dd := &Download{}
ordie(c.SelectOne(conn, dd, builder.Eq{"id": 123}))
found, err := c.SelectOne(conn, dd, builder.Eq{"id": 123})
ordie(err)
assert.True(t, found)
assert.EqualValues(t, 123, dd.ID)
assert.Nil(t, dd.FinishedAt)
......@@ -76,7 +78,9 @@ func Test_Null(t *testing.T) {
{
dd := &Download{}
ordie(c.SelectOne(conn, dd, builder.Eq{"id": 123}))
found, err := c.SelectOne(conn, dd, builder.Eq{"id": 123})
ordie(err)
assert.True(t, found)
assert.EqualValues(t, 123, dd.ID)
assert.EqualValues(t, *d.ErrorMessage, *dd.ErrorMessage)
......@@ -89,7 +93,9 @@ func Test_Null(t *testing.T) {
{
dd := &Download{}
ordie(c.SelectOne(conn, dd, builder.Eq{"id": 123}))
found, err := c.SelectOne(conn, dd, builder.Eq{"id": 123})
ordie(err)
assert.True(t, found)
assert.EqualValues(t, 123, dd.ID)
assert.Nil(t, dd.ErrorMessage)
......
......@@ -50,19 +50,20 @@ func (c *Context) Select(conn *sqlite.Conn, result interface{}, cond builder.Con
//
func (c *Context) SelectOne(conn *sqlite.Conn, result interface{}, cond builder.Cond) error {
func (c *Context) SelectOne(conn *sqlite.Conn, result interface{}, cond builder.Cond) (bool, error) {
found := false
resultVal := reflect.ValueOf(result)
originalType := resultVal.Type()
modelType := originalType
if resultVal.Type().Kind() != reflect.Ptr {
return errors.Errorf("SelectOne expects results to be a *Model, but it got a %v", originalType)
return found, errors.Errorf("SelectOne expects results to be a *Model, but it got a %v", originalType)
}
resultVal = resultVal.Elem()
scope := c.ScopeMap.ByType(modelType)
if scope == nil {
return errors.Errorf("%v is not a model known to this hades context", modelType)
return found, errors.Errorf("%v is not a model known to this hades context", modelType)
}
ms := scope.GetModelStruct()
......@@ -70,13 +71,19 @@ func (c *Context) SelectOne(conn *sqlite.Conn, result interface{}, cond builder.
query, args, err := builder.Select(columns...).From(ms.TableName).Where(cond).ToSQL()
if err != nil {
return err
return found, err
}
query = Search().Limit(1).Apply(query)
return c.ExecRaw(conn, query, func(stmt *sqlite.Stmt) error {
return c.Scan(stmt, fields, resultVal)
err = c.ExecRaw(conn, query, func(stmt *sqlite.Stmt) error {
err := c.Scan(stmt, fields, resultVal)
if err != nil {
return err
}
found = true
return nil
}, args...)
return found, err
}
func (c *Context) selectFields(ms *ModelStruct) ([]string, []*StructField) {
......
......@@ -60,7 +60,9 @@ func Test_Select(t *testing.T) {
assert.EqualValues(t, 4, count)
honor := &Honor{}
wtest.Must(t, c.SelectOne(conn, honor, builder.Eq{"id": 3}))
found, err := c.SelectOne(conn, honor, builder.Eq{"id": 3})
wtest.Must(t, err)
assert.True(t, found)
var honors []*Honor
wtest.Must(t, c.Select(conn, &honors, builder.Gte{"id": 2}, nil))
......@@ -86,15 +88,19 @@ func Test_Select(t *testing.T) {
// ---------
err = c.SelectOne(conn, []Honor{}, builder.Eq{"id": 3})
hhh := &Honor{}
_, err = c.SelectOne(conn, &hhh, builder.Eq{"id": 3})
assert.Error(t, err, "SelectOne must pointer to pointer")
_, err = c.SelectOne(conn, []Honor{}, builder.Eq{"id": 3})
assert.Error(t, err, "SelectOne must reject slice")
answer := 42
err = c.SelectOne(conn, &answer, builder.Eq{"id": 3})
_, err = c.SelectOne(conn, &answer, builder.Eq{"id": 3})
assert.Error(t, err, "SelectOne must reject pointer to non-struct")
nam := &NotAModel{}
err = c.SelectOne(conn, nam, builder.Eq{"id": 3})
_, err = c.SelectOne(conn, nam, builder.Eq{"id": 3})
assert.Error(t, err, "SelectOne must reject pointer to non-struct")
}
......@@ -152,7 +158,8 @@ func Test_SelectSquashed(t *testing.T) {
assert.EqualValues(t, 4, count)
a := &Android{}
err = c.SelectOne(conn, a, builder.Eq{"id": 1})
found, err := c.SelectOne(conn, a, builder.Eq{"id": 1})
wtest.Must(t, err)
assert.True(t, found)
assert.EqualValues(t, baseAndroids[0], *a)
}
......@@ -164,7 +164,9 @@ func Test_SquashedFull(t *testing.T) {
wtest.Must(t, c.SaveOne(conn, fu))
u := &FakeUser{}
wtest.Must(t, c.SelectOne(conn, u, builder.NewCond()))
found, err := c.SelectOne(conn, u, builder.NewCond())
wtest.Must(t, err)
assert.True(t, found)
assert.EqualValues(t, 15, u.ID)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment