aboutsummaryrefslogtreecommitdiffstats
path: root/internal/storage/db.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/storage/db.go')
-rw-r--r--internal/storage/db.go65
1 files changed, 34 insertions, 31 deletions
diff --git a/internal/storage/db.go b/internal/storage/db.go
index d83e71d..7f1d93d 100644
--- a/internal/storage/db.go
+++ b/internal/storage/db.go
@@ -1,16 +1,18 @@
package storage
import (
- "database/sql"
"errors"
"fmt"
- "slices"
"github.com/eugenetriguba/bolt/internal/configloader"
- _ "github.com/jackc/pgx/v5/stdlib"
+ "github.com/upper/db/v4"
+ "github.com/upper/db/v4/adapter/mysql"
+ "github.com/upper/db/v4/adapter/postgresql"
)
-var supportedDrivers = []string{"postgres"}
+var postgresqlDriverName = "postgresql"
+var mysqlDriverName = "mysql"
+var supportedDrivers = []string{postgresqlDriverName, mysqlDriverName}
var (
ErrMalformedConnectionString = errors.New(
@@ -20,42 +22,43 @@ var (
ErrUnsupportedDriver = fmt.Errorf("unsupported driver, supported drivers are %s", supportedDrivers)
)
-// DBConnect establishes a connection to the database using the driver
-// and connection information. Only "postgres" is supported as the driver.
+type DB struct {
+ Session db.Session
+}
+
+// DBConnect establishes a connection to the database using the
+// connection configuration.
//
// The following errors may be returned:
-// - ErrMalformedConnectionString: The provided connection parameters are
-// not in a valid format.
// - ErrUnableToConnect: Unable to make a connection to the database with
// the provided connection parameters.
// - ErrUnsupportedDriver: The provided driver is not supported.
-func DBConnect(driver string, connectionParams string) (*sql.DB, error) {
- if !slices.Contains(supportedDrivers, driver) {
- return nil, ErrUnsupportedDriver
- }
+func DBConnect(cfg configloader.ConnectionConfig) (DB, error) {
+ var db db.Session
+ var err error = nil
- db, err := sql.Open("pgx", connectionParams)
- if err != nil {
- return nil, fmt.Errorf("%w: %v", ErrMalformedConnectionString, err)
+ switch cfg.Driver {
+ case postgresqlDriverName:
+ db, err = postgresql.Open(postgresql.ConnectionURL{
+ User: cfg.User,
+ Password: cfg.Password,
+ Host: cfg.Host,
+ Database: cfg.DBName,
+ })
+ case mysqlDriverName:
+ db, err = mysql.Open(mysql.ConnectionURL{
+ User: cfg.User,
+ Password: cfg.Password,
+ Host: cfg.Host,
+ Database: cfg.DBName,
+ })
+ default:
+ return DB{}, ErrUnsupportedDriver
}
- // Note: `sql.Open` only validates the connection string we provided is sane.
- // It doesn't actually open up a connection to the database. For that, we ping
- // the database to ensure the connection string is fully valid.
- err = db.Ping()
if err != nil {
- return nil, fmt.Errorf("%w: %v", ErrUnableToConnect, err)
+ return DB{}, fmt.Errorf("%w: %v", ErrUnableToConnect, err)
}
- return db, nil
-}
-
-// DBConnectionString generates a properly formatted connection
-// string that can be used to establish a database connection.
-// Note that it always creates a connection string with sslmode disabled.
-func DBConnectionString(cfg *configloader.ConnectionConfig) string {
- return fmt.Sprintf(
- "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
- cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName,
- )
+ return DB{Session: db}, nil
}