database

package module
v0.0.0-...-3fb1b5e Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2020 License: MIT Imports: 9 Imported by: 0

README

database

数据库

data := make(map[string]string)
data[database.ProfileId] = "test"
data[database.ProfileDriver] = "mysql"
data[database.ProfileHost] = "127.0.0.1"
data[database.ProfileDatabase] = "test"
data[database.ProfileUsername] = "root"
data[database.ProfilePassword] = "123456"
data[database.ProfileWrite] = "true"

profile, err := database.NewProfile(data)
if err != nil {
    fmt.Println(err)
    return
}

builder := database.DriversBuilder{}

err2 := builder.AddProfile(profile)
if err2 != nil {
    fmt.Println(err2)
    return
}

drivers, err3 := builder.Build()
if err3 != nil {
    fmt.Println(err3)
    return
}

driver, err4 := drivers.GetWriter()
if err4 != nil {
    fmt.Println(err4)
    return
}
query := `DROP TABLE IF EXISTS user;`
_, err11 := database.Exec(driver, query)
if err11 != nil {
	fmt.Println(err11)
	return
}

query = `CREATE TABLE user (
    id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    name varchar(255) NOT NULL DEFAULT '' COMMENT '姓名',
    phone varchar(32) NOT NULL DEFAULT '' COMMENT '手机号',
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;`

_, err12 := database.Exec(driver, query)
if err12 != nil {
	fmt.Println(err12)
	return
}
r21, err21 := database.Exec(driver, "INSERT INTO user (name, phone) VALUES (?, ?);", "张三", "13000000001")
if err21 != nil {
	fmt.Println(err21)
	return
}

fmt.Println(r21)

r22, err22 := database.Exec(driver, "INSERT INTO user (name, phone) VALUES (?, ?);", "李四", "13000000002")
if err22 != nil {
	fmt.Println(err22)
	return
}

fmt.Println(r22)
r31, err31 := database.Exec(driver, "UPDATE user SET phone = ? WHERE id = ?", "18000000001", 1)
if err31 != nil {
    fmt.Println(err31)
    return
}

fmt.Println(r31)
r41, err41 := database.Exec(driver, "DELETE FROM user WHERE id = ?;", 2)
if err41 != nil {
    fmt.Println(err41)
    return
}

fmt.Println(r41)
r51, err51, closeErr51 := database.First(driver, "SELECT * FROM user WHERE id = ?", 1)
if err51 != nil {
    fmt.Println(err51)
    return
}

if closeErr51 != nil {
    fmt.Println(closeErr51)
    return
}

fmt.Println(r51)

r52, err52, closeErr52 := database.Find(driver, "SELECT * FROM user")
if err52 != nil {
    fmt.Println(err52)
    return
}

if closeErr52 != nil {
    fmt.Println(closeErr52)
    return
}

fmt.Println(r52)

r53, err53, closeErr53 := database.AggregateInt(driver, "SELECT COUNT(1) AS 'aggregate' FROM user")
if err53 != nil {
    fmt.Println(err53)
    return
}

if closeErr53 != nil {
    fmt.Println(closeErr53)
    return
}

fmt.Println(r53)
// 进程正常关闭前
err61 := drivers.Close()
fmt.Println(err61)

Documentation

Index

Constants

View Source
const (
	Charset           = "utf8mb4"
	Collation         = "utf8mb4_general_ci"
	Proto             = "tcp"
	Port              = 3306
	Write             = 1              // 主库,写库
	Read              = 2              // 从库,只读库
	Backup            = 4              // 备库,复杂查询
	EmptyResult       = "empty result" // 查询结果空
	AggregateAlias    = "aggregate"    // 统计字段别名
	ShardingSeparator = "_"            // 拼接库名和分库数
)
View Source
const (
	ProfileId                = "id"
	ProfileShardingFirst     = "sharding_first"
	ProfileShardingLast      = "sharding_last"
	ProfileShardingSeparator = "sharding_separator"
	ProfileWrite             = "write"
	ProfileRead              = "read"
	ProfileBackup            = "backup"
	ProfileHost              = "host"
	ProfileUsername          = "username"
	ProfilePassword          = "password"
	ProfileDriver            = "driver"
	ProfileProto             = "proto"
	ProfilePort              = "port"
	ProfileDatabase          = "database"
	ProfileCharset           = "charset"
	ProfileCollation         = "collation"
	ProfileTimeout           = "timeout"
	ProfileMaxOpen           = "max_open"
	ProfileMaxIdle           = "max_idle"
	ProfileMaxLifetime       = "max_lifetime"
	ProfileDsn               = "dsn"
)

Variables

This section is empty.

Functions

func Aggregate

func Aggregate(driver *Driver, query string, args ...interface{}) (result interface{}, err error, closeErr error)

"AS 'aggregate'" must be contained in Query

func AggregateFloat

func AggregateFloat(driver *Driver, query string, args ...interface{}) (result float64, err error, closeErr error)

"AS 'aggregate'" must be contained in Query

func AggregateInt

func AggregateInt(driver *Driver, query string, args ...interface{}) (result int64, err error, closeErr error)

"AS 'aggregate'" must be contained in Query

func CheckMode

func CheckMode(num int) error

func DsnJoiner

func DsnJoiner(s *Schema) string

func DsnPath

func DsnPath(s *Schema) string

func DsnQuery

func DsnQuery(s *Schema) string

func Exec

func Exec(driver *Driver, query string, args ...interface{}) (sql.Result, error)

func Find

func Find(driver *Driver, query string, args ...interface{}) (result []map[string]interface{}, err error, closeErr error)

func First

func First(driver *Driver, query string, args ...interface{}) (result map[string]interface{}, err error, closeErr error)

func IsBackup

func IsBackup(mode int) bool

func IsEmptyResult

func IsEmptyResult(err error) bool

func IsMode

func IsMode(num int) bool

func IsRead

func IsRead(mode int) bool

func IsWrite

func IsWrite(mode int) bool

func NewEmptyResult

func NewEmptyResult() error

func Scan

func Scan(rows *sql.Rows) ([]map[string]interface{}, error)

func ScanFirst

func ScanFirst(rows *sql.Rows) (map[string]interface{}, error)

func ShardingJoiner

func ShardingJoiner(database string, num int) string

Types

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

func (*Driver) Close

func (x *Driver) Close() error

func (*Driver) GetDb

func (x *Driver) GetDb() *sql.DB

func (*Driver) GetDsn

func (x *Driver) GetDsn() string

func (*Driver) GetName

func (x *Driver) GetName() string

func (*Driver) GetSchema

func (x *Driver) GetSchema() *Schema

type DriverBuilder

type DriverBuilder struct {
	// contains filtered or unexported fields
}

func (*DriverBuilder) Build

func (x *DriverBuilder) Build() (*Driver, error)

func (*DriverBuilder) SetJoiner

func (x *DriverBuilder) SetJoiner(f func(s *Schema) string) *DriverBuilder

func (*DriverBuilder) SetName

func (x *DriverBuilder) SetName(s string) *DriverBuilder

func (*DriverBuilder) SetSchema

func (x *DriverBuilder) SetSchema(s *Schema) *DriverBuilder

type Drivers

type Drivers struct {
	// contains filtered or unexported fields
}

func (*Drivers) Close

func (x *Drivers) Close() []error

关闭全部sql.DB

func (*Drivers) GetBackup

func (x *Drivers) GetBackup() (*Driver, error)

备库列表,随机取一个Driver

func (*Drivers) GetBackups

func (x *Drivers) GetBackups() []*Driver

func (*Drivers) GetId

func (x *Drivers) GetId() string

func (*Drivers) GetReader

func (x *Drivers) GetReader() (*Driver, error)

