Created
November 20, 2014 22:41
-
-
Save nickvanw/4bcf857c1f04d5c08e39 to your computer and use it in GitHub Desktop.
Go SQL Queries
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
package datastore | |
import ( | |
"database/sql" | |
) | |
const ( | |
createUser = ` | |
INSERT INTO users (name, email) | |
VALUES(?, | |
?)` | |
findUser = ` | |
SELECT id, | |
name, | |
FROM users | |
WHERE email = ? LIMIT 1` | |
) | |
var stmts = struct { | |
createUser *sql.Stmt | |
findUser *sql.Stmt | |
}{} | |
var stmtSql = map[**sql.Stmt]string{ | |
&stmts.createUser: createUser, | |
&stmts.findUser: findUser, | |
} | |
// initQueries loops through all of the statements and prepares them to make sure they're valid. | |
func initQueries(db *sql.DB) error { | |
var err error | |
for stmt, sql := range stmtSql { | |
*stmt, err = db.Prepare(sql) | |
if err != nil { | |
return err | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment