SQL Dialects
var ( SQLServer = SQLDialect{Name: "mssql", DefaultPort: 1433} MySQL = SQLDialect{Name: "mysql", DefaultPort: 3306} Postgres = SQLDialect{Name: "postgres", DefaultPort: 5432} SQLite = SQLDialect{Name: "sqlite3"} )
func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB
Paginate returns a GORM scope function to apply pagination.
Connection defines the method to get a database connection.
type Connection interface { GetConnection() (*gorm.DB, error) }
func NewConnection(dialect SQLDialect, options ...DBOptionFunc) (Connection, error)
NewConnection is a generalized factory for creating a database connection.
func NewMySQLConnection(host, databaseName, user, password string, options ...DBOptionFunc) (Connection, error)
NewMySQLConnection Factory function for MySQL
func NewPostgresConnection(host, databaseName, user, password string, options ...DBOptionFunc) (Connection, error)
NewPostgresConnection Factory function for PostgreSQL
func NewSQLConnection(optionFuncs ...DBOptionFunc) (Connection, error)
NewSQLConnection creates a new SQL database connection based on the provided options.
func NewSQLServerConnection(host, databaseName, user, password string, options ...DBOptionFunc) (Connection, error)
NewSQLServerConnection Factory function for SQL Server
func NewSQLiteConnection(databaseName string, options ...DBOptionFunc) (Connection, error)
NewSQLiteConnection Factory function for SQLite
DBOption holds the configuration for database connection.
type DBOption struct {
// contains filtered or unexported fields
}
DBOptionFunc defines a function type for setting options.
type DBOptionFunc func(*DBOption)
func WithConnMaxIdleTime(connMaxIdleTime time.Duration) DBOptionFunc
WithConnMaxIdleTime sets the maximum idle time for a connection.
func WithConnMaxLifetime(connMaxLifetime time.Duration) DBOptionFunc
WithConnMaxLifetime sets the maximum connection lifetime.
func WithDatabaseName(name string) DBOptionFunc
WithDatabaseName sets the database name option.
func WithHost(host string) DBOptionFunc
WithHost sets the database host option.
func WithLogLevel(logLevel gormLogger.LogLevel) DBOptionFunc
WithLogLevel sets the log level for GORM.
func WithMaxIdleConns(maxIdleConns int) DBOptionFunc
WithMaxIdleConns sets the maximum number of idle connections.
func WithMaxOpenConns(maxOpenConns int) DBOptionFunc
WithMaxOpenConns sets the maximum number of open connections.
func WithPassword(password string) DBOptionFunc
WithPassword sets the database password option.
func WithPort(port int) DBOptionFunc
WithPort sets the database port option.
func WithSQLDialect(dialect SQLDialect) DBOptionFunc
WithSQLDialect sets the SQL dialect option.
func WithTimezone(timezone string) DBOptionFunc
WithTimezone sets the timezone option.
func WithUser(user string) DBOptionFunc
WithUser sets the database user option.
SQLDialect represents a SQL dialect with its name and default port.
type SQLDialect struct { Name string DefaultPort int }