从库列表,随机取一个Driver

func (*Drivers) GetReaders

func (x *Drivers) GetReaders() []*Driver

func (*Drivers) GetWriter

func (x *Drivers) GetWriter() (*Driver, error)

主库列表,随机取一个Driver

func (*Drivers) GetWriters

func (x *Drivers) GetWriters() []*Driver

type DriversBuilder

type DriversBuilder struct {
	// contains filtered or unexported fields
}

func (*DriversBuilder) AddBackup

func (x *DriversBuilder) AddBackup(s *Schema) error

func (*DriversBuilder) AddProfile

func (x *DriversBuilder) AddProfile(p *Profile) error

func (*DriversBuilder) AddReader

func (x *DriversBuilder) AddReader(s *Schema) error

func (*DriversBuilder) AddSchema

func (x *DriversBuilder) AddSchema(s *Schema, write bool, read bool, backup bool) error

func (*DriversBuilder) AddWriter

func (x *DriversBuilder) AddWriter(s *Schema) error

func (*DriversBuilder) Build

func (x *DriversBuilder) Build() (*Drivers, error)

func (*DriversBuilder) SetDsnJoiner

func (x *DriversBuilder) SetDsnJoiner(f func(s *Schema) string) *DriversBuilder

func (*DriversBuilder) SetId

func (x *DriversBuilder) SetId(s string) error

func (*DriversBuilder) SetName

func (x *DriversBuilder) SetName(s string) error

type Profile

type Profile struct {
	// contains filtered or unexported fields
}

func NewProfile

func NewProfile(data map[string]string) (*Profile, error)

func (*Profile) GetBackup

func (x *Profile) GetBackup() bool

func (*Profile) GetCharset

func (x *Profile) GetCharset() string

func (*Profile) GetCollation

func (x *Profile) GetCollation() string

func (*Profile) GetDatabase

func (x *Profile) GetDatabase() string

func (*Profile) GetDriver

func (x *Profile) GetDriver() string

func (*Profile) GetDsn

func (x *Profile) GetDsn() string

func (*Profile) GetHost

func (x *Profile) GetHost() string

func (*Profile) GetId

func (x *Profile) GetId() string

func (*Profile) GetMaxIdle

func (x *Profile) GetMaxIdle() int

func (*Profile) GetMaxLifetime

func (x *Profile) GetMaxLifetime() time.Duration

func (*Profile) GetMaxOpen

func (x *Profile) GetMaxOpen() int

func (*Profile) GetPassword

func (x *Profile) GetPassword() string

func (*Profile) GetPort

func (x *Profile) GetPort() int

func (*Profile) GetProto

func (x *Profile) GetProto() string

func (*Profile) GetRead

func (x *Profile) GetRead() bool

func (*Profile) GetShardingFirst

func (x *Profile) GetShardingFirst() int

func (*Profile) GetShardingLast

func (x *Profile) GetShardingLast() int

func (*Profile) GetShardingSeparator

func (x *Profile) GetShardingSeparator() string

func (*Profile) GetTimeout

func (x *Profile) GetTimeout() string

func (*Profile) GetUsername

func (x *Profile) GetUsername() string

func (*Profile) GetWrite

func (x *Profile) GetWrite() bool

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

func NewSchema

func NewSchema(p *Profile) (*Schema, error)

func (*Schema) GetCharset

func (x *Schema) GetCharset() string

func (*Schema) GetCollation

func (x *Schema) GetCollation() string

func (*Schema) GetDatabase

func (x *Schema) GetDatabase() string

func (*Schema) GetDsn

func (x *Schema) GetDsn() string

func (*Schema) GetHost

func (x *Schema) GetHost() string

func (*Schema) GetMaxIdle

func (x *Schema) GetMaxIdle() int

func (*Schema) GetMaxLifetime

func (x *Schema) GetMaxLifetime() time.Duration

func (*Schema) GetMaxOpen

