database

package
v0.25.3 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-03 20:55:05 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-04-28 11:16:58 * @FilePath: \go-config\pkg\database\cockroachdb.go * @Description: CockroachDB统一配置管理 * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-03 20:55:05 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-12 22:50:17 * @FilePath: \go-config\pkg\database\database.go * @Description: 数据库统一配置管理 * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-12 23:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-22 16:54:04 * @FilePath: \go-config\pkg\database\mysql.go * @Description: MySQL数据库配置 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-12 23:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-22 16:53:36 * @FilePath: \go-config\pkg\database\postgresql.go * @Description: PostgreSQL数据库配置 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-12 23:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-16 19:03:40 * @FilePath: \go-config\pkg\database\sqlite.go * @Description: SQLite数据库配置 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetProviderName

func GetProviderName(dbType DBType) string

GetProviderName 获取提供商显示名称

Types

type CockroachDB

type CockroachDB struct {
	ModuleName                               string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"`
	Host                                     string `mapstructure:"host" yaml:"host" json:"host" validate:"required"`
	Port                                     string `mapstructure:"port" yaml:"port" json:"port" validate:"required"`
	Config                                   string `mapstructure:"config" yaml:"config" json:"config" validate:"required"`
	LogLevel                                 string `mapstructure:"log-level" yaml:"log-level" json:"logLevel" validate:"required"`
	SlowThreshold                            int    `mapstructure:"slow-threshold" yaml:"slow-threshold" json:"slowThreshold"`
	IgnoreRecordNotFoundError                bool   `mapstructure:"ignore-record-not-found-error" yaml:"ignore-record-not-found-error" json:"ignoreRecordNotFoundError"`
	Dbname                                   string `mapstructure:"db-name" yaml:"db-name" json:"dbName" validate:"required"`
	Username                                 string `mapstructure:"username" yaml:"username" json:"username" validate:"required"`
	Password                                 string `mapstructure:"password" yaml:"password" json:"password" validate:"required"`
	MaxIdleConns                             int    `mapstructure:"max-idle-conns" yaml:"max-idle-conns" json:"maxIdleConns" validate:"min=0"`
	MaxOpenConns                             int    `mapstructure:"max-open-conns" yaml:"max-open-conns" json:"maxOpenConns" validate:"min=0"`
	ConnMaxIdleTime                          int    `mapstructure:"conn-max-idle-time" yaml:"conn-max-idle-time" json:"connMaxIdleTime" validate:"min=0"`
	ConnMaxLifeTime                          int    `mapstructure:"conn-max-life-time" yaml:"conn-max-life-time" json:"connMaxLifeTime" validate:"min=0"`
	SkipDefaultTransaction                   bool   `mapstructure:"skip-default-transaction" yaml:"skip-default-transaction" json:"skipDefaultTransaction"`
	PrepareStmt                              bool   `mapstructure:"prepare-stmt" yaml:"prepare-stmt" json:"prepareStmt"`
	DisableForeignKeyConstraintWhenMigrating bool   `` /* 161-byte string literal not displayed */
	DisableNestedTransaction                 bool   `mapstructure:"disable-nested-transaction" yaml:"disable-nested-transaction" json:"disableNestedTransaction"`
	AllowGlobalUpdate                        bool   `mapstructure:"allow-global-update" yaml:"allow-global-update" json:"allowGlobalUpdate"`
	QueryFields                              bool   `mapstructure:"query-fields" yaml:"query-fields" json:"queryFields"`
	CreateBatchSize                          int    `mapstructure:"create-batch-size" yaml:"create-batch-size" json:"createBatchSize"`
	SingularTable                            bool   `mapstructure:"singular-table" yaml:"singular-table" json:"singularTable"`
}

func DefaultCockroachDB

func DefaultCockroachDB() *CockroachDB

func NewCockroachDBConfig

func NewCockroachDBConfig(opt *CockroachDB) *CockroachDB

func (*CockroachDB) Clone

func (c *CockroachDB) Clone() internal.Configurable

func (*CockroachDB) Get

func (c *CockroachDB) Get() interface{}

func (*CockroachDB) GetAllowGlobalUpdate

func (c *CockroachDB) GetAllowGlobalUpdate() bool

func (*CockroachDB) GetConfig

func (c *CockroachDB) GetConfig() string

func (*CockroachDB) GetCreateBatchSize

func (c *CockroachDB) GetCreateBatchSize() int

func (*CockroachDB) GetDBName

func (c *CockroachDB) GetDBName() string

func (*CockroachDB) GetDBType

func (c *CockroachDB) GetDBType() DBType

func (*CockroachDB) GetDisableForeignKeyConstraintWhenMigrating

func (c *CockroachDB) GetDisableForeignKeyConstraintWhenMigrating() bool

func (*CockroachDB) GetDisableNestedTransaction

func (c *CockroachDB) GetDisableNestedTransaction() bool

func (*CockroachDB) GetHost

func (c *CockroachDB) GetHost() string

func (*CockroachDB) GetIgnoreRecordNotFoundError

func (c *CockroachDB) GetIgnoreRecordNotFoundError() bool

