magiccol

package module
v0.1.2 Latest Latest
Warning

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

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

README

magiccol

Test Go Report Card Coverage Status godoc

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")

ErrNilRows a nil Rows interface is provided

Functions

This section is empty.

Types

type LookupMapper

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

LookupMapper implements Mapper interface

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 method allow to set custom types as scanneable types

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

DefaultMapper provides a mapping for most common sql types type list reference used is: http://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#predefined-type

type Options

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

Options for Scanner

type Rows added in v0.1.2

type Rows interface {
	ColumnTypes() ([]*sql.ColumnType, error)
	Columns() ([]string, error)
	Next() bool
	Err() error
	Scan(...interface{}) error
}

Rows allow to mock sql.Rows object

type Scanner

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

Scanner read data from an sql.Rows object into a map

func NewScanner

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

NewScanner create a new Scanner object, return an error if a nil Rows interface is provided or any error is returned by its

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 occurred. 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