From f22cbb04d1a323f21d4399782e0183d8dd857fc5 Mon Sep 17 00:00:00 2001 From: chaunceyjiang Date: Mon, 23 Mar 2020 11:54:32 +0800 Subject: [PATCH] Add custom table name --- gee-orm/day2-reflect-schema/schema/schema.go | 13 ++++++++++++- .../day2-reflect-schema/schema/schema_test.go | 16 ++++++++++++++++ gee-orm/day3-save-query/schema/schema.go | 14 ++++++++++++-- gee-orm/day3-save-query/schema/schema_test.go | 16 ++++++++++++++++ gee-orm/day4-chain-operation/schema/schema.go | 14 ++++++++++++-- .../day4-chain-operation/schema/schema_test.go | 16 ++++++++++++++++ gee-orm/day5-hooks/schema/schema.go | 13 ++++++++++++- gee-orm/day5-hooks/schema/schema_test.go | 16 ++++++++++++++++ gee-orm/day6-transaction/schema/schema.go | 14 ++++++++++++-- gee-orm/day6-transaction/schema/schema_test.go | 16 ++++++++++++++++ gee-orm/day7-migrate/schema/schema.go | 14 ++++++++++++-- gee-orm/day7-migrate/schema/schema_test.go | 16 ++++++++++++++++ 12 files changed, 168 insertions(+), 10 deletions(-) diff --git a/gee-orm/day2-reflect-schema/schema/schema.go b/gee-orm/day2-reflect-schema/schema/schema.go index ff76283..93d36da 100644 --- a/gee-orm/day2-reflect-schema/schema/schema.go +++ b/gee-orm/day2-reflect-schema/schema/schema.go @@ -37,12 +37,23 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} { return fieldValues } +type ITableName interface { + TableName() string +} + // Parse a struct to a Schema instance func Parse(dest interface{}, d dialect.Dialect) *Schema { modelType := reflect.Indirect(reflect.ValueOf(dest)).Type() + var tableName string + t, ok := dest.(ITableName) + if !ok { + tableName = modelType.Name() + } else { + tableName = t.TableName() + } schema := &Schema{ Model: dest, - Name: modelType.Name(), + Name: tableName, fieldMap: make(map[string]*Field), } diff --git a/gee-orm/day2-reflect-schema/schema/schema_test.go b/gee-orm/day2-reflect-schema/schema/schema_test.go index 47ae9fc..8f625cb 100644 --- a/gee-orm/day2-reflect-schema/schema/schema_test.go +++ b/gee-orm/day2-reflect-schema/schema/schema_test.go @@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) { t.Fatal("failed to get values") } } + +type UserTest struct { + Name string `geeorm:"PRIMARY KEY"` + Age int +} + +func (u *UserTest) TableName() string { + return "ns_user_test" +} + +func TestSchema_TableName(t *testing.T) { + schema := Parse(&UserTest{}, TestDial) + if schema.Name != "ns_user_test" || len(schema.Fields) != 2 { + t.Fatal("failed to parse User struct") + } +} diff --git a/gee-orm/day3-save-query/schema/schema.go b/gee-orm/day3-save-query/schema/schema.go index ff76283..2c9b927 100644 --- a/gee-orm/day3-save-query/schema/schema.go +++ b/gee-orm/day3-save-query/schema/schema.go @@ -37,15 +37,25 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} { return fieldValues } +type ITableName interface { + TableName() string +} + // Parse a struct to a Schema instance func Parse(dest interface{}, d dialect.Dialect) *Schema { modelType := reflect.Indirect(reflect.ValueOf(dest)).Type() + var tableName string + t, ok := dest.(ITableName) + if !ok { + tableName = modelType.Name() + } else { + tableName = t.TableName() + } schema := &Schema{ Model: dest, - Name: modelType.Name(), + Name: tableName, fieldMap: make(map[string]*Field), } - for i := 0; i < modelType.NumField(); i++ { p := modelType.Field(i) if !p.Anonymous && ast.IsExported(p.Name) { diff --git a/gee-orm/day3-save-query/schema/schema_test.go b/gee-orm/day3-save-query/schema/schema_test.go index 47ae9fc..8f625cb 100644 --- a/gee-orm/day3-save-query/schema/schema_test.go +++ b/gee-orm/day3-save-query/schema/schema_test.go @@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) { t.Fatal("failed to get values") } } + +type UserTest struct { + Name string `geeorm:"PRIMARY KEY"` + Age int +} + +func (u *UserTest) TableName() string { + return "ns_user_test" +} + +func TestSchema_TableName(t *testing.T) { + schema := Parse(&UserTest{}, TestDial) + if schema.Name != "ns_user_test" || len(schema.Fields) != 2 { + t.Fatal("failed to parse User struct") + } +} diff --git a/gee-orm/day4-chain-operation/schema/schema.go b/gee-orm/day4-chain-operation/schema/schema.go index ff76283..2c9b927 100644 --- a/gee-orm/day4-chain-operation/schema/schema.go +++ b/gee-orm/day4-chain-operation/schema/schema.go @@ -37,15 +37,25 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} { return fieldValues } +type ITableName interface { + TableName() string +} + // Parse a struct to a Schema instance func Parse(dest interface{}, d dialect.Dialect) *Schema { modelType := reflect.Indirect(reflect.ValueOf(dest)).Type() + var tableName string + t, ok := dest.(ITableName) + if !ok { + tableName = modelType.Name() + } else { + tableName = t.TableName() + } schema := &Schema{ Model: dest, - Name: modelType.Name(), + Name: tableName, fieldMap: make(map[string]*Field), } - for i := 0; i < modelType.NumField(); i++ { p := modelType.Field(i) if !p.Anonymous && ast.IsExported(p.Name) { diff --git a/gee-orm/day4-chain-operation/schema/schema_test.go b/gee-orm/day4-chain-operation/schema/schema_test.go index 47ae9fc..8f625cb 100644 --- a/gee-orm/day4-chain-operation/schema/schema_test.go +++ b/gee-orm/day4-chain-operation/schema/schema_test.go @@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) { t.Fatal("failed to get values") } } + +type UserTest struct { + Name string `geeorm:"PRIMARY KEY"` + Age int +} + +func (u *UserTest) TableName() string { + return "ns_user_test" +} + +func TestSchema_TableName(t *testing.T) { + schema := Parse(&UserTest{}, TestDial) + if schema.Name != "ns_user_test" || len(schema.Fields) != 2 { + t.Fatal("failed to parse User struct") + } +} diff --git a/gee-orm/day5-hooks/schema/schema.go b/gee-orm/day5-hooks/schema/schema.go index ff76283..93d36da 100644 --- a/gee-orm/day5-hooks/schema/schema.go +++ b/gee-orm/day5-hooks/schema/schema.go @@ -37,12 +37,23 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} { return fieldValues } +type ITableName interface { + TableName() string +} + // Parse a struct to a Schema instance func Parse(dest interface{}, d dialect.Dialect) *Schema { modelType := reflect.Indirect(reflect.ValueOf(dest)).Type() + var tableName string + t, ok := dest.(ITableName) + if !ok { + tableName = modelType.Name() + } else { + tableName = t.TableName() + } schema := &Schema{ Model: dest, - Name: modelType.Name(), + Name: tableName, fieldMap: make(map[string]*Field), } diff --git a/gee-orm/day5-hooks/schema/schema_test.go b/gee-orm/day5-hooks/schema/schema_test.go index 47ae9fc..8f625cb 100644 --- a/gee-orm/day5-hooks/schema/schema_test.go +++ b/gee-orm/day5-hooks/schema/schema_test.go @@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) { t.Fatal("failed to get values") } } + +type UserTest struct { + Name string `geeorm:"PRIMARY KEY"` + Age int +} + +func (u *UserTest) TableName() string { + return "ns_user_test" +} + +func TestSchema_TableName(t *testing.T) { + schema := Parse(&UserTest{}, TestDial) + if schema.Name != "ns_user_test" || len(schema.Fields) != 2 { + t.Fatal("failed to parse User struct") + } +} diff --git a/gee-orm/day6-transaction/schema/schema.go b/gee-orm/day6-transaction/schema/schema.go index ff76283..2c9b927 100644 --- a/gee-orm/day6-transaction/schema/schema.go +++ b/gee-orm/day6-transaction/schema/schema.go @@ -37,15 +37,25 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} { return fieldValues } +type ITableName interface { + TableName() string +} + // Parse a struct to a Schema instance func Parse(dest interface{}, d dialect.Dialect) *Schema { modelType := reflect.Indirect(reflect.ValueOf(dest)).Type() + var tableName string + t, ok := dest.(ITableName) + if !ok { + tableName = modelType.Name() + } else { + tableName = t.TableName() + } schema := &Schema{ Model: dest, - Name: modelType.Name(), + Name: tableName, fieldMap: make(map[string]*Field), } - for i := 0; i < modelType.NumField(); i++ { p := modelType.Field(i) if !p.Anonymous && ast.IsExported(p.Name) { diff --git a/gee-orm/day6-transaction/schema/schema_test.go b/gee-orm/day6-transaction/schema/schema_test.go index 47ae9fc..8f625cb 100644 --- a/gee-orm/day6-transaction/schema/schema_test.go +++ b/gee-orm/day6-transaction/schema/schema_test.go @@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) { t.Fatal("failed to get values") } } + +type UserTest struct { + Name string `geeorm:"PRIMARY KEY"` + Age int +} + +func (u *UserTest) TableName() string { + return "ns_user_test" +} + +func TestSchema_TableName(t *testing.T) { + schema := Parse(&UserTest{}, TestDial) + if schema.Name != "ns_user_test" || len(schema.Fields) != 2 { + t.Fatal("failed to parse User struct") + } +} diff --git a/gee-orm/day7-migrate/schema/schema.go b/gee-orm/day7-migrate/schema/schema.go index ff76283..2c9b927 100644 --- a/gee-orm/day7-migrate/schema/schema.go +++ b/gee-orm/day7-migrate/schema/schema.go @@ -37,15 +37,25 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} { return fieldValues } +type ITableName interface { + TableName() string +} + // Parse a struct to a Schema instance func Parse(dest interface{}, d dialect.Dialect) *Schema { modelType := reflect.Indirect(reflect.ValueOf(dest)).Type() + var tableName string + t, ok := dest.(ITableName) + if !ok { + tableName = modelType.Name() + } else { + tableName = t.TableName() + } schema := &Schema{ Model: dest, - Name: modelType.Name(), + Name: tableName, fieldMap: make(map[string]*Field), } - for i := 0; i < modelType.NumField(); i++ { p := modelType.Field(i) if !p.Anonymous && ast.IsExported(p.Name) { diff --git a/gee-orm/day7-migrate/schema/schema_test.go b/gee-orm/day7-migrate/schema/schema_test.go index 47ae9fc..8f625cb 100644 --- a/gee-orm/day7-migrate/schema/schema_test.go +++ b/gee-orm/day7-migrate/schema/schema_test.go @@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) { t.Fatal("failed to get values") } } + +type UserTest struct { + Name string `geeorm:"PRIMARY KEY"` + Age int +} + +func (u *UserTest) TableName() string { + return "ns_user_test" +} + +func TestSchema_TableName(t *testing.T) { + schema := Parse(&UserTest{}, TestDial) + if schema.Name != "ns_user_test" || len(schema.Fields) != 2 { + t.Fatal("failed to parse User struct") + } +}