Skip to content

Commit

Permalink
Add custom table name
Browse files Browse the repository at this point in the history
  • Loading branch information
chaunceyjiang committed Mar 23, 2020
1 parent 0eb57c0 commit f22cbb0
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 10 deletions.
13 changes: 12 additions & 1 deletion gee-orm/day2-reflect-schema/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
16 changes: 16 additions & 0 deletions gee-orm/day2-reflect-schema/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
14 changes: 12 additions & 2 deletions gee-orm/day3-save-query/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions gee-orm/day3-save-query/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
14 changes: 12 additions & 2 deletions gee-orm/day4-chain-operation/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions gee-orm/day4-chain-operation/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
13 changes: 12 additions & 1 deletion gee-orm/day5-hooks/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
16 changes: 16 additions & 0 deletions gee-orm/day5-hooks/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
14 changes: 12 additions & 2 deletions gee-orm/day6-transaction/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions gee-orm/day6-transaction/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
14 changes: 12 additions & 2 deletions gee-orm/day7-migrate/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions gee-orm/day7-migrate/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit f22cbb0

Please sign in to comment.