Remove unnecessary exported fields

This commit is contained in:
Jinzhu 2013-10-28 15:55:41 +08:00
parent f4aa435446
commit 3bd7daf889
2 changed files with 26 additions and 26 deletions

20
do.go
View File

@ -49,7 +49,7 @@ func (s *Do) setModel(value interface{}) {
s.value = value s.value = value
s.model = &Model{Data: value, driver: s.driver} s.model = &Model{Data: value, driver: s.driver}
var err error var err error
s.TableName, err = s.model.TableName() s.TableName, err = s.model.tableName()
s.err(err) s.err(err)
} }
@ -73,7 +73,7 @@ func (s *Do) exec(sql ...string) {
} }
func (s *Do) save() *Do { func (s *Do) save() *Do {
if s.model.PrimaryKeyZero() { if s.model.primaryKeyZero() {
s.create() s.create()
} else { } else {
s.update() s.update()
@ -82,7 +82,7 @@ func (s *Do) save() *Do {
} }
func (s *Do) prepareCreateSql() *Do { func (s *Do) prepareCreateSql() *Do {
columns, values := s.model.ColumnsAndValues("create") columns, values := s.model.columnsAndValues("create")
var sqls []string var sqls []string
for _, value := range values { for _, value := range values {
@ -94,7 +94,7 @@ func (s *Do) prepareCreateSql() *Do {
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 s return s
} }
@ -117,7 +117,7 @@ func (s *Do) create() *Do {
s.err(err) s.err(err)
} }
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)
s.err(s.model.callMethod("AfterCreate")) s.err(s.model.callMethod("AfterCreate"))
s.err(s.model.callMethod("AfterSave")) s.err(s.model.callMethod("AfterSave"))
@ -127,7 +127,7 @@ func (s *Do) create() *Do {
} }
func (s *Do) prepareUpdateSql() *Do { func (s *Do) prepareUpdateSql() *Do {
columns, values := s.model.ColumnsAndValues("update") columns, values := s.model.columnsAndValues("update")
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])))
@ -281,7 +281,7 @@ func (s *Do) where(querystring interface{}, args ...interface{}) *Do {
} }
func (s *Do) primaryCondiation(value interface{}) string { func (s *Do) primaryCondiation(value interface{}) string {
return fmt.Sprintf("(%v = %v)", s.quote(s.model.PrimaryKeyDb()), value) return fmt.Sprintf("(%v = %v)", s.quote(s.model.primaryKeyDb()), value)
} }
func (s *Do) buildWhereCondition(clause map[string]interface{}) (str string) { func (s *Do) buildWhereCondition(clause map[string]interface{}) (str string) {
@ -325,8 +325,8 @@ func (s *Do) whereSql() (sql string) {
var primary_condiation string var primary_condiation string
var and_conditions, or_conditions []string var and_conditions, or_conditions []string
if !s.model.PrimaryKeyZero() { if !s.model.primaryKeyZero() {
primary_condiation = s.primaryCondiation(s.addToVars(s.model.PrimaryKeyValue())) primary_condiation = s.primaryCondiation(s.addToVars(s.model.primaryKeyValue()))
} }
for _, clause := range s.whereClause { for _, clause := range s.whereClause {
@ -397,7 +397,7 @@ func (s *Do) combinedSql() string {
func (s *Do) createTable() *Do { func (s *Do) createTable() *Do {
var sqls []string var sqls []string
for _, field := range s.model.Fields("null") { for _, field := range s.model.fields("null") {
sqls = append(sqls, field.DbName+" "+field.SqlType) sqls = append(sqls, field.DbName+" "+field.SqlType)
} }

View File

@ -24,35 +24,35 @@ type Field struct {
IsPrimaryKey bool IsPrimaryKey bool
} }
func (m *Model) PrimaryKeyZero() bool { func (m *Model) primaryKeyZero() bool {
return m.PrimaryKeyValue() == 0 return m.primaryKeyValue() == 0
} }
func (m *Model) PrimaryKeyValue() int64 { func (m *Model) primaryKeyValue() int64 {
t := reflect.TypeOf(m.Data).Elem() t := reflect.TypeOf(m.Data).Elem()
switch t.Kind() { switch t.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice: case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice:
return 0 return 0
default: default:
result := reflect.ValueOf(m.Data).Elem() result := reflect.ValueOf(m.Data).Elem()
value := result.FieldByName(m.PrimaryKey()) value := result.FieldByName(m.primaryKey())
if value.IsValid() { if value.IsValid() {
return result.FieldByName(m.PrimaryKey()).Interface().(int64) return result.FieldByName(m.primaryKey()).Interface().(int64)
} else { } else {
return 0 return 0
} }
} }
} }
func (m *Model) PrimaryKey() string { func (m *Model) primaryKey() string {
return "Id" return "Id"
} }
func (m *Model) PrimaryKeyDb() string { func (m *Model) primaryKeyDb() string {
return toSnake(m.PrimaryKey()) return toSnake(m.primaryKey())
} }
func (m *Model) Fields(operation string) (fields []Field) { func (m *Model) fields(operation string) (fields []Field) {
typ := reflect.TypeOf(m.Data).Elem() typ := reflect.TypeOf(m.Data).Elem()
for i := 0; i < typ.NumField(); i++ { for i := 0; i < typ.NumField(); i++ {
@ -61,7 +61,7 @@ func (m *Model) Fields(operation string) (fields []Field) {
var field Field var field Field
field.Name = p.Name field.Name = p.Name
field.DbName = toSnake(p.Name) field.DbName = toSnake(p.Name)
field.IsPrimaryKey = m.PrimaryKeyDb() == field.DbName field.IsPrimaryKey = m.primaryKeyDb() == field.DbName
field.AutoCreateTime = "created_at" == field.DbName field.AutoCreateTime = "created_at" == field.DbName
field.AutoUpdateTime = "updated_at" == field.DbName field.AutoUpdateTime = "updated_at" == field.DbName
value := reflect.ValueOf(m.Data).Elem().FieldByName(p.Name) value := reflect.ValueOf(m.Data).Elem().FieldByName(p.Name)
@ -92,8 +92,8 @@ func (m *Model) Fields(operation string) (fields []Field) {
return return
} }
func (m *Model) ColumnsAndValues(operation string) (columns []string, values []interface{}) { func (m *Model) columnsAndValues(operation string) (columns []string, values []interface{}) {
for _, field := range m.Fields(operation) { for _, field := range m.fields(operation) {
if !field.IsPrimaryKey { if !field.IsPrimaryKey {
columns = append(columns, field.DbName) columns = append(columns, field.DbName)
values = append(values, field.Value) values = append(values, field.Value)
@ -102,7 +102,7 @@ func (m *Model) ColumnsAndValues(operation string) (columns []string, values []i
return return
} }
func (m *Model) TableName() (str string, err error) { func (m *Model) tableName() (str string, err error) {
if m.Data == nil { if m.Data == nil {
err = errors.New("Model haven't been set") err = errors.New("Model haven't been set")
return return
@ -146,13 +146,13 @@ func (m *Model) callMethod(method string) error {
return nil return nil
} }
func (model *Model) MissingColumns() (results []string) { func (model *Model) missingColumns() (results []string) {
return return
} }
func (model *Model) ReturningStr() (str string) { func (model *Model) returningStr() (str string) {
if model.driver == "postgres" { if model.driver == "postgres" {
str = fmt.Sprintf("RETURNING \"%v\"", model.PrimaryKeyDb()) str = fmt.Sprintf("RETURNING \"%v\"", model.primaryKeyDb())
} }
return return
} }