gogorm

package
v1.2.107 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

go-db 数据操作

目前已封装

  • mysql
  • sqlite3
  • clickhouse
  • postgresql
  • sqlserver
  • tidb
func testSqlite3() {
    err := gogorm.Init(gogorm.Config{
        DataSource: "./test.db",
        DBType:     gogorm.DATABASE_SQLITE,
    })
    if err != nil {
        golog.WithTag("godb").Error(err.Error())
        return
    }
    db := gogorm.Default().DB
    err = db.AutoMigrate(&Product{})
    if err != nil {
        golog.WithTag("godb").Error(err.Error())
        return
    }
    
    // Create
    insertProduct := &Product{Code: "D42", Price: 100}
    db.Insert(insertProduct)
    fmt.Println(insertProduct.ID)
    // Read
    var product Product
    tx := db.First(&product, 1) // find product with integer primary key
    if tx.Error != nil {
    fmt.Println("not found first ", tx.Error.Error())
    }
    db.First(&product, "code = ?", "D42")
    // Delete - delete product
    db.Delete(&product, 1)
    
    err = goutils.RemoveFile("./test.db")
    if err != nil {
        golog.WithTag("godb").Error(err.Error())
    }
}
func mysqlTest() {
    err := gogorm.Init(gogorm.Config{
        DataSource: "root:223238@tcp(127.0.0.1:33060)/gromdb?charset=utf8mb4&parseTime=True&loc=Localb",
    })
    if err != nil {
        golog.WithTag("godb").Error(err.Error())
        return
    }
    db := gogorm.Default().DB
	
	err = db.AutoMigrate(&Product{})
	if err != nil {
		golog.WithTag("godb").Error(err.Error())
		return
	}

	// Create
	insertProduct := &Product{Code: "D42", Price: 100}
	db.Insert(insertProduct)
	fmt.Println(insertProduct.ID)
	// Read
	var product Product
	tx := db.First(&product, 1) // find product with integer primary key
	if tx.Error != nil {
		fmt.Println("not found first ", tx.Error.Error())
	}
	db.First(&product, "code = ?", "D42")
	// Delete - delete product
	db.Delete(&product, 1)

}
func testClickhouse() {
	dsn := "tcp://localhost:9000?database=gorm&username=gorm&password=gorm&read_timeout=10&write_timeout=20"
    err := gogorm.Init(gogorm.Config{
        DataSource: dsn,
		DBType:     gogorm.DATABASE_CLICKHOUSE,
    })
	
    if err != nil {
        golog.WithTag("godb").Error(err.Error())
        return
    }
    db := gogorm.Default().DB
	
	err = db.Set("gorm:table_options", "ENGINE=Distributed(cluster, default, hits)").AutoMigrate(&Product{})
	if err != nil {
		golog.WithTag("godb").Error(err.Error())
		return
	}
	// Set table options

	// Create
	insertProduct := &Product{Code: "D42", Price: 100}
	db.Insert(insertProduct)
	fmt.Println(insertProduct.ID)
	// Read
	var product Product
	tx := db.First(&product, 1) // find product with integer primary key
	if tx.Error != nil {
		fmt.Println("not found first ", tx.Error.Error())
	}
	db.First(&product, "code = ?", "D42")
	// Delete - delete product
	db.Delete(&product, 1)
}
mongodb 不是关系性数据库 暂时不支持

Documentation

Index

Constants

View Source
const (
	DATABASE_MYSQL      = "mysql"
	DATABASE_POSTGRESQL = "postgres"
	DATABASE_SQLITE     = "sqlite"
	DATABASE_SQLSERVER  = "sqlserver"
	DATABASE_CLICKHOUSE = "clickhouse"
	DATABASE_TIDB       = "tidb"
)

Variables

This section is empty.

Functions

func Init added in v1.2.103

func Init(configs ...Config) (err error)

Types

type Config added in v1.2.52

type Config struct {
	Name         string `yaml:"Name" json:"name,optional"`
	DBType       string `yaml:"DBType" json:"dbType,optional"` // default mysql
	DataSource   string `yaml:"DataSource" json:"dataSource,optional"`
	MaxIdleCount int    `yaml:"MaxIdleCount" json:"maxIdleCount,optional"` // zero means defaultMaxIdleConns; negative means 0
	MaxOpen      int    `yaml:"MaxOpen" json:"maxOpen,optional"`           // <= 0 means unlimited
	MaxLifetime  int    `yaml:"MaxLifetime" json:"maxLifetime,optional"`   // maximum amount of time a connection may be reused minutes
}

type GoGorm added in v1.2.103

type GoGorm struct {
	DB     *gorm.DB
	Config *Config
}

func Default added in v1.2.103

func Default() *GoGorm

func GetClient added in v1.2.103

func GetClient(names ...string) *GoGorm

func New added in v1.2.103

func New(config *Config) (*GoGorm, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL