sqlout – git.sr.ht/~ixoh/sqlout Index | Examples | Files

package sqlout

import "git.sr.ht/~ixoh/sqlout"

Package sqlout scans database row data into variables.

Index

Examples

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

Code:

{
	// 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
	}
}

Source Files

sqlout.go

Version
v0.1.0 (latest)
Published
Apr 21, 2023
Platform
linux/amd64
Imports
3 packages
Last checked
23 minutes ago

Tools for package owners.