From b30969704a95063addd40f3e0ef271323c238a6a Mon Sep 17 00:00:00 2001 From: Back Yu Date: Thu, 30 Nov 2017 18:12:49 +0800 Subject: [PATCH 1/2] Proposal to #2952 --- orm/cmd_utils.go | 8 +++++--- orm/db.go | 6 +++--- orm/db_mysql.go | 1 + orm/db_oracle.go | 1 + orm/db_postgres.go | 1 + orm/db_sqlite.go | 1 + orm/models_fields.go | 3 ++- orm/models_info_f.go | 6 ++++-- orm/models_test.go | 2 +- orm/models_utils.go | 6 +++--- 10 files changed, 22 insertions(+), 13 deletions(-) diff --git a/orm/cmd_utils.go b/orm/cmd_utils.go index de47cb02..ba7afb53 100644 --- a/orm/cmd_utils.go +++ b/orm/cmd_utils.go @@ -51,12 +51,14 @@ checkColumn: switch fieldType { case TypeBooleanField: col = T["bool"] - case TypeCharField: + case TypeVarCharField: if al.Driver == DRPostgres && fi.toText { col = T["string-text"] } else { col = fmt.Sprintf(T["string"], fieldSize) } + case TypeCharField: + col = fmt.Sprintf(T["string-char"], fieldSize) case TypeTextField: col = T["string-text"] case TypeTimeField: @@ -96,13 +98,13 @@ checkColumn: } case TypeJSONField: if al.Driver != DRPostgres { - fieldType = TypeCharField + fieldType = TypeVarCharField goto checkColumn } col = T["json"] case TypeJsonbField: if al.Driver != DRPostgres { - fieldType = TypeCharField + fieldType = TypeVarCharField goto checkColumn } col = T["jsonb"] diff --git a/orm/db.go b/orm/db.go index 12f0f54d..5862d003 100644 --- a/orm/db.go +++ b/orm/db.go @@ -142,7 +142,7 @@ func (d *dbBase) collectFieldValue(mi *modelInfo, fi *fieldInfo, ind reflect.Val } else { value = field.Bool() } - case TypeCharField, TypeTextField, TypeJSONField, TypeJsonbField: + case TypeVarCharField, TypeCharField, TypeTextField, TypeJSONField, TypeJsonbField: if ns, ok := field.Interface().(sql.NullString); ok { value = nil if ns.Valid { @@ -1240,7 +1240,7 @@ setValue: } value = b } - case fieldType == TypeCharField || fieldType == TypeTextField || fieldType == TypeJSONField || fieldType == TypeJsonbField: + case fieldType == TypeVarCharField || fieldType == TypeCharField || fieldType == TypeTextField || fieldType == TypeJSONField || fieldType == TypeJsonbField: if str == nil { value = ToStr(val) } else { @@ -1386,7 +1386,7 @@ setValue: field.SetBool(value.(bool)) } } - case fieldType == TypeCharField || fieldType == TypeTextField || fieldType == TypeJSONField || fieldType == TypeJsonbField: + case fieldType == TypeVarCharField || fieldType == TypeCharField || fieldType == TypeTextField || fieldType == TypeJSONField || fieldType == TypeJsonbField: if isNative { if ns, ok := field.Interface().(sql.NullString); ok { if value == nil { diff --git a/orm/db_mysql.go b/orm/db_mysql.go index 51185563..6e99058e 100644 --- a/orm/db_mysql.go +++ b/orm/db_mysql.go @@ -46,6 +46,7 @@ var mysqlTypes = map[string]string{ "pk": "NOT NULL PRIMARY KEY", "bool": "bool", "string": "varchar(%d)", + "string-char": "char(%d)", "string-text": "longtext", "time.Time-date": "date", "time.Time": "datetime", diff --git a/orm/db_oracle.go b/orm/db_oracle.go index f5d6aaa2..5d121f83 100644 --- a/orm/db_oracle.go +++ b/orm/db_oracle.go @@ -34,6 +34,7 @@ var oracleTypes = map[string]string{ "pk": "NOT NULL PRIMARY KEY", "bool": "bool", "string": "VARCHAR2(%d)", + "string-char": "CHAR(%d)", "string-text": "VARCHAR2(%d)", "time.Time-date": "DATE", "time.Time": "TIMESTAMP", diff --git a/orm/db_postgres.go b/orm/db_postgres.go index e972c4a2..c488fb38 100644 --- a/orm/db_postgres.go +++ b/orm/db_postgres.go @@ -43,6 +43,7 @@ var postgresTypes = map[string]string{ "pk": "NOT NULL PRIMARY KEY", "bool": "bool", "string": "varchar(%d)", + "string-char": "char(%d)", "string-text": "text", "time.Time-date": "date", "time.Time": "timestamp with time zone", diff --git a/orm/db_sqlite.go b/orm/db_sqlite.go index a43a5594..0f54d81a 100644 --- a/orm/db_sqlite.go +++ b/orm/db_sqlite.go @@ -43,6 +43,7 @@ var sqliteTypes = map[string]string{ "pk": "NOT NULL PRIMARY KEY", "bool": "bool", "string": "varchar(%d)", + "string-char": "character(%d)", "string-text": "text", "time.Time-date": "date", "time.Time": "datetime", diff --git a/orm/models_fields.go b/orm/models_fields.go index 3e9f24ed..78003a75 100644 --- a/orm/models_fields.go +++ b/orm/models_fields.go @@ -23,6 +23,7 @@ import ( // Define the Type enum const ( TypeBooleanField = 1 << iota + TypeVarCharField TypeCharField TypeTextField TypeTimeField @@ -126,7 +127,7 @@ func (e *CharField) String() string { // FieldType return the enum type func (e *CharField) FieldType() int { - return TypeCharField + return TypeVarCharField } // SetRaw set the interface to string diff --git a/orm/models_info_f.go b/orm/models_info_f.go index bbb7d71f..646e2273 100644 --- a/orm/models_info_f.go +++ b/orm/models_info_f.go @@ -244,8 +244,10 @@ checkType: if err != nil { goto end } - if fieldType == TypeCharField { + if fieldType == TypeVarCharField { switch tags["type"] { + case "char": + fieldType = TypeCharField case "text": fieldType = TypeTextField case "json": @@ -357,7 +359,7 @@ checkType: switch fieldType { case TypeBooleanField: - case TypeCharField, TypeJSONField, TypeJsonbField: + case TypeVarCharField, TypeCharField, TypeJSONField, TypeJsonbField: if size != "" { v, e := StrTo(size).Int32() if e != nil { diff --git a/orm/models_test.go b/orm/models_test.go index 9843a87d..d6c2b581 100644 --- a/orm/models_test.go +++ b/orm/models_test.go @@ -49,7 +49,7 @@ func (e *SliceStringField) String() string { } func (e *SliceStringField) FieldType() int { - return TypeCharField + return TypeVarCharField } func (e *SliceStringField) SetRaw(value interface{}) error { diff --git a/orm/models_utils.go b/orm/models_utils.go index 44a0e76a..0c3bee5d 100644 --- a/orm/models_utils.go +++ b/orm/models_utils.go @@ -149,7 +149,7 @@ func getFieldType(val reflect.Value) (ft int, err error) { case reflect.TypeOf(new(bool)): ft = TypeBooleanField case reflect.TypeOf(new(string)): - ft = TypeCharField + ft = TypeVarCharField case reflect.TypeOf(new(time.Time)): ft = TypeDateTimeField default: @@ -176,7 +176,7 @@ func getFieldType(val reflect.Value) (ft int, err error) { case reflect.Bool: ft = TypeBooleanField case reflect.String: - ft = TypeCharField + ft = TypeVarCharField default: if elm.Interface() == nil { panic(fmt.Errorf("%s is nil pointer, may be miss setting tag", val)) @@ -189,7 +189,7 @@ func getFieldType(val reflect.Value) (ft int, err error) { case sql.NullBool: ft = TypeBooleanField case sql.NullString: - ft = TypeCharField + ft = TypeVarCharField case time.Time: ft = TypeDateTimeField } From e67e57f8fb847a3609b74dc6efdf84406d02be48 Mon Sep 17 00:00:00 2001 From: Back Yu Date: Thu, 30 Nov 2017 20:26:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?orm:=20=E4=BF=AE=E5=A4=8Dlogic=20enum?= =?UTF-8?q?=E5=9B=A0=E4=B8=BAtype=20enum=E6=94=B9=E5=8F=98=E8=80=8C?= =?UTF-8?q?=E4=BA=A7=E7=94=9F=E7=9A=84=E4=BD=8D=E9=94=99=E4=BD=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- orm/models_fields.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/orm/models_fields.go b/orm/models_fields.go index 78003a75..d23c49fa 100644 --- a/orm/models_fields.go +++ b/orm/models_fields.go @@ -50,9 +50,9 @@ const ( // Define some logic enum const ( - IsIntegerField = ^-TypePositiveBigIntegerField >> 5 << 6 - IsPositiveIntegerField = ^-TypePositiveBigIntegerField >> 9 << 10 - IsRelField = ^-RelReverseMany >> 17 << 18 + IsIntegerField = ^-TypePositiveBigIntegerField >> 6 << 7 + IsPositiveIntegerField = ^-TypePositiveBigIntegerField >> 10 << 11 + IsRelField = ^-RelReverseMany >> 18 << 19 IsFieldType = ^-RelReverseMany<<1 + 1 )