route/vendor/github.com/gchaincl/dotsql/examples_test.go

37 lines
651 B
Go
Raw Normal View History

2018-01-22 04:29:29 +00:00
package dotsql_test
import (
"database/sql"
"github.com/gchaincl/dotsql"
)
func ExampleLoadFromFile() {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
panic(err)
}
dot, err := dotsql.LoadFromFile("queries/users.sql")
if err != nil {
panic(err)
}
/* users.sql looks like:
--name: create-user
INSERT INTO users (email) VALUES(?)
--name: find-user-by-email
SELECT email FROM users WHERE email = ?
*/
if _, err := dot.Exec(db, "create-user", "user@example.com"); err != nil {
panic(err)
}
rows, err := dot.Query(db, "find-user-by-email", "user@example.com")
if err != nil {
panic(err)
}
defer rows.Close()
}