sql

package
v0.0.0-...-ceb351d Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PostgresDNS is postgres connection path format for gorm.
	// host=localhost:3306 dbname=app sslmode=require user=svc password=pw
	PostgresDNS = "host=%s port=%d dbname=%s sslmode=%s user=%s password=%s"

	// MysqlDNS is mysql connection path format for gorm.
	// app:password@tcp(localhost:3306)/app?charset=utf8&parseTime=True&loc=Local
	// Default charset is utf8
	MysqlDNS = "%s:%s@%s(%s:%d)/%s?charset=%s&parseTime=True&loc=Local"
)
View Source
const (
	// DBContextKey ...
	DBContextKey contextKey = iota
)
View Source
const (
	// DefaultMySQLCharset is the default mysql character set
	DefaultMySQLCharset string = "utf8"
)

Variables

View Source
var ErrBadRequestRecordNotFound = errors.New("BAD_REQUEST_RECORD_NOT_FOUND")

ErrBadRequestRecordNotFound ...

View Source
var ErrorUnsupportedDialect = errors.New(
	"dialect for the db is not supported",
)

ErrorUnsupportedDialect tells the SQL dialect is not supported

Functions

func ConnectionURL

func ConnectionURL(c *Config) string

ConnectionURL makes the connection URL from the config

Types

type Config

type Config struct {
	// Dialect of SQL
	Dialect Dialect
	// Protocol ...
	Protocol string
	// URL ...
	URL string
	// Port ...
	Port int
	// Username ...
	Username string
	// Password ...
	Password string
	// SslMode ...
	SslMode string
	// Name ...
	Name string
	// Schema ...
	Schema string
	// Charset for client side interaction
	// only MYSQL uses it at present and uses utf8 as default
	CharSet string
	// MaxOpenConnections of the database allowed
	MaxOpenConnections int
	// MaxIdleConnections for the database before closing conn
	MaxIdleConnections int
	// ConnectionMaxLifeTime ...
	ConnectionMaxLifetime int
	// Debug turns on the debug logs for the ORM
	Debug bool

	// SQLiteDSN is used for testing purpose
	SQLiteDSN string
}

Config this is to be used in application toml for loading the data into a storage config struct

type Dialect

type Dialect string

Dialect is the SQL dialect to support all kinds of SQL

var (
	// DialectMySQL is for mysql database
	DialectMySQL Dialect = "mysql"
	// DialectPostgres is for postgres database
	DialectPostgres Dialect = "postgres"
	// DialectSQLite is for sqlite inmemory database
	DialectSQLite Dialect = "sqlite3"
)

func (Dialect) String

func (d Dialect) String() string

String returns Dilalect as string

type Gorm

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

Gorm implements store.Storage for all SQL stores

func NewGorm

func NewGorm(ctx context.Context, config *Config) (*Gorm, error)

NewGorm creates a new instance of gorm for all sql based interactions with the database, it wraps gorm to prevent direct dependency with gorm

func (*Gorm) Create

func (g *Gorm) Create(ctx context.Context, model interface{}) (any, error)

Create saves the model into the database using gorm

func (*Gorm) DB

func (g *Gorm) DB(ctx context.Context) (*sql.DB, error)

DB is required for migrations

func (*Gorm) Delete

func (g *Gorm) Delete(
	ctx context.Context,
	model interface{}) (any, error)

Delete deletes a record with primary id

func (*Gorm) DeleteWithWhere

func (g *Gorm) DeleteWithWhere(
	ctx context.Context,
	models interface{},
	condition map[string]interface{}) (any, error)

DeleteWithWhere deletes all record which matches where clause

func (*Gorm) ExecuteTxn

func (g *Gorm) ExecuteTxn(
	ctx context.Context, fn func(ctx context.Context) error) error

ExecuteTxn runs multiple commands within a transaction, committing changes upon success and rolling them back if an error occurs.

func (*Gorm) FindByID

func (g *Gorm) FindByID(ctx context.Context, dest interface{},
	id string) (any, error)

FindByID finds using the primary key of the model and sets in the destination. Assumes that primary key is id column. Pass the dest as pointer

func (*Gorm) FindByIds

func (g *Gorm) FindByIds(
	ctx context.Context, dest interface{}, ids []string) (any, error)

FindByIds finds using the primary key of the model and sets in the destination. Assumes that primary key is id column. Pass the dest as pointer

func (*Gorm) FindInBatches

func (g *Gorm) FindInBatches(
	ctx context.Context,
	dest interface{},
	page int, limit int) (any, error)

FindInBatches retrieves a paginated set of results from the database.

func (*Gorm) FindWithQuery

func (g *Gorm) FindWithQuery(ctx context.Context, dest interface{},
	qb *query.Query) (any, error)

FindWithQuery allows general search criteria and sets in the destination

func (*Gorm) FindWithWhere

func (g *Gorm) FindWithWhere(ctx context.Context, dest interface{},
	condition map[string]interface{}) (any, error)

FindWithWhere finds using the conditions passed in map and sets in the destination

func (*Gorm) FindWithWhereInBatches

func (g *Gorm) FindWithWhereInBatches(
	ctx context.Context,
	dest interface{},
	condition map[string]interface{},
	page int, limit int) (any, error)

FindWithWhereInBatches retrieves a paginated set of results from the database with conditions.

func (*Gorm) Instance

func (g *Gorm) Instance() (*sql.DB, error)

Instance is used for performing direct operations on gorm One use case: database migrations

func (*Gorm) Update

func (g *Gorm) Update(
	ctx context.Context, model interface{}, values interface{}) (any, error)

Update updated the model attributes that are part of values into the database using gorm. Returns the rows affected with the update and error. Uses primary key in the where clause returns error if primary key of the model is null.

func (*Gorm) UpdateWithUpdateables

func (g *Gorm) UpdateWithUpdateables(ctx context.Context, updateables []string, model interface{},
	values interface{},
	query interface{}, args ...interface{}) (any, error)

UpdateWithUpdateables Update updated the model attributes that are part of values into the database using gorm. Returns the rows affected with the update and error Uses query and args in the where clause, returns error if args length is less than 1.

func (*Gorm) UpdateWithWhere

func (g *Gorm) UpdateWithWhere(
	ctx context.Context,
	model interface{},
	values interface{},
	condition map[string]interface{}) (any, error)

UpdateWithWhere Update updated the model attributes that are part of values into the database using gorm. Returns the rows affected with the update and error Uses query and args in the where clause returns error if args length is less than 1.

func (*Gorm) Upsert

func (g *Gorm) Upsert(ctx context.Context, model interface{}) (any, error)

Upsert saves the model into the database using gorm Assumes that the model has a primary key defined.

type Migrator

type Migrator struct {
	Gorm *Gorm
	// contains filtered or unexported fields
}

Migrator is responsible for running database migrations.

func NewMigrator

func NewMigrator(ctx context.Context, conf MigratorConfig) (*Migrator, error)

NewMigrator creates a new Migrator with the provided database connection and configuration.

func (*Migrator) Migrate

func (m *Migrator) Migrate(
	ctx context.Context, command string, arguments []string) error

Migrate runs the migration using the provided configuration.

type MigratorConfig

type MigratorConfig struct {
	Dir     string
	Storage Config
}

MigratorConfig holds the configuration options for the migrator.

Jump to

Keyboard shortcuts

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