Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom table name #12

Merged
merged 1 commit into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}