2015-03-04 19:09:08 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2015-03-05 12:53:38 +00:00
|
|
|
"fmt"
|
2015-03-04 19:14:11 +00:00
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2015-03-04 19:09:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
conn *sql.DB
|
|
|
|
stmts map[string]*sql.Stmt
|
|
|
|
)
|
|
|
|
|
|
|
|
func Connect(uri string) (err error) {
|
|
|
|
conn, err = sql.Open("mysql", uri)
|
|
|
|
stmts = map[string]*sql.Stmt{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func stmt(query string) *sql.Stmt {
|
|
|
|
if stmt, ok := stmts[query]; ok {
|
|
|
|
return stmt
|
|
|
|
} else {
|
|
|
|
stmt := prepareStatement(query)
|
|
|
|
stmts[query] = stmt
|
|
|
|
return stmt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareStatement(query string) *sql.Stmt {
|
2015-03-05 12:53:38 +00:00
|
|
|
if stmt, err := conn.Prepare(query); err == nil {
|
2015-03-04 19:09:08 +00:00
|
|
|
return stmt
|
|
|
|
} else {
|
2015-03-05 12:53:38 +00:00
|
|
|
fmt.Println(query)
|
2015-03-04 19:09:08 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|