From 4bcb0fbd6ad8532f27aa57f7aeaa97e3b1d2559d Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 21 Apr 2014 15:13:32 +0800 Subject: [PATCH 1/4] rename ScanStruct to ScanStructByIndex and ScanStruct2 to ScanStructByName --- db.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db.go b/db.go index ed97987a..92691d0e 100644 --- a/db.go +++ b/db.go @@ -252,7 +252,7 @@ type Rows struct { } // scan data to a struct's pointer according field index -func (rs *Rows) ScanStruct(dest ...interface{}) error { +func (rs *Rows) ScanStructByIndex(dest ...interface{}) error { if len(dest) == 0 { return errors.New("at least one struct") } @@ -319,7 +319,7 @@ func fieldByName(v reflect.Value, name string) reflect.Value { } // scan data to a struct's pointer according field name -func (rs *Rows) ScanStruct2(dest interface{}) error { +func (rs *Rows) ScanStructByName(dest interface{}) error { vv := reflect.ValueOf(dest) if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct { return errors.New("dest should be a struct's pointer") From e649ef538f981cc7240c73ba89862e956f00a64a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 4 May 2014 13:52:56 +0800 Subject: [PATCH 2/4] add FormatBytes for dialect --- dialect.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dialect.go b/dialect.go index f17cf784..3291d069 100644 --- a/dialect.go +++ b/dialect.go @@ -29,6 +29,7 @@ type Dialect interface { DB() *DB DBType() DbType SqlType(*Column) string + FormatBytes(b []byte) string QuoteStr() string AndStr() string @@ -90,6 +91,10 @@ func (b *Base) DBType() DbType { return b.Uri.DbType } +func (b *Base) FormatBytes(bs []byte) string { + return fmt.Sprintf("0x%x", bs) +} + func (b *Base) DriverName() string { return b.driverName } From 724a99021f942c570f74d4cc365d5c2d46762d90 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 5 May 2014 17:55:25 +0800 Subject: [PATCH 3/4] add sql type bool methods --- type.go | 99 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 39 deletions(-) diff --git a/type.go b/type.go index 68e2a23c..4f0b4d48 100644 --- a/type.go +++ b/type.go @@ -22,15 +22,35 @@ type SQLType struct { DefaultLength2 int } +const ( + UNKNOW_TYPE = iota + TEXT_TYPE + BLOB_TYPE + TIME_TYPE + NUMERIC_TYPE +) + +func (s *SQLType) IsType(st int) bool { + if t, ok := SqlTypes[s.Name]; ok && t == st { + return true + } + return false +} + func (s *SQLType) IsText() bool { - return s.Name == Char || s.Name == Varchar || s.Name == TinyText || - s.Name == Text || s.Name == MediumText || s.Name == LongText + return s.IsType(TEXT_TYPE) } func (s *SQLType) IsBlob() bool { - return (s.Name == TinyBlob) || (s.Name == Blob) || - s.Name == MediumBlob || s.Name == LongBlob || - s.Name == Binary || s.Name == VarBinary || s.Name == Bytea + return s.IsType(BLOB_TYPE) +} + +func (s *SQLType) IsTime() bool { + return s.IsType(TIME_TYPE) +} + +func (s *SQLType) IsNumeric() bool { + return s.IsType(NUMERIC_TYPE) } var ( @@ -75,46 +95,47 @@ var ( Serial = "SERIAL" BigSerial = "BIGSERIAL" - SqlTypes = map[string]bool{ - Bit: true, - TinyInt: true, - SmallInt: true, - MediumInt: true, - Int: true, - Integer: true, - BigInt: true, + SqlTypes = map[string]int{ + Bit: NUMERIC_TYPE, + TinyInt: NUMERIC_TYPE, + SmallInt: NUMERIC_TYPE, + MediumInt: NUMERIC_TYPE, + Int: NUMERIC_TYPE, + Integer: NUMERIC_TYPE, + BigInt: NUMERIC_TYPE, - Char: true, - Varchar: true, - TinyText: true, - Text: true, - MediumText: true, - LongText: true, + Char: TEXT_TYPE, + Varchar: TEXT_TYPE, + TinyText: TEXT_TYPE, + Text: TEXT_TYPE, + MediumText: TEXT_TYPE, + LongText: TEXT_TYPE, - Date: true, - DateTime: true, - Time: true, - TimeStamp: true, - TimeStampz: true, + Date: TIME_TYPE, + DateTime: TIME_TYPE, + Time: TIME_TYPE, + TimeStamp: TIME_TYPE, + TimeStampz: TIME_TYPE, - Decimal: true, - Numeric: true, + Decimal: NUMERIC_TYPE, + Numeric: NUMERIC_TYPE, + Real: NUMERIC_TYPE, + Float: NUMERIC_TYPE, + Double: NUMERIC_TYPE, - Binary: true, - VarBinary: true, - Real: true, - Float: true, - Double: true, - TinyBlob: true, - Blob: true, - MediumBlob: true, - LongBlob: true, - Bytea: true, + Binary: BLOB_TYPE, + VarBinary: BLOB_TYPE, - Bool: true, + TinyBlob: BLOB_TYPE, + Blob: BLOB_TYPE, + MediumBlob: BLOB_TYPE, + LongBlob: BLOB_TYPE, + Bytea: BLOB_TYPE, - Serial: true, - BigSerial: true, + Bool: NUMERIC_TYPE, + + Serial: NUMERIC_TYPE, + BigSerial: NUMERIC_TYPE, } intTypes = sort.StringSlice{"*int", "*int16", "*int32", "*int8"} From c42f893230cd34310e11cbb40617cadd8929f0f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=95=86=E8=AE=AF=E5=9C=A8=E7=BA=BF?= Date: Mon, 5 May 2014 22:33:28 +0800 Subject: [PATCH 4/4] support enum type for mysql MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 商讯在线 --- column.go | 23 +++++++++++++++++++++-- type.go | 4 +++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/column.go b/column.go index 20b3ad01..b4aa5cfd 100644 --- a/column.go +++ b/column.go @@ -31,11 +31,30 @@ type Column struct { IsVersion bool fieldPath []string DefaultIsEmpty bool + EnumOptions map[string]int } func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column { - return &Column{name, fieldName, sqlType, len1, len2, nullable, "", make(map[string]bool), false, false, - TWOSIDES, false, false, false, false, nil, false} + return &Column{ + Name: name, + FieldName: fieldName, + SQLType: sqlType, + Length: len1, + Length2: len2, + Nullable: nullable, + Default: "", + Indexes: make(map[string]bool), + IsPrimaryKey: false, + IsAutoIncrement: false, + MapType: TWOSIDES, + IsCreated: false, + IsUpdated: false, + IsCascade: false, + IsVersion: false, + fieldPath: nil, + DefaultIsEmpty: false, + EnumOptions: make(map[string]int), + } } // generate column description string according dialect diff --git a/type.go b/type.go index 4f0b4d48..c1e54efe 100644 --- a/type.go +++ b/type.go @@ -62,6 +62,7 @@ var ( Integer = "INTEGER" BigInt = "BIGINT" + Enum = "ENUM" Char = "CHAR" Varchar = "VARCHAR" TinyText = "TINYTEXT" @@ -104,6 +105,7 @@ var ( Integer: NUMERIC_TYPE, BigInt: NUMERIC_TYPE, + Enum: TEXT_TYPE, Char: TEXT_TYPE, Varchar: TEXT_TYPE, TinyText: TEXT_TYPE, @@ -293,7 +295,7 @@ func SQLType2Type(st SQLType) reflect.Type { return reflect.TypeOf(float32(1)) case Double: return reflect.TypeOf(float64(1)) - case Char, Varchar, TinyText, Text, MediumText, LongText: + case Char, Varchar, TinyText, Text, MediumText, LongText, Enum: return reflect.TypeOf("") case TinyBlob, Blob, LongBlob, Bytea, Binary, MediumBlob, VarBinary: return reflect.TypeOf([]byte{})