steino / odbc

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

odbc SQLExecute: {HY090} errors #37

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
run sql exec, occurs SQLExecute: {HY090}  errors

1) when insert column data' length less than 8000bytes, OK, Otherwise occurs 
SQLExecute: {HY090}  errors
2) the field type is image

see more information at: 
https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/8uz8k9xzJ0k

The bug should be here:

https://code.google.com/p/odbc/source/browse/param.go#117

                ctype = api.SQL_C_BINARY
                b := make([]byte, len(d))
                copy(b, d)
                p.Data = b
                buf = unsafe.Pointer(&b[0])
                buflen = api.SQLLEN(len(b))
                plen = p.StoreStrLen_or_IndPtr(buflen)
                size = api.SQLULEN(len(b))
                sqltype = api.SQL_BINARY

Binding Parameters for SQL Character Types [1]
If the SQL data type passed in is a character type, ColumnSize is the size in 
characters (not bytes). If the length of the data string in bytes is greater 
than 8000,ColumnSize should be set to SQL_SS_LENGTH_UNLIMITED, indicating that 
there is no limit to the size of the SQL type.
For instance, if the SQL data type is SQL_WVARCHAR, ColumnSize should not be 
greater than 4000. If the actual data length is greater than 4000, then 
ColumnSizeshould be set to SQL_SS_LENGTH_UNLIMITED so that nvarchar(max) will 
be used by driver.
[1]: http://msdn.microsoft.com/en-us/library/ms131462.aspx

change the code as the following:

        ctype = api.SQL_C_BINARY
        b := make([]byte, len(d))
        copy(b, d)
        p.Data = b
        buf = unsafe.Pointer(&b[0])
        buflen = api.SQLLEN(len(b))
        plen = p.StoreStrLen_or_IndPtr(buflen)
        if len(b) < 8000 {
            size = api.SQLULEN(len(b))
            sqltype = api.SQL_BINARY
        } else {
            size = api.SQLULEN(0)   // SQL_SS_LENGTH_UNLIMITED
            sqltype = api.SQL_VARBINARY
        }

all passed! :)

Original issue reported on code.google.com by teg...@gmail.com on 25 Mar 2014 at 2:41

GoogleCodeExporter commented 9 years ago
Please, provide small program to demonstrate your issue. Something that I can 
run to reproduce your issue here. Also, provide your environment: your OS, the 
database you use. Thank you.

Alex

Original comment by alex.bra...@gmail.com on 26 Mar 2014 at 1:25

GoogleCodeExporter commented 9 years ago
OS: Windows Server 2003 Enterprise Edition SP2
Database: SQL Server 2008 Enterprise Edition

1) create the table named as "files" in the database named as "database1"  

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[files](
    [fileid] [uniqueidentifier] NOT NULL,
    [filecontent] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

2) go run the following program:

package main

import (
    _ "code.google.com/p/odbc"
    "bytes"
    "database/sql"
    "fmt"
)
func main() {
    svr := "localhost"
    dbase := "database1"
    usr := "userid"
    psw := "password"
    connstring := fmt.Sprintf("Driver={SQL Server Native Client 10.0};Server=%s;Database=%s;Uid=%s;Pwd=%s;",
        svr,
        dbase,
        usr,
        psw,
    )

    webdb, err := sql.Open("odbc", connstring)
`   if err != nil {
        fmt.Println(err)
        return
    }
    defer webdb.Close()

    ls_sql := "insert files(filecontent) values(?)"
    b := bytes.Repeat([]byte("a"), 8001)
    _, err = webdb.Exec(ls_sql, b)
    if err != nil {
        fmt.Println(err)
        return
    }
}

Original comment by teg...@gmail.com on 26 Mar 2014 at 2:22

GoogleCodeExporter commented 9 years ago
when I use "Driver={SQL Server}" instead of "Driver={SQL Server Native Client 
10.0}".

my patches still occurs SQLExecute: {HY090} errors. :(

Original comment by teg...@gmail.com on 26 Mar 2014 at 2:35

GoogleCodeExporter commented 9 years ago
Thank you for your example. Here https://codereview.appspot.com/81140043/ is my 
fix. Please, check it covers your scenario. Thank you.

Alex

Original comment by alex.bra...@gmail.com on 27 Mar 2014 at 1:27

GoogleCodeExporter commented 9 years ago
This issue was closed by revision 003d75da13f5.

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