Created
November 6, 2015 22:40
-
-
Save squishykid/b18856fbaabeece9e75b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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