Skip to content

Commit

Permalink
Add support for alias column
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengjia authored and James Cooper committed May 14, 2014
1 parent eeb38f7 commit b5ce3b9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
13 changes: 7 additions & 6 deletions gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1572,14 +1572,16 @@ func columnToFieldIndex(m *DbMap, t reflect.Type, cols []string) ([][]int, error
// a field in the i struct
for x := range cols {
colName := strings.ToLower(cols[x])

field, found := t.FieldByNameFunc(func(fieldName string) bool {
var mappedFieldName string
field, _ := t.FieldByName(fieldName)
fieldName = field.Tag.Get("db")

if fieldName == "-" {
lowerFieldName := strings.ToLower(field.Name)
mappedFieldName = field.Tag.Get("db")
if mappedFieldName == "-" && colName != lowerFieldName {
return false
} else if fieldName == "" {
} else if mappedFieldName == "-" && colName == lowerFieldName {
return true
} else if mappedFieldName == "" {
fieldName = field.Name
}
if tableMapped {
Expand All @@ -1588,7 +1590,6 @@ func columnToFieldIndex(m *DbMap, t reflect.Type, cols []string) ([][]int, error
fieldName = colMap.ColumnName
}
}

return colName == strings.ToLower(fieldName)
})
if found {
Expand Down
41 changes: 41 additions & 0 deletions gorp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ type WithIgnoredColumn struct {
Created int64
}

type IgnoredColumnExported struct {
Id int64
External int64 `db:"-"`
Created int64
}

type WithStringPk struct {
Id string
Name string
Expand Down Expand Up @@ -1427,6 +1433,40 @@ func TestSelectSingleVal(t *testing.T) {
}
}

func TestSelectAlias(t *testing.T) {
dbmap := initDbMap()
defer dropAndClose(dbmap)

p1 := &IgnoredColumnExported{Id: 1, External: 2, Created: 3}
_insert(dbmap, p1)

var p2 IgnoredColumnExported

err := dbmap.SelectOne(&p2, "select * from ignored_column_exported_test where Id=1")
if err != nil {
t.Error(err)
}
if p2.Id != 1 || p2.Created != 3 || p2.External != 0 {
t.Error("Expected ignorred field defaults to not set")
}

err = dbmap.SelectOne(&p2, "SELECT *, 1 AS external FROM ignored_column_exported_test")
if err != nil {
t.Error(err)
}
if p2.External != 1 {
t.Error("Expected select as can map to exported field.")
}

var rows *sql.Rows
var cols []string
rows, err = dbmap.Db.Query("SELECT * FROM ignored_column_exported_test")
cols, err = rows.Columns()
if err != nil || len(cols) != 2 {
t.Error("Expected ignored column not created")
}
}

func TestMysqlPanicIfDialectNotInitialized(t *testing.T) {
_, driver := dialectAndDriver()
// this test only applies to MySQL
Expand Down Expand Up @@ -1561,6 +1601,7 @@ func initDbMap() *DbMap {
dbmap.AddTableWithName(OverriddenInvoice{}, "invoice_override_test").SetKeys(false, "Id")
dbmap.AddTableWithName(Person{}, "person_test").SetKeys(true, "Id")
dbmap.AddTableWithName(WithIgnoredColumn{}, "ignored_column_test").SetKeys(true, "Id")
dbmap.AddTableWithName(IgnoredColumnExported{}, "ignored_column_exported_test").SetKeys(true, "Id")
dbmap.AddTableWithName(TypeConversionExample{}, "type_conv_test").SetKeys(true, "Id")
dbmap.AddTableWithName(WithEmbeddedStruct{}, "embedded_struct_test").SetKeys(true, "Id")
dbmap.AddTableWithName(WithEmbeddedStructBeforeAutoincrField{}, "embedded_struct_before_autoincr_test").SetKeys(true, "Id")
Expand Down

0 comments on commit b5ce3b9

Please sign in to comment.