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.model = &Model{Data: value, driver: s.driver}
var err error
s.TableName, err = s.model.TableName()
s.TableName, err = s.model.tableName()
s.err(err)
}
@ -73,7 +73,7 @@ func (s *Do) exec(sql ...string) {
}
func (s *Do) save() *Do {
if s.model.PrimaryKeyZero() {
if s.model.primaryKeyZero() {
s.create()
} else {
s.update()
@ -82,7 +82,7 @@ func (s *Do) save() *Do {
}
func (s *Do) prepareCreateSql() *Do {
columns, values := s.model.ColumnsAndValues("create")
columns, values := s.model.columnsAndValues("create")
var sqls []string
for _, value := range values {
@ -94,7 +94,7 @@ func (s *Do) prepareCreateSql() *Do {
s.TableName,
strings.Join(s.quoteMap(columns), ","),
strings.Join(sqls, ","),
s.model.ReturningStr(),
s.model.returningStr(),
)
return s
}
@ -117,7 +117,7 @@ func (s *Do) create() *Do {
s.err(err)
}
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("AfterSave"))
@ -127,7 +127,7 @@ func (s *Do) create() *Do {
}
func (s *Do) prepareUpdateSql() *Do {
columns, values := s.model.ColumnsAndValues("update")
columns, values := s.model.columnsAndValues("update")
var sets []string
for index, column := range columns {
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 {
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) {
@ -325,8 +325,8 @@ func (s *Do) whereSql() (sql string) {
var primary_condiation string
var and_conditions, or_conditions []string
if !s.model.PrimaryKeyZero() {
primary_condiation = s.primaryCondiation(s.addToVars(s.model.PrimaryKeyValue()))
if !s.model.primaryKeyZero() {
primary_condiation = s.primaryCondiation(s.addToVars(s.model.primaryKeyValue()))
}
for _, clause := range s.whereClause {
@ -397,7 +397,7 @@ func (s *Do) combinedSql() string {
func (s *Do) createTable() *Do {
var sqls []string
for _, field := range s.model.Fields("null") {
for _, field := range s.model.fields("null") {
sqls = append(sqls, field.DbName+" "+field.SqlType)
}

View File

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