feat(dbmanager): add support for existing SQL connections

* Introduced NewConnectionFromDB function to create connections from existing *sql.DB instances.
* Added ExistingDBProvider to wrap existing database connections for dbmanager features.
* Implemented tests for NewConnectionFromDB and ExistingDBProvider functionalities.
This commit is contained in:
Hein
2026-01-13 12:50:12 +02:00
parent cf6a81e805
commit 276854768e
4 changed files with 531 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package dbmanager
import (
"database/sql"
"fmt"
"github.com/bitechdev/ResolveSpec/pkg/dbmanager/providers"
@@ -49,3 +50,18 @@ func createProvider(dbType DatabaseType) (Provider, error) {
// Provider is an alias to the providers.Provider interface
// This allows dbmanager package consumers to use Provider without importing providers
type Provider = providers.Provider
// NewConnectionFromDB creates a new Connection from an existing *sql.DB
// This allows you to use dbmanager features (ORM wrappers, health checks, etc.)
// with a database connection that was opened outside of dbmanager
//
// Parameters:
// - name: A unique name for this connection
// - dbType: The database type (DatabaseTypePostgreSQL, DatabaseTypeSQLite, or DatabaseTypeMSSQL)
// - db: An existing *sql.DB connection
//
// Returns a Connection that wraps the existing *sql.DB
func NewConnectionFromDB(name string, dbType DatabaseType, db *sql.DB) Connection {
provider := providers.NewExistingDBProvider(db, name)
return newSQLConnection(name, dbType, ConnectionConfig{Name: name, Type: dbType}, provider)
}