func (*CockroachDB) GetLogLevel

func (c *CockroachDB) GetLogLevel() string

func (*CockroachDB) GetModuleName

func (c *CockroachDB) GetModuleName() string

func (*CockroachDB) GetPassword

func (c *CockroachDB) GetPassword() string

func (*CockroachDB) GetPort

func (c *CockroachDB) GetPort() string

func (*CockroachDB) GetPrepareStmt

func (c *CockroachDB) GetPrepareStmt() bool

func (*CockroachDB) GetQueryFields

func (c *CockroachDB) GetQueryFields() bool

func (*CockroachDB) GetSingularTable

func (c *CockroachDB) GetSingularTable() bool

func (*CockroachDB) GetSkipDefaultTransaction

func (c *CockroachDB) GetSkipDefaultTransaction() bool

func (*CockroachDB) GetSlowThreshold

func (c *CockroachDB) GetSlowThreshold() int

func (*CockroachDB) GetUsername

func (c *CockroachDB) GetUsername() string

func (*CockroachDB) Set

func (c *CockroachDB) Set(data interface{})

func (*CockroachDB) SetCredentials

func (c *CockroachDB) SetCredentials(username, password string)

func (*CockroachDB) SetDBName

func (c *CockroachDB) SetDBName(dbName string)

func (*CockroachDB) SetHost

func (c *CockroachDB) SetHost(host string)

func (*CockroachDB) SetPort

func (c *CockroachDB) SetPort(port string)

func (*CockroachDB) Validate

func (c *CockroachDB) Validate() error

func (*CockroachDB) WithConfig

func (c *CockroachDB) WithConfig(config string) *CockroachDB

func (*CockroachDB) WithConnMaxIdleTime

func (c *CockroachDB) WithConnMaxIdleTime(connMaxIdleTime int) *CockroachDB

func (*CockroachDB) WithConnMaxLifeTime

func (c *CockroachDB) WithConnMaxLifeTime(connMaxLifeTime int) *CockroachDB

func (*CockroachDB) WithDbname

func (c *CockroachDB) WithDbname(dbname string) *CockroachDB

func (*CockroachDB) WithHost

func (c *CockroachDB) WithHost(host string) *CockroachDB

func (*CockroachDB) WithLogLevel

func (c *CockroachDB) WithLogLevel(logLevel string) *CockroachDB

func (*CockroachDB) WithMaxIdleConns

func (c *CockroachDB) WithMaxIdleConns(maxIdleConns int) *CockroachDB

func (*CockroachDB) WithMaxOpenConns

func (c *CockroachDB) WithMaxOpenConns(maxOpenConns int) *CockroachDB

func (*CockroachDB) WithModuleName

func (c *CockroachDB) WithModuleName(moduleName string) *CockroachDB

func (*CockroachDB) WithPassword

func (c *CockroachDB) WithPassword(password string) *CockroachDB

func (*CockroachDB) WithPort

func (c *CockroachDB) WithPort(port string) *CockroachDB

func (*CockroachDB) WithUsername

func (c *CockroachDB) WithUsername(username string) *CockroachDB

type DBType

type DBType string

DBType 定义数据库类型

const (
	DBTypeMySQL       DBType = "mysql"
	DBTypePostgreSQL  DBType = "postgres"
	DBTypeSQLite      DBType = "sqlite"
	DBTypeCockroachDB DBType = "cockroachdb"
)

func GetSupportedTypes

func GetSupportedTypes() []DBType

GetSupportedTypes 获取所有支持的数据库类型

func ParseDBType

func ParseDBType(typeStr string) (DBType, error)

ParseDBType 解析数据库类型字符串

type Database

type Database struct {
	Type        DBType       `mapstructure:"type" yaml:"type" json:"type"`                      // 数据库类型
	Enabled     bool         `mapstructure:"enabled" yaml:"enabled" json:"enabled"`             // 是否启用
	Default     string       `mapstructure:"default" yaml:"default" json:"default"`             // 默认使用的数据库
	MySQL       *MySQL       `mapstructure:"mysql" yaml:"mysql" json:"mysql"`                   // MySQL配置
	PostgreSQL  *PostgreSQL  `mapstructure:"postgresql" yaml:"postgresql" json:"postgresql"`    // PostgreSQL配置
	SQLite      *SQLite      `mapstructure:"sqlite" yaml:"sqlite" json:"sqlite"`                // SQLite配置
	CockroachDB *CockroachDB `mapstructure:"cockroachdb" yaml:"cockroachdb" json:"cockroachdb"` // CockroachDB配置
}

Database 数据库统一配置结构

func DefaultDatabaseConfig

func DefaultDatabaseConfig() *Database

DefaultDatabaseConfig 返回默认的数据库配置

func NewDatabase

func NewDatabase() *Database

NewDatabase 创建新的数据库配置管理器

func (*Database) AfterLoad

func (c *Database) AfterLoad() error

AfterLoad 配置加载后的钩子

func (*Database) BeforeLoad

func (c *Database) BeforeLoad() error

BeforeLoad 配置加载前的钩子

