liborm

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package liborm provides ORM-based query execution using Gorm.

Index

Constants

View Source
const (
	// App is the context key for the application/program identifier in audit trail variables.
	App = "request.APP"
	// User is the context key for the user identifier in audit trail variables.
	User = "request.USER"
	// Module is the context key for the module name in audit trail variables.
	Module = "request.MODULE"
	// Method is the context key for the method name in audit trail variables.
	Method = "request.METHOD"
	// SetCommandError is the error description format for SetTrxVariable failures.
	SetCommandError = "error in Dml->SetTrxVariable(%s,%s,%s)"
	// ErrorExecuteDML is the error code for DML execution failures.
	ErrorExecuteDML = "ERROR_EXECUTE_DML"
	// OracleSetVariableCommand is the SQL command for setting audit trail variables in Oracle.
	OracleSetVariableCommand = `--sql
		BEGIN 
			AUDIT_TRAIL.SET_MODIF_ARGS(:1, :2);
		END;`
	// PostgresSetVariableCommand is the SQL command for setting audit trail variables in PostgreSQL.
	PostgresSetVariableCommand = "SELECT set_config($1,$2,true);"
	// MySQLSetVariableCommand is the SQL command format for setting audit trail variables in MySQL.
	MySQLSetVariableCommand = "SET @%s = '%s';"
	// SQLiteSetVariableCommand is the SQL command format for setting audit trail variables in SQLite.
	SQLiteSetVariableCommand = "PRAGMA %s = '%s';"
)

Variables

This section is empty.

Functions

func GetQuery

func GetQuery[R any](query string, core OrmInterface, args ...any) ([]R, error)

GetQuery executes a raw SQL query and returns the result rows, returning NoDataFound error if empty.

func Query

func Query[R any](command libQuery.CommandInterface, core OrmInterface, args ...any) ([]R, error)

Query executes a command interface query and returns result rows, handling single vs all result modes.

func QueryToStruct

func QueryToStruct[Target any](db *gorm.DB, querySQL string, args ...any) ([]Target, error)

QueryToStruct executes a raw SQL query and scans the result rows into a slice of Target.

func SetVariable

func SetVariable(_ context.Context, tx *gorm.DB, command, key, value string) error

SetVariable executes the given command to set a transaction variable (key/value) in the database.

Types

type ContextKey

type ContextKey string

ContextKey is the type used for context keys in ORM transaction variables.

type OrmInterface

type OrmInterface interface {
	GetDbMode() libQuery.DBMode
	GetDB() *gorm.DB
}

OrmInterface defines the interface for ORM-based database access with mode and DB handle.

type OrmModel

type OrmModel struct {
	DB          *gorm.DB
	ProgramName string
	ModuleName  string
	SetVariable string
	Mode        libQuery.DBMode
}

OrmModel holds the Gorm DB connection, program/module names, and DML configuration.

func Init

func Init(
	DB *gorm.DB,
	ProgramName string,
	ModuleName string,
	mode libQuery.DBMode) (*OrmModel, error)

Init creates and returns an OrmModel configured for the given database mode.

func (OrmModel) Dml

func (m OrmModel) Dml(ctx context.Context, moduleName, methodName, command string, args ...any) (int64, error)

Dml executes a DML command within a transaction with audit trail variables set, returning rows affected.

func (OrmModel) GetDB

func (m OrmModel) GetDB() *gorm.DB

GetDB returns the underlying Gorm DB connection.

func (OrmModel) GetDbMode

func (m OrmModel) GetDbMode() libQuery.DBMode

GetDbMode returns the database mode (Oracle, Postgres, MySQL, SQLite).

func (OrmModel) SetModifVariables

func (m OrmModel) SetModifVariables(ctx context.Context, moduleName, methodName string, tx *gorm.DB) error

SetModifVariables sets audit trail modification variables (app, user, module, method) on the transaction.

Jump to

Keyboard shortcuts

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