Sorry to threadjack here, but this made me think of something...

Does this mean that sqlite3_column_text always makes a copy of the string to 
put a null terminator on the end?  My ORM uses std::strings in UTF8 everywhere, 
so does that mean it would be quite a bit faster to pull strings out using 
sqlite3_column_bytes?  When I'm inserting, I'm always using bound columns with 
sqlite3_bind_text, explicitly stating the number of characters (which doesn't 
include a trailing null).  So, I can easily reconstruct std::strings from a 
void * and a number of bytes, without sqlite having to make a copy of it and 
null terminate it for me, if sqlite is doing extra work in my circumstance.

Thanks!
-David

-----Original Message-----
From: sqlite-users-boun...@sqlite.org [mailto:sqlite-users-boun...@sqlite.org] 
On Behalf Of Richard Hipp
Sent: Sunday, December 15, 2013 5:10 PM
To: General Discussion of SQLite Database
Subject: Re: [sqlite] SQL_STATIC unterminated strings, and sqlite3_result_text

On Sun, Dec 15, 2013 at 6:04 PM, James K. Lowden
<jklow...@schemamania.org>wrote:

> http://www.sqlite.org/c3ref/result_blob.html
>
> I found a documentation typo and have a question about SQLITE_STATIC.
>
> The documentation for sqlite3_result_text says,
>
>         "If the 3rd parameter is non-negative, then it must be the 
> byte offset into the string where the NUL terminator would appear if 
> the string where NUL terminated."
>
> I believe the intent is subjunctive,
>
>         "if the string were NUL terminated".
>
> meaning no NUL is required.  It continues:
>
>         "If the 4th parameter to the sqlite3_result_text* interfaces 
> or to sqlite3_result_blob is the special constant SQLITE_STATIC, then 
> SQLite assumes that the text or BLOB result is in constant space and 
> does not copy the content...."
>

That statement would be more precise if it read:  "... does not copy the
content RIGHT AWAY..."   If you are inserting into the database, obviously
SQLite needs to copy the content in order to put it on disk.  If you later 
query for the content, then it will copy off of disk again.

If your statements is:

    SELECT ?1;

And you bind a string that is not zero-terminated then request the result using 
sqlite3_column_text(), then SQLite will make a copy of the string in order to 
add the zero-terminator.  But, if you request the string using
sqlite3_column_blob() it will not make a copy.  In other words, it delays 
copying the string until it really must, and avoids making a copy if possible.

The application has no idea if and when SQLite might make a copy of the 
SQLITE_STATIC-bound string, so the application must guarantee that the string 
does not change until the statement is finalized or until the same parameter is 
rebound to a different value.



>
> My data are static (a read-only mmap'ed file), and the columns are not 
> null-terminated.
>
> The documentation for the usual column-reading function
> sqlite3_column_text() says it always returns a null-terminated string:
>
>         "Strings returned by sqlite3_column_text() and 
> sqlite3_column_text16(), even empty strings, are always 
> zero-terminated."
>
> According to the above, the user gets a null-terminated string from 
> static data in a virtual table that is not copied and need not contain 
> a NUL terminator. That seems unlikely.  I would think either a copy is 
> made or the supplied static data must end in NUL.
>
> If sqlite3_result_text() is provided data with a nonzero length marked 
> as SQLITE_STATIC, where does the NUL come from that is returned to the 
> application by sqlite3_column_text()?
>
> --jkl
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@sqlite.org
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>



--
D. Richard Hipp
d...@sqlite.org
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to