Newbie API questions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Newbie API questions

Clark Christensen
OK, I'm not a C programmer (maybe I'll get there
eventually).  But I am writing a wrapper for WinBatch, and
I have a couple of questions about SQLite3's datatypes
(using 3.2.1 on Windows)

[1] I see in the API reference for sqlite3_open() and
sqlite3_prepare() what looks like a "sqlite3" datatype.  In
sqlite3.h, I see that's defined as a "struct".  Using
WinBatch's interface to the DLL, I store the resulting DB
or statement handle in a "binary buffer", then read a 32
bit integer from the buffer to get the handle into a scalar
variable, then pass the integer handles back to other
sqlite3 functions as a long.  This seems to work OK in my
proof of concept program, but I'm worried it's the wrong
datatype or 32 bits isn't enough.

So, based on that long-winded preamble, my question is,
"Under Windows, is a 32 bit integer a reliable enough
datatype for handles?"

[2]  Looking at the sqlite3_bind_* APIs, I'm curious about
the destructor functions, and the "special values",
SQLITE_STATIC, and SQLITE_TRANSIENT.  Not being a C
programmer, these constants are a mystery to me.  I think
it'd be useful to use the sqlite3_bind_* APIs, but I don't
have any clue what to do about the destructors.  My
inclination is to use one of the special values, but I
don't think WinBatch is gonna understand.  Wouldn't these
just compile down to integers 0, or -1?  Could I just pass
them to the APIs as integers?

Any help is much appreciated.

Thanks!

 -Clark

Reply | Threaded
Open this post in threaded view
|

Re: Newbie API questions

Henry Miller

On 5/26/2005 at 16:44 Clark Christensen wrote:

>OK, I'm not a C programmer (maybe I'll get there
>eventually).  But I am writing a wrapper for WinBatch, and
>I have a couple of questions about SQLite3's datatypes
>(using 3.2.1 on Windows)
>
>[1] I see in the API reference for sqlite3_open() and
>sqlite3_prepare() what looks like a "sqlite3" datatype.  In
>sqlite3.h, I see that's defined as a "struct".  Using
>WinBatch's interface to the DLL, I store the resulting DB
>or statement handle in a "binary buffer", then read a 32
>bit integer from the buffer to get the handle into a scalar
>variable, then pass the integer handles back to other
>sqlite3 functions as a long.  This seems to work OK in my
>proof of concept program, but I'm worried it's the wrong
>datatype or 32 bits isn't enough.
>
>So, based on that long-winded preamble, my question is,
>"Under Windows, is a 32 bit integer a reliable enough
>datatype for handles?"

NO!   Do not do this.  The type is a pointer, not an integer.   On
older 32 bit platforms this works by chance because integers and
pointers both happen to be 32 bit.   On new 64 bit platforms this will
FAIL because pointers are changed to 64 bits, while integers are often
still 32 bits!    

Most people are not running 64 bit windows on the 64 bit CPUs, but this
will change soon, Microsoft is working on 64 bit windows, and both
Intel and AMD have committed to 64 bit CPUs as the future.   Already
these chips are getting out of the early adopter stage and ready to go
prime time.   Linux is already running in full 64 bit mode.

If WinBatch does not have any way to wrap pointers demand one.  That is
the only useful solution.   Looking at what others are doing though, I
suspect there is a way to do this - ask them as they know their product
better.   Note that if the answer is what you came up with, that is a
sign that their code is badly written and you should run - even though
it looks nice on the outside the internal are clearly screwed up.
Most likely they have a good solution.  (Calling something to get
pointer size, and then coping that many bits would be acceptable, but a
true pointer type is preferred)

>[2]  Looking at the sqlite3_bind_* APIs, I'm curious about
>the destructor functions, and the "special values",
>SQLITE_STATIC, and SQLITE_TRANSIENT.  Not being a C
>programmer, these constants are a mystery to me.  I think
>it'd be useful to use the sqlite3_bind_* APIs, but I don't
>have any clue what to do about the destructors.  My
>inclination is to use one of the special values, but I
>don't think WinBatch is gonna understand.  Wouldn't these
>just compile down to integers 0, or -1?  Could I just pass
>them to the APIs as integers?

If you must.   However should sqlite need to change these values you
would need to change your stuff.  This is unlikely, but you better
document it, and assign someone to check each time you upgrade sqlite.
 Once again you should ask the WinBatch people if there is a better
way.   I suspect there is.

Reply | Threaded
Open this post in threaded view
|

