mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-16 07:54:25 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c2d86c9880 | |||
| 70bf0a4be1 |
@@ -3,6 +3,7 @@ package dbmanager
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -159,6 +160,9 @@ func (c *sqlConnection) Close() error {
|
|||||||
|
|
||||||
// HealthCheck verifies the connection is alive
|
// HealthCheck verifies the connection is alive
|
||||||
func (c *sqlConnection) HealthCheck(ctx context.Context) error {
|
func (c *sqlConnection) HealthCheck(ctx context.Context) error {
|
||||||
|
if c == nil {
|
||||||
|
return fmt.Errorf("connection is nil")
|
||||||
|
}
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
@@ -188,6 +192,9 @@ func (c *sqlConnection) Reconnect(ctx context.Context) error {
|
|||||||
|
|
||||||
// Native returns the native *sql.DB connection
|
// Native returns the native *sql.DB connection
|
||||||
func (c *sqlConnection) Native() (*sql.DB, error) {
|
func (c *sqlConnection) Native() (*sql.DB, error) {
|
||||||
|
if c == nil {
|
||||||
|
return nil, fmt.Errorf("connection is nil")
|
||||||
|
}
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
if c.nativeDB != nil {
|
if c.nativeDB != nil {
|
||||||
defer c.mu.RUnlock()
|
defer c.mu.RUnlock()
|
||||||
@@ -219,6 +226,9 @@ func (c *sqlConnection) Native() (*sql.DB, error) {
|
|||||||
|
|
||||||
// Bun returns a Bun ORM instance wrapping the native connection
|
// Bun returns a Bun ORM instance wrapping the native connection
|
||||||
func (c *sqlConnection) Bun() (*bun.DB, error) {
|
func (c *sqlConnection) Bun() (*bun.DB, error) {
|
||||||
|
if c == nil {
|
||||||
|
return nil, fmt.Errorf("connection is nil")
|
||||||
|
}
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
if c.bunDB != nil {
|
if c.bunDB != nil {
|
||||||
defer c.mu.RUnlock()
|
defer c.mu.RUnlock()
|
||||||
@@ -249,6 +259,9 @@ func (c *sqlConnection) Bun() (*bun.DB, error) {
|
|||||||
|
|
||||||
// GORM returns a GORM instance wrapping the native connection
|
// GORM returns a GORM instance wrapping the native connection
|
||||||
func (c *sqlConnection) GORM() (*gorm.DB, error) {
|
func (c *sqlConnection) GORM() (*gorm.DB, error) {
|
||||||
|
if c == nil {
|
||||||
|
return nil, fmt.Errorf("connection is nil")
|
||||||
|
}
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
if c.gormDB != nil {
|
if c.gormDB != nil {
|
||||||
defer c.mu.RUnlock()
|
defer c.mu.RUnlock()
|
||||||
@@ -283,6 +296,9 @@ func (c *sqlConnection) GORM() (*gorm.DB, error) {
|
|||||||
|
|
||||||
// Database returns the common.Database interface using the configured default ORM
|
// Database returns the common.Database interface using the configured default ORM
|
||||||
func (c *sqlConnection) Database() (common.Database, error) {
|
func (c *sqlConnection) Database() (common.Database, error) {
|
||||||
|
if c == nil {
|
||||||
|
return nil, fmt.Errorf("connection is nil")
|
||||||
|
}
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
defaultORM := c.config.DefaultORM
|
defaultORM := c.config.DefaultORM
|
||||||
c.mu.RUnlock()
|
c.mu.RUnlock()
|
||||||
@@ -307,6 +323,9 @@ func (c *sqlConnection) MongoDB() (*mongo.Client, error) {
|
|||||||
|
|
||||||
// Stats returns connection statistics
|
// Stats returns connection statistics
|
||||||
func (c *sqlConnection) Stats() *ConnectionStats {
|
func (c *sqlConnection) Stats() *ConnectionStats {
|
||||||
|
if c == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
defer c.mu.RUnlock()
|
defer c.mu.RUnlock()
|
||||||
|
|
||||||
@@ -336,6 +355,9 @@ func (c *sqlConnection) Stats() *ConnectionStats {
|
|||||||
|
|
||||||
// getBunAdapter returns or creates the Bun adapter
|
// getBunAdapter returns or creates the Bun adapter
|
||||||
func (c *sqlConnection) getBunAdapter() (common.Database, error) {
|
func (c *sqlConnection) getBunAdapter() (common.Database, error) {
|
||||||
|
if c == nil {
|
||||||
|
return nil, fmt.Errorf("connection is nil")
|
||||||
|
}
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
if c.bunAdapter != nil {
|
if c.bunAdapter != nil {
|
||||||
defer c.mu.RUnlock()
|
defer c.mu.RUnlock()
|
||||||
@@ -350,17 +372,28 @@ func (c *sqlConnection) getBunAdapter() (common.Database, error) {
|
|||||||
return c.bunAdapter, nil
|
return c.bunAdapter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
bunDB, err := c.Bun()
|
// Double-check bunDB exists (while already holding write lock)
|
||||||
if err != nil {
|
if c.bunDB == nil {
|
||||||
return nil, err
|
// Get native connection first
|
||||||
|
native, err := c.provider.GetNative()
|
||||||
|
if err != nil {
|
||||||
|
return nil, NewConnectionError(c.name, "get bun", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Bun DB wrapping the same sql.DB
|
||||||
|
dialect := c.getBunDialect()
|
||||||
|
c.bunDB = bun.NewDB(native, dialect)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.bunAdapter = database.NewBunAdapter(bunDB)
|
c.bunAdapter = database.NewBunAdapter(c.bunDB)
|
||||||
return c.bunAdapter, nil
|
return c.bunAdapter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getGORMAdapter returns or creates the GORM adapter
|
// getGORMAdapter returns or creates the GORM adapter
|
||||||
func (c *sqlConnection) getGORMAdapter() (common.Database, error) {
|
func (c *sqlConnection) getGORMAdapter() (common.Database, error) {
|
||||||
|
if c == nil {
|
||||||
|
return nil, fmt.Errorf("connection is nil")
|
||||||
|
}
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
if c.gormAdapter != nil {
|
if c.gormAdapter != nil {
|
||||||
defer c.mu.RUnlock()
|
defer c.mu.RUnlock()
|
||||||
@@ -375,17 +408,33 @@ func (c *sqlConnection) getGORMAdapter() (common.Database, error) {
|
|||||||
return c.gormAdapter, nil
|
return c.gormAdapter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
gormDB, err := c.GORM()
|
// Double-check gormDB exists (while already holding write lock)
|
||||||
if err != nil {
|
if c.gormDB == nil {
|
||||||
return nil, err
|
// Get native connection first
|
||||||
|
native, err := c.provider.GetNative()
|
||||||
|
if err != nil {
|
||||||
|
return nil, NewConnectionError(c.name, "get gorm", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create GORM DB wrapping the same sql.DB
|
||||||
|
dialector := c.getGORMDialector(native)
|
||||||
|
db, err := gorm.Open(dialector, &gorm.Config{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, NewConnectionError(c.name, "initialize gorm", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.gormDB = db
|
||||||
}
|
}
|
||||||
|
|
||||||
c.gormAdapter = database.NewGormAdapter(gormDB)
|
c.gormAdapter = database.NewGormAdapter(c.gormDB)
|
||||||
return c.gormAdapter, nil
|
return c.gormAdapter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getNativeAdapter returns or creates the native adapter
|
// getNativeAdapter returns or creates the native adapter
|
||||||
func (c *sqlConnection) getNativeAdapter() (common.Database, error) {
|
func (c *sqlConnection) getNativeAdapter() (common.Database, error) {
|
||||||
|
if c == nil {
|
||||||
|
return nil, fmt.Errorf("connection is nil")
|
||||||
|
}
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
if c.nativeAdapter != nil {
|
if c.nativeAdapter != nil {
|
||||||
defer c.mu.RUnlock()
|
defer c.mu.RUnlock()
|
||||||
@@ -400,21 +449,31 @@ func (c *sqlConnection) getNativeAdapter() (common.Database, error) {
|
|||||||
return c.nativeAdapter, nil
|
return c.nativeAdapter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
native, err := c.Native()
|
// Double-check nativeDB exists (while already holding write lock)
|
||||||
if err != nil {
|
if c.nativeDB == nil {
|
||||||
return nil, err
|
if !c.connected {
|
||||||
|
return nil, ErrConnectionClosed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get native connection from provider
|
||||||
|
db, err := c.provider.GetNative()
|
||||||
|
if err != nil {
|
||||||
|
return nil, NewConnectionError(c.name, "get native", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.nativeDB = db
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a native adapter based on database type
|
// Create a native adapter based on database type
|
||||||
switch c.dbType {
|
switch c.dbType {
|
||||||
case DatabaseTypePostgreSQL:
|
case DatabaseTypePostgreSQL:
|
||||||
c.nativeAdapter = database.NewPgSQLAdapter(native)
|
c.nativeAdapter = database.NewPgSQLAdapter(c.nativeDB)
|
||||||
case DatabaseTypeSQLite:
|
case DatabaseTypeSQLite:
|
||||||
// For SQLite, we'll use the PgSQL adapter as it works with standard sql.DB
|
// For SQLite, we'll use the PgSQL adapter as it works with standard sql.DB
|
||||||
c.nativeAdapter = database.NewPgSQLAdapter(native)
|
c.nativeAdapter = database.NewPgSQLAdapter(c.nativeDB)
|
||||||
case DatabaseTypeMSSQL:
|
case DatabaseTypeMSSQL:
|
||||||
// For MSSQL, we'll use the PgSQL adapter as it works with standard sql.DB
|
// For MSSQL, we'll use the PgSQL adapter as it works with standard sql.DB
|
||||||
c.nativeAdapter = database.NewPgSQLAdapter(native)
|
c.nativeAdapter = database.NewPgSQLAdapter(c.nativeDB)
|
||||||
default:
|
default:
|
||||||
return nil, ErrUnsupportedDatabase
|
return nil, ErrUnsupportedDatabase
|
||||||
}
|
}
|
||||||
@@ -424,6 +483,7 @@ func (c *sqlConnection) getNativeAdapter() (common.Database, error) {
|
|||||||
|
|
||||||
// getBunDialect returns the appropriate Bun dialect for the database type
|
// getBunDialect returns the appropriate Bun dialect for the database type
|
||||||
func (c *sqlConnection) getBunDialect() schema.Dialect {
|
func (c *sqlConnection) getBunDialect() schema.Dialect {
|
||||||
|
|
||||||
switch c.dbType {
|
switch c.dbType {
|
||||||
case DatabaseTypePostgreSQL:
|
case DatabaseTypePostgreSQL:
|
||||||
return database.GetPostgresDialect()
|
return database.GetPostgresDialect()
|
||||||
|
|||||||
Reference in New Issue
Block a user