Help us shape the future of Go! Take the 2026 Go Developer Survey

insert

package
v0.26.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 18 Imported by: 4

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBuilder

func NewBuilder(table string, columns []string, dialect *info.Dialect, identity string, batchSize int, options ...option.Option) (io.Builder, error)

NewBuilder return insert builder

func ShowSQL added in v0.5.4

func ShowSQL(b bool)

Types

type Builder

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

Builder represent insert DML builder

func (*Builder) Build

func (b *Builder) Build(record interface{}, options ...option.Option) string

Build builds insert statement

type Insertable added in v0.5.4

type Insertable interface {
	OnInsert(ctx context.Context) error
}

Insertable interface to be called on inserting data

type Service

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

Service represents generic db writer

func New

func New(ctx context.Context, db *sql.DB, tableName string, options ...option.Option) (*Service, error)

New creates an inserter service

func (*Service) Exec

func (s *Service) Exec(ctx context.Context, any interface{}, options ...option.Option) (rowsAffected int64, lastInsertedID int64, err error)

Exec runs insertService SQL

Example
package main

import (
	"context"
	"database/sql"
	"fmt"
	"github.com/viant/sqlx/io/insert"
	"github.com/viant/sqlx/option"
	"log"

	_ "github.com/viant/sqlx/metadata/product/mysql"
)

func main() {
	type Foo struct {
		ID   int
		Name string
	}
	dsn := ""
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		log.Fatalln(err)
	}

	insert, err := insert.New(context.TODO(), db, "mytable", option.BatchSize(1024))
	if err != nil {
		log.Fatalln(err)
	}
	var records []*Foo
	//records = getAppRecords()

	affected, lastID, err := insert.Exec(context.TODO(), records)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Printf("affected: %v, last ID: %v\n", affected, lastID)
}

func (*Service) NewSession added in v0.1.1

func (s *Service) NewSession(ctx context.Context, record interface{}, db *sql.DB, batchSize int, options ...option.Option) (*session, error)

NewSession creates a new session

func (*Service) NextSequence added in v0.1.1

func (s *Service) NextSequence(ctx context.Context, any interface{}, recordCount int, options ...option.Option) (*sink.Sequence, error)

NextSequence resets next updateSequencer

func (*Service) ReserveSequence added in v0.26.1

func (s *Service) ReserveSequence(ctx context.Context, records any, count int, options ...option.Option) (result *sink.Reservation, err error)

ReserveSequence allocates exact numeric values using the selected native strategy. Transient strategies may execute source DML and roll it back. A supplied transaction retains ownership. Products choose their native reservation semantics; no contiguous range is inferred from sequence outputs.

func (*Service) SequenceInfo added in v0.26.0

func (s *Service) SequenceInfo(ctx context.Context, records interface{}, options ...option.Option) (*sink.Sequence, error)

SequenceInfo returns the native numeric sequence identity and arithmetic metadata without reserving values or modifying the record/counters. The reservation strategy acquires transaction write intent and may initialize native allocator metadata before reading identity. Otherwise this is read-only. Metadata resolution uses the same typed insert mapping and caller transaction as NextSequence. An unresolved identity is an error, never an authored fallback.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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