2020-10-16 22:26:47 +02:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"log"
|
2020-10-20 20:00:46 +02:00
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
2020-10-16 22:26:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// SQLStore is a SQL database
|
|
|
|
type SQLStore struct {
|
|
|
|
db *sql.DB
|
|
|
|
dbType string
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new SQL implementation of the store
|
|
|
|
func New(dbType, connectionString string) (*SQLStore, error) {
|
|
|
|
log.Println("connectDatabase")
|
|
|
|
var err error
|
|
|
|
|
|
|
|
db, err := sql.Open(dbType, connectionString)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("connectDatabase: ", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Ping()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(`Database Ping failed`)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
store := &SQLStore{
|
|
|
|
db: db,
|
|
|
|
dbType: dbType,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.Migrate()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(`Table creation failed`)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return store, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLStore) Shutdown() error {
|
|
|
|
return s.db.Close()
|
|
|
|
}
|
2020-10-20 20:00:46 +02:00
|
|
|
|
|
|
|
func (s *SQLStore) getQueryBuilder() sq.StatementBuilderType {
|
|
|
|
builder := sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
|
|
|
|
return builder.RunWith(s.db)
|
|
|
|
}
|