2022-04-20 17:06:00 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2022-04-20 19:44:15 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql"
|
2022-04-20 17:06:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ sql.Database = &DB{}
|
|
|
|
|
|
|
|
// TODO: Make NewPostgresDB accept a string and Config. IT should
|
|
|
|
// Create a driver of its own.
|
|
|
|
// This will make sure that if you want a driver, it conforms to the interface.
|
|
|
|
|
|
|
|
// NewPostgresDB returns a postgres.DB using the provided Config and driver type.
|
2022-04-20 19:44:15 +00:00
|
|
|
func NewPostgresDB(c Config) (*DB, error) {
|
2022-04-20 17:06:00 +00:00
|
|
|
var driver *pgxDriver
|
|
|
|
|
2022-04-20 19:44:15 +00:00
|
|
|
driver, err := createDriver(c)
|
2022-04-20 17:06:00 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DB{driver}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 19:44:15 +00:00
|
|
|
func createDriver(c Config) (*pgxDriver, error) {
|
|
|
|
switch c.Driver {
|
2022-04-20 17:06:00 +00:00
|
|
|
case PGX:
|
2022-04-20 19:44:15 +00:00
|
|
|
log.Debug("Creating New Driver")
|
2022-04-20 17:06:00 +00:00
|
|
|
driver, err := newPGXDriver(context.Background(), c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error Creating Driver, err: %e", err)
|
|
|
|
}
|
2022-04-20 19:44:15 +00:00
|
|
|
log.Info("Successfully created a driver for PGX")
|
2022-04-20 17:06:00 +00:00
|
|
|
return driver, nil
|
|
|
|
default:
|
2022-04-20 22:12:44 +00:00
|
|
|
log.Error("Couldnt find a driver to create for: ", c.Driver)
|
2022-04-20 17:06:00 +00:00
|
|
|
return nil, fmt.Errorf("Can't find a driver to create")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// DB implements sql.Database using a configured driver and Postgres statement syntax
|
|
|
|
type DB struct {
|
|
|
|
sql.Driver
|
|
|
|
}
|