extensions/sqlite/sqlite-source/callback.c
author Asher Baker <asherkin@gmail.com>
Thu Apr 26 16:58:18 2012 +0100 (2012-04-26)
changeset 3488 bee90c8ef6c3
parent 1952 b7aa2b626807
permissions -rw-r--r--
Fixed client serials not being unique on Windows (bug 5285, r=psychonic).
ds@1952
     1
/*
ds@1952
     2
** 2005 May 23 
ds@1952
     3
**
ds@1952
     4
** The author disclaims copyright to this source code.  In place of
ds@1952
     5
** a legal notice, here is a blessing:
ds@1952
     6
**
ds@1952
     7
**    May you do good and not evil.
ds@1952
     8
**    May you find forgiveness for yourself and forgive others.
ds@1952
     9
**    May you share freely, never taking more than you give.
ds@1952
    10
**
ds@1952
    11
*************************************************************************
ds@1952
    12
**
ds@1952
    13
** This file contains functions used to access the internal hash tables
ds@1952
    14
** of user defined functions and collation sequences.
ds@1952
    15
**
ds@1952
    16
** $Id$
ds@1952
    17
*/
ds@1952
    18
ds@1952
    19
#include "sqliteInt.h"
ds@1952
    20
ds@1952
    21
/*
ds@1952
    22
** Invoke the 'collation needed' callback to request a collation sequence
ds@1952
    23
** in the database text encoding of name zName, length nName.
ds@1952
    24
** If the collation sequence
ds@1952
    25
*/
ds@1952
    26
static void callCollNeeded(sqlite3 *db, const char *zName, int nName){
ds@1952
    27
  assert( !db->xCollNeeded || !db->xCollNeeded16 );
ds@1952
    28
  if( nName<0 ) nName = strlen(zName);
ds@1952
    29
  if( db->xCollNeeded ){
ds@1952
    30
    char *zExternal = sqlite3DbStrNDup(db, zName, nName);
ds@1952
    31
    if( !zExternal ) return;
ds@1952
    32
    db->xCollNeeded(db->pCollNeededArg, db, (int)ENC(db), zExternal);
ds@1952
    33
    sqlite3_free(zExternal);
ds@1952
    34
  }
ds@1952
    35
#ifndef SQLITE_OMIT_UTF16
ds@1952
    36
  if( db->xCollNeeded16 ){
ds@1952
    37
    char const *zExternal;
ds@1952
    38
    sqlite3_value *pTmp = sqlite3ValueNew(db);
ds@1952
    39
    sqlite3ValueSetStr(pTmp, nName, zName, SQLITE_UTF8, SQLITE_STATIC);
ds@1952
    40
    zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
ds@1952
    41
    if( zExternal ){
ds@1952
    42
      db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
ds@1952
    43
    }
ds@1952
    44
    sqlite3ValueFree(pTmp);
ds@1952
    45
  }
ds@1952
    46
#endif
ds@1952
    47
}
ds@1952
    48
ds@1952
    49
/*
ds@1952
    50
** This routine is called if the collation factory fails to deliver a
ds@1952
    51
** collation function in the best encoding but there may be other versions
ds@1952
    52
** of this collation function (for other text encodings) available. Use one
ds@1952
    53
** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
ds@1952
    54
** possible.
ds@1952
    55
*/
ds@1952
    56
static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
ds@1952
    57
  CollSeq *pColl2;
ds@1952
    58
  char *z = pColl->zName;
ds@1952
    59
  int n = strlen(z);
ds@1952
    60
  int i;
ds@1952
    61
  static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
ds@1952
    62
  for(i=0; i<3; i++){
ds@1952
    63
    pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0);
ds@1952
    64
    if( pColl2->xCmp!=0 ){
ds@1952
    65
      memcpy(pColl, pColl2, sizeof(CollSeq));
ds@1952
    66
      pColl->xDel = 0;         /* Do not copy the destructor */
ds@1952
    67
      return SQLITE_OK;
ds@1952
    68
    }
ds@1952
    69
  }
ds@1952
    70
  return SQLITE_ERROR;
ds@1952
    71
}
ds@1952
    72
ds@1952
    73
