Single query calls

Send a single query call and use its results.

Usage

Read arguments

Read an argument values from input index. The value keeps its original type. Note that gjson.Result type does not convert to the contained type automatically so it would need to be converted first (ex: value.Int, value.String etc).

{{ $id := .Arg 0 }}

Set query

Sets or replaces the current query.

{{ .SetQuery `SELECT id FROM product` }}

Add query

Appends to the query. Useful when adding conditional query elements.

{{ .AddQuery ` WHERE id = ?` }}

Return column types

By default, all values in the result are returned as strings (or not at all). By setting the column types we can return the values in their original type.

{{ .SetColumnTypes "id" "int" }}

Arguments

Important! Always use ? placeholders and the following set argument call for queries. This protects against injections, filling argument values directly into queries poses severe security risks.

{{ .AddArg $id }}

From array argument helper

We can use a helper to fill query in values from an array.

{{ .AddQueryAndArgsFromArray ` WHERE id IN (?)` $ids }}

From string argument helper

We can use a helper to fill query in values from a comma separated string.

{{ .AddQueryAndArgsFromString ` WHERE id IN (?)` $ids "," }}