FAQ
For anyone else who digs up this old thread and finds that 1) there are no
examples of this, and 2) the way of doing it is entirely not obvious, you
probably want to do something like a cleaned up and more generic version of:

if self.Rows.Next() {
var values []interface{}
var generic = reflect.TypeOf(values).Elem()
for i := 0; i < len(cols); i++ {
values = append(values, reflect.New(generic).Interface())
}

self.Rows.Scan(values...)
for i := 0; i < len(cols); i++ {
var raw_value = *(values[i].(*interface{}))
var raw_type = reflect.TypeOf(raw_value)
switch {
case raw_type == reflect.TypeOf(int64(0)):
rtn[cols[i]] = raw_value.(int64)
}
}

~
Doug.
On Monday, April 16, 2012 5:34:24 AM UTC+8, sdeg...@8thlight.com wrote:

You can use .Columns() on a sql.Rows (
http://golang.org/pkg/database/sql/#Rows.Columns)

Also you can use *interface{} (
http://golang.org/pkg/database/sql/#Rows.Scan) and then do type
switching, including on nil, for a few simple types (
http://golang.org/pkg/database/sql/#Scanner)

-Steven

On Sunday, April 15, 2012 3:38:01 PM UTC-5, Peter Kleiweg wrote:

I changed the code so it handles values of different types, using a
prefix in column labels to indicate the type:

func printTable(rows *sql.Rows) {

pr := func(t interface{}) (r string) {
r = "\\N"
switch v := t.(type) {
case *sql.NullBool:
if v.Valid {
r = fmt.Sprintf("%v", v.Bool)
}
case *sql.NullString:
if v.Valid {
r = v.String
}
case *sql.NullInt64:
if v.Valid {
r = fmt.Sprintf("%6d", v.Int64)
}
case *sql.NullFloat64:
if v.Valid {
r = fmt.Sprintf("%.2f", v.Float64)
}
case *time.Time:
if v.Year() > 1900 {
r = v.Format("_2 Jan 2006")
}
default:
r = fmt.Sprintf("%#v", t)
}
return
}

c, _ := rows.Columns()
n := len(c)

// print labels
for i := 0; i < n; i++ {
if len(c[i]) > 1 && c[i][1] == ':' {
fmt.Print(c[i][2:], "\t")
} else {
fmt.Print(c[i], "\t")
}
}
fmt.Print("\n\n")

// print data
var field []interface{}
for i := 0; i < n; i++ {
switch {
case c[i][:2] == "b:":
field = append(field, new(sql.NullBool))
case c[i][:2] == "f:":
field = append(field, new(sql.NullFloat64))
case c[i][:2] == "i:":
field = append(field, new(sql.NullInt64))
case c[i][:2] == "s:":
field = append(field, new(sql.NullString))
case c[i][:2] == "t:":
field = append(field, new(time.Time))
default:
field = append(field, new(sql.NullString))
}
}
for rows.Next() {
checkErr(rows.Scan(field...))
for i := 0; i < n; i++ {
fmt.Print(pr(field[i]), "\t")
}
fmt.Println()
}
fmt.Println()
}

func checkErr(err error) {
if err != nil {
_, filename, lineno, ok := runtime.Caller(1)
if ok {
fmt.Fprintf(os.Stderr, "%v:%v: %v\n", filename,
lineno, err)
}
panic(err)
}
}

Here is an example call:

rows, err = db.Query("select id as \"i:id\", name as \"s:name\",
date as \"t:date\" from `item`")
checkErr(err)
printTable(rows)
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 1 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 10, '13 at 4:37a
activeApr 10, '13 at 4:37a
posts1
users1
websitegolang.org

1 user in discussion

Dougx: 1 post

People

Translate

site design / logo © 2021 Grokbase