sqlout

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2023 License: MIT Imports: 3 Imported by: 0

README

sqlout

Go Documentation

builds.sr.ht status

Scan values from the database into slices, structs, and basic types.

Documentation

Overview

Package sqlout scans database row data into variables.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rows

type Rows interface {
	Next() bool
	Scan(dest ...any) error
	Columns() ([]string, error)
	Close() error
	Err() error
}

Rows are rows returned by a database query.

type Scanner

type Scanner struct {
	// Tag is the struct tag to map the column name to.
	Tag string

	// ColumnTagMap takes a database column name and returns what struct tag it
	// should match against.
	ColumnTagMap func(col string) string
}

Scanner holds options for scanning.

func (*Scanner) Scan

func (s *Scanner) Scan(rs Rows, v any) (err error)

Scan takes database rows and scans the data into v. v must be a pointer to a basic type, a slice of basic types, a struct, or a slice of basic structs.

Example
// Call sql.Open to get a valid *sql.DB.
var db *sql.DB

rows, err := db.Query(
	"SELECT verified, first_name, id FROM user WHERE verified = ?",
	true,
)
if err != nil {
	// Query failed
	return
}

sc := Scanner{Tag: "sql"}
var users []struct {
	ID        int    `sql:"id"`
	FirstName string `sql:"first_name"`
	Verified  bool   `sql:"verified"`
}
if err := sc.Scan(rows, &users); err != nil {
	// Failed to scan values
	return
}
Output:

Jump to

Keyboard shortcuts

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