This fragment uses the open database handle db after the previous step creates the people. The query input is in the selected format. It selects each person's name and orders the result by name. Parse it once, open a query against the database, and run it.
Typed row iteration decodes each row into a language value, one native page at a time. C uses the native document directly. The iterator borrows the rows. Release the rows and query when finished: open rows hold a store read transaction. Every page reads the same snapshot. A row that does not fit the type stops iteration with an error that names its path, such as [0].name. The scene encodes the decoded names again only to show the captured result.
Read names as Go structs
queryText holds this document:
find [?name]
where [[?e name ?name]]
orderBy [?name]
type Name struct {
Name string `json:"name"`
}
definition, err := stardust.Parse(queryText, stardust.DUST)
if err != nil {
return err
}
defer definition.Close()
query, err := db.Query(definition)
if err != nil {
return err
}
defer query.Close()
rows, err := query.Run(nil, stardust.RunOptions{}, nil)
if err != nil {
return err
}
defer rows.Close()
var names []Name
for row, err := range stardust.Values[Name](rows) {
if err != nil {
return err
}
names = append(names, row)
}
result, err := stardust.Encode(names)
if err != nil {
return err
}
defer result.Close()[{name Ada} {name Bob} {name Cy}]The captured names are Ada, Bob and Cy in order. Native pages remain available through the Pages iterator when you want documents instead of values. See Language values and documents for the costs of each path.