Skip to content

Commit f22cbb0

Browse files
author
chaunceyjiang
committed
Add custom table name
1 parent 0eb57c0 commit f22cbb0

File tree

12 files changed

+168
-10
lines changed

12 files changed

+168
-10
lines changed

gee-orm/day2-reflect-schema/schema/schema.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,23 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} {
3737
return fieldValues
3838
}
3939

40+
type ITableName interface {
41+
TableName() string
42+
}
43+
4044
// Parse a struct to a Schema instance
4145
func Parse(dest interface{}, d dialect.Dialect) *Schema {
4246
modelType := reflect.Indirect(reflect.ValueOf(dest)).Type()
47+
var tableName string
48+
t, ok := dest.(ITableName)
49+
if !ok {
50+
tableName = modelType.Name()
51+
} else {
52+
tableName = t.TableName()
53+
}
4354
schema := &Schema{
4455
Model: dest,
45-
Name: modelType.Name(),
56+
Name: tableName,
4657
fieldMap: make(map[string]*Field),
4758
}
4859

gee-orm/day2-reflect-schema/schema/schema_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) {
3333
t.Fatal("failed to get values")
3434
}
3535
}
36+
37+
type UserTest struct {
38+
Name string `geeorm:"PRIMARY KEY"`
39+
Age int
40+
}
41+
42+
func (u *UserTest) TableName() string {
43+
return "ns_user_test"
44+
}
45+
46+
func TestSchema_TableName(t *testing.T) {
47+
schema := Parse(&UserTest{}, TestDial)
48+
if schema.Name != "ns_user_test" || len(schema.Fields) != 2 {
49+
t.Fatal("failed to parse User struct")
50+
}
51+
}

gee-orm/day3-save-query/schema/schema.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,25 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} {
3737
return fieldValues
3838
}
3939

40+
type ITableName interface {
41+
TableName() string
42+
}
43+
4044
// Parse a struct to a Schema instance
4145
func Parse(dest interface{}, d dialect.Dialect) *Schema {
4246
modelType := reflect.Indirect(reflect.ValueOf(dest)).Type()
47+
var tableName string
48+
t, ok := dest.(ITableName)
49+
if !ok {
50+
tableName = modelType.Name()
51+
} else {
52+
tableName = t.TableName()
53+
}
4354
schema := &Schema{
4455
Model: dest,
45-
Name: modelType.Name(),
56+
Name: tableName,
4657
fieldMap: make(map[string]*Field),
4758
}
48-
4959
for i := 0; i < modelType.NumField(); i++ {
5060
p := modelType.Field(i)
5161
if !p.Anonymous && ast.IsExported(p.Name) {

gee-orm/day3-save-query/schema/schema_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) {
3333
t.Fatal("failed to get values")
3434
}
3535
}
36+
37+
type UserTest struct {
38+
Name string `geeorm:"PRIMARY KEY"`
39+
Age int
40+
}
41+
42+
func (u *UserTest) TableName() string {
43+
return "ns_user_test"
44+
}
45+
46+
func TestSchema_TableName(t *testing.T) {
47+
schema := Parse(&UserTest{}, TestDial)
48+
if schema.Name != "ns_user_test" || len(schema.Fields) != 2 {
49+
t.Fatal("failed to parse User struct")
50+
}
51+
}

gee-orm/day4-chain-operation/schema/schema.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,25 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} {
3737
return fieldValues
3838
}
3939

40+
type ITableName interface {
41+
TableName() string
42+
}
43+
4044
// Parse a struct to a Schema instance
4145
func Parse(dest interface{}, d dialect.Dialect) *Schema {
4246
modelType := reflect.Indirect(reflect.ValueOf(dest)).Type()
47+
var tableName string
48+
t, ok := dest.(ITableName)
49+
if !ok {
50+
tableName = modelType.Name()
51+
} else {
52+
tableName = t.TableName()
53+
}
4354
schema := &Schema{
4455
Model: dest,
45-
Name: modelType.Name(),
56+
Name: tableName,
4657
fieldMap: make(map[string]*Field),
4758
}
48-
4959
for i := 0; i < modelType.NumField(); i++ {
5060
p := modelType.Field(i)
5161
if !p.Anonymous && ast.IsExported(p.Name) {

gee-orm/day4-chain-operation/schema/schema_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) {
3333
t.Fatal("failed to get values")
3434
}
3535
}
36+
37+
type UserTest struct {
38+
Name string `geeorm:"PRIMARY KEY"`
39+
Age int
40+
}
41+
42+
func (u *UserTest) TableName() string {
43+
return "ns_user_test"
44+
}
45+
46+
func TestSchema_TableName(t *testing.T) {
47+
schema := Parse(&UserTest{}, TestDial)
48+
if schema.Name != "ns_user_test" || len(schema.Fields) != 2 {
49+
t.Fatal("failed to parse User struct")
50+
}
51+
}

gee-orm/day5-hooks/schema/schema.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,23 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} {
3737
return fieldValues
3838
}
3939

40+
type ITableName interface {
41+
TableName() string
42+
}
43+
4044
// Parse a struct to a Schema instance
4145
func Parse(dest interface{}, d dialect.Dialect) *Schema {
4246
modelType := reflect.Indirect(reflect.ValueOf(dest)).Type()
47+
var tableName string
48+
t, ok := dest.(ITableName)
49+
if !ok {
50+
tableName = modelType.Name()
51+
} else {
52+
tableName = t.TableName()
53+
}
4354
schema := &Schema{
4455
Model: dest,
45-
Name: modelType.Name(),
56+
Name: tableName,
4657
fieldMap: make(map[string]*Field),
4758
}
4859

gee-orm/day5-hooks/schema/schema_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) {
3333
t.Fatal("failed to get values")
3434
}
3535
}
36+
37+
type UserTest struct {
38+
Name string `geeorm:"PRIMARY KEY"`
39+
Age int
40+
}
41+
42+
func (u *UserTest) TableName() string {
43+
return "ns_user_test"
44+
}
45+
46+
func TestSchema_TableName(t *testing.T) {
47+
schema := Parse(&UserTest{}, TestDial)
48+
if schema.Name != "ns_user_test" || len(schema.Fields) != 2 {
49+
t.Fatal("failed to parse User struct")
50+
}
51+
}

gee-orm/day6-transaction/schema/schema.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,25 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} {
3737
return fieldValues
3838
}
3939

40+
type ITableName interface {
41+
TableName() string
42+
}
43+
4044
// Parse a struct to a Schema instance
4145
func Parse(dest interface{}, d dialect.Dialect) *Schema {
4246
modelType := reflect.Indirect(reflect.ValueOf(dest)).Type()
47+
var tableName string
48+
t, ok := dest.(ITableName)
49+
if !ok {
50+
tableName = modelType.Name()
51+
} else {
52+
tableName = t.TableName()
53+
}
4354
schema := &Schema{
4455
Model: dest,
45-
Name: modelType.Name(),
56+
Name: tableName,
4657
fieldMap: make(map[string]*Field),
4758
}
48-
4959
for i := 0; i < modelType.NumField(); i++ {
5060
p := modelType.Field(i)
5161
if !p.Anonymous && ast.IsExported(p.Name) {

gee-orm/day6-transaction/schema/schema_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) {
3333
t.Fatal("failed to get values")
3434
}
3535
}
36+
37+
type UserTest struct {
38+
Name string `geeorm:"PRIMARY KEY"`
39+
Age int
40+
}
41+
42+
func (u *UserTest) TableName() string {
43+
return "ns_user_test"
44+
}
45+
46+
func TestSchema_TableName(t *testing.T) {
47+
schema := Parse(&UserTest{}, TestDial)
48+
if schema.Name != "ns_user_test" || len(schema.Fields) != 2 {
49+
t.Fatal("failed to parse User struct")
50+
}
51+
}

gee-orm/day7-migrate/schema/schema.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,25 @@ func (schema *Schema) RecordValues(dest interface{}) []interface{} {
3737
return fieldValues
3838
}
3939

40+
type ITableName interface {
41+
TableName() string
42+
}
43+
4044
// Parse a struct to a Schema instance
4145
func Parse(dest interface{}, d dialect.Dialect) *Schema {
4246
modelType := reflect.Indirect(reflect.ValueOf(dest)).Type()
47+
var tableName string
48+
t, ok := dest.(ITableName)
49+
if !ok {
50+
tableName = modelType.Name()
51+
} else {
52+
tableName = t.TableName()
53+
}
4354
schema := &Schema{
4455
Model: dest,
45-
Name: modelType.Name(),
56+
Name: tableName,
4657
fieldMap: make(map[string]*Field),
4758
}
48-
4959
for i := 0; i < modelType.NumField(); i++ {
5060
p := modelType.Field(i)
5161
if !p.Anonymous && ast.IsExported(p.Name) {

gee-orm/day7-migrate/schema/schema_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@ func TestSchema_RecordValues(t *testing.T) {
3333
t.Fatal("failed to get values")
3434
}
3535
}
36+
37+
type UserTest struct {
38+
Name string `geeorm:"PRIMARY KEY"`
39+
Age int
40+
}
41+
42+
func (u *UserTest) TableName() string {
43+
return "ns_user_test"
44+
}
45+
46+
func TestSchema_TableName(t *testing.T) {
47+
schema := Parse(&UserTest{}, TestDial)
48+
if schema.Name != "ns_user_test" || len(schema.Fields) != 2 {
49+
t.Fatal("failed to parse User struct")
50+
}
51+
}

0 commit comments

Comments
 (0)