magiccol

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2020 License: MIT Imports: 4 Imported by: 3

README

magiccol

Dinamyc columns for database/sql. Magiccol allows to scan rows for a sql query, without known which columns it returns

Example

package main

import (
    "database/sql"
    "fmt"
    "log"
    "reflect"

    "github.com/go-sql-driver/mysql"
    "github.com/licaonfee/magiccol"
)

func main() {
    db, err := sql.Open("mysql", "user:password@tcp(localhost:3306)/employees?charset=utf8mb4")
    if err != nil {
        log.Fatal(err)
    }
    r, err := db.Query("select * from employees limit 10;")
    if err != nil {
        log.Fatal(err)
    }
    m := magiccol.DefaultMapper()
    //Use mysql native Time type see
    //https://github.com/go-sql-driver/mysql#timetime-support
    m.Type(reflect.TypeOf(mysql.NullTime{}), "DATE", "DATETIME", "TIMESTAMP")

    sc, err := magiccol.NewScanner(magiccol.Options{Rows:r, Mapper: m})
    if err != nil {
        log.Fatal(err)
    }
    for sc.Scan() {
        value := sc.Value()
        fmt.Printf("%v\n", value)
    }
    if sc.Err() != nil {
        log.Fatal(sc.Err())
    }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNilRows = errors.New("nil *sql.Rows as argument")

Functions

This section is empty.

Types

type LookupMapper

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

func (LookupMapper) Get

func (l LookupMapper) Get(typeName string, fallback reflect.Type) reflect.Type

Get do a map lookup if type is not found return a ScanType itself

func (*LookupMapper) Type

func (l *LookupMapper) Type(t reflect.Type, asType ...string)

type Mapper

type Mapper interface {
	//Get typeName should be sql type as is called in sql.ColumnType.DatabaseTypeName()
	Get(typeName string, fallback reflect.Type) reflect.Type
	//Type allow to set alias, extends or fix mapper behaviour
	Type(t reflect.Type, asTypes ...string)
}

Mapper translate sql types to golang types

func DefaultMapper

func DefaultMapper() Mapper

type Options

type Options struct {
	//Rows must be a valid sql.Rows object
	Rows *sql.Rows
	//Mapper can be nil, if so DefaultMapper is used
	Mapper Mapper
}

type Scanner

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

func NewScanner

func NewScanner(o Options) (*Scanner, error)

Inspect set scanner to read a column set

func (*Scanner) Err

func (s *Scanner) Err() error

Err return last error in Scanner

func (*Scanner) Scan

func (s *Scanner) Scan() bool

Scan return true if there are rows in queue and false if there is no more rows or an error occured. To distinguish between error or no more rows Err() method should be consulted

func (*Scanner) SetMap

func (s *Scanner) SetMap(value map[string]interface{})

SetMap read values from current row and load it in a given map[string]interface{} this allow to set default values, or reutilize same map in multiple iterations SetMap does not clear map object and any preexistent key will be preserved

func (*Scanner) Value

func (s *Scanner) Value() map[string]interface{}

Value returns a new map object with all values from current row successives calls to Value without call Scan returns always same values in a new allocated map. Call Value() before Scan return all values as Zero

Jump to

Keyboard shortcuts

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