func (*Database) Clone

func (c *Database) Clone() internal.Configurable

Clone 返回Database配置的副本

func (*Database) EnableCockroachDB

func (d *Database) EnableCockroachDB() *Database

EnableCockroachDB 启用CockroachDB并设置默认配置

func (*Database) EnableDatabase

func (d *Database) EnableDatabase() *Database

EnableDatabase 启用数据库

func (*Database) EnableMySQL

func (d *Database) EnableMySQL() *Database

EnableMySQL 启用MySQL并设置默认配置

func (*Database) EnablePostgreSQL

func (d *Database) EnablePostgreSQL() *Database

EnablePostgreSQL 启用PostgreSQL并设置默认配置

func (*Database) EnableSQLite

func (d *Database) EnableSQLite() *Database

EnableSQLite 启用SQLite并设置默认配置

func (*Database) EnsureDefaults

func (c *Database) EnsureDefaults()

EnsureDefaults 确保所有子配置都有默认值,避免空指针

func (*Database) Get

func (c *Database) Get() interface{}

Get 返回Database配置

func (*Database) GetDefaultProvider

func (c *Database) GetDefaultProvider() (DatabaseProvider, error)

GetDefaultProvider 获取默认的数据库提供商

func (*Database) GetProvider

func (c *Database) GetProvider(dbType DBType) (DatabaseProvider, error)

GetProvider 获取指定类型的数据库提供商

func (*Database) GetSafe

func (c *Database) GetSafe() interface{}

GetSafe 安全获取配置,确保所有字段都有默认值

func (*Database) ListAvailableProviders

func (c *Database) ListAvailableProviders() []DBType

ListAvailableProviders 列出所有可用的数据库提供商

func (*Database) Set

func (c *Database) Set(data interface{})

Set 设置Database配置

func (*Database) SetDefaultProvider

func (c *Database) SetDefaultProvider(dbType DBType)

SetDefaultProvider 设置默认数据库提供商

func (*Database) Validate

func (c *Database) Validate() error

Validate 验证Database配置的有效性

func (*Database) ValidateAll

func (c *Database) ValidateAll() error

ValidateAll 验证所有配置的提供商

func (*Database) ValidateProvider

func (c *Database) ValidateProvider(dbType DBType) error

ValidateProvider 验证指定提供商的配置

func (*Database) WithCockroachDB

func (d *Database) WithCockroachDB(cockroachdb *CockroachDB) *Database

WithCockroachDB 设置CockroachDB配置

func (*Database) WithDefault

func (d *Database) WithDefault(defaultDB string) *Database

WithDefault 设置默认数据库

func (*Database) WithEnabled

func (d *Database) WithEnabled(enabled bool) *Database

WithEnabled 设置是否启用数据库

func (*Database) WithMySQL

func (d *Database) WithMySQL(mysql *MySQL) *Database

WithMySQL 设置MySQL配置

func (*Database) WithPostgreSQL

func (d *Database) WithPostgreSQL(postgresql *PostgreSQL) *Database

WithPostgreSQL 设置PostgreSQL配置

func (*Database) WithSQLite

func (d *Database) WithSQLite(sqlite *SQLite) *Database

WithSQLite 设置SQLite配置

func (*Database) WithType

func (d *Database) WithType(dbType DBType) *Database

WithType 设置数据库类型

type DatabaseProvider

type DatabaseProvider interface {
	internal.Configurable

	// GetDBType 获取数据库类型
	GetDBType() DBType

	// GetHost 获取主机地址
	GetHost() string

	// GetPort 获取端口
	GetPort() string

	// GetDBName 获取数据库名称
	GetDBName() string

	// GetUsername 获取用户名
	GetUsername() string

	// GetPassword 获取密码
	GetPassword() string

	// GetConfig 获取额外配置
	GetConfig() string

	// GetModuleName 获取模块名称
	GetModuleName() string

	// GetSlowThreshold 获取慢查询阈值(毫秒)
	GetSlowThreshold() int

	// GetLogLevel 获取SQL日志等级(silent/error/warn/info,gorm logger 消费)
	GetLogLevel() string

	// GetIgnoreRecordNotFoundError 获取是否忽略RecordNotFound错误
	GetIgnoreRecordNotFoundError() bool

	// GORM 性能配置 Getter 方法
	// GetSkipDefaultTransaction 获取是否跳过默认事务
	GetSkipDefaultTransaction() bool

	// GetPrepareStmt 获取是否启用预编译语句
	GetPrepareStmt() bool

	// GetDisableForeignKeyConstraintWhenMigrating 获取迁移时是否禁用外键约束
	GetDisableForeignKeyConstraintWhenMigrating() bool

	// GetDisableNestedTransaction 获取是否禁用嵌套事务
	GetDisableNestedTransaction() bool

	// GetAllowGlobalUpdate 获取是否允许全局更新
	GetAllowGlobalUpdate() bool

	// GetQueryFields 获取是否在查询时选择所有字段
	GetQueryFields() bool

	// GetCreateBatchSize 获取批量创建的大小
	GetCreateBatchSize() int

	// GetSingularTable 获取是否使用单数表名
	GetSingularTable() bool

	// SetCredentials 设置凭证
	SetCredentials(username, password string)

	// SetHost 设置主机地址
	SetHost(host string)

	// SetPort 设置端口
	SetPort(port string)

	// SetDBName 设置数据库名称
	SetDBName(dbName string)
}

DatabaseProvider 数据库提供商接口,统一不同数据库的操作

type MySQL

type MySQL struct {
	ModuleName                               string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"`                                                    // 模块名称
	Host                                     string `mapstructure:"host" yaml:"host" json:"host"            validate:"required"`                                         // 数据库 IP 地址
	Port                                     string `mapstructure:"port" yaml:"port" json:"port"            validate:"required"`                                         // 端口
	Config                                   string `mapstructure:"config" yaml:"config" json:"config"          validate:"required"`                                     // 后缀配置 默认配置 charset=utf8mb4&parseTime=True&loc=Local
	LogLevel                                 string `mapstructure:"log-level" yaml:"log-level" json:"logLevel"       validate:"required"`                                // SQL 日志等级
	SlowThreshold                            int    `mapstructure:"slow-threshold" yaml:"slow-threshold" json:"slowThreshold"`                                           // 慢查询阈值(毫秒)
	IgnoreRecordNotFoundError                bool   `mapstructure:"ignore-record-not-found-error" yaml:"ignore-record-not-found-error" json:"ignoreRecordNotFoundError"` // 是否忽略ErrRecordNotFound错误
	Dbname                                   string `mapstructure:"db-name" yaml:"db-name" json:"dbName"         validate:"required"`                                    // 数据库名称
	Username                                 string `mapstructure:"username" yaml:"username" json:"username"        validate:"required"`                                 // 数据库用户名
	Password                                 string `mapstructure:"password" yaml:"password" json:"password"        validate:"required"`                                 // 数据库密码
	MaxIdleConns                             int    `mapstructure:"max-idle-conns" yaml:"max-idle-conns" json:"maxIdleConns"  validate:"min=0"`                          // 最大空闲连接数
	MaxOpenConns                             int    `mapstructure:"max-open-conns" yaml:"max-open-conns" json:"maxOpenConns"  validate:"min=0"`                          // 最大连接数
	ConnMaxIdleTime                          int    `mapstructure:"conn-max-idle-time" yaml:"conn-max-idle-time" json:"connMaxIdleTime" validate:"min=0"`                // 连接最大空闲时间 单位:秒
	ConnMaxLifeTime                          int    `mapstructure:"conn-max-life-time" yaml:"conn-max-life-time" json:"connMaxLifeTime" validate:"min=0"`                // 连接最大生命周期 单位:秒
	SkipDefaultTransaction                   bool   `mapstructure:"skip-default-transaction" yaml:"skip-default-transaction" json:"skipDefaultTransaction"`              // 跳过默认事务
	PrepareStmt                              bool   `mapstructure:"prepare-stmt" yaml:"prepare-stmt" json:"prepareStmt"`                                                 // 预编译语句
	DisableForeignKeyConstraintWhenMigrating bool   ``                                                                                                                   // 禁用自动创建外键约束
	/* 161-byte string literal not displayed */
	DisableNestedTransaction bool `mapstructure:"disable-nested-transaction" yaml:"disable-nested-transaction" json:"disableNestedTransaction"` // 禁用嵌套事务
	AllowGlobalUpdate        bool `mapstructure:"allow-global-update" yaml:"allow-global-update" json:"allowGlobalUpdate"`                      // 允许全局更新
	QueryFields              bool `mapstructure:"query-fields" yaml:"query-fields" json:"queryFields"`                                          // 执行查询时选择所有字段
	CreateBatchSize          int  `mapstructure:"create-batch-size" yaml:"create-batch-size" json:"createBatchSize"`                            // 批量创建大小
	SingularTable            bool `mapstructure:"singular-table" yaml:"singular-table" json:"singularTable"`                                    // 使用单数表名
}

MySQL MySQL数据库配置

func DefaultMySQL

func DefaultMySQL() *MySQL

DefaultMySQL 创建默认MySQL配置

func NewMySQLConfig

func NewMySQLConfig(opt *MySQL) *MySQL

NewMySQLConfig 创建一个新的MySQL配置实例

func (*MySQL) Clone

func (m *MySQL) Clone() internal.Configurable

func (*MySQL) Get

func (m *MySQL) Get() interface{}

func (*MySQL) GetAllowGlobalUpdate

func (m *MySQL) GetAllowGlobalUpdate() bool

func (*MySQL) GetConfig

func (m *MySQL) GetConfig() string

func (*MySQL) GetCreateBatchSize

func (m *MySQL) GetCreateBatchSize() int

func (*MySQL) GetDBName

func (m *MySQL) GetDBName() string

func (*MySQL) GetDBType

func (m *MySQL) GetDBType() DBType

为MySQL配置实现DatabaseProvider接口

func (*MySQL) GetDisableForeignKeyConstraintWhenMigrating

