Create common db package. Move original message to fetch to db.
This commit is contained in:
43
db/util.go
Normal file
43
db/util.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
var (
|
||||
user = flag.String("dbuser", "gomail", "PostgreSQL user name")
|
||||
name = flag.String("dbname", "gomail", "PostgreSQL DB name")
|
||||
sslmode = flag.String("dbsslmode", "disable", "PostgreSQL sslmode setting")
|
||||
)
|
||||
|
||||
type conn struct {
|
||||
*sql.DB
|
||||
}
|
||||
|
||||
// NewDB creates connection to PostgreSQL DB. If dsn is empty, the flags
|
||||
// -dbuser, -dbname, -dbsslmode are used.
|
||||
func NewConn(dsn string) (*conn, error) {
|
||||
if dsn == "" {
|
||||
dsn = fmt.Sprintf("user=%s dbname=%s sslmode=%s", *user, *name,
|
||||
*sslmode)
|
||||
}
|
||||
db, err := sql.Open("postgres", dsn)
|
||||
return &conn{DB: db}, err
|
||||
}
|
||||
|
||||
func (c *conn) OriginalBlobByHash(hash string, blob *[]byte) error {
|
||||
row := c.QueryRow(`
|
||||
SELECT
|
||||
blob
|
||||
FROM
|
||||
original
|
||||
WHERE
|
||||
hash = $1
|
||||
`, hash)
|
||||
|
||||
return row.Scan(blob)
|
||||
}
|
||||
Reference in New Issue
Block a user