/*
ds@1952
    74
** This function is responsible for invoking the collation factory callback
ds@1952
    75
** or substituting a collation sequence of a different encoding when the
ds@1952
    76
** requested collation sequence is not available in the database native
ds@1952
    77
** encoding.
ds@1952
    78
** 
ds@1952
    79
** If it is not NULL, then pColl must point to the database native encoding 
ds@1952
    80
** collation sequence with name zName, length nName.
ds@1952
    81
**
ds@1952
    82
** The return value is either the collation sequence to be used in database
ds@1952
    83
** db for collation type name zName, length nName, or NULL, if no collation
ds@1952
    84
** sequence can be found.
ds@1952
    85
*/
ds@1952
    86
CollSeq *sqlite3GetCollSeq(
ds@1952
    87
  sqlite3* db, 
ds@1952
    88
  CollSeq *pColl, 
ds@1952
    89
  const char *zName, 
ds@1952
    90
  int nName
ds@1952
    91
){
ds@1952
    92
  CollSeq *p;
ds@1952
    93
ds@1952
    94
  p = pColl;
ds@1952
    95
  if( !p ){
ds@1952
    96
    p = sqlite3FindCollSeq(db, ENC(db), zName, nName, 0);
ds@1952
    97
  }
ds@1952
    98
  if( !p || !p->xCmp ){
ds@1952
    99
    /* No collation sequence of this type for this encoding is registered.
ds@1952
   100
    ** Call the collation factory to see if it can supply us with one.
ds@1952
   101
    */
ds@1952
   102
    callCollNeeded(db, zName, nName);
ds@1952
   103
    p = sqlite3FindCollSeq(db, ENC(db), zName, nName, 0);
ds@1952
   104
  }
ds@1952
   105
  if( p && !p->xCmp && synthCollSeq(db, p) ){
ds@1952
   106
    p = 0;
ds@1952
   107
  }
ds@1952
   108
  assert( !p || p->xCmp );
ds@1952
   109
  return p;
ds@1952
   110
}
ds@1952
   111
ds@1952
   112
/*
ds@1952
   113
** This routine is called on a collation sequence before it is used to
ds@1952
   114
** check that it is defined. An undefined collation sequence exists when
ds@1952
   115
** a database is loaded that contains references to collation sequences
ds@1952
   116
** that have not been defined by sqlite3_create_collation() etc.
ds@1952
   117
**
ds@1952
   118
** If required, this routine calls the 'collation needed' callback to
ds@1952
   119
** request a definition of the collating sequence. If this doesn't work, 
ds@1952
   120
** an equivalent collating sequence that uses a text encoding different
ds@1952
   121
** from the main database is substituted, if one is available.
ds@1952
   122
*/
ds@1952
   123
int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
ds@1952
   124
  if( pColl ){
ds@1952
   125
    const char *zName = pColl->zName;
ds@1952
   126
    CollSeq *p = sqlite3GetCollSeq(pParse->db, pColl, zName, -1);
ds@1952
   127
    if( !p ){
ds@1952
   128
      if( pParse->nErr==0 ){
ds@1952
   129
        sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
ds@1952
   130
      }
ds@1952
   131
      pParse->nErr++;
ds@1952
   132
      return SQLITE_ERROR;
ds@1952
   133
    }
ds@1952
   134
    assert( p==pColl );
ds@1952
   135
  }
ds@1952
   136
  return SQLITE_OK;
ds@1952
   137
}
ds@1952
   138
ds@1952
   139
ds@1952
   140
ds@1952
   141
/*
ds@1952
   142
** Locate and return an entry from the db.aCollSeq hash table. If the entry
ds@1952
   143
** specified by zName and nName is not found and parameter 'create' is
ds@1952
   144
** true, then create a new entry. Otherwise return NULL.
ds@1952
   145
**
ds@1952
   146
** Each pointer stored in the sqlite3.aCollSeq hash table contains an
ds@1952
   147
** array of three CollSeq structures. The first is the collation sequence
ds@1952
   148
** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
ds@1952
   149
**
ds@1952
   150
** Stored immediately after the three collation sequences is a copy of
ds@1952
   151
** the collation sequence name. A pointer to this string is stored in
ds@1952
   152
** each collation sequence structure.
ds@1952
   153
*/
ds@1952
   154
