qt-interest@trolltech.com
[Top] [All Lists]

Re: Unicode aware COLLATION in QSQLITE

Subject: Re: Unicode aware COLLATION in QSQLITE
From: Bill KING
Date: Thu, 06 Sep 2007 11:23:29 +1000
marco bubke wrote:
> Hi
>
> What is the best way to do Unicode aware COLLATION in sqlite. Is there
> a easy way which is working with qt out of the box(use Qt function for
> comparision)?
>
> Thanks and regards
>
> Marco
We ended up using a custom collation type in qtopia, but found that it
added a significant overhead to our performance. If you can cop that
performance hit, then the below code may help:
Otherwise, we ended up using a custom sorting column, that we repopulate
when we change language. (Note, code is not likely to compile out of the
box for you, but will give you an idea of where to travel).

void QtopiaSqlPrivate::installSorting( QSqlDatabase &db)
{
#if defined(Q_USE_SQLITE) && defined(USE_LOCALE_AWARE)
        int sqliteLocaleAwareCompare(void *, int ll, const void *l, int
rl, const void *r);
        QVariant v = db.driver()->handle();
        if (v.isValid() && strcmp(v.typeName(), "sqlite3*") == 0) {
            // v.data() returns a pointer to the handle
            sqlite3 *handle = *static_cast<sqlite3 **>(v.data());
            if (handle != 0) { // check that it is not NULL
                int result = sqlite3_create_collation(
                        handle,
                        "localeAwareCompare",
                        SQLITE_UTF16, // ANY would be nice, but we only
encode in 16 anyway.
                        0,
                        sqliteLocaleAwareCompare);
                if (result != SQLITE_OK)
                    qLog(Sql) << "Could not add string collation
function: " << result;
            } else {
                qLog(Sql) << "Could not get sqlite handle";
            }
        } else {
            qLog(Sql) << "handle variant returned typename " <<
v.typeName();
        }
#endif
}

int sqliteLocaleAwareCompare(void *, int ll, const void *l, int rl,
const void *r)
{
    QString left = QString::fromUtf16((const ushort *)l, ll/2);
    QString right = QString::fromUtf16((const ushort *)r, rl/2);
    //qLog(Sql) << "comparing:" << left << "with" << right << "result"
<< QString::localeAwareCompare(left, right);
    return QString::localeAwareCompare(left, right);
}

-- 
Bill King, Software Engineer
Trolltech, Brisbane Technology Park
26 Brandl St, Eight Mile Plains, 
QLD, Australia, 4113
Tel + 61 7 3219 9906 (x137)
Fax + 61 7 3219 9938
mobile: 0423 532 733

--
To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
"unsubscribe" in the subject or the body.
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>