sql

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2025 License: GPL-3.0 Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateTransaction

func CreateTransaction(db *sql.DB, fn TransactionFn) error

CreateTransaction creates a transaction for the database

Parameters:

  • db: The database connection
  • fn: The function to execute within the transaction

Returns:

  • error: An error if the transaction fails

func RunQueriesConcurrently added in v0.4.8

func RunQueriesConcurrently(
	db *sql.DB,
	queries ...func(db *sql.DB) error,
) []error

RunQueriesConcurrently runs multiple queries concurrently

Parameters:

db: The database connection
queries: The queries to run

Returns:

[]error: A slice of errors, or nil if no errors occurred

func RunQueriesConcurrentlyWithCancel added in v0.4.12

func RunQueriesConcurrentlyWithCancel(
	db *sql.DB,
	queries ...func(db *sql.DB, ctx context.Context) error,
) []error

RunQueriesConcurrentlyWithCancel runs multiple queries concurrently with a cancel context

Parameters:

db: The database connection
queries: The queries to run

Returns:

[]error: A slice of errors, or nil if no errors occurred

Types

type Config added in v0.4.6

type Config interface {
	DriverName() string
	DataSourceName() string
	MaxOpenConnections() int
	MaxIdleConnections() int
	ConnectionMaxLifetime() time.Duration
	ConnectionMaxIdleTime() time.Duration
}

Config interface

type ConnConfig added in v0.5.0

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

ConnConfig struct

func NewConnConfig added in v0.5.0

func NewConnConfig(
	driverName,
	dataSourceName string,
	maxOpenConnections,
	maxIdleConnections int,
	connectionMaxIdleTime,
	connectionMaxLifetime time.Duration,
) (*ConnConfig, error)

NewConnConfig creates a new configuration for the connection

Parameters:

  • driverName: the name of the driver
  • dataSourceName: the data source name
  • maxOpenConnections: the maximum number of open connections
  • maxIdleConnections: the maximum number of idle connections
  • connectionMaxIdleTime: the maximum idle time for a connection
  • connectionMaxLifetime: the maximum lifetime for a connection

Returns:

  • *ConnConfig: the connection configuration
  • error: if any error occurs

func (ConnConfig) ConnectionMaxIdleTime added in v0.5.0

func (c ConnConfig) ConnectionMaxIdleTime() time.Duration

ConnectionMaxIdleTime returns the connection max idle time

Returns:

  • time.Duration: the connection max idle time

func (ConnConfig) ConnectionMaxLifetime added in v0.5.0

func (c ConnConfig) ConnectionMaxLifetime() time.Duration

ConnectionMaxLifetime returns the connection max lifetime

Returns:

  • time.Duration: the connection max lifetime

func (ConnConfig) DataSourceName added in v0.5.0

func (c ConnConfig) DataSourceName() string

DataSourceName returns the data source name

Returns:

  • string: the data source name

func (ConnConfig) DriverName added in v0.5.0

func (c ConnConfig) DriverName() string

DriverName returns the driver name

Returns:

  • string: the driver name

func (ConnConfig) MaxIdleConnections added in v0.5.0

func (c ConnConfig) MaxIdleConnections() int

MaxIdleConnections returns the maximum idle connections

Returns:

  • int: the maximum idle connections

func (ConnConfig) MaxOpenConnections added in v0.5.0

func (c ConnConfig) MaxOpenConnections() int

MaxOpenConnections returns the maximum open connections

Returns:

  • int: the maximum open connections

type ConnHandler added in v0.5.0

type ConnHandler interface {
	Connect() (*sql.DB, error)
	DB() (*sql.DB, error)
	Disconnect()
}

ConnHandler interface

type DefaultConnHandler added in v0.5.0

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

DefaultConnHandler struct

func NewDefaultConnHandler added in v0.5.0

func NewDefaultConnHandler(
	config Config,
) (*DefaultConnHandler, error)

NewDefaultConnHandler creates a new connection

Parameters:

  • config: the configuration for the connection

Returns:

  • *DefaultConnHandler: the connection handler

func (*DefaultConnHandler) Connect added in v0.5.0

func (d *DefaultConnHandler) Connect() (*sql.DB, error)

Connect returns a new SQL connection

Returns:

  • *sql.DB: the SQL connection
  • error: if any error occurred

func (*DefaultConnHandler) DB added in v0.5.0

func (d *DefaultConnHandler) DB() (*sql.DB, error)

DB returns the SQL connection

Returns:

  • *sql.DB: the SQL connection
  • error: if any error occurred

func (*DefaultConnHandler) Disconnect added in v0.6.2

func (d *DefaultConnHandler) Disconnect() error

Disconnect closes the SQL connection

Returns:

  • error: if any error occurred

type DefaultService added in v0.5.0

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

DefaultService is the default service struct

func NewDefaultService added in v0.5.0

func NewDefaultService(db *sql.DB) (
	instance *DefaultService,
	err error,
)

NewDefaultService creates a new default service

Parameters:

*db *sql.DB: the database connection

Returns:

*DefaultService: the default service error: if there was an error creating the service

func (*DefaultService) CreateTransaction added in v0.5.0

func (d *DefaultService) CreateTransaction(fn TransactionFn) error

CreateTransaction creates a transaction for the database

func (*DefaultService) DB added in v0.5.0

func (d *DefaultService) DB() *sql.DB

DB returns the database

Returns:

*sql.DB: the database

func (*DefaultService) Exec added in v0.5.0

func (d *DefaultService) Exec(query *string, params ...interface{}) (
	sql.Result,
	error,
)

Exec executes a query with parameters and returns the result

func (*DefaultService) Migrate added in v0.5.0

func (d *DefaultService) Migrate(queries ...string) error

Migrate migrates the database

Parameters:

*queries ...string: the queries to migrate

Returns:

error: if there was an error migrating the database

func (*DefaultService) QueryRow added in v0.5.0

func (d *DefaultService) QueryRow(
	query *string,
	params ...interface{},
) *sql.Row

QueryRow runs a query row with parameters and returns the result row

func (*DefaultService) ScanRow added in v0.5.0

func (d *DefaultService) ScanRow(
	row *sql.Row,
	destinations ...interface{},
) error

ScanRow scans a row

type Service added in v0.5.0

type Service interface {
	DB() *sql.DB
	Migrate(queries ...string) error
	CreateTransaction(fn TransactionFn) error
	Exec(query *string, params ...interface{}) (sql.Result, error)
	QueryRow(query *string, params ...interface{}) *sql.Row
	ScanRow(row *sql.Row, destinations ...interface{}) error
}

Service is the interface for the service

type TransactionFn added in v0.6.1

type TransactionFn func(tx *sql.Tx) error

TransactionFn is the function type for transactions

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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