Skip to content

Instantly share code, notes, and snippets.

@squishykid
Created November 6, 2015 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squishykid/b18856fbaabeece9e75b to your computer and use it in GitHub Desktop.
Save squishykid/b18856fbaabeece9e75b to your computer and use it in GitHub Desktop.
/*
We want to get a potato out of the DB
*/
const findPotatoes = "SELECT * FROM potato;"
//container - passes child
type scanFunc func(rowScanner)
//child - iterates for each row
type rowScanner func(...interface{}) error
//Entry point
func (s *SQLHandler) findPotato() {
potatoes := []Potato{}
add := func(p Potato) {
potatoes = append(potatoes, p)
}
scan := PotatoScanner(add)
s.queryRows(findPotatoes, scan, 1)
roblog.Dump(potatoes)
}
//one of these for each model
func PotatoScanner(handler func(Potato)) scanFunc {
potato := Potato{}
return func(scan rowScanner) {
err := scan(&potato.Id, &potato.Column1, &potato.Column2)
if err == nil {
handler(potato)
} else {
roblog.Info("Fuck", err.Error())
}
}
}
//generic form of rows queryafier
func (s *SQLHandler) queryRows(statement string, scan scanFunc, args ...interface{}) {
rows, err := s.db.Query(statement, args...)
if err != nil {
panic(err.Error())
}
for rows.Next() {
scan(rows.Scan)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment