db

package
v1.20.100 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: Zlib Imports: 26 Imported by: 0

Documentation

Overview

*

  • @author huqiuyun
  • 数据Elasticsearch

*

  • @author huqiuyun
  • 数据服务

Index

Constants

This section is empty.

Variables

View Source
var DefaultDnsFunc = func(cnf *config.Database, tls, serverPubKey bool) string {
	if cnf == nil {
		return ""
	}
	if cnf.DriverType == "mysql" {
		if cnf.Charset == "" {
			cnf.Charset = "utf8mb4"
		}
		dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local", cnf.User, cnf.Pwd, cnf.Host, cnf.Port, cnf.Name, cnf.Charset)
		if tls {
			dsn += "&tls=" + url.QueryEscape(cnf.Tls.Key)
		}
		if serverPubKey {
			dsn += "&serverPubKey=" + url.QueryEscape(cnf.ServerPubKey.Key)
		}
		return dsn
	} else if cnf.DriverType == "postgres" {
		return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s", cnf.Host, cnf.Port, cnf.User, cnf.Pwd, cnf.Name)
	} else if cnf.DriverType == "sqlite3" {
		return cnf.Name
	} else if cnf.DriverType == "sql" {
		return fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s", cnf.User, cnf.Pwd, cnf.Host, cnf.Port, cnf.Name)
	}
	return ""
}
View Source
var DefaultTlsFunc = func(cnf *config.Database) (*tls.Config, error) {
	if strings.EqualFold(cnf.Tls.Key, "true") ||
		strings.EqualFold(cnf.Tls.Key, "false") ||
		strings.EqualFold(cnf.Tls.Key, "skip-verify") ||
		strings.EqualFold(cnf.Tls.Key, "preferred") {
		return nil, nil
	}
	cert, rootCertPool, err := daotls.GetCertificate(&cnf.Tls)
	if err != nil {
		return nil, err
	}
	return &tls.Config{
		RootCAs:      rootCertPool,
		Certificates: []tls.Certificate{cert},
	}, nil
}

Functions

func AddDriver added in v1.6.1

func AddDriver(driverName string, dial DialFunc)

func UseServerPubKey added in v1.9.15

func UseServerPubKey(cnf *config.Database, opts ...option.Option) error

func UseTls added in v1.9.15

func UseTls(cnf *config.Database, opts ...option.Option) error

func WithDnsOption added in v1.9.15

func WithDnsOption(f func(cnf *config.Database, tls, pubKey bool) string) option.Option

func WithServerPubKeyOption added in v1.9.15

func WithServerPubKeyOption(f func(cnf *config.Database) (*rsa.PublicKey, error)) option.Option

func WithTlsOption added in v1.9.15

func WithTlsOption(f func(cnf *config.Database) (*tls.Config, error)) option.Option

Types

type Adapter

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

Adapter represents the Gorm adapter for policy storage.

func NewAdapter

func NewAdapter(cfg *config.Sql, opts ...option.Option) (*Adapter, error)

NewAdapter is the constructor for Adapter.

func (*Adapter) Close

func (a *Adapter) Close()

func (*Adapter) Db

func (a *Adapter) Db() *gorm.DB

func (*Adapter) Name

func (a *Adapter) Name() string

type ConditionCallback

type ConditionCallback func(ctx context.Context, data interface{}, db *gorm.DB) *gorm.DB

type DialFunc added in v1.6.1

type DialFunc func(string) gorm.Dialector

type EsDatabase

type EsDatabase interface {
	Get(dbname string) (*elastic.Client, error)
	Do(name string, call func(es *elastic.Client) error) error
	Close(name string) error
	CloseAll()
	Select(name string)
}

func NewES

func NewES(conf []config.Elastic) (EsDatabase, error)

type Logger added in v0.5.27

type Logger struct {
	config.SqlLogger
}

func (*Logger) Error added in v0.5.30

func (l *Logger) Error(ctx context.Context, msg string, data ...interface{})

Error print error messages

func (*Logger) Info added in v0.5.30

func (l *Logger) Info(ctx context.Context, msg string, data ...interface{})

Info print info

func (*Logger) LogMode added in v0.5.30

func (l *Logger) LogMode(level gormLogger.LogLevel) gormLogger.Interface

LogMode log mode

func (*Logger) Trace added in v0.5.30

func (l *Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)

Trace print sql message

func (*Logger) Warn added in v0.5.30

func (l *Logger) Warn(ctx context.Context, msg string, data ...interface{})

Warn print warn messages

type ScopesFunc

type ScopesFunc func(db *gorm.DB) *gorm.DB

type ServerPubKeyFunc added in v1.9.15

type ServerPubKeyFunc func(cnf *config.Database) (*rsa.PublicKey, error)

func (ServerPubKeyFunc) String added in v1.9.15

func (t ServerPubKeyFunc) String() string

func (ServerPubKeyFunc) Type added in v1.9.15

func (t ServerPubKeyFunc) Type() int

func (ServerPubKeyFunc) Value added in v1.9.15

func (t ServerPubKeyFunc) Value() interface{}

type Sql

type Sql interface {
	Db() *gorm.DB
	Name() string
}

type SqlDatabase

type SqlDatabase interface {
	Get(dbname string) (Sql, error)
	Do(dbname string, c func(db Sql) error) error
	Close(dbname string)
	CloseAll()
	Select(name string)
}

func NewDB

func NewDB(cfgs []config.Sql, opts ...option.Option) (SqlDatabase, error)

Jump to

Keyboard shortcuts

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