Change internal API

This commit is contained in:
Jinzhu 2013-10-27 07:52:04 +08:00
parent 9d2f7650be
commit 284cf3f649
2 changed files with 16 additions and 16 deletions

14
orm.go
View File

@ -14,7 +14,7 @@ type Orm struct {
Error error Error error
Sql string Sql string
SqlVars []interface{} SqlVars []interface{}
Model *Model model *Model
db *sql.DB db *sql.DB
driver string driver string
@ -26,10 +26,10 @@ type Orm struct {
operation string operation string
} }
func (s *Orm) setModel(model interface{}) (err error) { func (s *Orm) Model(model interface{}) (err error) {
s.Model = s.toModel(model) s.model = s.toModel(model)
s.TableName = s.Model.TableName() s.TableName = s.model.TableName()
s.PrimaryKey = s.Model.PrimaryKeyDb() s.PrimaryKey = s.model.PrimaryKeyDb()
return return
} }
@ -86,8 +86,8 @@ func (s *Orm) Select(value interface{}) *Orm {
} }
func (s *Orm) Save(value interface{}) *Orm { func (s *Orm) Save(value interface{}) *Orm {
s.setModel(value) s.Model(value)
if s.Model.PrimaryKeyIsEmpty() { if s.model.PrimaryKeyIsEmpty() {
s.explain(value, "Create").create(value) s.explain(value, "Create").create(value)
} else { } else {
s.explain(value, "Update").update(value) s.explain(value, "Update").update(value)

18
sql.go
View File

@ -8,7 +8,7 @@ import (
) )
func (s *Orm) explain(value interface{}, operation string) *Orm { func (s *Orm) explain(value interface{}, operation string) *Orm {
s.setModel(value) s.Model(value)
switch operation { switch operation {
case "Create": case "Create":
s.createSql(value) s.createSql(value)
@ -19,7 +19,7 @@ func (s *Orm) explain(value interface{}, operation string) *Orm {
case "Query": case "Query":
s.querySql(value) s.querySql(value)
case "CreateTable": case "CreateTable":
s.Sql = s.Model.CreateTable() s.Sql = s.model.CreateTable()
} }
return s return s
} }
@ -76,7 +76,7 @@ func (s *Orm) query(out interface{}) {
} }
func (s *Orm) createSql(value interface{}) { func (s *Orm) createSql(value interface{}) {
columns, values := s.Model.ColumnsAndValues() columns, values := s.model.ColumnsAndValues()
var sqls []string var sqls []string
for _, value := range values { for _, value := range values {
@ -88,7 +88,7 @@ func (s *Orm) createSql(value interface{}) {
s.TableName, s.TableName,
strings.Join(s.quoteMap(columns), ","), strings.Join(s.quoteMap(columns), ","),
strings.Join(sqls, ","), strings.Join(sqls, ","),
s.Model.ReturningStr(), s.model.ReturningStr(),
) )
return return
} }
@ -102,12 +102,12 @@ func (s *Orm) create(value interface{}) {
id, s.Error = s.SqlResult.LastInsertId() id, s.Error = s.SqlResult.LastInsertId()
} }
result := reflect.ValueOf(s.Model.Data).Elem() result := reflect.ValueOf(s.model.Data).Elem()
result.FieldByName(s.Model.PrimaryKey()).SetInt(id) result.FieldByName(s.model.PrimaryKey()).SetInt(id)
} }
func (s *Orm) updateSql(value interface{}) { func (s *Orm) updateSql(value interface{}) {
columns, values := s.Model.ColumnsAndValues() columns, values := s.model.ColumnsAndValues()
var sets []string var sets []string
for index, column := range columns { for index, column := range columns {
sets = append(sets, fmt.Sprintf("%v = %v", s.quote(column), s.addToVars(values[index]))) 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) { func (s *Orm) whereSql() (sql string) {
var conditions []string var conditions []string
if !s.Model.PrimaryKeyIsEmpty() { if !s.model.PrimaryKeyIsEmpty() {
conditions = append(conditions, fmt.Sprintf("(%v = %v)", s.quote(s.Model.PrimaryKeyDb()), s.addToVars(s.Model.PrimaryKeyValue()))) conditions = append(conditions, fmt.Sprintf("(%v = %v)", s.quote(s.model.PrimaryKeyDb()), s.addToVars(s.model.PrimaryKeyValue())))
} }
if len(s.whereClause) > 0 { if len(s.whereClause) > 0 {