func (m *MySQL) GetDisableForeignKeyConstraintWhenMigrating() bool

func (*MySQL) GetDisableNestedTransaction

func (m *MySQL) GetDisableNestedTransaction() bool

func (*MySQL) GetHost

func (m *MySQL) GetHost() string

func (*MySQL) GetIgnoreRecordNotFoundError

func (m *MySQL) GetIgnoreRecordNotFoundError() bool

func (*MySQL) GetLogLevel

func (m *MySQL) GetLogLevel() string

func (*MySQL) GetModuleName

func (m *MySQL) GetModuleName() string

func (*MySQL) GetPassword

func (m *MySQL) GetPassword() string

func (*MySQL) GetPort

func (m *MySQL) GetPort() string

func (*MySQL) GetPrepareStmt

func (m *MySQL) GetPrepareStmt() bool

func (*MySQL) GetQueryFields

func (m *MySQL) GetQueryFields() bool

func (*MySQL) GetSingularTable

func (m *MySQL) GetSingularTable() bool

func (*MySQL) GetSkipDefaultTransaction

func (m *MySQL) GetSkipDefaultTransaction() bool

func (*MySQL) GetSlowThreshold

func (m *MySQL) GetSlowThreshold() int

func (*MySQL) GetUsername

func (m *MySQL) GetUsername() string

func (*MySQL) Set

func (m *MySQL) Set(data interface{})

func (*MySQL) SetCredentials

func (m *MySQL) SetCredentials(username, password string)

func (*MySQL) SetDBName

func (m *MySQL) SetDBName(dbName string)

func (*MySQL) SetHost

func (m *MySQL) SetHost(host string)

func (*MySQL) SetPort

func (m *MySQL) SetPort(port string)

func (*MySQL) Validate

func (m *MySQL) Validate() error

func (*MySQL) WithConfig

func (m *MySQL) WithConfig(config string) *MySQL

WithConfig 设置数据库配置参数

func (*MySQL) WithConnMaxIdleTime

func (m *MySQL) WithConnMaxIdleTime(connMaxIdleTime int) *MySQL

WithConnMaxIdleTime 设置连接最大空闲时间

func (*MySQL) WithConnMaxLifeTime

func (m *MySQL) WithConnMaxLifeTime(connMaxLifeTime int) *MySQL

WithConnMaxLifeTime 设置连接最大生命周期

func (*MySQL) WithDbname

func (m *MySQL) WithDbname(dbname string) *MySQL

WithDbname 设置数据库名称

func (*MySQL) WithHost

func (m *MySQL) WithHost(host string) *MySQL

WithHost 设置数据库主机地址

func (*MySQL) WithLogLevel

func (m *MySQL) WithLogLevel(logLevel string) *MySQL

WithLogLevel 设置日志级别

func (*MySQL) WithMaxIdleConns

func (m *MySQL) WithMaxIdleConns(maxIdleConns int) *MySQL

WithMaxIdleConns 设置最大空闲连接数

func (*MySQL) WithMaxOpenConns

func (m *MySQL) WithMaxOpenConns(maxOpenConns int) *MySQL

WithMaxOpenConns 设置最大连接数

func (*MySQL) WithModuleName

func (m *MySQL) WithModuleName(moduleName string) *MySQL

WithModuleName 设置模块名称

func (*MySQL) WithPassword

func (m *MySQL) WithPassword(password string) *MySQL

WithPassword 设置数据库密码

func (*MySQL) WithPort

func (m *MySQL) WithPort(port string) *MySQL

WithPort 设置数据库端口

func (*MySQL) WithUsername

func (m *MySQL) WithUsername(username string) *MySQL

WithUsername 设置数据库用户名

type PostgreSQL

