sqls

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2023 License: MIT Imports: 4 Imported by: 0

README

sqls

SQL helper functions for queries and transactions to remove common boilerplate code.

import (
	"context"
	"database/sql"
	"github.com/tomcz/gotools/sqls"
)

func main() {
	ctx := context.Background()

	var db *sql.DB // initialisation omitted

	results := make(map[string]string)

	selectLeadersSQL := "select leader, node from current_leaders"
	err := sqls.QueryRowsContext(ctx, db, selectLeadersSQL)(func(row sqls.ScanFunc) error {
		var leader, node string
		if err := row(&leader, &node); err != nil {
			return nil
		}
		results[leader] = node
		return nil
	})
	// error handling omitted

	insertLeaderSQL := "insert into old_leaders (leader, node, created_at) values (?, ?, ?)"
	err = sqls.InTxContext(ctx, db, func(tx *sql.Tx) error {
		for leader, node := range results {
			_, err := tx.ExecContext(ctx, insertLeaderSQL, leader, node, time.Now())
			if err != nil {
				return err // tx will be rolled-back
			}
		}
		return nil // tx will be committed
	})
	// error handling omitted
}

Documentation

Overview

Package sqls provides SQL helper functions for queries and transactions to remove common boilerplate code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InTx

func InTx(db *sql.DB, callback func(tx *sql.Tx) error) error

InTx starts a database transaction, executes the callback function, and either commits the transaction if the callback exits without an error, or rolls-back the transaction if the callback returns an error.

func InTxContext

func InTxContext(ctx context.Context, db *sql.DB, callback func(tx *sql.Tx) error, opts ...*sql.TxOptions) error

InTxContext starts a context-aware database transaction, executes the callback function, and either commits the transaction if the callback exits without an error, or rolls-back the transaction if the callback returns an error.

func MapToAny added in v0.2.0

func MapToAny[A any](src []A) []any

MapToAny performs a type conversion useful for query arguments.

Types

type EachRowFunc

type EachRowFunc func(row ScanFunc) error

EachRowFunc is called to process each query result row.

type PartialQuery

type PartialQuery func(row EachRowFunc) error

PartialQuery is a curried query that passes each result row to EachRowFunc.

func QueryRows

func QueryRows(db *sql.DB, query string, args ...any) PartialQuery

QueryRows provides the entry point to retrieve a number of rows from a given query and arguments, using database/sql/#DB.QueryRow as inspiration.

func QueryRowsContext

func QueryRowsContext(ctx context.Context, db *sql.DB, query string, args ...any) PartialQuery

QueryRowsContext provides the entry point to retrieve a number of rows from a given query and arguments, using database/sql/#DB.QueryRowContext as inspiration.

type ScanFunc

type ScanFunc func(dest ...any) error

ScanFunc provides an interface for the database/sql/#Rows.Scan function so that we can limit what is exposed by EachRowFunc.

Jump to

Keyboard shortcuts

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