diff --git a/README.md b/README.md index acb0e9f3..fea460fe 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,9 @@ Yet Another ORM library for Go, aims for developer friendly ## TODO -* Soft Delete * Better First method (First(&user, primary_key, where conditions)) +* Update, Updates +* Soft Delete * Even more complex where query (with map or struct) * FindOrInitialize / FindOrCreate * SQL Log diff --git a/orm.go b/orm.go index df6ef77f..da7fe785 100644 --- a/orm.go +++ b/orm.go @@ -36,6 +36,12 @@ func (s *Orm) err(err error) { } } +func (s *Orm) Copy() *Orm { + c := *s + c.SqlVars = c.SqlVars[:0] + return &c +} + func (s *Orm) Model(model interface{}) *Orm { s.model = s.toModel(model) s.TableName = s.model.TableName() @@ -114,12 +120,12 @@ func (s *Orm) Save(value interface{}) *Orm { } else { s.update(value) } - return s + return s.Copy() } func (s *Orm) Delete(value interface{}) *Orm { s.explain(value, "Delete").delete(value) - return s + return s.Copy() } func (s *Orm) Update(column string, value string) *Orm { @@ -138,22 +144,22 @@ func (s *Orm) Exec(sql ...string) *Orm { s.SqlResult, err = s.db.Exec(sql[0]) } s.err(err) - return s + return s.Copy() } func (s *Orm) First(out interface{}) *Orm { s.explain(out, "Query").query(out) - return s + return s.Copy() } func (s *Orm) Find(out interface{}) *Orm { s.explain(out, "Query").query(out) - return s + return s.Copy() } func (s *Orm) Pluck(column string, value interface{}) (orm *Orm) { s.Select(column).explain(s.model.Data, "Query").pluck(value) - return s + return s.Copy() } func (s *Orm) Or(querystring interface{}, args ...interface{}) *Orm { diff --git a/orm_test.go b/orm_test.go index dcde82a7..8d5e4e3b 100644 --- a/orm_test.go +++ b/orm_test.go @@ -61,6 +61,11 @@ func init() { db.Save(&User{Name: "5", Age: 26, Birthday: t4}) } +func TestFirst(t *testing.T) { + var u1, u2 User + db.Where("name = ?", "3").Order("age desc").First(&u1).First(&u2) +} + func TestSaveAndFind(t *testing.T) { name := "save_and_find" u := &User{Name: name, Age: 1}