static CollSeq *findCollSeqEntry(
ds@1952
   155
  sqlite3 *db,
ds@1952
   156
  const char *zName,
ds@1952
   157
  int nName,
ds@1952
   158
  int create
ds@1952
   159
){
ds@1952
   160
  CollSeq *pColl;
ds@1952
   161
  if( nName<0 ) nName = strlen(zName);
ds@1952
   162
  pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
ds@1952
   163
ds@1952
   164
  if( 0==pColl && create ){
ds@1952
   165
    pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
ds@1952
   166
    if( pColl ){
ds@1952
   167
      CollSeq *pDel = 0;
ds@1952
   168
      pColl[0].zName = (char*)&pColl[3];
ds@1952
   169
      pColl[0].enc = SQLITE_UTF8;
ds@1952
   170
      pColl[1].zName = (char*)&pColl[3];
ds@1952
   171
      pColl[1].enc = SQLITE_UTF16LE;
ds@1952
   172
      pColl[2].zName = (char*)&pColl[3];
ds@1952
   173
      pColl[2].enc = SQLITE_UTF16BE;
ds@1952
   174
      memcpy(pColl[0].zName, zName, nName);
ds@1952
   175
      pColl[0].zName[nName] = 0;
ds@1952
   176
      pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
ds@1952
   177
ds@1952
   178
      /* If a malloc() failure occured in sqlite3HashInsert(), it will 
ds@1952
   179
      ** return the pColl pointer to be deleted (because it wasn't added
ds@1952
   180
      ** to the hash table).
ds@1952
   181
      */
ds@1952
   182
      assert( pDel==0 || pDel==pColl );
ds@1952
   183
      if( pDel!=0 ){
ds@1952
   184
        db->mallocFailed = 1;
ds@1952
   185
        sqlite3_free(pDel);
ds@1952
   186
        pColl = 0;
ds@1952
   187
      }
ds@1952
   188
    }
ds@1952
   189
  }
ds@1952
   190
  return pColl;
ds@1952
   191
}
ds@1952
   192
ds@1952
   193
/*
ds@1952
   194
** Parameter zName points to a UTF-8 encoded string nName bytes long.
ds@1952
   195
** Return the CollSeq* pointer for the collation sequence named zName
ds@1952
   196
** for the encoding 'enc' from the database 'db'.
ds@1952
   197
**
ds@1952
   198
** If the entry specified is not found and 'create' is true, then create a
ds@1952
   199
** new entry.  Otherwise return NULL.
ds@1952
   200
**
ds@1952
   201
** A separate function sqlite3LocateCollSeq() is a wrapper around
ds@1952
   202
** this routine.  sqlite3LocateCollSeq() invokes the collation factory
ds@1952
   203
** if necessary and generates an error message if the collating sequence
ds@1952
   204
** cannot be found.
ds@1952
   205
*/
ds@1952
   206
CollSeq *sqlite3FindCollSeq(
ds@1952
   207
  sqlite3 *db,
ds@1952
   208
  u8 enc,
ds@1952
   209
  const char *zName,
ds@1952
   210
  int nName,
ds@1952
   211
  int create
ds@1952
   212
){
ds@1952
   213
  CollSeq *pColl;
ds@1952
   214
  if( zName ){
ds@1952
   215
    pColl = findCollSeqEntry(db, zName, nName, create);
ds@1952
   216
  }else{
ds@1952
   217
    pColl = db->pDfltColl;
ds@1952
   218
  }
ds@1952
   219
  assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
ds@1952
   220
  assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
ds@1952
   221
  if( pColl ) pColl += enc-1;
ds@1952
   222
  return pColl;
ds@1952
   223
}
ds@1952
   224
ds@1952
   225
