ganef: noun |ˈgä-nəf| THIEF, RASCAL variant goniff
ganef is a simple program to query sqlite databases using Go's templates. ganef takes a sqlite database and a template as input and returns the output of executing the template against the database. ganef adds the query function to the template. query takes an sqlite select statement and returns an array. The array contains a map of {column: value} for each row from the query.
Go 1.8 or higher required.
go get github.com/blezek/ganef# Get an example database
wget -O chinook.zip 'http://www.sqlitetutorial.net/download/sqlite-sample-database/?wpdmdl=94'
unzip chinook.zip
Note, the SQLite driver returns the column names in the actual case used to create the table, not what from the query string.
# Create a template
cat <<EOF > template.txt
All tracks
{{ range query "select TrackId, Name, Composer, unitprice from tracks" }}
TrackID: {{.TrackId}}
Name: {{.Name}}
Composer: {{.Composer}}
UnitPrice: {{.UnitPrice}}
{{end}}
EOF
ganef chinook.db template.txt -
Variables can be passed using multiple -v key=value flags on the command line. They are passed into the template as {{.key}}.
# Create a template
cat <<EOF > album_detail.txt
{{ range printf "select * from albums where albumid = %v" .album | query -}}
Detail for album "{{.Title}}"
{{end }}
{{ range printf "SELECT name, milliseconds, bytes, albumid FROM tracks WHERE albumid = %v;" .album | query -}}
Name: {{.Name}}
Length(ms): {{.Milliseconds}}
Size(bytes): {{.Bytes}}
{{end}}
EOF
ganef -v album=279 chinook.db album_detail.txt -
ganef -v album=275 chinook.db album_detail.txt -
In a clone of the repo (kudos to the fine Hellogopher):
make