sql

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithNestedTransaction

func WithNestedTransaction(db *gorm.DB, fn TransactionFunc) error

WithNestedTransaction handles nested transactions using savepoints

func WithNestedTransactionContext

func WithNestedTransactionContext(ctx context.Context, db *gorm.DB, fn TransactionFunc) error

WithNestedTransactionContext handles nested transactions using savepoints with context

func WithTransaction

func WithTransaction(db *gorm.DB, fn TransactionFunc) error

WithTransaction executes a function within a database transaction

func WithTransactionContext

func WithTransactionContext(ctx context.Context, db *gorm.DB, fn TransactionFunc) error

WithTransactionContext executes a function within a database transaction with context

Types

type BaseModel

type BaseModel struct {
	ID        uuid.UUID  `gorm:"type:uuid;default:gen_random_uuid();primaryKey" json:"id"`
	CreatedAt time.Time  `gorm:"index;not null" json:"createdAt"`
	UpdatedAt time.Time  `gorm:"index;not null" json:"updatedAt"`
	DeletedAt *time.Time `gorm:"index" json:"deletedAt,omitempty"`
}

BaseModel provides common fields for all models

func (*BaseModel) BeforeCreate

func (b *BaseModel) BeforeCreate(tx *gorm.DB) error

BeforeCreate hook to ensure ID is set

func (BaseModel) IsDeleted

func (b BaseModel) IsDeleted() bool

IsDeleted checks if the model is soft deleted

type DBManager

type DBManager struct {
	*gorm.DB
	// contains filtered or unexported fields
}

DBManager wraps gorm.DB and provides additional functionality

func OpenGorm

func OpenGorm(c GormConfig, automigrateModels ...interface{}) (*DBManager, error)

func (*DBManager) Close

func (dm *DBManager) Close() error

Close closes the database connection

func (*DBManager) Ping

func (dm *DBManager) Ping(ctx context.Context) error

Ping tests the database connection

func (*DBManager) Stats

func (dm *DBManager) Stats() sql.DBStats

Stats returns database statistics

type GormConfig

type GormConfig struct {
	Host                      string
	User                      string
	Password                  string
	Name                      string
	Port                      string
	SSLMode                   string
	Timezone                  string
	MaxIdleConns              int
	MaxOpenConns              int
	ConnMaxLifetime           time.Duration
	ConnMaxIdleTime           time.Duration
	TranslateErrors           bool
	LogLevel                  logger.LogLevel
	SlowThreshold             time.Duration
	IgnoreRecordNotFoundError bool
}

type ID

type ID struct {
	ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primaryKey" json:"id"`
}

ID represents a UUID identifier

func (*ID) BeforeCreate

func (i *ID) BeforeCreate(tx *gorm.DB) error

BeforeCreate hook to ensure ID is set

type JSONB

type JSONB map[string]any

JSONB represents a PostgreSQL JSONB column

func (JSONB) Clone

func (j JSONB) Clone() JSONB

Clone creates a deep copy of the JSONB

func (JSONB) Delete

func (j JSONB) Delete(key string)

Delete removes a key from the JSONB

func (JSONB) Get

func (j JSONB) Get(key string) (any, bool)

Get retrieves a value from the JSONB

func (JSONB) GetBool

func (j JSONB) GetBool(key string) (bool, bool)

GetBool retrieves a bool value from the JSONB

func (JSONB) GetInt

func (j JSONB) GetInt(key string) (int, bool)

GetInt retrieves an int value from the JSONB

func (JSONB) GetString

func (j JSONB) GetString(key string) (string, bool)

GetString retrieves a string value from the JSONB

func (JSONB) Has

func (j JSONB) Has(key string) bool

Has checks if a key exists in the JSONB

func (JSONB) IsEmpty

func (j JSONB) IsEmpty() bool

IsEmpty checks if the JSONB is empty

func (JSONB) Keys

func (j JSONB) Keys() []string

Keys returns all keys in the JSONB

func (*JSONB) Scan

func (j *JSONB) Scan(value any) error

Scan implements the sql.Scanner interface

func (JSONB) Set

func (j JSONB) Set(key string, value any)

Set sets a value in the JSONB

func (JSONB) String

func (j JSONB) String() string

String returns the JSON string representation

func (JSONB) Value

func (j JSONB) Value() (driver.Value, error)

Value implements the driver.Valuer interface

type JSONBArray

type JSONBArray []any

JSONBArray represents a PostgreSQL JSONB array

func (*JSONBArray) Scan

func (j *JSONBArray) Scan(value any) error

Scan implements the sql.Scanner interface

func (JSONBArray) Value

func (j JSONBArray) Value() (driver.Value, error)

Value implements the driver.Valuer interface

type NullableUUID

type NullableUUID struct {
	UUID  uuid.UUID
	Valid bool
}

NullableUUID provides a nullable UUID type

func (*NullableUUID) Scan

func (nu *NullableUUID) Scan(value interface{}) error

Scan implements the Scanner interface

func (NullableUUID) Value

func (nu NullableUUID) Value() (driver.Value, error)

Value implements the driver Valuer interface

type SoftDelete

type SoftDelete struct {
	DeletedAt *time.Time `gorm:"index" json:"deletedAt,omitempty"`
}

SoftDelete provides soft delete functionality

func (*SoftDelete) Delete

func (s *SoftDelete) Delete()

Delete marks the model as soft deleted

func (SoftDelete) IsDeleted

func (s SoftDelete) IsDeleted() bool

IsDeleted checks if the model is soft deleted

func (*SoftDelete) Restore

func (s *SoftDelete) Restore()

Restore removes the soft delete mark

type Timestamp

type Timestamp struct {
	CreatedAt time.Time `gorm:"index;not null" json:"createdAt"`
	UpdatedAt time.Time `gorm:"index;not null" json:"updatedAt"`
}

Timestamp provides creation and update timestamps

type TransactionFunc

type TransactionFunc func(*gorm.DB) error

TransactionFunc defines the function signature for transaction operations

Jump to

Keyboard shortcuts

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