Execution
In this section we'll learn how to execute queries in various modes as well as how we unmarshall the data into a Go structure. We'll also learn how to execute multiple query blocks
#
Execute QueryIn order to execute a query we must run the Execute
method.
Every time we call Execute
a new transaction is created, and it is committed upon success.
#
Manual Transactionif you want control over the transaction you can certainly do that.
We do that passing dqlx.WithTnx()
function to the Execute
method
#
Read Only TransactionSimilarly, we use WithReadOnly
if we want to mark this query to be read-only
#
UnmarshallingOnce the query is executed successfully, we want to grab the result set and Unmarshall it into a go data structure.
We can Unmarshall the results by calling Unmarshall
on the response object.
Another way to achieve the same as above but with fewer lines of code is to use the UnmarshallInto
method during the building phase.
#
StructsYou can also Unmarshall into well-defined structs, which is usually the recommended way.
The marshaller takes in consideration the json:
tag of the field to automatically map the data.