type PostgreSQL struct {
	ModuleName                               string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"`                                                    // 模块名称
	Host                                     string `mapstructure:"host" yaml:"host" json:"host"            validate:"required"`                                         // 数据库 IP 地址
	Port                                     string `mapstructure:"port" yaml:"port" json:"port"            validate:"required"`                                         // 端口
	Config                                   string `mapstructure:"config" yaml:"config" json:"config"          validate:"required"`                                     // 后缀配置
	LogLevel                                 string `mapstructure:"log-level" yaml:"log-level" json:"logLevel"       validate:"required"`                                // SQL 日志等级
	SlowThreshold                            int    `mapstructure:"slow-threshold" yaml:"slow-threshold" json:"slowThreshold"`                                           // 慢查询阈值(毫秒)
	IgnoreRecordNotFoundError                bool   `mapstructure:"ignore-record-not-found-error" yaml:"ignore-record-not-found-error" json:"ignoreRecordNotFoundError"` // 是否忽略ErrRecordNotFound错误
	Dbname                                   string `mapstructure:"db-name" yaml:"db-name" json:"dbName"         validate:"required"`                                    // 数据库名称
	Username                                 string `mapstructure:"username" yaml:"username" json:"username"        validate:"required"`                                 // 数据库用户名
	Password                                 string `mapstructure:"password" yaml:"password" json:"password"        validate:"required"`                                 // 数据库密码
	MaxIdleConns                             int    `mapstructure:"max-idle-conns" yaml:"max-idle-conns" json:"maxIdleConns"  validate:"min=0"`                          // 最大空闲连接数
	MaxOpenConns                             int    `mapstructure:"max-open-conns" yaml:"max-open-conns" json:"maxOpenConns"  validate:"min=0"`                          // 最大连接数
	ConnMaxIdleTime                          int    `mapstructure:"conn-max-idle-time" yaml:"conn-max-idle-time" json:"connMaxIdleTime" validate:"min=0"`                // 连接最大空闲时间 单位:秒
	ConnMaxLifeTime                          int    `mapstructure:"conn-max-life-time" yaml:"conn-max-life-time" json:"connMaxLifeTime" validate:"min=0"`                // 连接最大生命周期 单位:秒
	SkipDefaultTransaction                   bool   `mapstructure:"skip-default-transaction" yaml:"skip-default-transaction" json:"skipDefaultTransaction"`              // 跳过默认事务
	PrepareStmt                              bool   `mapstructure:"prepare-stmt" yaml:"prepare-stmt" json:"prepareStmt"`                                                 // 预编译语句
	DisableForeignKeyConstraintWhenMigrating bool   ``                                                                                                                   // 禁用自动创建外键约束
	/* 161-byte string literal not displayed */
	DisableNestedTransaction bool `mapstructure:"disable-nested-transaction" yaml:"disable-nested-transaction" json:"disableNestedTransaction"` // 禁用嵌套事务
	AllowGlobalUpdate        bool `mapstructure:"allow-global-update" yaml:"allow-global-update" json:"allowGlobalUpdate"`                      // 允许全局更新
	QueryFields              bool `mapstructure:"query-fields" yaml:"query-fields" json:"queryFields"`                                          // 执行查询时选择所有字段
	CreateBatchSize          int  `mapstructure:"create-batch-size" yaml:"create-batch-size" json:"createBatchSize"`                            // 批量创建大小
	SingularTable            bool `mapstructure:"singular-table" yaml:"singular-table" json:"singularTable"`                                    // 使用单数表名
}

PostgreSQL PostgreSQL数据库配置

func DefaultPostgreSQL

func DefaultPostgreSQL() *PostgreSQL

DefaultPostgreSQL 创建默认PostgreSQL配置

func NewPostgreSQLConfig

func NewPostgreSQLConfig(opt *PostgreSQL) *PostgreSQL

NewPostgreSQLConfig 创建一个新的PostgreSQL配置实例

func (*PostgreSQL) Clone

func (p *PostgreSQL) Clone() internal.Configurable

func (*PostgreSQL) Get

func (p *PostgreSQL) Get() interface{}

func (*PostgreSQL) GetAllowGlobalUpdate

func (p *PostgreSQL) GetAllowGlobalUpdate() bool

func (*PostgreSQL) GetConfig

func (p *PostgreSQL) GetConfig() string

func (*PostgreSQL) GetCreateBatchSize

func (p *PostgreSQL) GetCreateBatchSize() int

func (*PostgreSQL) GetDBName

func (p *PostgreSQL) GetDBName() string

func (*PostgreSQL) GetDBType

func (p *PostgreSQL) GetDBType() DBType

为PostgreSQL配置实现DatabaseProvider接口

func (*PostgreSQL) GetDisableForeignKeyConstraintWhenMigrating

func (p *PostgreSQL) GetDisableForeignKeyConstraintWhenMigrating() bool

func (*PostgreSQL) GetDisableNestedTransaction

func (p *PostgreSQL) GetDisableNestedTransaction() bool

func (*PostgreSQL) GetHost

func (p *PostgreSQL) GetHost() string

func (*PostgreSQL) GetIgnoreRecordNotFoundError

func (p *PostgreSQL) GetIgnoreRecordNotFoundError() bool

func (*PostgreSQL) GetLogLevel

func (p *PostgreSQL) GetLogLevel() string

func (*PostgreSQL) GetModuleName

func (p *PostgreSQL) GetModuleName() string

func (*PostgreSQL) GetPassword

func (p *PostgreSQL) GetPassword() string

func (*PostgreSQL) GetPort

func (p *PostgreSQL) GetPort() string

func (*PostgreSQL) GetPrepareStmt

func (p *PostgreSQL) GetPrepareStmt() bool

func (*PostgreSQL) GetQueryFields

func (p *PostgreSQL) GetQueryFields() bool

func (*PostgreSQL) GetSingularTable

func (p *PostgreSQL) GetSingularTable() bool

func (*PostgreSQL) GetSkipDefaultTransaction

func (p *PostgreSQL) GetSkipDefaultTransaction() bool

func (*PostgreSQL) GetSlowThreshold

func (p *PostgreSQL) GetSlowThreshold() int

func (*PostgreSQL) GetUsername

func (p *PostgreSQL) GetUsername() string

func (*PostgreSQL) Set

func (p *PostgreSQL) Set(data interface{})

func (*PostgreSQL) SetCredentials

func (p *PostgreSQL) SetCredentials(username, password string)

