sqlstruct

package module
v0.0.0-...-643fe7b Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2013 License: MIT Imports: 5 Imported by: 0

README

sqlstruct

This is a modified version of http://go.pkgdoc.org/github.com/kisielk/sqlstruct.

It adds support for embedded structs and adds a slightly different type info cache.

Basic use:


// struct to represent a query from bar
type Rows struct {
	Name string `sql:"name_attr"`
	Id   int64  `sql:"@id"`
	...
}

// Create a new session - this will also maintain a type info cache
// so it's beneficial for complex structs
s := sqlstruct.NewSession()

r := Rows{}
db := sql.Open(...)
dbquery := "select * from bar"

rows := mustQuery(db, dbquery)
for rows.Next() {
	s.MustScan(&r, rows)
	// do something with the Rows data in r
}

sqlstruct provides some convenience functions for using structs with go's database/sql package

Documentation can be found via godoc or at http://go.pkgdoc.org/github.com/kisielk/sqlstruct

Documentation

Overview

Package sqlstruct provides some convenience functions for using structs with the Go standard library's database/sql package.

The package works with structs that are tagged with a "sql" tag that identifies which column of a SQL query the field corresponds to.

For example:

type T struct {
	F1 string `sql:"f1"`
	F2 string `sql:"f2"`
}

rows, err := db.Query(fmt.Sprintf("SELECT %s FROM tablename", sqlstruct.Columns(T)))
...

for rows.Next() {
	var t T
	err = sqlstruct.Scan(&t, rows)
	...
}

err = rows.Err() // get any errors encountered during iteration

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Columns

func Columns(s interface{}) (names []string)

func MustScan

func MustScan(dest interface{}, rows Rows)

func Scan

func Scan(dest interface{}, rows Rows) error

Types

type Rows

type Rows interface {
	Scan(...interface{}) error
	Columns() ([]string, error)
}

Rows defines the interface of types that are scannable with the Scan function. It is implemented by the sql.Rows type from the standard library

type Session

type Session struct {
	// contains filtered or unexported fields
}

func NewSession

func NewSession() *Session

func (*Session) Columns

func (s *Session) Columns(d interface{}) (names []string)

func (*Session) MustScan

func (s *Session) MustScan(dest interface{}, rows Rows)

func (*Session) Scan

func (s *Session) Scan(dest interface{}, rows Rows) error

Jump to

Keyboard shortcuts

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