mysql

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2022 License: MIT Imports: 10 Imported by: 1

README

mysql

Go codecov Go Report Card Release

Golang mysql package based on GORM V2.

Install

go get gitlab.com/go-ecosystem/mysql

Usage

DB

See more at gorm.

package examples

import (
	"fmt"

	"gitlab.com/go-ecosystem/mysql"
	"gorm.io/gorm/logger"
)

type Product struct {
	// mysql base model
	mysql.Model
	Code  string
	Price uint
}

func ExampleRegister() {
	cnf := mysql.NewConfig("root",
		"123456",
		"127.0.0.1",
		"3306",
		"test",
		"utf8mb4",
		logger.Error)
	// register default db
	mysql.Register(cnf)

	cnf2 := mysql.NewConfig("root",
		"123456",
		"127.0.0.1",
		"3306",
		"test2",
		"utf8mb4",
		logger.Error)
	// register another db with key "test2"
	mysql.RegisterByKey(cnf2, "test2")

	defer mysql.Close()

	// default db

	db := mysql.GetDB()

	// Migrate the schema
	db.AutoMigrate(&Product{})

	// Create
	db.Create(&Product{Code: "L1212", Price: 1000})

	// Read
	var product Product
	db.First(&product, "code = ?", "L1212") // find product with code l1212

	// Update - update product's price to 2000
	db.Model(&product).Update("Price", 2000)

	// Delete - delete product
	db.Delete(&product)

	fmt.Println("default db finish")

	// db with key "test2"

	db2 := mysql.GetDBByKey("test2")

	// Migrate the schema
	db2.AutoMigrate(&Product{})

	// Create
	db2.Create(&Product{Code: "L1212", Price: 1000})

	// Read
	db2.First(&product, "code = ?", "L1212") // find product with code l1212

	// Update - update product's price to 2000
	db2.Model(&product).Update("Price", 2000)

	// Delete - delete product
	db2.Delete(&product)

	fmt.Println("db2 finish")

	// Output: default db finish
	// db2 finish
}
MockDB

mock usage see more at https://github.com/DATA-DOG/go-sqlmock/blob/master/examples/orders/orders_test.go

_, mock := mysql.MockDB()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close()

Close closes current db connection

func Deregister

func Deregister()

Deregister deregister default examples

func DeregisterByKey

func DeregisterByKey(key string)

DeregisterByKey deregister examples by key

func GetDB

func GetDB() *gorm.DB

GetDB get default db

func GetDBByKey

func GetDBByKey(key string) *gorm.DB

GetDBByKey get by with key

func MockDB

func MockDB(config *Config) (*sql.DB, sqlmock.Sqlmock)

MockDB mock default DB

func MockDBByKey

func MockDBByKey(config *Config, key string) (*sql.DB, sqlmock.Sqlmock)

MockDBByKey mock DB by key

func Register

func Register(config *Config)

Register register default examples

func RegisterByKey

func RegisterByKey(config *Config, key string)

RegisterByKey register examples by key

func SetLogger

func SetLogger(db *gorm.DB, logger logger.Interface)

SetLogger replace default logger

Types

type Config

type Config struct {
	// connection string
	Connection *ConnectionConfig
	// the maximum number of open connections to the database
	MaxOpenConns int
	// the maximum number of connections in the idle connection pool
	MaxIdleConns int
	// the maximum amount of time a connection may be reused in second
	ConnMaxLifetime time.Duration
	// log level
	LogLevel logger.LogLevel
	// TablePrefix table prefix
	TablePrefix string
	// SingularTable enable singular table names
	SingularTable bool
}

Config mysql configuration

func NewConfig

func NewConfig(user, pwd, host, port, dbname, charset string, logLevel logger.LogLevel, opts ...ConfigOption) *Config

NewConfig new config with default value

func (*Config) GenDSN

func (c *Config) GenDSN() string

GenDSN generate DSN string

type ConfigOption

type ConfigOption interface {
	// contains filtered or unexported methods
}

ConfigOption config option

func WithConnMaxLifetime

func WithConnMaxLifetime(connMaxLifetime time.Duration) ConfigOption

WithConnMaxLifetime new connMaxLifetime option

func WithMaxIdleConns

func WithMaxIdleConns(maxIdleConns int) ConfigOption

WithMaxIdleConns new maxIdleConns option

func WithMaxOpenConns

func WithMaxOpenConns(maxOpenConns int) ConfigOption

WithMaxOpenConns new maxOpenConns option

func WithSingularTable added in v1.1.0

func WithSingularTable(singularTable bool) ConfigOption

WithSingularTable new singularTable option

func WithTablePrefix added in v1.1.0

func WithTablePrefix(tablePrefix string) ConfigOption

WithTablePrefix new tablePrefix option

type ConnectionConfig

type ConnectionConfig struct {
	User      string
	Pwd       string
	Host      string
	Port      string
	DBName    string
	Charset   string
	ParseTime bool
	Loc       string
}

ConnectionConfig connection configuration

type Model

type Model struct {
	ID        int64 `gorm:"primary_key;AUTO_INCREMENT;column:id" json:"id"`
	CreatedAt int64 `gorm:"column:created_at" json:"createdAt"`
	UpdatedAt int64 `gorm:"column:updated_at" json:"updatedAt"`
}

Model base model

Jump to

Keyboard shortcuts

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