From 22e93ed7a40c5cc731a8344010abb96722ee9e04 Mon Sep 17 00:00:00 2001 From: James Cooper Date: Sun, 26 May 2013 09:33:40 -0700 Subject: [PATCH] #44 - support uint primary key types --- dialect.go | 10 +++++----- gorp.go | 2 ++ gorp_test.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/dialect.go b/dialect.go index d14dfaa..022bf63 100644 --- a/dialect.go +++ b/dialect.go @@ -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" @@ -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" } @@ -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" diff --git a/gorp.go b/gorp.go index a766428..7371ee7 100644 --- a/gorp.go +++ b/gorp.go @@ -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)) } diff --git a/gorp_test.go b/gorp_test.go index eb74f33..0a21ebb 100644 --- a/gorp_test.go +++ b/gorp_test.go @@ -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) { @@ -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")