sqlx

package
v12.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2023 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultSchema = NewSchema()

DefaultSchema initializes a common Schema.

View Source
var DefaultTag = "db"

DefaultTag is the default struct field tag.

Functions

func Bind

func Bind(dst interface{}, src *sql.Rows) error

Bind sets "dst" to the result of "src" and reports any errors.

func Query

func Query(ctx context.Context, db *sql.DB, dst interface{}, query string, args ...interface{}) error

Query is a shortcut of executing a query and bind the result to "dst".

Types

type Column

type Column struct {
	Name       string
	Index      int
	FieldIndex []int
}

Column holds the database column name and other properties extracted by a struct's field.

type ColumnNameFunc

type ColumnNameFunc = func(string) string

type Row

type Row struct {
	Schema     string // e.g. public
	Name       string // e.g. users. Must set to a custom one if the select query contains AS names.
	StructType reflect.Type
	Columns    map[string]*Column // e.g. "id":{"id", 0, [0]}
}

Row holds the column definitions and the struct type & name.

type Schema

type Schema struct {
	Name           string
	Rows           map[reflect.Type]*Row
	ColumnNameFunc ColumnNameFunc
	AutoCloseRows  bool
}

Schema holds the row definitions.

func NewSchema

func NewSchema() *Schema

NewSchema returns a new Schema. Use its Register() method to cache a structure value so Bind() can fill all struct's fields based on a query.

func Register

func Register(tableName string, value interface{}) *Schema

Register caches a struct value to the default schema.

func (*Schema) Bind

func (s *Schema) Bind(dst interface{}, src *sql.Rows) error

Bind sets "dst" to the result of "src" and reports any errors.

func (*Schema) Query

func (s *Schema) Query(ctx context.Context, db *sql.DB, dst interface{}, query string, args ...interface{}) error

Query is a shortcut of executing a query and bind the result to "dst".

func (*Schema) Register

func (s *Schema) Register(tableName string, value interface{}) *Schema

Register caches a struct value to the schema.

Jump to

Keyboard shortcuts

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