Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database/sql: doesn't support output bool or float types #2328

Closed
gopherbot opened this issue Oct 3, 2011 · 5 comments
Closed

database/sql: doesn't support output bool or float types #2328

gopherbot opened this issue Oct 3, 2011 · 5 comments
Assignees

Comments

@gopherbot
Copy link

@gopherbot gopherbot commented Oct 3, 2011

by jbarham:

The exp/sql documentation mentions support for bool and float values, but the conversion
function used by Scan doesn't support bool or float destination values.  The patch below
adds support for bool, float32 and float64 output types.

jbarham@ubuntu:~/go/src/pkg/exp/sql$ hg diff 
diff -r 978acc122f2e src/pkg/exp/sql/convert.go
--- a/src/pkg/exp/sql/convert.go    Thu Sep 29 21:31:41 2011 -0700
+++ b/src/pkg/exp/sql/convert.go    Mon Oct 03 22:04:32 2011 +1100
@@ -66,6 +66,15 @@
    }
 
    switch dv.Kind() {
+   case reflect.Bool:
+       if s, ok := asString(src); ok {
+           tf, err := strconv.Atob(s)
+           if err != nil {
+               return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err)
+           }
+           dv.SetBool(tf)
+           return nil
+       }
    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
        if s, ok := asString(src); ok {
            i64, err := strconv.Atoi64(s)
@@ -90,6 +99,18 @@
            dv.SetUint(u64)
            return nil
        }
+   case reflect.Float32, reflect.Float64:
+       if s, ok := asString(src); ok {
+           f64, err := strconv.Atof64(s)
+           if err != nil {
+               return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err)
+           }
+           if dv.OverflowFloat(f64) {
+               return fmt.Errorf("string %q overflows %s", s, dv.Kind())
+           }
+           dv.SetFloat(f64)
+           return nil
+       }
    }
 
    return fmt.Errorf("unsupported driver -> Scan pair: %T -> %T", src, dest)
@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Oct 3, 2011

Comment 1:

Yeah, exp/sql is not done yet.
If you'd like to help, see:
   http://golang.org/doc/contribute.html
(Notably, we don't do patches on the bug tracker.)

Owner changed to @bradfitz.

Status changed to Accepted.

@gopherbot
Copy link
Author

@gopherbot gopherbot commented Oct 3, 2011

Comment 2 by jbarham:

Understood.  I just wanted to double-check I wasn't missing anything obvious.
I'll submit a patch through the official channels.
@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Oct 3, 2011

Comment 3:

Cool, thanks.  I'm happy to get help on this stuff.  Glad you're interested!
@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Oct 25, 2011

@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Nov 11, 2011

Comment 5:

This should've been automatically closed.  Something must have failed.
Anyway, fix went in:
http://code.google.com/p/go/source/detail?r=1cf0ffaa8490#

Status changed to Fixed.

@mikioh mikioh changed the title exp/sql doesn't support output bool or float types database/sql: doesn't support output bool or float types Feb 18, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.