Documentation
¶
Overview ¶
Package sqlset is a way to store SQL queries separated from the go code. Query sets are stored in the .sql files, every filename without extension is an SQL set ID. Every file contains queries, marked with query IDs using special syntax, see `testdata/valid/*.sql` files for examples. Also file may contain JSON-encoded query set metadata with name and description.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEmpty is the base error for when an object is empty. ErrEmpty = errors.New("empty") // ErrArgumentEmpty indicates that a argument is empty. ErrArgumentEmpty = fmt.Errorf("argument %w", ErrEmpty) // ErrQuerySetsEmpty indicates that a query sets is empty. ErrQuerySetsEmpty = fmt.Errorf("query sets %w", ErrEmpty) // ErrQuerySetEmpty indicates that a query sets is empty. ErrQuerySetEmpty = fmt.Errorf("query set %w", ErrEmpty) // ErrNotFound is the base error for when an item is not found. ErrNotFound = errors.New("not found") // ErrQuerySetNotFound indicates that a specific query set was not found. ErrQuerySetNotFound = fmt.Errorf("query set %w", ErrNotFound) // ErrQueryNotFound indicates that a specific query was not found within a set. ErrQueryNotFound = fmt.Errorf("query %w", ErrNotFound) // ErrInvalidSyntax is returned when the parser encounters a syntax error in a .sql file. ErrInvalidSyntax = errors.New("invalid SQLSetList syntax") // ErrMaxLineLenExceeded is returned when a line in a .sql file is too long, // which may indicate a corrupted file. ErrMaxLineLenExceeded = errors.New("line too long, possible line corruption") // ErrInvalidArgCount is returned when a function is called // with an invalid number of arguments. ErrInvalidArgCount = errors.New("invalid number of arguments") // ErrRequiredArgMissing is returned when a required argument is not specified. ErrRequiredArgMissing = errors.New("required argument not specified") )
Functions ¶
This section is empty.
Types ¶
type QuerySet ¶
type QuerySet struct {
// contains filtered or unexported fields
}
QuerySet represents a single set of queries, usually from a single .sql file.
func (*QuerySet) GetMeta ¶
func (qs *QuerySet) GetMeta() QuerySetMeta
GetMeta returns the metadata associated with the query set.
type QuerySetMeta ¶
type QuerySetMeta struct {
// ID is the unique identifier for the set, derived from the filename.
ID string `json:"id"`
// Name is a human-readable name for the query set, from the metadata block.
Name string `json:"name"`
// Description provides more details about the query set, from the metadata block.
Description string `json:"description,omitempty"`
}
QuerySetMeta holds the metadata for a query set.
type SQLQueriesProvider ¶
type SQLQueriesProvider interface {
// Get returns a query by set ID and query ID.
// If the set or query is not found, it returns an error.
Get(ids ...string) (string, error)
// MustGet returns a query by set ID and query ID.
// It panics if the set or query is not found.
MustGet(ids ...string) string
}
SQLQueriesProvider is the interface for getting SQL queries.
type SQLSet ¶
type SQLSet struct {
// contains filtered or unexported fields
}
SQLSet is a container for multiple query sets, organized by set ID. It provides methods to access SQL queries and metadata. Use New to create a new instance.
func New ¶
New creates a new SQLSet by walking the directory tree of the provided fsys. It parses all .sql files it finds and adds them to the SQLSet. The walk starts from the root of the fsys. If you are using embed.FS and your queries are in a subdirectory, you should create a sub-filesystem using fs.Sub.
Example with embed.FS:
//go:embed queries var queriesFS embed.FS sqlSet, err := sqlset.New(queriesFS)
func (*SQLSet) Get ¶
Get returns an SQL query by its identifiers.
Supported forms:
Get(setID, queryID) Returns the query identified by queryID from the query set setID.
Get("setID.queryID") Equivalent to Get(setID, queryID).
Get(queryID) Returns the query identified by queryID from the only available query set. If there is more than one query set, an error is returned.
An error is returned if:
- the number of arguments is invalid,
- any identifier is empty,
- the query set or query cannot be found.
func (*SQLSet) GetQueryIDs ¶
GetQueryIDs returns a sorted slice of all query IDs within a specific query set.
func (*SQLSet) GetSetsMetas ¶
func (s *SQLSet) GetSetsMetas() []QuerySetMeta
GetSetsMetas returns a slice of metadata for all the query sets loaded. The order of the returned slice is not guaranteed.
type SQLSetsProvider ¶
type SQLSetsProvider interface {
// GetSetsMetas returns metadata for all registered query sets.
GetSetsMetas() []QuerySetMeta
// GetQueryIDs returns a slice of all query IDs.
GetQueryIDs(setID string) ([]string, error)
}
SQLSetsProvider is the interface for getting information about query sets.