sqlpro

package module
v0.0.0-...-73ab595 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2025 License: MIT Imports: 22 Imported by: 1

README

go-sqlpro

WIP

This package is under active development and work-in-progress.

Query(target interface{}, query string, args...)

If you want to insert a custom type (e.g. a struct) into the db your type needs to fullfill follwing interface functions for (un-)marshalling from/to the database.

Marshalling to DB

func (f Metadata) Value() (driver.Value, error)

Unmarshalling from DB

func (f *Metadata) Scan(v interface{}) error`

json is supported as tag!

Documentation

Index

Constants

View Source
const POSTGRES = "postgres"

The driver strings must match the driver from the stdlib

View Source
const SQLITE3 = "sqlite3"

Variables

View Source
var ErrMismatchedRowsAffected error = errors.New("Mismatched rows affected")
View Source
var ErrQueryReturnedZeroRows error = errors.New("Query returned 0 rows")

Functions

func CtxWithTX

func CtxWithTX(ctx context.Context, tx TX) context.Context

CtxWithTX returns ctx with TX stored

func Scan

func Scan(target interface{}, rows *sql.Rows) error

Scan reads data from the given rows into the target.

*int64, *string, etc: First column of first row *struct: First row []int64, []*int64, []string, []*string: First column, all rows []struct, []*struct: All columns, all rows

The mapping into structs is done by analyzing the struct's tag names and using the given "db" key for the mapping. The mapping works on exported fields only. Use "-" as mapping name to ignore the field.

Types

type DB

type DB interface {
	Query
	Exec
	Begin() (TX, error)
	BeginRead() (TX, error)
	BeginContext(context.Context, *sql.TxOptions) (TX, error)
	Close() error
	IsClosed() bool
	Name() (string, error)
	DB() *sql.DB
	Log() DB
	Version() (string, error)
	ExecTX(context.Context, func(context.Context) error, *sql.TxOptions) error
}

func Open

func Open(driverS, dsn string) (DB, error)

Open opens a database connection and returns an sqlpro wrap handle

type DebugLevel

type DebugLevel int
const (
	PANIC      DebugLevel = 1
	ERROR      DebugLevel = 2
	UPDATE     DebugLevel = 4
	INSERT     DebugLevel = 8
	EXEC       DebugLevel = 16
	QUERY      DebugLevel = 32
	QUERY_DUMP DebugLevel = 64
)

type Exec

type Exec interface {
	Query

	ExecContext(context.Context, string, ...any) error
	Exec(string, ...any) error
	ExecContextRowsAffected(context.Context, string, ...any) (int64, int64, error)

	Insert(string, any) error
	InsertBulk(string, any) error
	InsertBulkContext(context.Context, string, any) error
	InsertBulkOnConflictDoNothingContext(context.Context, string, any, ...string) error
	InsertContext(context.Context, string, any) error
	Save(string, any) error
	Update(string, any) error
	UpdateContext(context.Context, string, any) error
	UpdateBulkContext(context.Context, string, any) error
}

type NullJson

type NullJson struct {
	Data  []byte
	Valid bool
}

func (*NullJson) Scan

func (nj *NullJson) Scan(value interface{}) error

type NullRawMessage

type NullRawMessage struct {
	Data  json.RawMessage
	Valid bool
}

func (*NullRawMessage) Scan

func (nj *NullRawMessage) Scan(value interface{}) error

type NullTime

type NullTime struct {
	Time  time.Time
	Valid bool
}

func (*NullTime) Scan

func (ni *NullTime) Scan(value interface{}) error

Scan implements the Scanner interface.

type PlaceholderMode

type PlaceholderMode int
const (
	DOLLAR   PlaceholderMode = 1
	QUESTION PlaceholderMode = 2
)

type Query

type Query interface {
	ExecContext(context.Context, string, ...any) error
	Exec(string, ...any) error
	ExecContextRowsAffected(context.Context, string, ...any) (int64, int64, error)

	Insert(string, any) error
	InsertBulk(string, any) error
	InsertBulkContext(context.Context, string, any) error
	InsertBulkOnConflictDoNothingContext(context.Context, string, any, ...string) error
	InsertContext(context.Context, string, any) error
	Save(string, any) error
	Update(string, any) error
	UpdateContext(context.Context, string, any) error
	UpdateBulkContext(context.Context, string, any) error

	QueryContext(context.Context, any, string, ...any) error
	Query(any, string, ...any) error

	Driver() dbDriver
	EscValue(string) string
}

type TX

type TX interface {
	Query
	Exec

	AfterCommit(func())
	AfterRollback(func())
	AfterTransaction(func())

	ActiveTX() bool
	IsWriteMode() bool

	Commit() error
	Rollback() error

	EscValue(string) string
}

func CtxTX

func CtxTX(ctx context.Context) TX

CtxTX returns the TX stored in ctx

Jump to

Keyboard shortcuts

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