scan

package
v0.0.0-...-d31700d Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Column2FieldPath

func Column2FieldPath(column string) (path []string)

func Scan

func Scan(rows *sql.Rows, data interface{}) error

data must be a sql.Scanner or a non nil pointer. If data a pointer, it's indirect once to get the target to store rows from database. And it's indirect only once, we cann't indirect all pointer until non pointer type, because nil value should be set to the second layer pointer, not the non pointer type. If target is a slice, it scan all rows into the slice, otherwise it scan a single row.

Example (Bool)
var b bool
if err := Scan(getTestRows(`select true`), &b); err != nil {
	log.Panic(err)
}
fmt.Println(b)
if err := Scan(getTestNull(), &b); err != nil {
	log.Panic(err)
}
fmt.Println(b)
Output:

true
false
Example (Float)
var f float32
if err := Scan(getTestRows(`select 1.23::float`), &f); err != nil {
	log.Panic(err)
}
fmt.Println(f)
Output:

1.23
Example (Int)
var i int
if err := Scan(getTestIntValues(), &i); err != nil {
	log.Panic(err)
}
fmt.Println(i)

if err := Scan(getTestNull(), &i); err != nil {
	fmt.Println(err)
}
fmt.Println(i)
Output:

9
0
Example (IntPointer)
var pointer *int
if err := Scan(getTestIntValues(), &pointer); err != nil {
	log.Panic(err)
}
fmt.Println(*pointer)

if err := Scan(getTestNull(), &pointer); err != nil {
	log.Panic(err)
}
fmt.Println(pointer)

var p **int
if err := Scan(getTestIntValues(), &p); err != nil {
	log.Panic(err)
}
fmt.Println(**p)
Output:

9
<nil>
9
Example (IntSlice)
var a []int
if err := Scan(getTestIntValues(), &a); err != nil {
	log.Panic(err)
}
fmt.Println(a)
Output:

[9 99 999]
Example (IntValueOutOfRange)
var i int8
if err := Scan(getTestRows(`select 128`), &i); err != nil {
	fmt.Println(strings.Replace(err.Error(), `, name "?column?"`, ``, 1))
}
Output:

sql: Scan error on column index 0: bsql: cannot assign int64(128) to int8: value out of range
Example (Interface)
var s struct {
	A interface{}
}
if err := Scan(getTestRows(`SELECT 12 AS a`), &s); err != nil {
	log.Panic(err)
}
fmt.Printf("%v %v\n", s, reflect.TypeOf(s.A))

var u uint64
s.A = &u
if err := Scan(getTestRows(`SELECT 12 AS a`), &s); err != nil {
	log.Panic(err)
}
value := *s.A.(*uint64)
fmt.Printf("%d %v %v\n", u, value, reflect.TypeOf(value))
Output:

{12} int64
12 12 uint64
Example (Map)
var row = map[string]interface{}{
	"Cities":    []string{},
	"FriendIds": pq.Int64Array{},
}
if err := Scan(getTestStudents(), &row); err != nil {
	log.Panic(err)
}
fmt.Println(row["CreatedAt"].(time.Time).UTC())
delete(row, "CreatedAt")

fmt.Println(row)
Output:

2001-09-01 04:25:48 +0000 UTC
map[Cities:{成都,上海} FriendIds:{1001,1002} Id:1 Money:25.04 Name:李雷 Scores:["语文",99,"数学",100] Status:0]
Example (MultiLayers)
var s struct {
	A struct {
		B int
	}
}
if err := Scan(getTestRows(`SELECT 12 AS "a.b"`), &s); err != nil {
	log.Panic(err)
}
fmt.Println(s)
Output:

{{12}}
Example (PqInt64Array)
var a pq.Int64Array
if err := Scan(getTestRows(`select '{9,99,999}'::int[]`), &a); err != nil {
	log.Panic(err)
}
fmt.Println(a)

if err := Scan(getTestNull(), &a); err != nil {
	log.Panic(err)
}
fmt.Println(a)
Output:

[9 99 999]
[]
Example (Scanner)
var d ***decimal.Decimal
if err := Scan(getTestRows(`select '12.34'`), &d); err != nil {
	log.Panic(err)
}
fmt.Println(***d)

if err := Scan(getTestNull(), &d); err != nil {
	log.Panic(err)
}
fmt.Println(d)
Output:

12.34
<nil>
Example (SingleValueIntoStruct)
var s struct{ Id int }
if err := Scan(getTestIntValues(), &s); err != nil {
	log.Panic(err)
}
fmt.Println(s)
Output:

{9}
Example (String)
var s string
if err := Scan(getTestRows(`select 'abc'`), &s); err != nil {
	log.Panic(err)
}
fmt.Println(s)

if err := Scan(getTestNull(), &s); err != nil {
	log.Panic(err)
}
fmt.Printf("'%s'\n", s)
Output:

abc
''
Example (StringPointer)
var pointer *string
if err := Scan(getTestRows(`select 'abc'`), &pointer); err != nil {
	log.Panic(err)
}
fmt.Println(*pointer)

if err := Scan(getTestNull(), &pointer); err != nil {
	log.Panic(err)
}
fmt.Println(pointer)

var p **string
if err := Scan(getTestRows(`select 'abc'`), &p); err != nil {
	log.Panic(err)
}
fmt.Println(**p)
Output:

abc
<nil>
abc
Example (Struct)
var row Student
if err := Scan(getTestStudents(), &row); err != nil {
	log.Panic(err)
}
row.CreatedAt = row.CreatedAt.UTC()
fmt.Printf("{%d %s %v %v %v %v %d\n  %v %v}\n",
	row.Id, row.Name, row.Cities, row.FriendIds, row.Scores, row.Money, row.Status,
	row.CreatedAt, row.UpdatedAt,
)
Output:

{1 李雷 [成都 上海] [1001 1002] [语文 99 数学 100] 25.04 0
  2001-09-01 04:25:48 +0000 UTC <nil>}
Example (StructSlice)
var rows []Student
if err := Scan(getTestStudents(), &rows); err != nil {
	log.Panic(err)
}
for _, row := range rows {
	row.CreatedAt = row.CreatedAt.UTC()
	if row.UpdatedAt != nil {
		t := row.UpdatedAt.UTC()
		row.UpdatedAt = &t
	}
	fmt.Printf("{%d %s %v %v %v %v %d\n  %v %v}\n",
		row.Id, row.Name, row.Cities, row.FriendIds, row.Scores, row.Money, row.Status,
		row.CreatedAt, row.UpdatedAt,
	)
}
Output:

{1 李雷 [成都 上海] [1001 1002] [语文 99 数学 100] 25.04 0
  2001-09-01 04:25:48 +0000 UTC <nil>}
{2 韩梅梅 [广州 北京] [1001 1003] [语文 98 数学 95] 95.9 0
  2001-09-01 02:25:48 +0000 UTC 2001-09-02 02:25:58 +0000 UTC}
Example (Time)
var t time.Time
if err := Scan(getTestRows(`select '2001-09-01 12:25:48+08'::timestamptz`), &t); err != nil {
	log.Panic(err)
}
fmt.Println(t.UTC())
Output:

2001-09-01 04:25:48 +0000 UTC
Example (Uint)
var i uint
if err := Scan(getTestIntValues(), &i); err != nil {
	log.Panic(err)
}
fmt.Println(i)

if err := Scan(getTestNull(), &i); err != nil {
	fmt.Println(err)
}
fmt.Println(i)
Output:

9
0

func ScanRow

func ScanRow(rows *sql.Rows, columns []ColumnType, target reflect.Value) error

If target is a struct, it scan all columns into the struct, otherwise it scan a single column. No indirect is performed, because nil value should be set to pointer.

Types

type ColumnType

type ColumnType struct {
	FieldPath []string
	FieldName string
	*sql.ColumnType
}

func ColumnTypes

func ColumnTypes(rows *sql.Rows) ([]ColumnType, error)

Jump to

Keyboard shortcuts

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