goetl

package module
v0.0.0-...-dfccc89 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2022 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Golang Database ETL Library

This is a simple library to facilitate data transfers in go programs between different data stores. It is currently designed around the database/sql interface, but the long term intent is to support any kind of data transfer. This code is in an alpha state and the API is not stable.

To use the library,

  • create a struct with both a db tag and a desttype tag (if you would like the tables to be automatically created in the destination).
  • create connections to the source and destination database
  • define the transfer options
    • CreateTable: whether the tools should create the table if it does not exists. If the table does exists, the transfer will append any data to the existing table.
    • CommitSize: the transaction commit size.
    • BatchSize: this is currently unsupported in most golang db drivers, but does exists for the pgx postgres driver. Batching commands reduces the number of networks calls and can significantly improve insert performance.
  • Create the Table struct for the table to transfer
    • Name: name of the table
      • For selects, this is used for the query if a select statement was not provided.
      • For table creation, this will be the name of the new table.
      • For inserts, this will be used if an insert query was not provided.
    • SelectSql: user supplied SQL for the select if you do not want if autogenerated from struct tags
    • InsertSql: user supplied SQL for inserts if you do not want the insert autogenerated from struct tags
    • Fields: Struct that will be used to scan result data into
  • Create an ETL instance with the source, destination, and transfer options
  • Transfer as many tables as you need using the etl.Transfer function

type PrecipDay struct {
	SourceCode     string    `db:"SOURCE_CODE" desttype:"varchar(2)"`
	AreaTypeCode   string    `db:"AREA_TYPE_CODE" desttype:"varchar(2)"`
	AreaId         int       `db:"AREA_ID" desttype:"int"`
	EndDate        time.Time `db:"END_DATE" desttype:"date"`
	DayOfWaterYear int       `db:"DAY_OF_WATER_YEAR" desttype:"int"`
	Value          float32   `db:"VALUE" desttype:"double precision"`
	UnitCode       string    `db:"UNIT_CODE" desttype:"varchar(2)"`
}

func TestPostgres(t *testing.T) {
	oraConfig := DbConfig{
		ExternalLib: os.Getenv("INSTANTCLIENT"),
		Dbhost:      os.Getenv("DBHOST"),
		Dbname:      os.Getenv("DBNAME"),
		Dbuser:      os.Getenv("DBUSER"),
		Dbpass:      os.Getenv("DBPASS"),
	}

	pgPort, err := strconv.Atoi(os.Getenv("PGDBPORT"))
	if err != nil {
		log.Fatal(err)
	}

	pgConfig := PostgresConfig{
		BatchSize: 100,
		DbConfig: DbConfig{
			Dbhost: os.Getenv("PGDBHOST"),
			Dbname: os.Getenv("PGDBNAME"),
			Dbuser: os.Getenv("PGDBUSER"),
			Dbpass: os.Getenv("PGDBPASS"),
			Dbport: pgPort,
		},
	}

	source, err := NewOracleSqlImpl(oraConfig)
	if err != nil {
		log.Fatal(err)
	}
	defer source.Close()

	dest, err := NewPostgresDb(pgConfig)
	if err != nil {
		log.Fatal(err)
	}
	defer dest.Close()

	options := TransferOptions{
		CreateTable: false,
		CommitSize:  100,
	}

	table := Table{
		Name:      "precip_day",
		SelectSql: "select * from (select * from precip_day_new) t1 where rownum<100",
		InsertSql: "insert into precip_day values (:SOURCE_CODE,:AREA_TYPE_CODE,:AREA_ID,:END_DATE,:DAY_OF_WATER_YEAR,:VALUE,:UNIT_CODE)",
		Fields:    PrecipDay{},
	}

	etl := ETL{source, dest, options}

	err = etl.Transfer(&table)
	if err != nil {
		log.Println(err)
	}
}

This initial version includes support for Oracle (db source only), Sqlite, and Postgres

The library uses:

  • godror for the Oracle driver
  • go-sqlite3 for sqlite
  • pgx for postgres
  • scany for struct scanning

TODO

  • improve named statement caching (use combination of driver and statement)
  • Allow user to override Read and Write methods with their own functions
  • add option for case insenstitive named statement parameter matching ! - generate sql insert statements from struct
  • generate sql update statements from struct
  • remove nesting of DbConfig in the Postgresconfig
  • unify batch and commit size
  • enumerate and return errors from postgres batched statements
  • review error handling ! - progress reporting

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateTableSql

func CreateTableSql(tablename string, t reflect.Type) string

func NamedInsertSql

func NamedInsertSql(tablename string, t reflect.Type) (string, error)

@TODO should replace with more flexible structToInsert

func TagAsPositionMap

func TagAsPositionMap(tag string, data interface{}) map[string]int

func TagAsStringArray

func TagAsStringArray(tag string, data interface{}) []string

func TagsAndVals

func TagsAndVals(tag string, data interface{}) ([]string, []interface{})

func ValsAsInterfaceArray

func ValsAsInterfaceArray(data interface{}) []interface{}

func ValueMap

func ValueMap(tag string, data interface{}) map[string]interface{}

Types

type BindParamTemplateFunction

type BindParamTemplateFunction func(field string, i int) string

type Connx

type Connx struct {
	// contains filtered or unexported fields
}

func (*Connx) FlushBatch

func (c *Connx) FlushBatch()

func (*Connx) NamedExec

func (c *Connx) NamedExec(driver string, table *Table, data interface{})

type DbConfig

type DbConfig struct {
	Path        string
	Dbuser      string
	Dbpass      string
	Dbname      string
	Dbhost      string
	Dbport      int
	ExternalLib string
}

func (*DbConfig) ToDsn

func (c *DbConfig) ToDsn() string

type Destination

type Destination interface {
	StartTransaction() error
	Rollback() error
	Commit() error
	TableExists(schema string, name string) (bool, error)
	CreateTable(table *Table) error
	CopyRow(table *Table, rownum int, row interface{})
	Close()
}

type DevNullDb

type DevNullDb struct{}

func NewDevNullDb

func NewDevNullDb() (*DevNullDb, error)

func (*DevNullDb) Close

func (dndb *DevNullDb) Close()

func (*DevNullDb) Commit

func (dndb *DevNullDb) Commit() error

func (*DevNullDb) CopyRow

func (dndb *DevNullDb) CopyRow(table *Table, rowNum int, row interface{})

func (*DevNullDb) CreateTable

func (dndb *DevNullDb) CreateTable(table *Table) error

func (*DevNullDb) GetRows

func (dndb *DevNullDb) GetRows(table *Table) (Rows, error)

func (*DevNullDb) Rollback

func (dndb *DevNullDb) Rollback() error

func (*DevNullDb) StartTransaction

func (dndb *DevNullDb) StartTransaction() error

func (*DevNullDb) TableExists

func (dndb *DevNullDb) TableExists(schema string, name string) (bool, error)

type ETL

type ETL struct {
	// contains filtered or unexported fields
}

func NewEtl

func NewEtl(source Source, dest Destination, options TransferOptions) ETL

func (*ETL) Transfer

func (etl *ETL) Transfer(table *Table) error

type NamedStatement

type NamedStatement struct {
	ParamSql string
	// contains filtered or unexported fields
}

func NewNamedStatement

func NewNamedStatement(driver string, templateFunction BindParamTemplateFunction, table *Table) (NamedStatement, error)

func (*NamedStatement) ParamArray

func (ns *NamedStatement) ParamArray(data interface{}) []interface{}

type PgxRows

type PgxRows struct {
	// contains filtered or unexported fields
}

func (PgxRows) Close

func (pgr PgxRows) Close()

func (PgxRows) Next

func (pgr PgxRows) Next() bool

func (PgxRows) StructScan

func (pgr PgxRows) StructScan(ref interface{}) error

type PostgresDb

type PostgresDb struct {
	// contains filtered or unexported fields
}

func NewPostgresDb

func NewPostgresDb(c DbConfig) (*PostgresDb, error)

func (*PostgresDb) Close

func (pdb *PostgresDb) Close()

func (*PostgresDb) Commit

func (pdb *PostgresDb) Commit() error

func (*PostgresDb) CopyRow

func (pdb *PostgresDb) CopyRow(table *Table, rowNum int, row interface{})

func (*PostgresDb) CreateTable

func (pdb *PostgresDb) CreateTable(table *Table) error

func (*PostgresDb) GetRows

func (pdb *PostgresDb) GetRows(table *Table) (Rows, error)

func (*PostgresDb) Rollback

func (pdb *PostgresDb) Rollback() error

func (*PostgresDb) StartTransaction

func (pdb *PostgresDb) StartTransaction() error

func (*PostgresDb) TableExists

func (pdb *PostgresDb) TableExists(schema string, name string) (bool, error)

type Rows

type Rows interface {
	Next() bool
	Close()
	StructScan(ref interface{}) error
}

type Source

type Source interface {
	GetRows(table *Table) (Rows, error)
	Close()
}

type SqlDb

type SqlDb struct {
	// contains filtered or unexported fields
}

func NewOracleSqlImpl

func NewOracleSqlImpl(config DbConfig) (*SqlDb, error)

func NewPostgresSqlImpl

func NewPostgresSqlImpl(config DbConfig) (*SqlDb, error)

func NewSqliteSqlImpl

func NewSqliteSqlImpl(config DbConfig) (*SqlDb, error)

func (*SqlDb) Close

func (sdb *SqlDb) Close()

func (*SqlDb) Commit

func (sdb *SqlDb) Commit() error

func (*SqlDb) CopyRow

func (sdb *SqlDb) CopyRow(table *Table, rowNum int, row interface{})

func (*SqlDb) CreateTable

func (sdb *SqlDb) CreateTable(table *Table) error

func (*SqlDb) GetRows

func (sdb *SqlDb) GetRows(table *Table) (Rows, error)

func (*SqlDb) Rollback

func (sdb *SqlDb) Rollback() error

func (*SqlDb) StartTransaction

func (sdb *SqlDb) StartTransaction() error

func (*SqlDb) TableExists

func (sdb *SqlDb) TableExists(schema string, name string) (bool, error)

type SqlDbImpl

type SqlDbImpl struct {
	Driver                    string
	Url                       string
	TableExistsSql            string
	BindParamTemplateFunction BindParamTemplateFunction
}

type SqldbRows

type SqldbRows struct {
	// contains filtered or unexported fields
}

@TODO verify that the Next method is returning a reference from the Rows interface

func (SqldbRows) Close

func (sr SqldbRows) Close()

func (SqldbRows) Next

func (sr SqldbRows) Next() bool

func (SqldbRows) StructScan

func (sr SqldbRows) StructScan(ref interface{}) error

type Table

type Table struct {
	Name      string
	Schema    string //optional
	SelectSql string
	InsertSql string
	Fields    interface{}
}

type TransferOptions

type TransferOptions struct {
	CreateTable       bool
	CommitSize        int
	ReportingFunction func(i int64)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址