github.com/prebid/prebid-server/v2@v2.18.0/stored_requests/backends/db_provider/db_provider.go (about)

     1  package db_provider
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  
     7  	"github.com/golang/glog"
     8  	"github.com/prebid/prebid-server/v2/config"
     9  )
    10  
    11  type DbProvider interface {
    12  	Config() config.DatabaseConnection
    13  	ConnString() (string, error)
    14  	Open() error
    15  	Close() error
    16  	Ping() error
    17  	PrepareQuery(template string, params ...QueryParam) (query string, args []interface{})
    18  	QueryContext(ctx context.Context, template string, params ...QueryParam) (*sql.Rows, error)
    19  }
    20  
    21  func NewDbProvider(dataType config.DataType, cfg config.DatabaseConnection) DbProvider {
    22  	var provider DbProvider
    23  
    24  	switch cfg.Driver {
    25  	case "mysql":
    26  		provider = &MySqlDbProvider{
    27  			cfg: cfg,
    28  		}
    29  	case "postgres":
    30  		provider = &PostgresDbProvider{
    31  			cfg: cfg,
    32  		}
    33  	default:
    34  		glog.Fatalf("Unsupported database driver %s", cfg.Driver)
    35  		return nil
    36  	}
    37  
    38  	if err := provider.Open(); err != nil {
    39  		glog.Fatalf("Failed to open %s database connection: %v", dataType, err)
    40  	}
    41  	if err := provider.Ping(); err != nil {
    42  		glog.Fatalf("Failed to ping %s database: %v", dataType, err)
    43  	}
    44  
    45  	return provider
    46  }
    47  
    48  type QueryParam struct {
    49  	Name  string
    50  	Value interface{}
    51  }