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 ¶
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.
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 ¶
Types ¶
type BatchResult ¶
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 ¶
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) DefaultBatchSize ¶
DefaultBatchSize returns the dialect's recommended batch size.
func (*Inserter) Exec ¶
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 ¶
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.