db

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MYSQL      = "mysql"
	POSTGRES   = "postgres"
	POSTGRESQL = "postgresql"
	SQLITE     = "sqlite"
)
View Source
const ConfigKey = "web.db"

Variables

View Source
var NoConfigDBError = &noConfigDBError{}

Functions

func ApplyConnectionPool added in v0.5.1

func ApplyConnectionPool(db *gorm.DB, cfg ConnectionPoolConfig) error

ApplyConnectionPool applies connection pool settings to the underlying sql.DB

Types

type Config added in v0.1.3

type Config struct {
	Type string
}

type ConnectionPoolConfig added in v0.5.1

type ConnectionPoolConfig interface {
	GetMaxOpenConns() int
	GetMaxIdleConns() int
	GetConnMaxLifetime() int
}

ConnectionPoolConfig defines the interface for database connection pool configuration

type DB

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

func ConnectionMysql added in v0.1.4

func ConnectionMysql(host string, port int, username string, password string, dbname string, charset string) (db *DB, err error)

func ConnectionPostgres added in v0.5.1

func ConnectionPostgres(host string, port int, username, password, database string, sslMode string) (db *DB, err error)

ConnectionPostgres creates a new PostgreSQL connection with given parameters

func ConnectionSQLite added in v0.1.4

func ConnectionSQLite(FilePath string) (db *DB, err error)

func CreateDB added in v0.1.3

func CreateDB(c config.IConfig) (*DB, error)

func (*DB) Migrator added in v0.1.3

func (d *DB) Migrator() gorm.Migrator

func (*DB) New added in v0.1.4

func (d *DB) New() *DB

func (*DB) Table added in v0.1.3

func (d *DB) Table(name string) *Table

func (*DB) Transaction added in v0.1.3

func (d *DB) Transaction(fc func(tx *DB) error) error

type IConfig added in v0.1.3

type IConfig interface {
	Connection() (*DB, error)
}

type MysqlConfig added in v0.1.1

type MysqlConfig struct {
	Dbname   string
	Database string
	Charset  string
	Username string
	User     string
	Password string
	Host     string
	Port     int
	// Connection pool settings
	MaxOpenConns    int `mapstructure:"max_open_conns"`
	MaxIdleConns    int `mapstructure:"max_idle_conns"`
	ConnMaxLifetime int `mapstructure:"conn_max_lifetime"` // in seconds
}

func (*MysqlConfig) Connection added in v0.1.3

func (mysqlConfig *MysqlConfig) Connection() (db *DB, err error)

func (*MysqlConfig) GetConnMaxLifetime added in v0.5.1

func (mysqlConfig *MysqlConfig) GetConnMaxLifetime() int

func (*MysqlConfig) GetMaxIdleConns added in v0.5.1

func (mysqlConfig *MysqlConfig) GetMaxIdleConns() int

func (*MysqlConfig) GetMaxOpenConns added in v0.5.1

func (mysqlConfig *MysqlConfig) GetMaxOpenConns() int

type PostgresConfig added in v0.5.1

type PostgresConfig struct {
	Host     string
	Port     int
	Username string
	User     string
	Password string
	Database string
	Dbname   string
	SSLMode  string // disable, require, verify-ca, verify-full

	// Connection pool settings
	MaxOpenConns    int `mapstructure:"max_open_conns"`
	MaxIdleConns    int `mapstructure:"max_idle_conns"`
	ConnMaxLifetime int `mapstructure:"conn_max_lifetime"` // in seconds
}

PostgresConfig holds PostgreSQL connection configuration

func (*PostgresConfig) Connection added in v0.5.1

func (c *PostgresConfig) Connection() (db *DB, err error)

Connection creates a new PostgreSQL database connection

func (*PostgresConfig) GetConnMaxLifetime added in v0.5.1

func (c *PostgresConfig) GetConnMaxLifetime() int

GetConnMaxLifetime implements ConnectionPoolConfig

func (*PostgresConfig) GetMaxIdleConns added in v0.5.1

func (c *PostgresConfig) GetMaxIdleConns() int

GetMaxIdleConns implements ConnectionPoolConfig

func (*PostgresConfig) GetMaxOpenConns added in v0.5.1

func (c *PostgresConfig) GetMaxOpenConns() int

GetMaxOpenConns implements ConnectionPoolConfig

type SQLiteConfig added in v0.1.2

type SQLiteConfig struct {
	FilePath string
	// Connection pool settings
	MaxOpenConns    int `mapstructure:"max_open_conns"`
	MaxIdleConns    int `mapstructure:"max_idle_conns"`
	ConnMaxLifetime int `mapstructure:"conn_max_lifetime"` // in seconds
}

func (*SQLiteConfig) Connection added in v0.1.3

func (sqliteConfig *SQLiteConfig) Connection() (db *DB, err error)

func (*SQLiteConfig) GetConnMaxLifetime added in v0.5.1

func (sqliteConfig *SQLiteConfig) GetConnMaxLifetime() int

func (*SQLiteConfig) GetMaxIdleConns added in v0.5.1

func (sqliteConfig *SQLiteConfig) GetMaxIdleConns() int

func (*SQLiteConfig) GetMaxOpenConns added in v0.5.1

func (sqliteConfig *SQLiteConfig) GetMaxOpenConns() int

type Session added in v0.1.3

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

func (*Session) Delete added in v0.1.3

func (s *Session) Delete(value any, conds ...any) error

type Source added in v0.1.3

type Source interface {
	Connection(cfg config.IConfig) (db *gorm.DB, err error)
}

type Table added in v0.1.3

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

func (*Table) AutoMigrate added in v0.1.3

func (t *Table) AutoMigrate(v ...any) error

func (*Table) Count added in v0.1.3

func (t *Table) Count(i *int64) error

func (*Table) Create added in v0.1.3

func (t *Table) Create(value any) error

func (*Table) CreateMapWithPk added in v0.1.4

func (t *Table) CreateMapWithPk(mapValue map[string]any, keyName string) (any, error)

CreateMapWithPk creates a record from a map and returns the generated primary key If keyName is empty, it will try common ID field names (ID, Id, id)

func (*Table) CreateMapWithUintPk added in v0.1.4

func (t *Table) CreateMapWithUintPk(mapValue map[string]any, keyName string) (uint, error)

CreateMapWithUintPk creates a record from a map and returns the primary key as uint

func (*Table) CreateWithPk added in v0.1.4

func (t *Table) CreateWithPk(value any, keyName string, kind reflect.Kind) (any, error)

CreateWithPk creates a record and returns the generated primary key If keyName is empty, it will try common ID field names (ID, Id, id) If kind is reflect.Invalid, it will try to determine the type automatically

func (*Table) Delete added in v0.1.3

func (t *Table) Delete(value any, conds ...any) error

func (*Table) Find added in v0.1.3

func (t *Table) Find(dest any, conds ...any) error

func (*Table) First added in v0.1.3

func (t *Table) First(dest any, conds ...any) error

func (*Table) Joins added in v0.5.1

func (t *Table) Joins(query string, args ...any) *Table

Joins performs a join operation (GORM join support) Usage: db.Table("users").Joins("Profile").Find(&users)

func (*Table) Limit added in v0.1.3

func (t *Table) Limit(size int) *Table

func (*Table) NewTable added in v0.1.4

func (t *Table) NewTable() *Table

func (*Table) Offset added in v0.1.3

func (t *Table) Offset(i int) *Table
func (t *Table) Set(column string, value any) *Table {
	tx := t.db.Set(column, value)
	return &Table{db: tx}
}

func (*Table) Order added in v0.1.3

func (t *Table) Order(query any) *Table

func (*Table) Preload added in v0.5.1

func (t *Table) Preload(query string, args ...any) *Table

Preload preloads associations for the query (GORM foreign key support) Usage: db.Table("users").Preload("Profile").Preload("Role").Find(&users)

func (*Table) Raw added in v0.5.1

func (t *Table) Raw(sql string, args ...any) *Table

Raw executes a raw SQL query and returns a Table for further chaining (e.g., with Scan)

func (*Table) Save added in v0.1.3

func (t *Table) Save(entry any) error

func (*Table) Scan added in v0.5.1

func (t *Table) Scan(dest any) error

Scan scans query results into dest

func (*Table) Session added in v0.1.3

func (t *Table) Session(g *gorm.Session) *Session

func (*Table) UpdateColumn added in v0.1.3

func (t *Table) UpdateColumn(column string, value any) error

func (*Table) Updates added in v0.1.3

func (t *Table) Updates(values any) error

func (*Table) Where added in v0.1.3

func (t *Table) Where(query any, args ...any) *Table

Jump to

Keyboard shortcuts

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