func (*PostgreSQL) SetDBName

func (p *PostgreSQL) SetDBName(dbName string)

func (*PostgreSQL) SetHost

func (p *PostgreSQL) SetHost(host string)

func (*PostgreSQL) SetPort

func (p *PostgreSQL) SetPort(port string)

func (*PostgreSQL) Validate

func (p *PostgreSQL) Validate() error

func (*PostgreSQL) WithConfig

func (p *PostgreSQL) WithConfig(config string) *PostgreSQL

WithConfig 设置数据库配置参数

func (*PostgreSQL) WithConnMaxIdleTime

func (p *PostgreSQL) WithConnMaxIdleTime(connMaxIdleTime int) *PostgreSQL

WithConnMaxIdleTime 设置连接最大空闲时间

func (*PostgreSQL) WithConnMaxLifeTime

func (p *PostgreSQL) WithConnMaxLifeTime(connMaxLifeTime int) *PostgreSQL

WithConnMaxLifeTime 设置连接最大生命周期

func (*PostgreSQL) WithDbname

func (p *PostgreSQL) WithDbname(dbname string) *PostgreSQL

WithDbname 设置数据库名称

func (*PostgreSQL) WithHost

func (p *PostgreSQL) WithHost(host string) *PostgreSQL

WithHost 设置数据库主机地址

func (*PostgreSQL) WithLogLevel

func (p *PostgreSQL) WithLogLevel(logLevel string) *PostgreSQL

WithLogLevel 设置日志级别

func (*PostgreSQL) WithMaxIdleConns

func (p *PostgreSQL) WithMaxIdleConns(maxIdleConns int) *PostgreSQL

WithMaxIdleConns 设置最大空闲连接数

func (*PostgreSQL) WithMaxOpenConns

func (p *PostgreSQL) WithMaxOpenConns(maxOpenConns int) *PostgreSQL

WithMaxOpenConns 设置最大连接数

func (*PostgreSQL) WithModuleName

func (p *PostgreSQL) WithModuleName(moduleName string) *PostgreSQL

WithModuleName 设置模块名称

func (*PostgreSQL) WithPassword

func (p *PostgreSQL) WithPassword(password string) *PostgreSQL

WithPassword 设置数据库密码

func (*PostgreSQL) WithPort

func (p *PostgreSQL) WithPort(port string) *PostgreSQL

WithPort 设置数据库端口

func (*PostgreSQL) WithUsername

func (p *PostgreSQL) WithUsername(username string) *PostgreSQL

WithUsername 设置数据库用户名

type SQLite

type SQLite struct {
	ModuleName                               string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"`                                                    // 模块名称
	Config                                   string `mapstructure:"config" yaml:"config" json:"config"          validate:"required"`                                     // 后缀配置
	LogLevel                                 string `mapstructure:"log-level" yaml:"log-level" json:"logLevel"       validate:"required"`                                // SQL 日志等级
	SlowThreshold                            int    `mapstructure:"slow-threshold" yaml:"slow-threshold" json:"slowThreshold"`                                           // 慢查询阈值(毫秒)
	IgnoreRecordNotFoundError                bool   `mapstructure:"ignore-record-not-found-error" yaml:"ignore-record-not-found-error" json:"ignoreRecordNotFoundError"` // 是否忽略ErrRecordNotFound错误
	DbPath                                   string `mapstructure:"db-path" yaml:"db-path" json:"dbPath"`                                                                // SQLite 文件存放位置
	Vacuum                                   bool   `mapstructure:"vacuum" yaml:"vacuum" json:"vacuum"`                                                                  // 是否执行清除命令
	MaxIdleConns                             int    `mapstructure:"max-idle-conns" yaml:"max-idle-conns" json:"maxIdleConns"  validate:"min=0"`                          // 最大空闲连接数
	MaxOpenConns                             int    `mapstructure:"max-open-conns" yaml:"max-open-conns" json:"maxOpenConns"  validate:"min=0"`                          // 最大连接数
	ConnMaxIdleTime                          int    `mapstructure:"conn-max-idle-time" yaml:"conn-max-idle-time" json:"connMaxIdleTime" validate:"min=0"`                // 连接最大空闲时间 单位:秒
	ConnMaxLifeTime                          int    `mapstructure:"conn-max-life-time" yaml:"conn-max-life-time" json:"connMaxLifeTime" validate:"min=0"`                // 连接最大生命周期 单位:秒
	SkipDefaultTransaction                   bool   `mapstructure:"skip-default-transaction" yaml:"skip-default-transaction" json:"skipDefaultTransaction"`              // 跳过默认事务
	PrepareStmt                              bool   `mapstructure:"prepare-stmt" yaml:"prepare-stmt" json:"prepareStmt"`                                                 // 预编译语句
	DisableForeignKeyConstraintWhenMigrating bool   ``                                                                                                                   // 禁用自动创建外键约束
	/* 161-byte string literal not displayed */
	DisableNestedTransaction bool `mapstructure:"disable-nested-transaction" yaml:"disable-nested-transaction" json:"disableNestedTransaction"` // 禁用嵌套事务
	AllowGlobalUpdate        bool `mapstructure:"allow-global-update" yaml:"allow-global-update" json:"allowGlobalUpdate"`                      // 允许全局更新
	QueryFields              bool `mapstructure:"query-fields" yaml:"query-fields" json:"queryFields"`                                          // 执行查询时选择所有字段
	CreateBatchSize          int  `mapstructure:"create-batch-size" yaml:"create-batch-size" json:"createBatchSize"`                            // 批量创建大小
	SingularTable            bool `mapstructure:"singular-table" yaml:"singular-table" json:"singularTable"`                                    // 使用单数表名
}

