forked from mirror/gorm
Add Method SingularTable
This commit is contained in:
parent
2400a46ff7
commit
da6ffd52dd
|
@ -70,6 +70,8 @@ db, err := Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
||||||
// Set the maximum idle database connections
|
// Set the maximum idle database connections
|
||||||
db.SetPool(100)
|
db.SetPool(100)
|
||||||
|
|
||||||
|
// By default, table name is plural of struct type, if you like singular table name
|
||||||
|
db.SingularTable(true)
|
||||||
|
|
||||||
// Gorm is goroutines friendly, so you can create a global variable to keep the connection and use it everywhere like this
|
// Gorm is goroutines friendly, so you can create a global variable to keep the connection and use it everywhere like this
|
||||||
|
|
||||||
|
|
10
chain.go
10
chain.go
|
@ -9,10 +9,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Chain struct {
|
type Chain struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
driver string
|
driver string
|
||||||
debug bool
|
debug bool
|
||||||
value interface{}
|
singularTableName bool
|
||||||
|
value interface{}
|
||||||
|
|
||||||
Errors []error
|
Errors []error
|
||||||
Error error
|
Error error
|
||||||
|
@ -68,6 +69,7 @@ func (s *Chain) do(value interface{}) *Do {
|
||||||
do.limitStr = s.limitStr
|
do.limitStr = s.limitStr
|
||||||
do.specifiedTableName = s.specifiedTableName
|
do.specifiedTableName = s.specifiedTableName
|
||||||
do.unscoped = s.unscoped
|
do.unscoped = s.unscoped
|
||||||
|
do.singularTableName = s.singularTableName
|
||||||
|
|
||||||
s.value = value
|
s.value = value
|
||||||
do.setModel(value)
|
do.setModel(value)
|
||||||
|
|
3
do.go
3
do.go
|
@ -35,12 +35,13 @@ type Do struct {
|
||||||
unscoped bool
|
unscoped bool
|
||||||
updateAttrs map[string]interface{}
|
updateAttrs map[string]interface{}
|
||||||
ignoreProtectedAttrs bool
|
ignoreProtectedAttrs bool
|
||||||
|
singularTableName bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Do) tableName() string {
|
func (s *Do) tableName() string {
|
||||||
if s.specifiedTableName == "" {
|
if s.specifiedTableName == "" {
|
||||||
var err error
|
var err error
|
||||||
s.guessedTableName, err = s.model.tableName()
|
s.guessedTableName, err = s.model.tableName(s.singularTableName)
|
||||||
s.err(err)
|
s.err(err)
|
||||||
return s.guessedTableName
|
return s.guessedTableName
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -81,7 +81,6 @@ var (
|
||||||
func init() {
|
func init() {
|
||||||
var err error
|
var err error
|
||||||
db, err = Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
db, err = Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
||||||
|
|
||||||
// CREATE USER 'gorm'@'localhost' IDENTIFIED BY 'gorm';
|
// CREATE USER 'gorm'@'localhost' IDENTIFIED BY 'gorm';
|
||||||
// CREATE DATABASE 'gorm';
|
// CREATE DATABASE 'gorm';
|
||||||
// GRANT ALL ON gorm.* TO 'gorm'@'localhost';
|
// GRANT ALL ON gorm.* TO 'gorm'@'localhost';
|
||||||
|
|
13
main.go
13
main.go
|
@ -3,9 +3,10 @@ package gorm
|
||||||
import "database/sql"
|
import "database/sql"
|
||||||
|
|
||||||
type DB struct {
|
type DB struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
driver string
|
driver string
|
||||||
DebugMode bool
|
DebugMode bool
|
||||||
|
SingularTableName bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func Open(driver, source string) (db DB, err error) {
|
func Open(driver, source string) (db DB, err error) {
|
||||||
|
@ -18,8 +19,12 @@ func (s *DB) SetPool(n int) {
|
||||||
s.db.SetMaxIdleConns(n)
|
s.db.SetMaxIdleConns(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *DB) SingularTable(result bool) {
|
||||||
|
s.SingularTableName = result
|
||||||
|
}
|
||||||
|
|
||||||
func (s *DB) buildChain() *Chain {
|
func (s *DB) buildChain() *Chain {
|
||||||
return &Chain{db: s.db, driver: s.driver, debug: s.DebugMode}
|
return &Chain{db: s.db, driver: s.driver, debug: s.DebugMode, singularTableName: s.SingularTableName}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Where(querystring interface{}, args ...interface{}) *Chain {
|
func (s *DB) Where(querystring interface{}, args ...interface{}) *Chain {
|
||||||
|
|
14
model.go
14
model.go
|
@ -276,7 +276,7 @@ func (m *Model) typeName() string {
|
||||||
return typ.Name()
|
return typ.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Model) tableName() (str string, err error) {
|
func (m *Model) tableName(singularTableName bool) (str string, err error) {
|
||||||
if m.data == nil {
|
if m.data == nil {
|
||||||
err = errors.New("Model haven't been set")
|
err = errors.New("Model haven't been set")
|
||||||
return
|
return
|
||||||
|
@ -284,11 +284,13 @@ func (m *Model) tableName() (str string, err error) {
|
||||||
|
|
||||||
str = toSnake(m.typeName())
|
str = toSnake(m.typeName())
|
||||||
|
|
||||||
pluralMap := map[string]string{"ch": "ches", "ss": "sses", "sh": "shes", "day": "days", "y": "ies", "x": "xes", "s?": "s"}
|
if !singularTableName {
|
||||||
for key, value := range pluralMap {
|
pluralMap := map[string]string{"ch": "ches", "ss": "sses", "sh": "shes", "day": "days", "y": "ies", "x": "xes", "s?": "s"}
|
||||||
reg := regexp.MustCompile(key + "$")
|
for key, value := range pluralMap {
|
||||||
if reg.MatchString(str) {
|
reg := regexp.MustCompile(key + "$")
|
||||||
return reg.ReplaceAllString(str, value), err
|
if reg.MatchString(str) {
|
||||||
|
return reg.ReplaceAllString(str, value), err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue