Documentation ¶
Overview ¶
Package database wraps gorm for convenience, we don't use goframe-gdb because of it's commitment of NEVER-SUPPORT-MIGRATE-SPECIALITY.
Example ¶
package main import ( "github.com/lovelacelee/clsgo/v1/database" "github.com/lovelacelee/clsgo/v1/log" ) func main() { ExampleNew() } type User struct { database.Model Name string `gorm:"name"` } func ExampleNew() { db := database.New() if db != nil { defer db.Close() if db.Valid() { db.Orm.AutoMigrate(&User{}) user := User{ Name: "Lee", } db.Orm.Create(&user) quser := User{} var count int64 = 0 db.CacheFind("", &quser, "id = ?", 2) db.Orm.Model(&User{}).Count(&count) log.Info(quser) log.Info(count) } } }
Output:
Index ¶
- Variables
- func ClickHouse(dsn string) gorm.Dialector
- func ConnPoolSetting(db *gorm.DB) error
- func MySQL(dsn string) gorm.Dialector
- func Postgres(dsn string) gorm.Dialector
- func SQLServer(dsn string) gorm.Dialector
- func Sqlite(dsn string) gorm.Dialector
- type Db
- type DbConfig
- type Model
- type PluginTrace
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Config map[string]DbConfig
var GormDriver map[string]func(string) gorm.Dialector = map[string]func(string) gorm.Dialector{ "SQLITE": Sqlite, "MYSQL": MySQL, "SQLSERVER": SQLServer, "POSTGRES": Postgres, "CLICKHOUSE": ClickHouse, }
var TracePlugin gorm.Plugin = &PluginTrace{}
Functions ¶
func ClickHouse ¶
dsn := "tcp://localhost:9000?database=gorm&username=gorm&password=gorm&read_timeout=10&write_timeout=20"
func ConnPoolSetting ¶
func MySQL ¶
dsn := "username:password@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local"
func Postgres ¶
dsn := "host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
Types ¶
type Db ¶
func New ¶
New return the instance of open gorm instance, group was config name under database section. Returns nil, if group not found.
Example ¶
package main import ( "github.com/lovelacelee/clsgo/v1/database" "github.com/lovelacelee/clsgo/v1/log" ) type User struct { database.Model Name string `gorm:"name"` } func main() { db := database.New() if db != nil { defer db.Close() if db.Valid() { // db.Orm.Use(database.TracePlugin) db.Orm.AutoMigrate(&User{}) user := User{ Name: "Lee", } db.Orm.Create(&user) quser := User{} var count int64 = 0 // db.Orm.Find(&quser, "id = ?", 2) db.CacheFind("", &quser, "id = ?", 2) db.Orm.Model(&User{}).Count(&count) log.Info(quser) log.Info(count) } } }
Output:
type Model ¶
See more https://gorm.io/zh_CN/docs/models.html https://gorm.io/zh_CN/docs/models.html#embedded_struct Field inluded:
ID uint `gorm:"primaryKey"` CreatedAt time.Time UpdatedAt time.Time DeletedAt gorm.DeletedAt `gorm:"index"`
type PluginTrace ¶
type PluginTrace struct{}
Plugin trace sql and time of it excuted
func (*PluginTrace) Initialize ¶
func (op *PluginTrace) Initialize(db *gorm.DB) (err error)
func (*PluginTrace) Name ¶
func (op *PluginTrace) Name() string