本文整理汇总了Golang中github.com/jinzhu/gorm.DB.SingularTable方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.SingularTable方法的具体用法?Golang DB.SingularTable怎么用?Golang DB.SingularTable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jinzhu/gorm.DB
的用法示例。
在下文中一共展示了DB.SingularTable方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Connect
//Connect init gorm ORM.
func (orm *AppOrm) Connect(conf interfaces.Config) error {
var db gorm.DB
dbUri, err := conf.Get("DatabaseUri")
if dbUri, ok := dbUri.(string); err == nil && ok {
log.Println(dbUri)
db, err = gorm.Open("mysql", dbUri)
}
if err != nil {
log.Fatal(err.Error())
return err
}
db.DB()
db.DB().Ping()
db.DB().SetMaxIdleConns(10)
db.DB().SetMaxOpenConns(100)
// Disable table name's pluralization
db.SingularTable(true)
if env, err := conf.Get("env"); err == nil && env == "dev" {
db.LogMode(true)
}
orm.driver = &db
return nil
}
示例2: ExtractDBSQL
//ExtractDBSQL ---
func ExtractDBSQL() (gormdb chaospeddler.GormDB) {
var err error
var db *gorm.DB
appEnv, _ := cfenv.Current()
service, _ := appEnv.Services.WithName("sql-info")
host := fmt.Sprintf("%v", service.Credentials["hostname"])
port := fmt.Sprintf("%v", service.Credentials["port"])
dbname := fmt.Sprintf("%v", service.Credentials["name"])
user := fmt.Sprintf("%v", service.Credentials["username"])
pass := fmt.Sprintf("%v", service.Credentials["password"])
connectionString := user + ":" + pass + "@tcp(" + host + ":" + port + ")" + "/" + dbname + "?charset=utf8&parseTime=True&loc=Local"
lo.G.Error("connection string: ", connectionString)
if db, err = gorm.Open("mysql", connectionString); err == nil {
db.DB()
db.DB().Ping()
db.DB().SetMaxIdleConns(10)
db.DB().SetMaxOpenConns(100)
db.SingularTable(true)
db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(
new(chaospeddler.ServiceInstance),
new(chaospeddler.ServiceBinding),
)
gormdb = db
} else {
lo.G.Error("there was an error connecting to mysql: ", err)
panic(err)
}
return
}
示例3: initializeDatabase
func initializeDatabase(db *gorm.DB) {
db.SingularTable(true) // 自動生成されるテーブル名を単数形にする.
db.CreateTable(&Property{})
db.CreateTable(&Room{})
// db.AutoMigrate(&Family{}, &FamilyCredential{}) // 使いこなせないので当面自動マイグレーションはoff.
db.LogMode(true)
defer beginTx(db)()
}
示例4: InitDatabase
// InitDatabase initialize new database connection
// the first argument is a driver to use, the second one - connection parameters
// Examples:
// InitDatabase("sqlite3","/var/lib/gossha/gossha.db")
// InitDatabase("sqlite3",":memory:")
// InitDatabase("mysql", "user:[email protected]/dbname?charset=utf8&parseTime=True&loc=Local")
// InitDatabase("postgres", "user=gorm dbname=gorm sslmode=disable")
// InitDatabase("postgres", "postgres://pqgotest:[email protected]/pqgotest?sslmode=verify-full")
func InitDatabase(driver, dbPath string, verboseLogging bool) error {
var db *gorm.DB
db, err := gorm.Open(driver, dbPath)
db.LogMode(verboseLogging)
err = db.DB().Ping()
if err != nil {
return err
}
db.DB().SetMaxIdleConns(5)
db.DB().SetMaxOpenConns(5)
// Disable table name's pluralization
db.SingularTable(true)
err = db.AutoMigrate(&User{}, &Message{}, &Key{}, &Session{}).Error
if err != nil {
return err
}
err = db.Model(&User{}).AddUniqueIndex("idx_contact_name", "name").Error
if err != nil {
return err
}
err = db.Model(&Message{}).AddIndex("idx_message", "user_id", "created_at").Error
if err != nil {
return err
}
err = db.Model(&Key{}).AddIndex("idx_key", "user_id", "created_at").Error
if err != nil {
return err
}
err = db.Model(&Key{}).AddUniqueIndex("idx_key_content", "content", "user_id", "created_at").Error
if err != nil {
return err
}
err = db.Model(&Session{}).AddIndex("idx_session", "user_id", "created_at").Error
if err != nil {
return err
}
DB = db
return nil
}
示例5: NewDb
func NewDb(Cnf DbConfig) *Db {
var err error
var db *gorm.DB
switch Cnf.DriverName {
case "sqlite3":
db, err = gorm.Open("sqlite3", Cnf.DataSourceName)
logger.CheckFatal(err, "Got error when connect database")
//this.DbMap = &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
case "mysql":
//db, err := sql.Open("mysql", "user:[email protected](localhost:5555)/dbname?charset=utf8&parseTime=True&loc=Local")
db, err = gorm.Open("mysql", fmt.Sprintf("%s:%[email protected](%s)/%s?charset=%s&parseTime=True&loc=Local",
Cnf.UserName,
Cnf.Password,
Cnf.Host,
Cnf.DataSourceName,
Cnf.Encoding))
logger.CheckFatal(err, "Got error when connect database")
//this.DbMap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
default:
logger.Fatal("The types of database does not support:"+Cnf.DriverName, err)
}
db.DB()
db.DB().Ping()
db.DB().SetMaxIdleConns(10)
db.DB().SetMaxOpenConns(100)
// Disable table name's pluralization
db.SingularTable(true)
// construct a gorp DbMap
//
return &Db{Cnf: Cnf, db: db}
}