...

Package sql

import "github.com/pzentenoe/gorm-connection/sql"
Overview
Index

Overview ▾

connection.go

options.go

dialect.go

factory.go

paginate.go

Index ▾

Package files

connection.go db_options.go dialector.go factories.go pagination.go

Variables

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

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB

Paginate returns a GORM scope function to apply pagination.

type Connection

Connection defines the method to get a database connection.

type Connection interface {
    GetConnection() (*gorm.DB, error)
}

func NewConnection

func NewConnection(dialect SQLDialect, options ...DBOptionFunc) (Connection, error)

NewConnection is a generalized factory for creating a database connection.

func NewMySQLConnection

func NewMySQLConnection(host, databaseName, user, password string, options ...DBOptionFunc) (Connection, error)

NewMySQLConnection Factory function for MySQL

func NewPostgresConnection

func NewPostgresConnection(host, databaseName, user, password string, options ...DBOptionFunc) (Connection, error)

NewPostgresConnection Factory function for PostgreSQL

func NewSQLConnection

func NewSQLConnection(optionFuncs ...DBOptionFunc) (Connection, error)

NewSQLConnection creates a new SQL database connection based on the provided options.

func NewSQLServerConnection

func NewSQLServerConnection(host, databaseName, user, password string, options ...DBOptionFunc) (Connection, error)

NewSQLServerConnection Factory function for SQL Server

func NewSQLiteConnection

func NewSQLiteConnection(databaseName string, options ...DBOptionFunc) (Connection, error)

NewSQLiteConnection Factory function for SQLite

type DBOption

DBOption holds the configuration for database connection.

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

type DBOptionFunc

DBOptionFunc defines a function type for setting options.

type DBOptionFunc func(*DBOption)

func WithConnMaxIdleTime

func WithConnMaxIdleTime(connMaxIdleTime time.Duration) DBOptionFunc

WithConnMaxIdleTime sets the maximum idle time for a connection.

func WithConnMaxLifetime

func WithConnMaxLifetime(connMaxLifetime time.Duration) DBOptionFunc

WithConnMaxLifetime sets the maximum connection lifetime.

func WithDatabaseName

func WithDatabaseName(name string) DBOptionFunc

WithDatabaseName sets the database name option.

func WithHost

func WithHost(host string) DBOptionFunc

WithHost sets the database host option.

func WithLogLevel

func WithLogLevel(logLevel gormLogger.LogLevel) DBOptionFunc

WithLogLevel sets the log level for GORM.

func WithMaxIdleConns

func WithMaxIdleConns(maxIdleConns int) DBOptionFunc

WithMaxIdleConns sets the maximum number of idle connections.

func WithMaxOpenConns

func WithMaxOpenConns(maxOpenConns int) DBOptionFunc

WithMaxOpenConns sets the maximum number of open connections.

func WithPassword

func WithPassword(password string) DBOptionFunc

WithPassword sets the database password option.

func WithPort

func WithPort(port int) DBOptionFunc

WithPort sets the database port option.

func WithSQLDialect

func WithSQLDialect(dialect SQLDialect) DBOptionFunc

WithSQLDialect sets the SQL dialect option.

func WithTimezone

func WithTimezone(timezone string) DBOptionFunc

WithTimezone sets the timezone option.

func WithUser

func WithUser(user string) DBOptionFunc

WithUser sets the database user option.

type SQLDialect

SQLDialect represents a SQL dialect with its name and default port.

type SQLDialect struct {
    Name        string
    DefaultPort int
}