Changeset 152134 in webkit


Ignore:
Timestamp:
Jun 27, 2013 3:56:32 PM (11 years ago)
Author:
andersca@apple.com
Message:

Stop using deprecatedCharactersWithNullTermination in SQLite code
https://bugs.webkit.org/show_bug.cgi?id=118146

Reviewed by Filip Pizlo.

Turns out SQLite uses UTF-8 internally so we might just as well use the SQLite functions
that take UTF-8 strings and do the conversion ourselves. This has the added advantage that we can
use String::utf8() which returns a null-terminated string.

Require a version of SQLite newer than 3.6.16 so we can remove two workarounds. 3.6.16 was released
4 years ago so supported port is likely to have it.

  • platform/sql/SQLiteFileSystem.cpp:

(WebCore::SQLiteFileSystem::openDatabase):
Use sqlite3_open instead of sqlite3_open16.

  • platform/sql/SQLiteFileSystem.h:

Fix parameter name capitalization.

  • platform/sql/SQLiteStatement.cpp:

(WebCore::SQLiteStatement::prepare):
Use sqlite3_prepare_v2. Also, pass the length of the string (including the null character), since
that lets SQLite avoid a buffer copy. Remove a workaround for versions of SQLite older than 3.6.16.

Location:
trunk/Source/WebCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r152132 r152134  
     12013-06-27  Anders Carlsson  <andersca@apple.com>
     2
     3        Stop using deprecatedCharactersWithNullTermination in SQLite code
     4        https://bugs.webkit.org/show_bug.cgi?id=118146
     5
     6        Reviewed by Filip Pizlo.
     7
     8        Turns out SQLite uses UTF-8 internally so we might just as well use the SQLite functions
     9        that take UTF-8 strings and do the conversion ourselves. This has the added advantage that we can
     10        use String::utf8() which returns a null-terminated string.
     11
     12        Require a version of SQLite newer than 3.6.16 so we can remove two workarounds. 3.6.16 was released
     13        4 years ago so supported port is likely to have it.
     14
     15        * platform/sql/SQLiteFileSystem.cpp:
     16        (WebCore::SQLiteFileSystem::openDatabase):
     17        Use sqlite3_open instead of sqlite3_open16.
     18
     19        * platform/sql/SQLiteFileSystem.h:
     20        Fix parameter name capitalization.
     21
     22        * platform/sql/SQLiteStatement.cpp:
     23        (WebCore::SQLiteStatement::prepare):
     24        Use sqlite3_prepare_v2. Also, pass the length of the string (including the null character), since
     25        that lets SQLite avoid a buffer copy. Remove a workaround for versions of SQLite older than 3.6.16.
     26
    1272013-06-27  Andrew Lo  <anlo@blackberry.com>
    228
  • trunk/Source/WebCore/platform/sql/SQLiteFileSystem.cpp

    r152069 r152134  
    4545}
    4646
    47 int SQLiteFileSystem::openDatabase(const String& fileName, sqlite3** database, bool)
     47int SQLiteFileSystem::openDatabase(const String& filename, sqlite3** database, bool)
    4848{
    49     // SQLite expects a null terminator on its UTF-16 strings.
    50     String path = fileName;
    51     return sqlite3_open16(path.deprecatedCharactersWithNullTermination(), database);
     49    return sqlite3_open(filename.utf8().data(), database);
    5250}
    5351
  • trunk/Source/WebCore/platform/sql/SQLiteFileSystem.h

    r149582 r152134  
    4747    // Opens a database file.
    4848    //
    49     // fileName - The name of the database file.
     49    // filemame - The name of the database file.
    5050    // database - The SQLite structure that represents the database stored
    5151    //            in the given file.
     
    5353    //                     Used by Chromium to determine if the DB file needs to be opened
    5454    //                     using a custom VFS.
    55     static int openDatabase(const String& fileName, sqlite3** database, bool forWebSQLDatabase);
     55    static int openDatabase(const String& filename, sqlite3** database, bool forWebSQLDatabase);
    5656
    5757    // Returns the file name for a database.
  • trunk/Source/WebCore/platform/sql/SQLiteStatement.cpp

    r152069 r152134  
    3333#include <wtf/text/StringImpl.h>
    3434
     35// SQLite 3.6.16 makes sqlite3_prepare_v2 automatically retry preparing the statement
     36// once if the database scheme has changed. We rely on this behavior.
     37#if SQLITE_VERSION_NUMBER < 3006016
     38#error SQLite version 3.6.16 or newer is required
     39#endif
     40
    3541namespace WebCore {
    36 
    37 #if SQLITE_VERSION_NUMBER < 3003009
    38 
    39 // FIXME: This overload helps us compile with older versions of SQLite 3, but things like quotas will not work.
    40 static inline int sqlite3_prepare16_v2(sqlite3* db, const void* zSql, int nBytes, sqlite3_stmt** ppStmt, const void** pzTail)
    41 {
    42     return sqlite3_prepare16(db, zSql, nBytes, ppStmt, pzTail);
    43 }
    44 
    45 #endif
    4642
    4743SQLiteStatement::SQLiteStatement(SQLiteDatabase& db, const String& sql)
     
    6864        return SQLITE_INTERRUPT;
    6965
    70     const void* tail = 0;
    71     LOG(SQLDatabase, "SQL - prepare - %s", m_query.ascii().data());
    72     String strippedQuery = m_query.stripWhiteSpace();
    73     const UChar* nullTermed = strippedQuery.deprecatedCharactersWithNullTermination();
    74     int error = sqlite3_prepare16_v2(m_database.sqlite3Handle(), nullTermed, -1, &m_statement, &tail);
    75 
    76     // Starting with version 3.6.16, sqlite has a patch (http://www.sqlite.org/src/ci/256ec3c6af)
    77     // that should make sure sqlite3_prepare16_v2 doesn't return a SQLITE_SCHEMA error.
    78     // If we're using an older sqlite version, try to emulate the patch.
    79     if (error == SQLITE_SCHEMA) {
    80       sqlite3_finalize(m_statement);
    81       error = sqlite3_prepare16_v2(m_database.sqlite3Handle(), m_query.deprecatedCharactersWithNullTermination(), -1, &m_statement, &tail);
    82     }
     66    CString query = m_query.stripWhiteSpace().utf8();
     67   
     68    LOG(SQLDatabase, "SQL - prepare - %s", query.data());
     69
     70    // Pass the length of the string including the null character to sqlite3_prepare_v2;
     71    // this lets SQLite avoid an extra string copy.
     72    size_t lengthIncludingNullCharacter = query.length() + 1;
     73
     74    const char* tail;
     75    int error = sqlite3_prepare_v2(m_database.sqlite3Handle(), query.data(), lengthIncludingNullCharacter, &m_statement, &tail);
    8376
    8477    if (error != SQLITE_OK)
    85         LOG(SQLDatabase, "sqlite3_prepare16 failed (%i)\n%s\n%s", error, m_query.ascii().data(), sqlite3_errmsg(m_database.sqlite3Handle()));
    86     const UChar* ch = static_cast<const UChar*>(tail);
    87     if (ch && *ch)
     78        LOG(SQLDatabase, "sqlite3_prepare16 failed (%i)\n%s\n%s", error, query.data(), sqlite3_errmsg(m_database.sqlite3Handle()));
     79
     80    if (tail && *tail)
    8881        error = SQLITE_ERROR;
     82
    8983#ifndef NDEBUG
    9084    m_isPrepared = error == SQLITE_OK;
Note: See TracChangeset for help on using the changeset viewer.