SQLite SQLite数据库配置

func DefaultSQLite

func DefaultSQLite() *SQLite

DefaultSQLite 创建默认SQLite配置

func NewSQLiteConfig

func NewSQLiteConfig(opt *SQLite) *SQLite

NewSQLiteConfig 创建一个新的SQLite配置实例

func (*SQLite) Clone

func (s *SQLite) Clone() internal.Configurable

func (*SQLite) Get

func (s *SQLite) Get() interface{}

func (*SQLite) GetAllowGlobalUpdate

func (s *SQLite) GetAllowGlobalUpdate() bool

func (*SQLite) GetConfig

func (s *SQLite) GetConfig() string

func (*SQLite) GetCreateBatchSize

func (s *SQLite) GetCreateBatchSize() int

func (*SQLite) GetDBName

func (s *SQLite) GetDBName() string

func (*SQLite) GetDBType

func (s *SQLite) GetDBType() DBType

为SQLite配置实现DatabaseProvider接口

func (*SQLite) GetDisableForeignKeyConstraintWhenMigrating

func (s *SQLite) GetDisableForeignKeyConstraintWhenMigrating() bool

func (*SQLite) GetDisableNestedTransaction

func (s *SQLite) GetDisableNestedTransaction() bool

func (*SQLite) GetHost

func (s *SQLite) GetHost() string

func (*SQLite) GetIgnoreRecordNotFoundError

func (s *SQLite) GetIgnoreRecordNotFoundError() bool

func (*SQLite) GetLogLevel

func (s *SQLite) GetLogLevel() string

func (*SQLite) GetModuleName

func (s *SQLite) GetModuleName() string

func (*SQLite) GetPassword

func (s *SQLite) GetPassword() string

func (*SQLite) GetPort

func (s *SQLite) GetPort() string

func (*SQLite) GetPrepareStmt

func (s *SQLite) GetPrepareStmt() bool

func (*SQLite) GetQueryFields

func (s *SQLite) GetQueryFields() bool

func (*SQLite) GetSingularTable

func (s *SQLite) GetSingularTable() bool

func (*SQLite) GetSkipDefaultTransaction

func (s *SQLite) GetSkipDefaultTransaction() bool

func (*SQLite) GetSlowThreshold

func (s *SQLite) GetSlowThreshold() int

func (*SQLite) GetUsername

func (s *SQLite) GetUsername() string

func (*SQLite) Set

func (s *SQLite) Set(data interface{})

func (*SQLite) SetCredentials

func (s *SQLite) SetCredentials(username, password string)

func (*SQLite) SetDBName

func (s *SQLite) SetDBName(dbName string)

func (*SQLite) SetHost

func (s *SQLite) SetHost(host string)

func (*SQLite) SetPort

func (s *SQLite) SetPort(port string)

func (*SQLite) Validate

func (s *SQLite) Validate() error

func (*SQLite) WithConfig

func (s *SQLite) WithConfig(config string) *SQLite

WithConfig 设置数据库配置参数

func (*SQLite) WithConnMaxIdleTime

func (s *SQLite) WithConnMaxIdleTime(connMaxIdleTime int) *SQLite

WithConnMaxIdleTime 设置连接最大空闲时间

func (*SQLite) WithConnMaxLifeTime

func (s *SQLite) WithConnMaxLifeTime(connMaxLifeTime int) *SQLite

WithConnMaxLifeTime 设置连接最大生命周期

func (*SQLite) WithDbPath

func (s *SQLite) WithDbPath(dbPath string) *SQLite

WithDbPath 设置SQLite数据库文件路径

func (*SQLite) WithLogLevel

func (s *SQLite) WithLogLevel(logLevel string) *SQLite

WithLogLevel 设置日志级别

func (*SQLite) WithMaxIdleConns

func (s *SQLite) WithMaxIdleConns(maxIdleConns int) *SQLite

WithMaxIdleConns 设置最大空闲连接数

func (*SQLite) WithMaxOpenConns

func (s *SQLite) WithMaxOpenConns(maxOpenConns int) *SQLite

WithMaxOpenConns 设置最大连接数

func (*SQLite) WithModuleName

func (s *SQLite) WithModuleName(moduleName string) *SQLite

WithModuleName 设置模块名称

func (*SQLite) WithVacuum

func (s *SQLite) WithVacuum(vacuum bool) *SQLite

WithVacuum 设置是否执行清除命令

Jump to

Keyboard shortcuts

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