From 95b7263ec717cfdb09ed7a731ad9a585aa5d7b34 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 26 Oct 2013 16:01:15 +0800 Subject: [PATCH] Add SQL Types --- sql_type.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 sql_type.go diff --git a/sql_type.go b/sql_type.go new file mode 100644 index 00000000..6069967d --- /dev/null +++ b/sql_type.go @@ -0,0 +1,61 @@ +package gorm + +import ( + "fmt" + "time" +) + +func getSqlType(adaptor string, column interface{}, size int) string { + switch adaptor { + case "mysql": + switch column.(type) { + case time.Time: + return "timestamp" + case bool: + return "boolean" + case int, int8, int16, int32, uint, uint8, uint16, uint32: + return "int" + case int64, uint64: + return "bigint" + case float32, float64: + return "double" + case []byte: + if size > 0 && size < 65532 { + return fmt.Sprintf("varbinary(%d)", size) + } + return "longblob" + case string: + if size > 0 && size < 65532 { + return fmt.Sprintf("varchar(%d)", size) + } + return "longtext" + default: + panic("invalid sql type") + } + + case "postgres": + switch column.(type) { + case time.Time: + return "timestamp with time zone" + case bool: + return "boolean" + case int, int8, int16, int32, uint, uint8, uint16, uint32: + return "integer" + case int64, uint64: + return "bigint" + case float32, float64: + return "double precision" + case []byte: + return "bytea" + case string: + if size > 0 && size < 65532 { + return fmt.Sprintf("varchar(%d)", size) + } + return "text" + default: + panic("invalid sql type") + } + } + + panic("unsupported sql adaptor, please submit an issue in github") +}