/*
ds@1952
   226
** Locate a user function given a name, a number of arguments and a flag
ds@1952
   227
** indicating whether the function prefers UTF-16 over UTF-8.  Return a
ds@1952
   228
** pointer to the FuncDef structure that defines that function, or return
ds@1952
   229
** NULL if the function does not exist.
ds@1952
   230
**
ds@1952
   231
** If the createFlag argument is true, then a new (blank) FuncDef
ds@1952
   232
** structure is created and liked into the "db" structure if a
ds@1952
   233
** no matching function previously existed.  When createFlag is true
ds@1952
   234
** and the nArg parameter is -1, then only a function that accepts
ds@1952
   235
** any number of arguments will be returned.
ds@1952
   236
**
ds@1952
   237
** If createFlag is false and nArg is -1, then the first valid
ds@1952
   238
** function found is returned.  A function is valid if either xFunc
ds@1952
   239
** or xStep is non-zero.
ds@1952
   240
**
ds@1952
   241
** If createFlag is false, then a function with the required name and
ds@1952
   242
** number of arguments may be returned even if the eTextRep flag does not
ds@1952
   243
** match that requested.
ds@1952
   244
*/
ds@1952
   245
FuncDef *sqlite3FindFunction(
ds@1952
   246
  sqlite3 *db,       /* An open database */
ds@1952
   247
  const char *zName, /* Name of the function.  Not null-terminated */
ds@1952
   248
  int nName,         /* Number of characters in the name */
ds@1952
   249
  int nArg,          /* Number of arguments.  -1 means any number */
ds@1952
   250
  u8 enc,            /* Preferred text encoding */
ds@1952
   251
  int createFlag     /* Create new entry if true and does not otherwise exist */
ds@1952
   252
){
ds@1952
   253
  FuncDef *p;         /* Iterator variable */
ds@1952
   254
  FuncDef *pFirst;    /* First function with this name */
ds@1952
   255
  FuncDef *pBest = 0; /* Best match found so far */
ds@1952
   256
  int bestmatch = 0;  
ds@1952
   257
ds@1952
   258
ds@1952
   259
  assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
ds@1952
   260
  if( nArg<-1 ) nArg = -1;
ds@1952
   261
ds@1952
   262
  pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
ds@1952
   263
  for(p=pFirst; p; p=p->pNext){
ds@1952
   264
    /* During the search for the best function definition, bestmatch is set
ds@1952
   265
    ** as follows to indicate the quality of the match with the definition
ds@1952
   266
    ** pointed to by pBest:
ds@1952
   267
    **
ds@1952
   268
    ** 0: pBest is NULL. No match has been found.
ds@1952
   269
    ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
ds@1952
   270
    **    encoding is requested, or vice versa.
ds@1952
   271
    ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
ds@1952
   272
    **    requested, or vice versa.
ds@1952
   273
    ** 3: A variable arguments function using the same text encoding.
ds@1952
   274
    ** 4: A function with the exact number of arguments requested that
ds@1952
   275
    **    prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
ds@1952
   276
    ** 5: A function with the exact number of arguments requested that
ds@1952
   277
    **    prefers UTF-16LE when UTF-16BE is requested, or vice versa.
ds@1952
   278
    ** 6: An exact match.
ds@1952
   279
    **
ds@1952
   280
    ** A larger value of 'matchqual' indicates a more desirable match.
ds@1952
   281
    */
ds@1952
   282
    if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
ds@1952
   283
      int match = 1;          /* Quality of this match */
ds@1952
   284
      if( p->nArg==nArg || nArg==-1 ){
ds@1952
   285
        match = 4;
ds@1952
   286
      }
ds@1952
   287
      if( enc==p->iPrefEnc ){
ds@1952
   288
        match += 2;
ds@1952
   289
      }
ds@1952
   290
      else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
ds@1952
   291
               (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
ds@1952
   292
        match += 1;
ds@1952
   293
      }
ds@1952
   294
ds@1952
   295
      if( match>bestmatch ){
ds@1952
   296
        pBest = p;
ds@1952
   297
        bestmatch = match;
ds@1952
   298
      }
ds@1952
   299
    }
ds@1952
   300
  }
ds@1952
   301
ds@1952
   302
  /* If the createFlag parameter is true, and the seach did not reveal an
ds@1952
   303
  ** exact match for the name, number of arguments and encoding, then add a
ds@1952
   304
  ** new entry to the hash table and return it.
ds@1952
   305
  */
