zeroair / odbc

Automatically exported from code.google.com/p/odbc
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

unsupported driver -> Scan pair: <nil> -> *string #40

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
when select columns from database, if some column's value is '', it will 
occurred error: unsupported driver -> Scan pair: <nil> -> *string,
so I changed the source at: column.go, line 134:
case api.SQL_C_WCHAR:
        if p == nil {
            //return nil, nil
            return "", nil
        }
        s := (*[1 << 20]uint16)(p)[:len(buf)/2]
        return utf16toutf8(s), nil

Original issue reported on code.google.com by Zuggie....@gmail.com on 11 Apr 2014 at 3:30

GoogleCodeExporter commented 8 years ago
Please, provide small program to demonstrate the problem. Thank you.

Alex

Original comment by alex.bra...@gmail.com on 11 Apr 2014 at 7:35

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Error: sql: Scan error on column index 4: unsupported driver -> Scan pair: 
<nil> -> *string
Query: SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY SSN_NO), SSN_NO, 
                 CURR_STATION, RESULT, ISNULL(FAILCODE, '') 
           FROM SFC_SSN(NOLOCK) 
          WHERE WONO=?
Condition: [NE03300016]

the column FAILCODE type is: NVARCHAR(30)

Original comment by Zuggie....@gmail.com on 11 Apr 2014 at 8:35

GoogleCodeExporter commented 8 years ago
SQL is nice, but I still don't see what the problem is. Please provide small Go 
program I can run to see your problem. Show what you program displays and 
explain why its output is wrong. Hopefully I will be able to run your program 
here.

Thank you.

Alex

Original comment by alex.bra...@gmail.com on 11 Apr 2014 at 11:05

GoogleCodeExporter commented 8 years ago
package main

import (
    _ "./odbc"
    "database/sql"
    "fmt"
    "reflect"
)

const (
    ConnectString = "driver={sql server};server=servername;database=database;uid=sa;pwd=password"
)

func FetchAllRowsPtr(query string, struc interface{}, cond ...interface{}) 
*[]interface{} {
    db, err := sql.Open("odbc", ConnectString)
    if err != nil {
        fmt.Printf("sql.Open Error: %v\n", err)
        panic(err)
    }
    defer db.Close()

    stmt, err := db.Prepare(query)
    if err != nil {
        fmt.Printf("db.Prepare Error: %v\n", err)
        panic(err)
    }
    defer stmt.Close()

    rows, err := stmt.Query(cond...)
    if err != nil {
        fmt.Printf("stmt.Query Error: %v\n", err)
        panic(err)
    }
    defer rows.Close()

    result := make([]interface{}, 0)
    s := reflect.ValueOf(struc).Elem()
    leng := s.NumField()
    onerow := make([]interface{}, leng)
    for i := 0; i < leng; i++ {
        onerow[i] = s.Field(i).Addr().Interface()
    }
    for rows.Next() {
        err = rows.Scan(onerow...)
        if err != nil {
            fmt.Printf("Error: %v\nQuery: %v\nCondition: %v\n", err, query, cond)
            panic(err)
        }
        result = append(result, s.Interface())
    }

    return &result
}

type WoDetail struct {
    Index    int
    Serial   string
    Curr     string
    Result   string
    Failcode string
}

func getdate(wono string) {
    wo_ls := FetchAllRowsPtr(
        `SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY SSN_NO), SSN_NO, 
                 CURR_STATION, RESULT, ISNULL(FAILCODE, '') 
           FROM SFC_SSN(NOLOCK) 
          WHERE WONO=?`, new(WoDetail), wono)
    fmt.Printf("values: %v\n", wo_ls)
}

func main() {
    getdate('NE03300016')
}

// at the line 47, will occurred the error

Original comment by Zuggie....@gmail.com on 11 Apr 2014 at 2:31

GoogleCodeExporter commented 8 years ago
Thank you for the code. I will try it.

Alex

Original comment by alex.bra...@gmail.com on 12 Apr 2014 at 2:26

GoogleCodeExporter commented 8 years ago
I can see your problem now. Here https://codereview.appspot.com/87490043/ is 
the fix. Please, review. Thank you.

Alex

Original comment by alex.bra...@gmail.com on 14 Apr 2014 at 4:14

GoogleCodeExporter commented 8 years ago

Original comment by alex.bra...@gmail.com on 14 Apr 2014 at 4:15

GoogleCodeExporter commented 8 years ago
I can't link the address: https://codereview.appspot.com/87490043/
Did you write a short fix at here, thanks.

Original comment by Zuggie....@gmail.com on 14 Apr 2014 at 4:28

GoogleCodeExporter commented 8 years ago
Sure. Please, see a.diff file attached.

Alex

Original comment by alex.bra...@gmail.com on 14 Apr 2014 at 4:32

Attachments:

GoogleCodeExporter commented 8 years ago
Issue 41 has been merged into this issue.

Original comment by alex.bra...@gmail.com on 28 Apr 2014 at 11:33

GoogleCodeExporter commented 8 years ago
This issue was closed by revision a8ac5d333051.

Original comment by alex.bra...@gmail.com on 6 May 2014 at 1:56