Skip to content

Commit

Permalink
jmoiron#44 - support uint primary key types
Browse files Browse the repository at this point in the history
  • Loading branch information
coopernurse committed May 26, 2013
1 parent 4058e6e commit 22e93ed
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
10 changes: 5 additions & 5 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (d SqliteDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool)
switch val.Kind() {
case reflect.Bool:
return "integer"
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return "integer"
case reflect.Float64, reflect.Float32:
return "real"
Expand Down Expand Up @@ -133,12 +133,12 @@ func (d PostgresDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr boo
switch val.Kind() {
case reflect.Bool:
return "boolean"
case reflect.Int, reflect.Int16, reflect.Int32:
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Uint16, reflect.Uint32:
if isAutoIncr {
return "serial"
}
return "integer"
case reflect.Int64:
case reflect.Int64, reflect.Uint64:
if isAutoIncr {
return "bigserial"
}
Expand Down Expand Up @@ -229,9 +229,9 @@ func (m MySQLDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool)
switch val.Kind() {
case reflect.Bool:
return "boolean"
case reflect.Int, reflect.Int16, reflect.Int32:
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Uint16, reflect.Uint32:
return "int"
case reflect.Int64:
case reflect.Int64, reflect.Uint64:
return "bigint"
case reflect.Float64, reflect.Float32:
return "double"
Expand Down
2 changes: 2 additions & 0 deletions gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,8 @@ func insert(m *DbMap, exec SqlExecutor, list ...interface{}) error {
k := f.Kind()
if (k == reflect.Int) || (k == reflect.Int16) || (k == reflect.Int32) || (k == reflect.Int64) {
f.SetInt(id)
} else if (k == reflect.Uint16) || (k == reflect.Uint32) || (k == reflect.Uint64) {
f.SetUint(uint64(id))
} else {
return errors.New(fmt.Sprintf("gorp: Cannot set autoincrement value on non-Int field. SQL=%s autoIncrIdx=%d", bi.query, bi.autoIncrIdx))
}
Expand Down
36 changes: 36 additions & 0 deletions gorp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ type TypeConversionExample struct {
Name CustomStringType
}

type PersonUInt32 struct {
Id uint32
Name string
}

type PersonUInt64 struct {
Id uint64
Name string
}

type PersonUInt16 struct {
Id uint16
Name string
}

type testTypeConverter struct{}

func (me testTypeConverter) ToDb(val interface{}) (interface{}, error) {
Expand Down Expand Up @@ -173,6 +188,27 @@ func TestCreateTablesIfNotExists(t *testing.T) {
}
}

func TestUIntPrimaryKey(t *testing.T) {
dbmap := newDbMap()
dbmap.TraceOn("", log.New(os.Stdout, "gorptest: ", log.Lmicroseconds))
dbmap.AddTable(PersonUInt64{}).SetKeys(true, "Id")
dbmap.AddTable(PersonUInt32{}).SetKeys(true, "Id")
dbmap.AddTable(PersonUInt16{}).SetKeys(true, "Id")
err := dbmap.CreateTablesIfNotExists()
if err != nil {
panic(err)
}
defer dbmap.DropTables()

p1 := &PersonUInt64{0, "name1"}
p2 := &PersonUInt32{0, "name2"}
p3 := &PersonUInt16{0, "name3"}
err = dbmap.Insert(p1, p2, p3)
if err != nil {
t.Error(err)
}
}

func TestPersistentUser(t *testing.T) {
dbmap := newDbMap()
dbmap.Exec("drop table if exists PersistentUser")
Expand Down

0 comments on commit 22e93ed

Please sign in to comment.