v1.3.0 Latest Latest

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

Go to latest
Published: Feb 8, 2019 License: Apache-2.0 Imports: 12 Imported by: 2



Package dsquery provides mechanisms for managing templated queries to be executed against a data source.

The types in this package are agnostic to the type of data source being used (e.g. RDBMS, NoSQL, cache). Instead, these types are concerned with reading templated queries from files and populating those templates with variables a runtime. The actual execution of queries is the responsibility of clients that understand the type of data source in use (see the rdbms package).

Most Granitic applications requiring access to a data source will enable the QueryManager facility which provides access to an instance of the TemplatedQueryManager type defined in this package. Instructions on configuring and using the QueryManager facility can be found at http://granitic.io/1.0/ref/query-manager also see the package documentation for the facility/querymanager package for some basic examples.



This section is empty.


This section is empty.


func NewQueryTemplateToken

func NewQueryTemplateToken(tokenType queryTokenType) *queryTemplateToken


type ConfigurableProcessor added in v1.1.0

type ConfigurableProcessor struct {
	// Whether string parameters should have their contents wrapped with the string defined in StringWrapWith before
	// being injected into the query. For example, SQL RDBMSs usually require strings to be wrapped with ' or "
	WrapStrings bool

	// Whether or not strings that are set to the same value as DefaultParameterValue should be wrapped
	DisableWrapWhenDefaultParameterValue bool

	// Use the value of 'DefaultParameterValue' instead of returning an error if a parameter required for a query is missing
	UseDefaultForMissingParameter bool

	// The value to use when populating a query if the named value is missing
	DefaultParameterValue interface{}

	// If a default value has been substituted for a missing parameter, prevent further escaping
	EscapeDefaultValues bool

	// A string that will be used as a prefix and suffix to a string parameter if WrapStrings is true.
	StringWrapWith string

ConfigurableProcessor allows missing parameter values to be substituted for a defined value and for string (and nilable string) values to be wrapped with a defined character (e.g. ')

func (*ConfigurableProcessor) EscapeParamValue added in v1.1.0

func (cp *ConfigurableProcessor) EscapeParamValue(v *ParamValueContext)

func (*ConfigurableProcessor) SubstituteUnset added in v1.1.0

func (cp *ConfigurableProcessor) SubstituteUnset(v *ParamValueContext) error

type ParamValueContext added in v1.1.0

type ParamValueContext struct {
	Key     string
	Value   interface{}
	QueryId string
	Escaped bool

type ParamValueProcessor added in v1.1.0

type ParamValueProcessor interface {
	EscapeParamValue(v *ParamValueContext)
	SubstituteUnset(v *ParamValueContext) error

Implemented by components able to escape the value of a parameter to a query and handle unset parameters

type QueryManager

type QueryManager interface {
	// BuildQueryFromId finds a template with the supplied query ID and uses the supplied named parameters to populate
	// that template. Returns the populated query or an error if the template could not be found or there was a problem
	// populating the query.
	BuildQueryFromId(qid string, params map[string]interface{}) (string, error)

	// FragmentFromId is used to recover a template which does not have any parameters to populate (a fragment). This is most commonly
	// used when code needs to dynamically construct a query from several fragments and templates. Returns the fragment
	// or an error if the fragment could not be found.
	FragmentFromId(qid string) (string, error)

A QueryManager is a type that is able to populate a pre-defined template query given a set of named parameters and return a complete query ready for execution against some data source.

type SqlProcessor added in v1.1.0

type SqlProcessor struct {
	BoolTrue  interface{}
	BoolFalse interface{}

SqlProcessor replaces missing values with the word null, wraps strings with single quotes and replaces bool values with the value the BoolTrue and BoolFalse members

func (*SqlProcessor) EscapeParamValue added in v1.1.0

func (sp *SqlProcessor) EscapeParamValue(v *ParamValueContext)

func (*SqlProcessor) SubstituteUnset added in v1.1.0

func (sp *SqlProcessor) SubstituteUnset(v *ParamValueContext) error

type TemplatedQueryManager

type TemplatedQueryManager struct {

	// The path to a folder where template files are stored.
	TemplateLocation string

	// A regular expression that allows variable names to be recognised in queries.
	VarMatchRegEx string

	// Logger used by Granitic framework components. Automatically injected.
	FrameworkLogger logging.Logger

	// Lines in a template file starting with this string are considered to indicate the start of a new query template. The remainder
	// of the line will be used as the ID of that query template.
	QueryIdPrefix string

	// Whether or not query IDs should have leading and trailing whitespace removed.
	TrimIdWhiteSpace bool

	// A component able to handle missing parameter values and the escaping of supplied parameters
	ValueProcessor ParamValueProcessor

	// Whether or not a stock ParamValueProcessor should be injected into this component (set to false if defining your own)
	CreateDefaultValueProcessor bool

	// The character sequence that indicates a new line in a template file (e.g. \n)
	NewLine string
	// contains filtered or unexported fields

An implementation of QueryManager that reads files containing template queries, tokenizes them and populates them on demand with maps of named parameters. This is the implementation provided by the QueryManager facility. See http://granitic.io/1.0/ref/query-manager for details.

func NewTemplatedQueryManager

func NewTemplatedQueryManager() *TemplatedQueryManager

NewTemplatedQueryManager creates a new, empty TemplatedQueryManager.

func (*TemplatedQueryManager) BuildQueryFromId added in v1.1.0

func (qm *TemplatedQueryManager) BuildQueryFromId(qid string, params map[string]interface{}) (string, error)

See QueryManager.BuildQueryFromId

func (*TemplatedQueryManager) FragmentFromId added in v1.1.0

func (qm *TemplatedQueryManager) FragmentFromId(qid string) (string, error)

See QueryManager.FragmentFromId

func (*TemplatedQueryManager) StartComponent

func (qm *TemplatedQueryManager) StartComponent() error

StartComponent is called by the IoC container. Loads, parses and tokenizes query templates. Returns an error if there was a problem loading, parsing or tokenizing.

Jump to

Keyboard shortcuts

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