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

SQLPrepare fails on long SQL strings #370

Open
xitology opened this issue May 28, 2023 · 0 comments
Open

SQLPrepare fails on long SQL strings #370

xitology opened this issue May 28, 2023 · 0 comments

Comments

@xitology
Copy link
Contributor

When you prepare a query with ODBC, it fails when the SQL string is longer than 2^15 characters.

Here is an example:

using ODBC, Tables

# Specify the ODBC connection string using this environment variable.
const ODBC_CONNECTION_STRING = ENV["ODBC_CONNECTION_STRING"]

conn = DBInterface.connect(ODBC.Connection, ODBC_CONNECTION_STRING)
sql = "SELECT" * " "^(2^16) * "1 AS x"

# Succeeds.
cr = DBInterface.execute(conn, sql)
display(Tables.rowtable(cr))

# Fails on the next line!
stmt = DBInterface.prepare(conn, sql)
cr = DBInterface.execute(stmt)
display(Tables.rowtable(cr))
1-element Vector{@NamedTuple{x::Int32}}:
 (x = 1,)
ERROR: LoadError: InexactError: trunc(Int16, 65548)
Stacktrace:
  [1] throw_inexacterror(f::Symbol, ::Type{Int16}, val::Int64)
    @ Core ./boot.jl:631
  [2] checked_trunc_sint(::Type{Int16}, x::Int64)
    @ Core ./boot.jl:653 [inlined]
  [3] toInt16(x::Int64)
    @ Core ./boot.jl:679 [inlined]
  [4] Int16(x::Int64)
    @ Core ./boot.jl:779 [inlined]
  [5] convert
    @ Base ./number.jl:7 [inlined]
  [6] cconvert
    @ Base ./essentials.jl:538 [inlined]
  [7] SQLPrepare(stmt::Ptr{Nothing}, query::String)
    @ ODBC.API ~/MechanicalRabbit/ODBC.jl/src/API.jl:89
  [8] prepare(dbc::ODBC.API.Handle, sql::String)
    @ ODBC.API ~/MechanicalRabbit/ODBC.jl/src/API.jl:119
  [9] prepare(conn::ODBC.Connection, sql::String)
    @ ODBC ~/MechanicalRabbit/ODBC.jl/src/dbinterface.jl:142
 [10] top-level scope
    @ ~/MechanicalRabbit/ODBC.jl/long-sql.jl:14
in expression starting at /home/xi/MechanicalRabbit/ODBC.jl/long-sql.jl:14

This is caused by invalid type specification in SQLPrepare. In fact, both SQLPrepare and SQLExecDirect specify the type of the query length parameter incorrectly: the former uses Int16 and the latter Int. But according to the documentation these functions expect a SQLINTEGER, that is, Int32.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant