rdbms

package
v0.0.0-...-c020bf5 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayBasedColumnSeeder

type ArrayBasedColumnSeeder struct {
	Rows [][]interface{}
	// contains filtered or unexported fields
}

ColumnSeeder implementation that uses 2D array TODO currently we only have an array, later if the data is too large, can be improved to a channel

func (*ArrayBasedColumnSeeder) LatestDataCount

func (a *ArrayBasedColumnSeeder) LatestDataCount() int

func (*ArrayBasedColumnSeeder) Next

func (a *ArrayBasedColumnSeeder) Next() (row []interface{}, hasData bool, err error)

type Column

type Column struct {
	Name string
}

Column repesent a column/field in the table. Name should match the database column name

type ColumnSeeder

type ColumnSeeder interface {
	// Next fetch the next data
	// returns row a slice that contains the data
	// hasData tell if the current position has data. It should be used as a signal that there is no more data
	// if hasData == false or hasData == true, seeder must not return any error
	// e.g.
	/*
		for {
			row, hasData, err := seeder.Next()
			if err != nil {
				return err // or wrap and close connection as needed
			}
			if !hasData {
				break
			}
			BulkInsert(row)
		}
	*/
	Next() (row []interface{}, hasData bool, err error)

	// return how many data has been seeded
	LatestDataCount() int
}

ColumnSeeder seeds data when BulkInsert is ongoing

type Instance

type Instance interface {
	// InsertBulk inserts all table by the order of table specified in creation or by following the parent table
	InsertBulk(ctx context.Context, count int) error
	// InsertBulk table inserts only 1 table. But the table should be registered on creation.
	// If not it raises an error because the prepared statement for that table is not built
	InsertBulkTable(ctx context.Context, table Table) error
}

Instance is an instance of RDBMS.

type Postgres

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

func NewPostgres

func NewPostgres(
	db *sql.DB,
	table []Table,
	log zerolog.Logger,
) (*Postgres, error)

func NewPostgresFromConnectionString

func NewPostgresFromConnectionString(
	connectionString string,
	tables []Table,
	log zerolog.Logger,
) (*Postgres, error)

func (*Postgres) InsertBulk

func (p *Postgres) InsertBulk(ctx context.Context) error

TODO combine commits from multiple store (the other project), rollback all on 1st error

func (*Postgres) InsertBulkTable

func (p *Postgres) InsertBulkTable(ctx context.Context, table Table) error

type Table

type Table struct {
	Name         string
	Columns      []Column
	Seeder       ColumnSeeder
	ParentTables []string
}

Table represent a table. Name should match the table name

It is not necessary to define all Columns because the generated prepared statement will be a follwing: INSERT INTO table_name (col1, col2) VALUES (x, y) so it respects only column that is supplied

Seeder is any implementation of ColumnSeeder

ParentTables is to define all parent table if the table has any foreign key (parent-child) relationships it is not mandatory, but if specified, it helps BulkInsert understands in which order the tables should be inserted if not, the bulk insert follows the order of table in Instance creation

Jump to

Keyboard shortcuts

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