func (x *Schema) GetMaxOpen() int

func (*Schema) GetPassword

func (x *Schema) GetPassword() string

func (*Schema) GetPort

func (x *Schema) GetPort() int

func (*Schema) GetProto

func (x *Schema) GetProto() string

func (*Schema) GetTimeout

func (x *Schema) GetTimeout() string

func (*Schema) GetUsername

func (x *Schema) GetUsername() string

func (*Schema) ToString

func (x *Schema) ToString() string

type SchemaBuilder

type SchemaBuilder struct {
	// contains filtered or unexported fields
}

func (*SchemaBuilder) Build

func (x *SchemaBuilder) Build() (*Schema, error)

func (*SchemaBuilder) SetCharset

func (x *SchemaBuilder) SetCharset(s string) *SchemaBuilder

func (*SchemaBuilder) SetCollation

func (x *SchemaBuilder) SetCollation(s string) *SchemaBuilder

func (*SchemaBuilder) SetDatabase

func (x *SchemaBuilder) SetDatabase(s string) *SchemaBuilder

func (*SchemaBuilder) SetDsn

func (x *SchemaBuilder) SetDsn(s string) *SchemaBuilder

func (*SchemaBuilder) SetHost

func (x *SchemaBuilder) SetHost(s string) *SchemaBuilder

func (*SchemaBuilder) SetMaxIdle

func (x *SchemaBuilder) SetMaxIdle(n int) *SchemaBuilder

func (*SchemaBuilder) SetMaxLifetime

func (x *SchemaBuilder) SetMaxLifetime(d time.Duration) *SchemaBuilder

func (*SchemaBuilder) SetMaxOpen

func (x *SchemaBuilder) SetMaxOpen(n int) *SchemaBuilder

func (*SchemaBuilder) SetPassword

func (x *SchemaBuilder) SetPassword(s string) *SchemaBuilder

func (*SchemaBuilder) SetPort

func (x *SchemaBuilder) SetPort(p int) *SchemaBuilder

func (*SchemaBuilder) SetProto

func (x *SchemaBuilder) SetProto(s string) *SchemaBuilder

func (*SchemaBuilder) SetTimeout

func (x *SchemaBuilder) SetTimeout(s string) *SchemaBuilder

func (*SchemaBuilder) SetUsername

func (x *SchemaBuilder) SetUsername(s string) *SchemaBuilder

type Sharding

type Sharding struct {
	// contains filtered or unexported fields
}

func (*Sharding) Close

func (x *Sharding) Close() []error

关闭全部sql.DB

func (*Sharding) GetDriver

func (x *Sharding) GetDriver(num int) (*Driver, error)

取第n个库

func (*Sharding) GetDrivers

func (x *Sharding) GetDrivers() []*Driver

func (*Sharding) GetId

func (x *Sharding) GetId() string

func (*Sharding) GetSize

func (x *Sharding) GetSize() int

type ShardingBuilder

type ShardingBuilder struct {
	// contains filtered or unexported fields
}

func (*ShardingBuilder) AddProfile

func (x *ShardingBuilder) AddProfile(p *Profile) error

func (*ShardingBuilder) Build

func (x *ShardingBuilder) Build() (*Sharding, error)

func (*ShardingBuilder) SetDsnJoiner

func (x *ShardingBuilder) SetDsnJoiner(f func(s *Schema) string) *ShardingBuilder

func (*ShardingBuilder) SetId

func (x *ShardingBuilder) SetId(s string) error

func (*ShardingBuilder) SetName

func (x *ShardingBuilder) SetName(s string) error

func (*ShardingBuilder) SetSchema

func (x *ShardingBuilder) SetSchema(num int, s *Schema) error

func (*ShardingBuilder) SetShardingJoiner

func (x *ShardingBuilder) SetShardingJoiner(f func(database string, num int) string) *ShardingBuilder

Jump to

Keyboard shortcuts

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