sqlh

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: May 9, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package sqlh provides some helper functions and types to simplify working with sql package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustPrepare

func MustPrepare(db *sql.DB, sql string) (stmt *sql.Stmt)

MustPrepare is like DB.Prepare but panics if the SQL cannot be parsed. It simplifies safe initialization of global variables holding prepared statements.

func StmtScanAll

func StmtScanAll(stmt *sql.Stmt, dst MultiScannable, args ...interface{}) error

StmtScanAll performs prepared statement stmt with arguments 'args' and stores all result rows in dst. StmtScanAll stop working on first error. Example:

 type Label struct {
 	Id       int32
 	Name     string
 }

 func (l *Label) SqlScanInterface() []interface{} {
 	return []interface{}{
 		&l.Id,
 		&l.Name,
 	}
 }

 type Labels []*Label

 func (l *Labels) SqlNewElement() sqlh.SingleScannable {
	e := &Label{}
	*l = append(*l, e)
	return e
 }
 ...
 var labels Labels
 if err := sqlh.StmtScanAll(someStmtGetLabels, &labels, someId, someOtherParam); err != nil {
 	return err
 }

Types

type MultiScannable

type MultiScannable interface {
	// SqlNewElement called for each row in query result. It should returns SingleScannable object for scanning row.
	// Usually this method add new element to the underlying slice and return this element.
	SQLNewElement() SingleScannable
}

MultiScannable represent object in that any amount of rows can be saved.

type SingleScannable

type SingleScannable interface {
	// SqlScanInterface return slice of interfaces which will be passed into Row.Scan at once.
	SQLScanInterface() []interface{}
}

SingleScannable represent object in that single row can be saved.

Jump to

Keyboard shortcuts

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