That's a wrong approach. First, you don't need to modify functions in
   SQLite code, you need to create your own. Your function will convert
   using locale saved in some variable in your application.
I have created my own function which uses a global variable for the default locale. (see attached)
At application startup you fill this variable with value
   from table db_locale or with some default value if there's nothing in
   db_locale. Then each time you change your application's variable you
   save new value in table db_locale. That's it.
I am struggling with this one, could someone help me out with sample code.
I figure that the code would go to openDatabase(..), but that is about all I know at the moment.

Thanks.

Regards,
*Grace Batumbya*
Research Assistant | Seneca CDOT
Phone: 416-491-5050 x3548
cdot.senecac.on.ca <http://cdot.senecac.on.ca/>

On 3/1/2012 09:48, Pavel Ivanov wrote:
Given that there exists a table db_locale [CREATE TABLE db_locale (locale
text)],
what lines of code would be used to query that table from inside this
function?
That's a wrong approach. First, you don't need to modify functions in
SQLite code, you need to create your own. Your function will convert
using locale saved in some variable in your application. At
application startup you fill this variable with value from table
db_locale or with some default value if there's nothing in db_locale.
Then each time you change your application's variable you save new
value in table db_locale. That's it.


Pavel


On Thu, Mar 1, 2012 at 9:33 AM, Grace Simon Batumbya
<grace.batum...@senecacollege.ca>  wrote:
I found the function that I would need to modify (see below).

static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value
**apArg){
   const UChar *zInput;
   UChar *zOutput;
   int nInput;
   int nOutput;

   UErrorCode status = U_ZERO_ERROR;
   const char *zLocale = 0;

   assert(nArg==1 || nArg==2);
   if( nArg==2 ){
     zLocale = (const char *)sqlite3_value_text(apArg[1]);
   }

   zInput = sqlite3_value_text16(apArg[0]);
   if( !zInput ){
     return;
   }
   nInput = sqlite3_value_bytes16(apArg[0]);

   nOutput = nInput * 2 + 2;
   zOutput = sqlite3_malloc(nOutput);
   if( !zOutput ){
     return;
   }

   if( sqlite3_user_data(p) ){
     u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale,&status);
   }else{
     u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale,&status);
   }

   if( !U_SUCCESS(status) ){
     icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
     return;
   }

   sqlite3_result_text16(p, zOutput, -1, xFree);
}

Given that there exists a table db_locale [CREATE TABLE db_locale (locale
text)],
what lines of code would be used to query that table from inside this
function?

Grace Batumbya
Research Assistant | Seneca CDOT
Phone: 416-491-5050 x3548
cdot.senecac.on.ca

On 3/1/2012 08:56, Grace Batumbya wrote:

Is there an example extension you know that I could look at that does this?
(i am a novice at SQLite)
________________________________________
From: sqlite-users-boun...@sqlite.org [sqlite-users-boun...@sqlite.org] on
behalf of Pavel Ivanov [paiva...@gmail.com]
Sent: March 1, 2012 8:52 AM
To: General Discussion of SQLite Database
Subject: Re: [sqlite] Set Locale for upper() and lower() using a pragma
variable

On Thu, Mar 1, 2012 at 8:50 AM, Grace Batumbya
<grace.batum...@senecacollege.ca>  wrote:

You can simply register your
own lower/upper with one argument that looks wherever you want to know
what locale to use.

The part of registering a function to override lower/upper I think I
understand.

But if I wanted to persist the locale, so that even if I disconnect and
reconnect it is still set to the last time I set it, how do I go about
accomplishing this.

Create a special table for that and store your last locale value in
it. Then after reconnect read the locale value from this table.


Pavel
_______________________________________________
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
// DB's default locale
char *DBLocale = 0;
int nLength = 0;

void setLocale(char *zLocale)
{
   sqlite3_free(DBLocale);
   nLength = strlen(zLocale);
   DBLocale = (char *) sqlite3_malloc(nLength);

   strcpy(DBLocale, zLocale);
}

void deleteDBLocale(void * zLocale)
{
   // do thing, since zLocale points to DBLocale.
}

// lower and upper using the DB locale 
static void lowerUpperDefaultLocale(sqlite3_context *p, int nArg, sqlite3_value 
**apArg)
{
   sqlite3_value *args[2];
        
   if (!DBLocale)
   {
      // hand off to inbuilt functions
      icuCaseFunc16(p, nArg, apArg);

      return;
   }

        args[0] = apArg[0];
   args[1] = (sqlite3_value *)malloc(sizeof(sqlite3_value));
   args[1]->db    = apArg[0]->db;       /* The associated database connection */
   args[1]->z     = DBLocale;          /* String or BLOB value */
   args[1]->r     = apArg[0]->r;          /* Real value */
   args[1]->u     = apArg[0]->u;
   args[1]->n     = nLength;             /* Number of characters in string 
value, excluding '\0' */
   args[1]->flags = apArg[0]->flags;         /* Some combination of MEM_Null, 
MEM_Str, MEM_Dyn, etc. */
   args[1]->type  = apArg[0]->type;          /* One of SQLITE_NULL, 
SQLITE_TEXT, SQLITE_INTEGER, etc */
   args[1]->enc   = apArg[0]->enc;           /* SQLITE_UTF8, SQLITE_UTF16BE, 
SQLITE_UTF16LE */
#ifdef SQLITE_DEBUG
  args[1]->pScopyFrom = apArg[0]->pScopyFrom;    /* This Mem is a shallow copy 
of pScopyFrom */
  args[1]->pFiller    = apArg[0]->pFiller;      /* So that sizeof(Mem) is a 
multiple of 8 */
#endif
   args[1]->xDel = deleteDBLocale;  /* If not null, call this function to 
delete Mem.z */
   args[1]->zMalloc = 0;

        // hand off to inbuilt functions
        icuCaseFunc16(p, 2, args);
}

// Override one parameter lower and upper functions
int RegisterLowerUpperDefaultLocale(sqlite3 *db)
{
    sqlite3_create_function(db, "lower",  1, SQLITE_UTF16,        0, 
lowerUpperDefaultLocale, 0, 0);
    sqlite3_create_function(db, "upper",  1, SQLITE_UTF16, (void*)1, 
lowerUpperDefaultLocale, 0, 0);
    sqlite3_create_function(db, "lower",  1, SQLITE_UTF8,         0, 
lowerUpperDefaultLocale, 0, 0);
    sqlite3_create_function(db, "upper",  1, SQLITE_UTF8,  (void*)1, 
lowerUpperDefaultLocale, 0, 0);
        
    return 0;
}
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to