github.com/tobgu/qframe@v0.4.0/config/sql/config.go (about) 1 package sql 2 3 import ( 4 qsqlio "github.com/tobgu/qframe/internal/io/sql" 5 ) 6 7 type coerceType int 8 9 const ( 10 _ coerceType = iota 11 // Int64ToBool casts an int64 type into a bool, 12 // useful for handling SQLite INT -> BOOL. 13 Int64ToBool 14 StringToFloat 15 ) 16 17 // CoercePair casts the scanned value in Column 18 // to another type. 19 type CoercePair struct { 20 Column string 21 Type coerceType 22 } 23 24 func coerceFunc(cType coerceType) qsqlio.CoerceFunc { 25 switch cType { 26 case Int64ToBool: 27 return qsqlio.Int64ToBool 28 case StringToFloat: 29 return qsqlio.StringToFloat 30 } 31 return nil 32 } 33 34 // Config holds configuration parameters for reading/writing to/from a SQL DB. 35 type Config qsqlio.SQLConfig 36 37 // ConfigFunc manipulates a Config object. 38 type ConfigFunc func(*Config) 39 40 // NewConfig creates a new config object. 41 func NewConfig(ff []ConfigFunc) Config { 42 conf := Config{} 43 for _, f := range ff { 44 f(&conf) 45 } 46 return conf 47 } 48 49 // Query is a Raw SQL statement which must return 50 // appropriate types which can be inferred 51 // and loaded into a new QFrame. 52 func Query(query string) ConfigFunc { 53 return func(c *Config) { 54 c.Query = query 55 } 56 } 57 58 // Table is the name of the table to be used 59 // for generating an INSERT statement. 60 func Table(table string) ConfigFunc { 61 return func(c *Config) { 62 c.Table = table 63 } 64 } 65 66 // Postgres configures the query builder 67 // to generate SQL that is compatible with 68 // PostgreSQL. See github.com/lib/pq 69 func Postgres() ConfigFunc { 70 return func(c *Config) { 71 EscapeChar('"')(c) 72 Incrementing()(c) 73 } 74 } 75 76 // SQLite configures the query builder to 77 // generate SQL that is compatible with 78 // SQLite3. See github.com/mattn/go-sqlite3 79 func SQLite() ConfigFunc { 80 return func(c *Config) { 81 EscapeChar('"')(c) 82 } 83 } 84 85 // MySQL configures the query builder to 86 // generate SQL that is compatible with MySQL/MariaDB 87 // See github.com/go-sql-driver/mysql 88 func MySQL() ConfigFunc { 89 return func(c *Config) { 90 EscapeChar('`')(c) 91 } 92 } 93 94 // Incrementing indicates the PostgreSQL variant 95 // of parameter markers will be used, e.g. $1..$2. 96 // The default style is ?..?. 97 func Incrementing() ConfigFunc { 98 return func(c *Config) { 99 c.Incrementing = true 100 } 101 } 102 103 // EscapeChar is a rune which column and table 104 // names will be escaped with. PostgreSQL and SQLite 105 // both accept double quotes "" while MariaDB/MySQL 106 // only accept backticks. 107 func EscapeChar(r rune) ConfigFunc { 108 return func(c *Config) { 109 c.EscapeChar = r 110 } 111 } 112 113 // Coerce accepts a map of column names that 114 // will be cast explicitly into the desired type. 115 func Coerce(pairs ...CoercePair) ConfigFunc { 116 return func(c *Config) { 117 c.CoerceMap = map[string]qsqlio.CoerceFunc{} 118 for _, pair := range pairs { 119 c.CoerceMap[pair.Column] = coerceFunc(pair.Type) 120 } 121 } 122 } 123 124 // Precision sets the precision float64 types will 125 // be rounded to when read from SQL. 126 func Precision(i int) ConfigFunc { 127 return func(c *Config) { 128 c.Precision = i 129 } 130 }