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 ( // 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") )
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(setID string, queryID string) (string, error)
// MustGet returns a query by set ID and query ID.
// It panics if the set or query is not found.
MustGet(setID string, queryID 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 ¶ added in v0.2.0
Get retrieves a specific SQL query by its set ID and query ID. It returns an error if the query set or the query itself cannot be found.
func (*SQLSet) GetQueryIDs ¶ added in v0.2.0
GetQueryIDs returns a sorted slice of all query IDs within a specific query set.
func (*SQLSet) GetSetsMetas ¶ added in v0.2.0
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.