otter-space-sdk/internal/postgres/custom.go
SoXX c2ab455d3e
All checks were successful
Gitea Build Check / Build (pull_request) Successful in 2m33s
Gitea Build Check / Build (push) Successful in 2m25s
feat: custom query's
fixed function for custom query's & added a function with return
2024-07-27 20:01:06 +02:00

43 lines
811 B
Go

package postgres
import (
"context"
"database/sql"
"errors"
"gorm.io/gorm"
)
func ExecuteRawStatement(ctx context.Context, db *gorm.DB, query string, args ...any) error {
if query == "" {
return errors.New("query can not be empty")
}
if args == nil {
return errors.New("arguments can not be nil")
}
result := db.WithContext(ctx).Exec(query, args...)
if result.Error != nil {
return result.Error
}
return nil
}
func QueryRawStatement(ctx context.Context, db *gorm.DB, query string, args ...any) (*sql.Rows, error) {
if query == "" {
return nil, errors.New("query can not be empty")
}
if args == nil {
return nil, errors.New("arguments can not be nil")
}
result, err := db.WithContext(ctx).Raw(query, args...).Rows()
if err != nil {
return nil, err
}
return result, nil
}