From 284cf3f649378fc019e31284f42b794b0299f6fb Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 27 Oct 2013 07:52:04 +0800 Subject: [PATCH] Change internal API --- orm.go | 14 +++++++------- sql.go | 18 +++++++++--------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/orm.go b/orm.go index 8a62da4f..4007dd6e 100644 --- a/orm.go +++ b/orm.go @@ -14,7 +14,7 @@ type Orm struct { Error error Sql string SqlVars []interface{} - Model *Model + model *Model db *sql.DB driver string @@ -26,10 +26,10 @@ type Orm struct { operation string } -func (s *Orm) setModel(model interface{}) (err error) { - s.Model = s.toModel(model) - s.TableName = s.Model.TableName() - s.PrimaryKey = s.Model.PrimaryKeyDb() +func (s *Orm) Model(model interface{}) (err error) { + s.model = s.toModel(model) + s.TableName = s.model.TableName() + s.PrimaryKey = s.model.PrimaryKeyDb() return } @@ -86,8 +86,8 @@ func (s *Orm) Select(value interface{}) *Orm { } func (s *Orm) Save(value interface{}) *Orm { - s.setModel(value) - if s.Model.PrimaryKeyIsEmpty() { + s.Model(value) + if s.model.PrimaryKeyIsEmpty() { s.explain(value, "Create").create(value) } else { s.explain(value, "Update").update(value) diff --git a/sql.go b/sql.go index 60d6a642..bd8a7dea 100644 --- a/sql.go +++ b/sql.go @@ -8,7 +8,7 @@ import ( ) func (s *Orm) explain(value interface{}, operation string) *Orm { - s.setModel(value) + s.Model(value) switch operation { case "Create": s.createSql(value) @@ -19,7 +19,7 @@ func (s *Orm) explain(value interface{}, operation string) *Orm { case "Query": s.querySql(value) case "CreateTable": - s.Sql = s.Model.CreateTable() + s.Sql = s.model.CreateTable() } return s } @@ -76,7 +76,7 @@ func (s *Orm) query(out interface{}) { } func (s *Orm) createSql(value interface{}) { - columns, values := s.Model.ColumnsAndValues() + columns, values := s.model.ColumnsAndValues() var sqls []string for _, value := range values { @@ -88,7 +88,7 @@ func (s *Orm) createSql(value interface{}) { s.TableName, strings.Join(s.quoteMap(columns), ","), strings.Join(sqls, ","), - s.Model.ReturningStr(), + s.model.ReturningStr(), ) return } @@ -102,12 +102,12 @@ func (s *Orm) create(value interface{}) { id, s.Error = s.SqlResult.LastInsertId() } - result := reflect.ValueOf(s.Model.Data).Elem() - result.FieldByName(s.Model.PrimaryKey()).SetInt(id) + result := reflect.ValueOf(s.model.Data).Elem() + result.FieldByName(s.model.PrimaryKey()).SetInt(id) } func (s *Orm) updateSql(value interface{}) { - columns, values := s.Model.ColumnsAndValues() + columns, values := s.model.ColumnsAndValues() var sets []string for index, column := range columns { sets = append(sets, fmt.Sprintf("%v = %v", s.quote(column), s.addToVars(values[index]))) @@ -135,8 +135,8 @@ func (s *Orm) deleteSql(value interface{}) { func (s *Orm) whereSql() (sql string) { var conditions []string - if !s.Model.PrimaryKeyIsEmpty() { - conditions = append(conditions, fmt.Sprintf("(%v = %v)", s.quote(s.Model.PrimaryKeyDb()), s.addToVars(s.Model.PrimaryKeyValue()))) + if !s.model.PrimaryKeyIsEmpty() { + conditions = append(conditions, fmt.Sprintf("(%v = %v)", s.quote(s.model.PrimaryKeyDb()), s.addToVars(s.model.PrimaryKeyValue()))) } if len(s.whereClause) > 0 {