Bulk query calls

Send multiple query calls in a single query template. Optionally the calls can be sent in a single transaction.

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 }}

Bulk call size

Set the amount of calls in a single bulk query template. This is required to be set before any calls are added. If we intend to send 3 calls in a single bulk query template then we would set the size to 3.

{{ .SetAsBulkQuery 3 }}

Transactional bulk query

We can send the calls in a single transaction by setting the transactional flag to true. This means that if any of the calls fail then all of the calls will be rolled back.

{{ .SetInTransaction }}

Set query

Sets or replaces the query in the given index. Note that in bulk we always pass the index of the call as second parameter. This is required to differenciate the calls from each other.

{{ .SetBulkQuery `INSERT INTO prodgroup (name_eng) VALUES (?)` 0 }}

Add query

Appends to the query in the given index. Useful when adding conditional query elements.

{{ .AddBulkQuery ` WHERE id = ?` 0 }}

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.

{{ .SetBulkColumnType "lastInsertId" "int" 0 }}

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.

{{ .AddBulkArg $id 0 }}

Using first query values in send index calls

Sometimes we need to use the results of the first query in the following calls. We can do this by using the ‘first query values’ argument in the following calls. Note that we need to set the column types for the returned values in the first query for this to work.

{{ .AddBulkArg "0.lastInsertId" 1 }}

From array argument helper

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

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

From string argument helper

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

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