ds@1952
   306
  if( createFlag && bestmatch<6 && 
ds@1952
   307
      (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName))!=0 ){
ds@1952
   308
    pBest->nArg = nArg;
ds@1952
   309
    pBest->pNext = pFirst;
ds@1952
   310
    pBest->iPrefEnc = enc;
ds@1952
   311
    memcpy(pBest->zName, zName, nName);
ds@1952
   312
    pBest->zName[nName] = 0;
ds@1952
   313
    if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){
ds@1952
   314
      db->mallocFailed = 1;
ds@1952
   315
      sqlite3_free(pBest);
ds@1952
   316
      return 0;
ds@1952
   317
    }
ds@1952
   318
  }
ds@1952
   319
ds@1952
   320
  if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
ds@1952
   321
    return pBest;
ds@1952
   322
  }
ds@1952
   323
  return 0;
ds@1952
   324
}
ds@1952
   325
ds@1952
   326
/*
ds@1952
   327
** Free all resources held by the schema structure. The void* argument points
ds@1952
   328
** at a Schema struct. This function does not call sqlite3_free() on the 
ds@1952
   329
** pointer itself, it just cleans up subsiduary resources (i.e. the contents
ds@1952
   330
** of the schema hash tables).
ds@1952
   331
*/
ds@1952
   332
void sqlite3SchemaFree(void *p){
ds@1952
   333
  Hash temp1;
ds@1952
   334
  Hash temp2;
ds@1952
   335
  HashElem *pElem;
ds@1952
   336
  Schema *pSchema = (Schema *)p;
ds@1952
   337
ds@1952
   338
  temp1 = pSchema->tblHash;
ds@1952
   339
  temp2 = pSchema->trigHash;
ds@1952
   340
  sqlite3HashInit(&pSchema->trigHash, SQLITE_HASH_STRING, 0);
ds@1952
   341
  sqlite3HashClear(&pSchema->aFKey);
ds@1952
   342
  sqlite3HashClear(&pSchema->idxHash);
ds@1952
   343
  for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
ds@1952
   344
    sqlite3DeleteTrigger((Trigger*)sqliteHashData(pElem));
ds@1952
   345
  }
ds@1952
   346
  sqlite3HashClear(&temp2);
ds@1952
   347
  sqlite3HashInit(&pSchema->tblHash, SQLITE_HASH_STRING, 0);
ds@1952
   348
  for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
ds@1952
   349
    Table *pTab = sqliteHashData(pElem);
ds@1952
   350
    sqlite3DeleteTable(pTab);
ds@1952
   351
  }
ds@1952
   352
  sqlite3HashClear(&temp1);
ds@1952
   353
  pSchema->pSeqTab = 0;
ds@1952
   354
  pSchema->flags &= ~DB_SchemaLoaded;
ds@1952
   355
}
ds@1952
   356
ds@1952
   357
/*
ds@1952
   358
** Find and return the schema associated with a BTree.  Create
ds@1952
   359
** a new one if necessary.
ds@1952
   360
*/
ds@1952
   361
Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
ds@1952
   362
  Schema * p;
ds@1952
   363
  if( pBt ){
ds@1952
   364
    p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaFree);
ds@1952
   365
  }else{
ds@1952
   366
    p = (Schema *)sqlite3MallocZero(sizeof(Schema));
ds@1952
   367
  }
ds@1952
   368
  if( !p ){
ds@1952
   369
    db->mallocFailed = 1;
ds@1952
   370
  }else if ( 0==p->file_format ){
ds@1952
   371
    sqlite3HashInit(&p->tblHash, SQLITE_HASH_STRING, 0);
ds@1952
   372
    sqlite3HashInit(&p->idxHash, SQLITE_HASH_STRING, 0);
ds@1952
   373
    sqlite3HashInit(&p->trigHash, SQLITE_HASH_STRING, 0);
ds@1952
   374
    sqlite3HashInit(&p->aFKey, SQLITE_HASH_STRING, 1);
ds@1952
   375
    p->enc = SQLITE_UTF8;
ds@1952
   376
  }
ds@1952
   377
  return p;
ds@1952
   378
}