github.com/tobgu/qframe@v0.4.0/internal/io/sql/types.go (about)

     1  package sql
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/tobgu/qframe/internal/bcolumn"
     8  	"github.com/tobgu/qframe/internal/column"
     9  	"github.com/tobgu/qframe/internal/ecolumn"
    10  	"github.com/tobgu/qframe/internal/fcolumn"
    11  	"github.com/tobgu/qframe/internal/icolumn"
    12  	"github.com/tobgu/qframe/internal/index"
    13  	"github.com/tobgu/qframe/internal/scolumn"
    14  	"github.com/tobgu/qframe/qerrors"
    15  )
    16  
    17  type SQLConfig struct {
    18  	// Query is a Raw SQL statement which must return
    19  	// appropriate types which can be inferred
    20  	// and loaded into a new QFrame.
    21  	Query string
    22  	// Incrementing indicates the PostgreSQL variant
    23  	// of parameter markers will be used, e.g. $1..$2.
    24  	// The default style is ?..?.
    25  	Incrementing bool
    26  	// Table is the name of the table to be used
    27  	// for generating an INSERT statement.
    28  	Table string
    29  	// EscapeChar is a rune which column and table
    30  	// names will be escaped with. PostgreSQL and SQLite
    31  	// both accept double quotes "" while MariaDB/MySQL
    32  	// only accept backticks.
    33  	EscapeChar rune
    34  	// CoerceMap is a map of columns to perform explicit
    35  	// type coercion on.
    36  	CoerceMap map[string]CoerceFunc
    37  	// Precision specifies how much precision float values
    38  	// should have. 0 has no effect.
    39  	Precision int
    40  }
    41  
    42  type ArgBuilder func(ix index.Int, i int) interface{}
    43  
    44  func NewArgBuilder(col column.Column) (ArgBuilder, error) {
    45  	switch c := col.(type) {
    46  	case bcolumn.Column:
    47  		return func(ix index.Int, i int) interface{} {
    48  			return c.View(ix).ItemAt(i)
    49  		}, nil
    50  	case icolumn.Column:
    51  		return func(ix index.Int, i int) interface{} {
    52  			return c.View(ix).ItemAt(i)
    53  		}, nil
    54  	case fcolumn.Column:
    55  		return func(ix index.Int, i int) interface{} {
    56  			return c.View(ix).ItemAt(i)
    57  		}, nil
    58  	case scolumn.Column:
    59  		return func(ix index.Int, i int) interface{} {
    60  			return c.View(ix).ItemAt(i)
    61  		}, nil
    62  	case ecolumn.Column:
    63  		return func(ix index.Int, i int) interface{} {
    64  			return c.View(ix).ItemAt(i)
    65  		}, nil
    66  	}
    67  	return nil, qerrors.New("NewArgBuilder", fmt.Sprintf("bad column type: %s", reflect.TypeOf(col).Name()))
    68  }