Documentation
¶
Overview ¶
Provides support for named parameters in SQL queries used by Go / golang programs and libraries.
Named parameters are not supported by all SQL query engines, and their standards are scattered. But positional parameters have wide adoption across all databases.
This package translates SQL queries which use named parameters into queries which use positional parameters.
Example usage:
query := NewNamedParameterQuery(" SELECT * FROM table WHERE col1 = :foo ") query.SetValue("foo", "bar") connection, _ := sql.Open("mysql", "user:pass@tcp(localhost:3306)/db") connection.QueryRow(query.GetParsedQuery(), (query.GetParsedParameters())...)
In the example above, note the format of "QueryRow". In order to use named parameter queries, you will need to use this exact format, including the variadic symbol "..."
Note that the example above uses "QueryRow", but named parameters used in this fashion work equally well for "Query" and "Exec".
It's also possible to pass in a map, instead of defining each parameter individually:
query := NewNamedParameterQuery(" SELECT * FROM table WHERE col1 = :foo AND col2 IN(:firstName, :middleName, :lastName) ") var parameterMap = map[string]interface{} { "foo": "bar", "firstName": "Alice", "lastName": "Bob" "middleName": "Eve", } query.SetValuesFromMap(parameterMap) connection, _ := sql.Open("mysql", "user:pass@tcp(localhost:3306)/db") connection.QueryRow(query.GetParsedQuery(), (query.GetParsedParameters())...)
But of course, sometimes you just want to pass in an entire struct. No problem:
type QueryValues struct { Foo string `sqlParameterName:"foo"` FirstName string `sqlParameterName:"firstName"` MiddleName string `sqlParameterName:"middleName"` LastName string `sqlParameterName:"lirstName"` } query := NewNamedParameterQuery(" SELECT * FROM table WHERE col1 = :foo AND col2 IN(:firstName, :middleName, :lastName) ") parameter = new(QueryValues) query.SetValuesFromStruct(parameter) connection, _ := sql.Open("mysql", "user:pass@tcp(localhost:3306)/db") connection.QueryRow(query.GetParsedQuery(), (query.GetParsedParameters())...)
Index ¶
- type NamedParameterQuery
- func (this *NamedParameterQuery) GetParsedParameters() []interface{}
- func (this *NamedParameterQuery) GetParsedQuery() string
- func (this *NamedParameterQuery) SetValue(parameterName string, parameterValue interface{})
- func (this *NamedParameterQuery) SetValuesFromMap(parameters map[string]interface{})
- func (this *NamedParameterQuery) SetValuesFromStruct(parameters interface{}) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NamedParameterQuery ¶
type NamedParameterQuery struct {
// contains filtered or unexported fields
}
NamedParameterQuery handles the translation of named parameters to positional parameters, for SQL statements. It is not recommended to create zero-valued NamedParameterQuery objects by yourself; instead use NewNamedParameterQuery
func NewNamedParameterQuery ¶
func NewNamedParameterQuery(queryText string) *NamedParameterQuery
NewNamedParameterQuery creates a new named parameter query using the given [queryText] as a SQL query which contains named parameters. Named parameters are identified by starting with a ":" e.g., ":name" refers to the parameter "name", and ":foo" refers to the parameter "foo".
Except for their names, named parameters follow all the same rules as positional parameters; they cannot be inside quoted strings, and cannot inject statements into a query. They can only be used to insert values.
func (*NamedParameterQuery) GetParsedParameters ¶
func (this *NamedParameterQuery) GetParsedParameters() []interface{}
GetParsedParameters returns an array of parameter objects that match the positional parameter list from GetParsedQuery
func (*NamedParameterQuery) GetParsedQuery ¶
func (this *NamedParameterQuery) GetParsedQuery() string
GetParsedQuery returns a version of the original query text whose named parameters have been replaced by positional parameters.
func (*NamedParameterQuery) SetValue ¶
func (this *NamedParameterQuery) SetValue(parameterName string, parameterValue interface{})
SetValue sets the value of the given [parameterName] to the given [parameterValue]. If the parsed query does not have a placeholder for the given [parameterName], this method does nothing.
func (*NamedParameterQuery) SetValuesFromMap ¶
func (this *NamedParameterQuery) SetValuesFromMap(parameters map[string]interface{})
SetValuesFromMap uses every key/value pair in the given [parameters] as a parameter replacement for this query. This is equivalent to calling SetValue for every key/value pair in the given [parameters] map. If there are any keys/values present in the map that aren't part of the query, they are ignored.
func (*NamedParameterQuery) SetValuesFromStruct ¶
func (this *NamedParameterQuery) SetValuesFromStruct(parameters interface{}) error
SetValuesFromStruct uses reflection to find every public field of the given struct [parameters] and set their key/value as named parameters in this query. If the given [parameters] is not a struct, this will return an error.
If you do not wish for a field in the struct to be added by its literal name, The struct may optionally specify the sqlParameterName as a tag on the field. e.g., a struct field may say something like:
type Test struct { Foo string `sqlParameterName:"foobar"` }