aboutsummaryrefslogtreecommitdiff
path: root/dbstore/sqlfs.go
blob: 99626a2cb3acf4fc1ddee363bca679cbc3bc4d1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package dbstore

import (
  "embed"
  "fmt"
)

//go:embed sql
var sqlFs embed.FS

// Get single query from embedded sqlFs.
func getQuery(id string) (string, error) {
  // read query
  if data, err := sqlFs.ReadFile(fmt.Sprintf("sql/%s.sql", id)); err != nil {
    return "", err
  } else {
    // return query
    return string(data), nil
  }
}

// Return queries from embedded sqlFs and return map of query ID to SQL.
func getQueries(ids []string) (map[string]string, error) {
  r := make(map[string]string)

  for _, id := range(ids) {
    // read query
    if sql, err := getQuery(id); err != nil {
      return r, fmt.Errorf("%s: %s", id, err.Error())
    } else {
      // save query
      r[id] = sql
    }
  }

  // return success
  return r, nil
}