Re: Newbie API questions

Jay Sprenkle
In reply to this post by Clark Christensen
> [2]  Looking at the sqlite3_bind_* APIs, I'm curious about
> the destructor functions, and the "special values",
> SQLITE_STATIC, and SQLITE_TRANSIENT.  Not being a C
> programmer, these constants are a mystery to me.  I think
> it'd be useful to use the sqlite3_bind_* APIs, but I don't
> have any clue what to do about the destructors.  My
> inclination is to use one of the special values, but I
> don't think WinBatch is gonna understand.  Wouldn't these
> just compile down to integers 0, or -1?  Could I just pass
> them to the APIs as integers?

These are defined using "#define". This is a text substitution at compile
time. If you look you'll see it's substituting integer numbers for those
name strings. It's done to make the code more readable. Passing the
appropriate integer value (taken from the #define) will work just fine.
Reply | Threaded
Open this post in threaded view
|

Re: Newbie API questions

Dennis Cote
Jay Sprenkle wrote:

>>[2]  Looking at the sqlite3_bind_* APIs, I'm curious about
>>the destructor functions, and the "special values",
>>SQLITE_STATIC, and SQLITE_TRANSIENT.  Not being a C
>>programmer, these constants are a mystery to me.  I think
>>it'd be useful to use the sqlite3_bind_* APIs, but I don't
>>have any clue what to do about the destructors.  My
>>inclination is to use one of the special values, but I
>>don't think WinBatch is gonna understand.  Wouldn't these
>>just compile down to integers 0, or -1?  Could I just pass
>>them to the APIs as integers?
>>    
>>
>
>These are defined using "#define". This is a text substitution at compile
>time. If you look you'll see it's substituting integer numbers for those
>name strings. It's done to make the code more readable. Passing the
>appropriate integer value (taken from the #define) will work just fine.
>
>  
>
I will usually work just fine, but may fail for the same reasons Henry
discussed about the sqlite structure handle. The destructor argument to
the sqlite_bind calls are actually pointers, in this case pointers to
functions. The special case values are replaced by the C preprocessor
with expressions that cast the integer constants 0 and -1 to the
required function pointer type. To call these APIs on a machine with 64
bit pointers and 32 bit integers you have the same issues as with the
sqlite3 handle. You need to ensure that you pass a 64 bit pointer to
these routines, passing integer values may not work.

Dennis Cote
Reply | Threaded
Open this post in threaded view
|

Re: Newbie API questions

Clark Christensen
In reply to this post by Clark Christensen

--- Dennis Cote <[hidden email]> wrote:

> Jay Sprenkle wrote:
>
> >>[2]  Looking at the sqlite3_bind_* APIs, I'm curious
> about
> >>the destructor functions, and the "special values",
> >>SQLITE_STATIC, and SQLITE_TRANSIENT.  Not being a C
> >>programmer, these constants are a mystery to me.  I
> think
> >>it'd be useful to use the sqlite3_bind_* APIs, but I
> don't
> >>have any clue what to do about the destructors.  My
> >>inclination is to use one of the special values, but I
> >>don't think WinBatch is gonna understand.  Wouldn't
> these
> >>just compile down to integers 0, or -1?  Could I just
> pass
> >>them to the APIs as integers?
> >>    
> >>
> >
> >These are defined using "#define". This is a text
> substitution at compile
> >time. If you look you'll see it's substituting integer
> numbers for those
> >name strings. It's done to make the code more readable.
> Passing the
> >appropriate integer value (taken from the #define) will
> work just fine.
> >
> >  
> >
> I will usually work just fine, but may fail for the same
> reasons Henry
> discussed about the sqlite structure handle. The
> destructor argument to
> the sqlite_bind calls are actually pointers, in this case
> pointers to
> functions. The special case values are replaced by the C
> preprocessor
> with expressions that cast the integer constants 0 and -1
> to the
> required function pointer type. To call these APIs on a
> machine with 64
> bit pointers and 32 bit integers you have the same issues
> as with the
> sqlite3 handle. You need to ensure that you pass a 64 bit
> pointer to
> these routines, passing integer values may not work.
>
> Dennis Cote
>

OK, good information from all.  Thanks very much.  It's
helped me move on with the project.

Obviously, this wrapper will be limited to 32 bit systems
until WinBatch adds a function to read the 64 bit pointer
from the buffer.  Currently it maxes-out with a 32 bit
read.   I'll ask them about that.

Short-term, I think I should be OK :-)

 -Clark