db

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 22 Imported by: 1 module, 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.

Documentation

Overview

Package db provides the target-database layer: one concrete Inserter that writes batches with per-row failure isolation, configured by a small Dialect (placeholder style, batch size, savepoint support) and a database/sql driver.Connector supplied per vendor (postgres.go, oracle.go, hana.go) — the same split database/sql itself uses between sql.DB and its drivers. Columns and table are entirely config-driven (see config.TargetConfig and config.MappingField) — there is no hardcoded schema anywhere here.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Postgres: $n placeholders, savepoint isolation.
	Postgres = Dialect{Name: "postgres", Placeholder: func(i int) string { return "$" + strconv.Itoa(i) }, Savepoints: true, BatchSize: 500}
	// Oracle: :n placeholders, savepoint isolation. 150 rows keeps bind
	// counts under Oracle's ~4,000 ceiling for typical column counts (e.g.
	// 150 rows x 24 cols = 3,600); tune target.batchSize for wider mappings.
	Oracle = Dialect{Name: "oracle", Placeholder: func(i int) string { return ":" + strconv.Itoa(i) }, Savepoints: true, BatchSize: 150}
	// HANA: ? placeholders, statement-level rollback (no savepoints), one
	// statement per request.
	HANA = Dialect{Name: "hana", Placeholder: func(int) string { return "?" }, BatchSize: 500, SplitScript: splitStatements}
)

The supported dialects.

View Source
var ErrCommitUncertain = errors.New("commit outcome unknown")

ErrCommitUncertain marks a failed transaction commit. The server may or may not have applied it, so a blind retry could insert the batch twice.

Functions

func Retryable

func Retryable(err error) bool

Retryable reports whether err from InsertBatch is a transient, connection-level failure that is safe to retry once: a dropped or refused connection, EOF, or a network timeout. Server-side errors (missing table, bad SQL), context cancellation and uncertain commits are not retryable.

Types

type BatchResult

type BatchResult struct {
	Success int
	Failed  int
	Errors  []error
}

BatchResult summarizes one InsertBatch call.

type DestinationResolver

type DestinationResolver func(ctx context.Context, name string) (*destination.Destination, *connectivity.Dialer, error)

DestinationResolver looks up a named SAP BTP destination and returns it with a Connectivity dialer for on-premise tunneling. (*btp.Bindings).Resolve satisfies it; a nil resolver means "no BTP".

type Dialect

type Dialect struct {
	// Name prefixes error messages ("postgres", "oracle", "hana").
	Name string
	// Placeholder returns the i-th (1-based) bind placeholder.
	Placeholder func(i int) string
	// Savepoints makes InsertBatch wrap each row in SAVEPOINT / ROLLBACK TO
	// SAVEPOINT. Leave false for databases that roll a failed statement back
	// on their own and keep the transaction usable (SAP HANA, which has no
	// SAVEPOINT statement at all).
	Savepoints bool
	// BatchSize is the recommended rows per transaction.
	BatchSize int
	// SplitScript, when set, splits a script into the individual statements
	// Exec runs one by one (for databases that accept one statement per
	// request). Nil means the script is sent whole.
	SplitScript func(script string) ([]string, error)
}

Dialect captures everything that differs between supported databases beyond how to connect to them.

type Inserter

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

Inserter writes record batches to one table. Create it with Open (or New around an existing *sql.DB); it is safe for concurrent use.

func New

func New(sqlDB *sql.DB, target config.TargetConfig, d Dialect) *Inserter

New returns an Inserter writing to sqlDB with the given dialect. It takes ownership of sqlDB: Close closes it.

func Open

func Open(ctx context.Context, target config.TargetConfig, resolve DestinationResolver) (*Inserter, error)

Open connects to the database selected by target.DB ("postgres" is the default; "oracle" and "hana" are also always available — all drivers are pure Go, no CGO required), verifies the connection with a ping, and returns a ready Inserter. resolve is used only when target.Destination is set and may be nil.

func (*Inserter) Close

func (i *Inserter) Close() error

Close releases the connection pool.

func (*Inserter) DefaultBatchSize

func (i *Inserter) DefaultBatchSize() int

DefaultBatchSize returns the dialect's recommended batch size.

func (*Inserter) Exec

func (i *Inserter) Exec(ctx context.Context, script string) error

Exec runs a SQL script — used by cmd/migrate and cmd/bench. Dialects with a SplitScript get it split into statements, executed in order.

func (*Inserter) InsertBatch

func (i *Inserter) InsertBatch(ctx context.Context, records []model.Record) (BatchResult, error)

InsertBatch inserts records with per-row failure isolation: one transaction, one prepared statement, and a failing row is recorded in the result without sinking the rest of the batch. Where the dialect has savepoints each row is wrapped in SAVEPOINT / ROLLBACK TO SAVEPOINT; otherwise the database's own statement-level rollback does the isolating. A transaction-level failure (begin, prepare, savepoint, commit) returns an error with every record counted as failed; see Retryable.

func (*Inserter) Ping

func (i *Inserter) Ping(ctx context.Context) error

Ping verifies the database connection is still alive.

Jump to

Keyboard shortcuts

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