From e0d310029489f90ce52b74fc27ab30de46314154 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 16 Nov 2013 14:15:33 +0800 Subject: [PATCH] Should copy DB when save, update, delete --- main.go | 13 +++++++------ private.go | 7 ++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 7978eb4b..95ce8f4f 100644 --- a/main.go +++ b/main.go @@ -42,7 +42,7 @@ func (s *DB) SetLogger(l Logger) { } func (s *DB) LogMode(b bool) { - s.parent.logMode = b + s.logMode = b } func (s *DB) SingularTable(b bool) { @@ -132,17 +132,17 @@ func (s *DB) Update(attrs ...interface{}) *DB { } func (s *DB) Updates(values interface{}, ignore_protected_attrs ...bool) *DB { - s.do(s.data).begin().updateAttrs(values, ignore_protected_attrs...).update().commit_or_rollback() + s.clone().do(s.data).begin().updateAttrs(values, ignore_protected_attrs...).update().commit_or_rollback() return s } func (s *DB) Save(value interface{}) *DB { - s.do(value).begin().save().commit_or_rollback() + s.clone().do(value).begin().save().commit_or_rollback() return s } func (s *DB) Delete(value interface{}) *DB { - s.do(value).begin().delete().commit_or_rollback() + s.clone().do(value).begin().delete().commit_or_rollback() return s } @@ -178,8 +178,9 @@ func (s *DB) Table(name string) *DB { // Debug func (s *DB) Debug() *DB { - s.logMode = true - return s + c := s.clone() + c.logMode = true + return c } // Transactions diff --git a/private.go b/private.go index b46c8ab8..c76b544c 100644 --- a/private.go +++ b/private.go @@ -6,14 +6,15 @@ import ( ) func (s *DB) clone() *DB { - db := &DB{db: s.db, parent: s.parent} + db := DB{db: s.db, parent: s.parent, logMode: s.logMode, data: s.data, Error: s.Error} + if s.parent.search == nil { db.search = &search{} } else { db.search = s.parent.search.clone() } - db.search.db = db - return db + db.search.db = &db + return &db } func (s *DB) do(data interface{}) *Do {