Documentation
¶
Overview ¶
Package libQuery provides database query runner abstractions for requestCore.
Index ¶
- Constants
- func ConvertJSONToStruct[Q any](row string) (Q, error)
- func DBModeStrings() []string
- func DmlCommandTypeStrings() []string
- func GetDBTagValue(name string, s any) (*string, *string, error)
- func GetFormTagValue(name string, s any) (*string, *string, error)
- func GetLocalArgs(parser webFramework.RequestParser, args []any) []any
- func GetOutArgs(parser webFramework.RequestParser, args ...any) map[string]string
- func GetQuery[R any](query string, core QueryRunnerInterface, args ...any) ([]R, error)
- func GetTagValue(name, tag string, s any) (*string, *string, error)
- func HandleCheckDuplicate(code int, desc, dupDesc string, record []QueryData, err error) (int, string, error)
- func HandleCheckExistence(code int, desc, notExistDesc string, record []QueryData, err error) (int, string, error)
- func ParseCommand(command, user, app, action, title string, value map[string]string, ...) string
- func ParseMap[Target any](input map[string]any) (*Target, error)
- func ParseQueryResult(result map[string]any, t reflect.Type, v reflect.Value)
- func PrepareArgs(args []any) []any
- func Query[R any](command CommandInterface, core QueryRunnerInterface, args ...any) ([]R, error)
- func QueryCommandTypeStrings() []string
- func QueryOld[Result QueryResult](core QueryRunnerInterface, command QueryCommand, args ...any) (any, error)
- func QueryToStruct[Target any](q QueryRunnerInterface, querySQL string, args ...any) ([]Target, error)
- func SerializeArray(arr []any) string
- func SerializeStringArray(arr []string) string
- func SetVariable(ctx context.Context, tx *sql.Tx, command, key, value string) error
- type CommandInterface
- type ContextKey
- type DBMode
- type DmlCommand
- func (command DmlCommand) Execute(core QueryRunnerInterface, moduleName, methodName string) (any, error)
- func (command DmlCommand) ExecuteWithContext(w context.Context, parser webFramework.RequestParser, ...) (any, error)
- func (d DmlCommand) GetArgs() []any
- func (d DmlCommand) GetCommand(mode DBMode) string
- func (d DmlCommand) GetType() int
- type DmlCommandType
- type DmlModel
- type DmlResult
- type QueryCommand
- type QueryCommandType
- type QueryData
- type QueryRequest
- type QueryResult
- type QueryRunnerInterface
- type QueryRunnerModel
- func (m QueryRunnerModel) CallDbFunction(callString string, args ...any) (int, string, error)
- func (m QueryRunnerModel) Close()
- func (m QueryRunnerModel) Dml(ctx context.Context, moduleName, methodName, command string, args ...any) (sql.Result, error)
- func (m QueryRunnerModel) GetDbMode() DBMode
- func (m QueryRunnerModel) GetModule() (string, string)
- func (m QueryRunnerModel) InsertRow(insert string, args ...any) (sql.Result, error)
- func (m QueryRunnerModel) NewStatement(command string) (*sql.Stmt, error)
- func (m QueryRunnerModel) SetModifVariables(ctx context.Context, moduleName, methodName string, tx *sql.Tx) error
- func (m QueryRunnerModel) SetVariableCommand() string
- type QueryWithDeps
- type RecordDataDml
- type RecordDataGet
- type Updatable
Constants ¶
const ( // App is the context key for the application name in audit trail variables. App = "request.APP" // User is the context key for the user name 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 format for set variable command failures. SetCommandError = "error in Dml->SetTrxVariable(%s,%s,%s)" // ErrorExecuteDML is the error description for DML execution failures. ErrorExecuteDML = "ERROR_EXECUTE_DML" // OracleSetVariableCommand is the Oracle SQL command for setting audit trail variables. OracleSetVariableCommand = `--sql BEGIN CARD_ISSUE.AUDIT_TRAIL.SET_MODIF_ARGS(:1, :2); END;` // PostgresSetVariableCommand is the PostgreSQL SQL command for setting audit trail variables. PostgresSetVariableCommand = "SELECT set_config($1,$2,true);" )
const ( // PrepareError is the error code for statement preparation failures. PrepareError = -1 // QueryError is the error code for query execution failures. QueryError = -2 // ParseError is the error code for result parsing failures. ParseError = -3 // ScanError is the error code for row scanning failures. ScanError = -4 )
const ( // NoDataFound is the error description when a query returns no rows. NoDataFound = "NoDataFound" // NoDataFoundDesc is the human-readable description for no data found. NoDataFoundDesc = "رکوردی یافت نشد" // DuplicateFound is the error description when a query returns duplicate rows. DuplicateFound = "DuplicateFound" // DuplicateFoundDesc is the human-readable description for duplicate data. DuplicateFoundDesc = "رکورد تکراری است" // DBReadError is the error description for database read failures. DBReadError = "DBReadError" // ParseDBRespError is the error description for database response parsing failures. ParseDBRespError = "ParseDBRespError" )
const (
// ErrorCallingDBFunction is the error description for database function call failures.
ErrorCallingDBFunction = "ERROR_CALLING_DB_FUNCTION"
)
Variables ¶
This section is empty.
Functions ¶
func ConvertJSONToStruct ¶ added in v0.28.1
ConvertJSONToStruct unmarshals a JSON string into the target type.
func DBModeStrings ¶ added in v0.6.3
func DBModeStrings() []string
DBModeStrings returns a slice of all String values of the enum
func DmlCommandTypeStrings ¶ added in v0.5.0
func DmlCommandTypeStrings() []string
DmlCommandTypeStrings returns a slice of all String values of the enum
func GetDBTagValue ¶ added in v0.9.22
GetDBTagValue retrieves a struct field value by its "db" tag name.
func GetFormTagValue ¶ added in v0.9.22
GetFormTagValue retrieves a struct field value by its "form" tag name.
func GetLocalArgs ¶ added in v0.9.49
func GetLocalArgs(parser webFramework.RequestParser, args []any) []any
GetLocalArgs resolves named arguments that reference parser local storage.
func GetOutArgs ¶ added in v0.9.33
func GetOutArgs(parser webFramework.RequestParser, args ...any) map[string]string
GetOutArgs extracts output parameter values from DML arguments and stores them in parser locals.
func GetQuery ¶ added in v0.9.20
func GetQuery[R any](query string, core QueryRunnerInterface, args ...any) ([]R, error)
GetQuery executes a SQL query and returns the result as a slice of the target type.
func GetTagValue ¶ added in v0.9.22
GetTagValue retrieves a struct field value by its tag name and returns the field name and value.
func HandleCheckDuplicate ¶
func HandleCheckDuplicate(code int, desc, dupDesc string, record []QueryData, err error) (int, string, error)
HandleCheckDuplicate checks query results for duplicate records and returns the appropriate status.
func HandleCheckExistence ¶ added in v0.3.6
func HandleCheckExistence(code int, desc, notExistDesc string, record []QueryData, err error) (int, string, error)
HandleCheckExistence checks query results for record existence and returns the appropriate status.
func ParseCommand ¶
func ParseCommand(command, user, app, action, title string, value map[string]string, parser webFramework.FieldParser) string
ParseCommand substitutes template variables in a command string with request context values.
func ParseMap ¶ added in v0.10.28
ParseMap decodes a map into the target type using mapstructure with db tags.
func ParseQueryResult ¶
ParseQueryResult populates struct fields from a database result map using db tags.
func PrepareArgs ¶ added in v0.6.10
PrepareArgs converts slice arguments into PostgreSQL array literals for DML execution.
func Query ¶ added in v0.5.0
func Query[R any](command CommandInterface, core QueryRunnerInterface, args ...any) ([]R, error)
Query executes a command interface query and returns results based on the command type.
func QueryCommandTypeStrings ¶ added in v0.5.0
func QueryCommandTypeStrings() []string
QueryCommandTypeStrings returns a slice of all String values of the enum
func QueryOld ¶ added in v0.16.2
func QueryOld[Result QueryResult](core QueryRunnerInterface, command QueryCommand, args ...any) (any, error)
QueryOld executes a query command and returns the result based on the command type.
func QueryToStruct ¶ added in v0.10.28
func QueryToStruct[Target any](q QueryRunnerInterface, querySQL string, args ...any) ([]Target, error)
QueryToStruct executes a SQL query and scans the results into a slice of the target type.
func SerializeArray ¶ added in v0.6.10
SerializeArray serializes an any slice into a PostgreSQL array literal.
func SerializeStringArray ¶ added in v0.6.10
SerializeStringArray serializes a string slice into a PostgreSQL array literal.
Types ¶
type CommandInterface ¶ added in v0.16.2
CommandInterface defines the interface for query commands that provide SQL and arguments.
type ContextKey ¶ added in v0.6.1
type ContextKey string
ContextKey is the type for context keys used in audit trail variable propagation.
type DBMode ¶ added in v0.6.3
type DBMode int
DBMode represents the type of database backend (e.g. Oracle, Postgres).
const ( // Oracle is the DBMode for Oracle databases. Oracle DBMode = iota // Postgres is the DBMode for PostgreSQL databases. Postgres // Sqlite is the DBMode for SQLite databases. Sqlite // MockDB is the DBMode for mock databases used in testing. MockDB // MySql is the DBMode for MySQL databases. //revive:disable-next-line:var-naming MySql )
func DBModeString ¶ added in v0.6.3
DBModeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.
func DBModeValues ¶ added in v0.6.3
func DBModeValues() []DBMode
DBModeValues returns all values of the enum
func (DBMode) IsADBMode ¶ added in v0.6.3
IsADBMode returns "true" if the value is listed in the enum definition. "false" otherwise
func (DBMode) MarshalJSON ¶ added in v0.6.3
MarshalJSON implements the json.Marshaler interface for DBMode
func (*DBMode) UnmarshalJSON ¶ added in v0.6.3
UnmarshalJSON implements the json.Unmarshaler interface for DBMode
type DmlCommand ¶ added in v0.4.21
type DmlCommand struct {
Name string
Command string
CommandMap map[DBMode]string
Args []any
Type DmlCommandType
CustomError error
}
DmlCommand represents a single DML command with optional database-specific variants.
func (DmlCommand) Execute ¶ added in v0.4.21
func (command DmlCommand) Execute(core QueryRunnerInterface, moduleName, methodName string) (any, error)
Execute runs the DML command using a background context.
func (DmlCommand) ExecuteWithContext ¶ added in v0.6.1
func (command DmlCommand) ExecuteWithContext(w context.Context, parser webFramework.RequestParser, moduleName, methodName string, core QueryRunnerInterface) (any, error)
ExecuteWithContext runs the DML command with the given context, parser, and query runner.
func (DmlCommand) GetArgs ¶ added in v0.16.2
func (d DmlCommand) GetArgs() []any
GetArgs returns the arguments for the DML command.
func (DmlCommand) GetCommand ¶ added in v0.16.2
func (d DmlCommand) GetCommand(mode DBMode) string
GetCommand returns the SQL command for the given database mode.
func (DmlCommand) GetType ¶ added in v0.16.2
func (d DmlCommand) GetType() int
GetType returns the integer type of the DML command.
type DmlCommandType ¶ added in v0.5.0
type DmlCommandType int
DmlCommandType represents the type of a DML command (insert, update, delete, etc.).
const ( // QueryCheckNotExists is the DML command type for verifying a record does not exist. QueryCheckNotExists DmlCommandType = iota // QueryCheckExists is the DML command type for verifying a record exists. QueryCheckExists // Insert is the DML command type for insert operations. Insert // Update is the DML command type for update operations. Update // Delete is the DML command type for delete operations. Delete )
func DmlCommandTypeString ¶ added in v0.5.0
func DmlCommandTypeString(s string) (DmlCommandType, error)
DmlCommandTypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.
func DmlCommandTypeValues ¶ added in v0.5.0
func DmlCommandTypeValues() []DmlCommandType
DmlCommandTypeValues returns all values of the enum
func (DmlCommandType) IsADmlCommandType ¶ added in v0.5.0
func (i DmlCommandType) IsADmlCommandType() bool
IsADmlCommandType returns "true" if the value is listed in the enum definition. "false" otherwise
func (DmlCommandType) MarshalJSON ¶ added in v0.5.0
func (i DmlCommandType) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface for DmlCommandType
func (DmlCommandType) String ¶ added in v0.5.0
func (i DmlCommandType) String() string
func (*DmlCommandType) UnmarshalJSON ¶ added in v0.5.0
func (i *DmlCommandType) UnmarshalJSON(data []byte) error
UnmarshalJSON implements the json.Unmarshaler interface for DmlCommandType
type DmlModel ¶ added in v0.4.21
type DmlModel interface {
PreControlCommands() map[string][]DmlCommand
DmlCommands() map[string][]DmlCommand
FinalizeCommands() map[string][]DmlCommand
}
DmlModel defines the interface for models that provide pre-control, DML, and finalize commands.
type DmlResult ¶
type DmlResult struct {
Rows map[string]string `json:"rows" form:"rows"`
LastInsertId int64 `json:"lastId" form:"lastId"`
RowsAffected int64 `json:"rowsAffected" form:"rowsAffected"`
}
DmlResult holds the result of a DML operation including affected rows and output parameters.
func GetDmlResult ¶ added in v0.8.5
GetDmlResult builds a DmlResult from a SQL result and output parameter rows.
func (*DmlResult) LoadFromMap ¶
LoadFromMap loads DML result data from a map by JSON round-trip.
type QueryCommand ¶ added in v0.5.0
type QueryCommand struct {
Name string
Command string
CommandMap map[DBMode]string
Type QueryCommandType
Args []any
}
QueryCommand represents a single query command with optional database-specific variants.
func (QueryCommand) GetArgs ¶ added in v0.16.2
func (q QueryCommand) GetArgs() []any
GetArgs returns the arguments for the query command.
func (QueryCommand) GetCommand ¶ added in v0.16.2
func (q QueryCommand) GetCommand(mode DBMode) string
GetCommand returns the SQL command for the given database mode.
func (QueryCommand) GetDriverArgs ¶ added in v0.9.21
func (q QueryCommand) GetDriverArgs(req any) []driver.Value
GetDriverArgs resolves form-tagged arguments from the request into driver values.
func (QueryCommand) GetType ¶ added in v0.16.2
func (q QueryCommand) GetType() int
GetType returns the integer type of the query command.
type QueryCommandType ¶ added in v0.5.0
type QueryCommandType int
QueryCommandType represents the type of a query command (select, procedure, etc.).
const ( // QuerySingle indicates a query that expects exactly one row. QuerySingle QueryCommandType = iota // QueryAll indicates a query that returns all matching rows. QueryAll // QueryMap indicates a query that returns results as a key-value map. QueryMap // Transforms indicates a query with custom row transformation. Transforms )
func QueryCommandTypeString ¶ added in v0.5.0
func QueryCommandTypeString(s string) (QueryCommandType, error)
QueryCommandTypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.
func QueryCommandTypeValues ¶ added in v0.5.0
func QueryCommandTypeValues() []QueryCommandType
QueryCommandTypeValues returns all values of the enum
func (QueryCommandType) IsAQueryCommandType ¶ added in v0.5.0
func (i QueryCommandType) IsAQueryCommandType() bool
IsAQueryCommandType returns "true" if the value is listed in the enum definition. "false" otherwise
func (QueryCommandType) MarshalJSON ¶ added in v0.5.0
func (i QueryCommandType) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface for QueryCommandType
func (QueryCommandType) String ¶ added in v0.5.0
func (i QueryCommandType) String() string
func (*QueryCommandType) UnmarshalJSON ¶ added in v0.5.0
func (i *QueryCommandType) UnmarshalJSON(data []byte) error
UnmarshalJSON implements the json.Unmarshaler interface for QueryCommandType
type QueryData ¶
type QueryData struct {
DataRaw string `json:"result,omitempty" db:"result"`
Key string `json:"key,omitempty" db:"key"`
Value string `json:"value,omitempty" db:"value"`
ValueArray []string `json:"valueArray,omitempty" db:"values"`
MapList string `json:"mapList,omitempty" db:"map_list"`
}
QueryData holds a generic query result row with key-value and array fields.
type QueryRequest ¶ added in v0.5.4
QueryRequest defines the interface for requests that provide query arguments.
type QueryResult ¶ added in v0.5.0
QueryResult defines the interface for query results that expose an ID and value.
type QueryRunnerInterface ¶
type QueryRunnerInterface interface {
NewStatement(command string) (*sql.Stmt, error)
CallDbFunction(callString string, args ...any) (int, string, error)
GetModule() (string, string)
InsertRow(insert string, args ...any) (sql.Result, error)
Dml(ctx context.Context, moduleName, methodName, command string, args ...any) (sql.Result, error)
SetVariableCommand() string
//Used in mock db for test
Close()
GetDbMode() DBMode
}
QueryRunnerInterface defines the methods for executing database queries and DML operations.
type QueryRunnerModel ¶
type QueryRunnerModel struct {
DB *sql.DB
ProgramName string
ModuleName string
SetVariable string
Mode DBMode
}
QueryRunnerModel holds the database connection and metadata for query execution.
func Init ¶ added in v0.6.3
Init creates a QueryRunnerModel with the database-specific set variable command.
func (QueryRunnerModel) CallDbFunction ¶
CallDbFunction executes a database function with the given call string and arguments.
func (QueryRunnerModel) Close ¶ added in v0.9.1
func (m QueryRunnerModel) Close()
Close closes the database connection for mock DB mode.
func (QueryRunnerModel) Dml ¶ added in v0.6.1
func (m QueryRunnerModel) Dml(ctx context.Context, moduleName, methodName, command string, args ...any) (sql.Result, error)
Dml executes a DML statement within a transaction with audit trail variables set.
func (QueryRunnerModel) GetDbMode ¶ added in v0.16.2
func (m QueryRunnerModel) GetDbMode() DBMode
GetDbMode returns the database mode of the query runner.
func (QueryRunnerModel) GetModule ¶
func (m QueryRunnerModel) GetModule() (string, string)
GetModule returns the module and program names of the query runner.
func (QueryRunnerModel) InsertRow ¶
InsertRow executes an insert statement with the given arguments.
func (QueryRunnerModel) NewStatement ¶ added in v0.10.28
func (m QueryRunnerModel) NewStatement(command string) (*sql.Stmt, error)
NewStatement prepares a SQL statement on the database.
func (QueryRunnerModel) SetModifVariables ¶ added in v0.6.1
func (m QueryRunnerModel) SetModifVariables(ctx context.Context, moduleName, methodName string, tx *sql.Tx) error
SetModifVariables sets audit trail modification variables on the transaction.
func (QueryRunnerModel) SetVariableCommand ¶ added in v0.6.1
func (m QueryRunnerModel) SetVariableCommand() string
SetVariableCommand returns the database-specific command for setting session variables.
type QueryWithDeps ¶
type QueryWithDeps interface {
GetFillable(core QueryRunnerInterface) (map[string]any, error)
}
QueryWithDeps defines the interface for queries with fillable dependencies.
type RecordDataDml ¶
type RecordDataDml interface {
SetID(string)
CheckDuplicate(core QueryRunnerInterface) (int, string, error)
Filler(headers map[string][]string, core QueryRunnerInterface, args ...any) (string, error)
Post(core QueryRunnerInterface, args map[string]string) (DmlResult, int, string, error)
CheckExistence(core QueryRunnerInterface) (int, string, error)
PreControl(core QueryRunnerInterface) (int, string, error)
Put(core QueryRunnerInterface, args map[string]string) (DmlResult, int, string, error)
}
RecordDataDml defines the interface for DML operations on record data.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package liborm provides ORM-based query execution using Gorm.
|
Package liborm provides ORM-based query execution using Gorm. |
|
Package mockdb provides mock database helpers for testing query runners.
|
Package mockdb provides mock database helpers for testing query runners. |
|
Package main provides database notification/listen utilities for PostgreSQL.
|
Package main provides database notification/listen utilities for PostgreSQL. |