1fd9a0a45Sdanielk1977 /* 2fd9a0a45Sdanielk1977 ** 2005 May 23 3fd9a0a45Sdanielk1977 ** 4fd9a0a45Sdanielk1977 ** The author disclaims copyright to this source code. In place of 5fd9a0a45Sdanielk1977 ** a legal notice, here is a blessing: 6fd9a0a45Sdanielk1977 ** 7fd9a0a45Sdanielk1977 ** May you do good and not evil. 8fd9a0a45Sdanielk1977 ** May you find forgiveness for yourself and forgive others. 9fd9a0a45Sdanielk1977 ** May you share freely, never taking more than you give. 10fd9a0a45Sdanielk1977 ** 11fd9a0a45Sdanielk1977 ************************************************************************* 12fd9a0a45Sdanielk1977 ** 13fd9a0a45Sdanielk1977 ** This file contains functions used to access the internal hash tables 14fd9a0a45Sdanielk1977 ** of user defined functions and collation sequences. 15fd9a0a45Sdanielk1977 */ 16fd9a0a45Sdanielk1977 17fd9a0a45Sdanielk1977 #include "sqliteInt.h" 18fd9a0a45Sdanielk1977 19fd9a0a45Sdanielk1977 /* 204dade037Sdanielk1977 ** Invoke the 'collation needed' callback to request a collation sequence 219aeda79cSdrh ** in the encoding enc of name zName, length nName. 224dade037Sdanielk1977 */ 239aeda79cSdrh static void callCollNeeded(sqlite3 *db, int enc, const char *zName){ 244dade037Sdanielk1977 assert( !db->xCollNeeded || !db->xCollNeeded16 ); 254dade037Sdanielk1977 if( db->xCollNeeded ){ 26c4a64facSdrh char *zExternal = sqlite3DbStrDup(db, zName); 274dade037Sdanielk1977 if( !zExternal ) return; 289aeda79cSdrh db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal); 29633e6d57Sdrh sqlite3DbFree(db, zExternal); 304dade037Sdanielk1977 } 314dade037Sdanielk1977 #ifndef SQLITE_OMIT_UTF16 324dade037Sdanielk1977 if( db->xCollNeeded16 ){ 334dade037Sdanielk1977 char const *zExternal; 341e536953Sdanielk1977 sqlite3_value *pTmp = sqlite3ValueNew(db); 35c4a64facSdrh sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC); 36b21c8cd4Sdrh zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE); 3726abcb1eSdrh if( zExternal ){ 3814db2665Sdanielk1977 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal); 394dade037Sdanielk1977 } 4026abcb1eSdrh sqlite3ValueFree(pTmp); 4126abcb1eSdrh } 424dade037Sdanielk1977 #endif 434dade037Sdanielk1977 } 444dade037Sdanielk1977 454dade037Sdanielk1977 /* 464dade037Sdanielk1977 ** This routine is called if the collation factory fails to deliver a 474dade037Sdanielk1977 ** collation function in the best encoding but there may be other versions 484dade037Sdanielk1977 ** of this collation function (for other text encodings) available. Use one 494dade037Sdanielk1977 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if 504dade037Sdanielk1977 ** possible. 514dade037Sdanielk1977 */ 524dade037Sdanielk1977 static int synthCollSeq(sqlite3 *db, CollSeq *pColl){ 534dade037Sdanielk1977 CollSeq *pColl2; 544dade037Sdanielk1977 char *z = pColl->zName; 554dade037Sdanielk1977 int i; 564dade037Sdanielk1977 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 }; 574dade037Sdanielk1977 for(i=0; i<3; i++){ 58c4a64facSdrh pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0); 594dade037Sdanielk1977 if( pColl2->xCmp!=0 ){ 604dade037Sdanielk1977 memcpy(pColl, pColl2, sizeof(CollSeq)); 61a9808b31Sdanielk1977 pColl->xDel = 0; /* Do not copy the destructor */ 624dade037Sdanielk1977 return SQLITE_OK; 634dade037Sdanielk1977 } 644dade037Sdanielk1977 } 654dade037Sdanielk1977 return SQLITE_ERROR; 664dade037Sdanielk1977 } 674dade037Sdanielk1977 684dade037Sdanielk1977 /* 694dade037Sdanielk1977 ** This function is responsible for invoking the collation factory callback 704dade037Sdanielk1977 ** or substituting a collation sequence of a different encoding when the 719aeda79cSdrh ** requested collation sequence is not available in the desired encoding. 724dade037Sdanielk1977 ** 734dade037Sdanielk1977 ** If it is not NULL, then pColl must point to the database native encoding 744dade037Sdanielk1977 ** collation sequence with name zName, length nName. 754dade037Sdanielk1977 ** 764dade037Sdanielk1977 ** The return value is either the collation sequence to be used in database 774dade037Sdanielk1977 ** db for collation type name zName, length nName, or NULL, if no collation 7879e72a50Sdrh ** sequence can be found. If no collation is found, leave an error message. 79c4a64facSdrh ** 80c4a64facSdrh ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq() 814dade037Sdanielk1977 */ 824dade037Sdanielk1977 CollSeq *sqlite3GetCollSeq( 8379e72a50Sdrh Parse *pParse, /* Parsing context */ 84cea72b2dSshane u8 enc, /* The desired encoding for the collating sequence */ 85c4a64facSdrh CollSeq *pColl, /* Collating sequence with native encoding, or NULL */ 86c4a64facSdrh const char *zName /* Collating sequence name */ 874dade037Sdanielk1977 ){ 884dade037Sdanielk1977 CollSeq *p; 8979e72a50Sdrh sqlite3 *db = pParse->db; 904dade037Sdanielk1977 914dade037Sdanielk1977 p = pColl; 924dade037Sdanielk1977 if( !p ){ 939aeda79cSdrh p = sqlite3FindCollSeq(db, enc, zName, 0); 944dade037Sdanielk1977 } 954dade037Sdanielk1977 if( !p || !p->xCmp ){ 964dade037Sdanielk1977 /* No collation sequence of this type for this encoding is registered. 974dade037Sdanielk1977 ** Call the collation factory to see if it can supply us with one. 984dade037Sdanielk1977 */ 999aeda79cSdrh callCollNeeded(db, enc, zName); 1009aeda79cSdrh p = sqlite3FindCollSeq(db, enc, zName, 0); 1014dade037Sdanielk1977 } 1024dade037Sdanielk1977 if( p && !p->xCmp && synthCollSeq(db, p) ){ 1034dade037Sdanielk1977 p = 0; 1044dade037Sdanielk1977 } 1054dade037Sdanielk1977 assert( !p || p->xCmp ); 10679e72a50Sdrh if( p==0 ){ 10779e72a50Sdrh sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName); 10879e72a50Sdrh } 1094dade037Sdanielk1977 return p; 1104dade037Sdanielk1977 } 1114dade037Sdanielk1977 1124dade037Sdanielk1977 /* 1134dade037Sdanielk1977 ** This routine is called on a collation sequence before it is used to 1144dade037Sdanielk1977 ** check that it is defined. An undefined collation sequence exists when 1154dade037Sdanielk1977 ** a database is loaded that contains references to collation sequences 1164dade037Sdanielk1977 ** that have not been defined by sqlite3_create_collation() etc. 1174dade037Sdanielk1977 ** 1184dade037Sdanielk1977 ** If required, this routine calls the 'collation needed' callback to 1194dade037Sdanielk1977 ** request a definition of the collating sequence. If this doesn't work, 1204dade037Sdanielk1977 ** an equivalent collating sequence that uses a text encoding different 1214dade037Sdanielk1977 ** from the main database is substituted, if one is available. 1224dade037Sdanielk1977 */ 1234dade037Sdanielk1977 int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){ 1244dade037Sdanielk1977 if( pColl ){ 1254dade037Sdanielk1977 const char *zName = pColl->zName; 1269aeda79cSdrh sqlite3 *db = pParse->db; 12779e72a50Sdrh CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName); 1284dade037Sdanielk1977 if( !p ){ 1294dade037Sdanielk1977 return SQLITE_ERROR; 1304dade037Sdanielk1977 } 131b3bf556eSdanielk1977 assert( p==pColl ); 1324dade037Sdanielk1977 } 1334dade037Sdanielk1977 return SQLITE_OK; 1344dade037Sdanielk1977 } 1354dade037Sdanielk1977 1364dade037Sdanielk1977 1374dade037Sdanielk1977 1384dade037Sdanielk1977 /* 139fd9a0a45Sdanielk1977 ** Locate and return an entry from the db.aCollSeq hash table. If the entry 140fd9a0a45Sdanielk1977 ** specified by zName and nName is not found and parameter 'create' is 141fd9a0a45Sdanielk1977 ** true, then create a new entry. Otherwise return NULL. 142fd9a0a45Sdanielk1977 ** 143fd9a0a45Sdanielk1977 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an 144fd9a0a45Sdanielk1977 ** array of three CollSeq structures. The first is the collation sequence 145fd9a0a45Sdanielk1977 ** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be. 146fd9a0a45Sdanielk1977 ** 147fd9a0a45Sdanielk1977 ** Stored immediately after the three collation sequences is a copy of 148fd9a0a45Sdanielk1977 ** the collation sequence name. A pointer to this string is stored in 149fd9a0a45Sdanielk1977 ** each collation sequence structure. 150fd9a0a45Sdanielk1977 */ 151fd9a0a45Sdanielk1977 static CollSeq *findCollSeqEntry( 152c4a64facSdrh sqlite3 *db, /* Database connection */ 153c4a64facSdrh const char *zName, /* Name of the collating sequence */ 154c4a64facSdrh int create /* Create a new entry if true */ 155fd9a0a45Sdanielk1977 ){ 156fd9a0a45Sdanielk1977 CollSeq *pColl; 157c4a64facSdrh int nName = sqlite3Strlen30(zName); 158fd9a0a45Sdanielk1977 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName); 159fd9a0a45Sdanielk1977 160fd9a0a45Sdanielk1977 if( 0==pColl && create ){ 16117435752Sdrh pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 ); 162fd9a0a45Sdanielk1977 if( pColl ){ 163fd9a0a45Sdanielk1977 CollSeq *pDel = 0; 164fd9a0a45Sdanielk1977 pColl[0].zName = (char*)&pColl[3]; 165fd9a0a45Sdanielk1977 pColl[0].enc = SQLITE_UTF8; 166fd9a0a45Sdanielk1977 pColl[1].zName = (char*)&pColl[3]; 167fd9a0a45Sdanielk1977 pColl[1].enc = SQLITE_UTF16LE; 168fd9a0a45Sdanielk1977 pColl[2].zName = (char*)&pColl[3]; 169fd9a0a45Sdanielk1977 pColl[2].enc = SQLITE_UTF16BE; 170fd9a0a45Sdanielk1977 memcpy(pColl[0].zName, zName, nName); 171fd9a0a45Sdanielk1977 pColl[0].zName[nName] = 0; 172fd9a0a45Sdanielk1977 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl); 173fd9a0a45Sdanielk1977 174be217793Sshane /* If a malloc() failure occurred in sqlite3HashInsert(), it will 175fd9a0a45Sdanielk1977 ** return the pColl pointer to be deleted (because it wasn't added 176fd9a0a45Sdanielk1977 ** to the hash table). 177fd9a0a45Sdanielk1977 */ 178f3a65f7eSdrh assert( pDel==0 || pDel==pColl ); 179f3a65f7eSdrh if( pDel!=0 ){ 180f3a65f7eSdrh db->mallocFailed = 1; 181633e6d57Sdrh sqlite3DbFree(db, pDel); 18291171cdeSdrh pColl = 0; 18391171cdeSdrh } 184fd9a0a45Sdanielk1977 } 185fd9a0a45Sdanielk1977 } 186fd9a0a45Sdanielk1977 return pColl; 187fd9a0a45Sdanielk1977 } 188fd9a0a45Sdanielk1977 189fd9a0a45Sdanielk1977 /* 190fd9a0a45Sdanielk1977 ** Parameter zName points to a UTF-8 encoded string nName bytes long. 191fd9a0a45Sdanielk1977 ** Return the CollSeq* pointer for the collation sequence named zName 192fd9a0a45Sdanielk1977 ** for the encoding 'enc' from the database 'db'. 193fd9a0a45Sdanielk1977 ** 194fd9a0a45Sdanielk1977 ** If the entry specified is not found and 'create' is true, then create a 195fd9a0a45Sdanielk1977 ** new entry. Otherwise return NULL. 196a34001c9Sdrh ** 197a34001c9Sdrh ** A separate function sqlite3LocateCollSeq() is a wrapper around 198a34001c9Sdrh ** this routine. sqlite3LocateCollSeq() invokes the collation factory 199a34001c9Sdrh ** if necessary and generates an error message if the collating sequence 200a34001c9Sdrh ** cannot be found. 201c4a64facSdrh ** 202c4a64facSdrh ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq() 203fd9a0a45Sdanielk1977 */ 204fd9a0a45Sdanielk1977 CollSeq *sqlite3FindCollSeq( 205fd9a0a45Sdanielk1977 sqlite3 *db, 206fd9a0a45Sdanielk1977 u8 enc, 207fd9a0a45Sdanielk1977 const char *zName, 208fd9a0a45Sdanielk1977 int create 209fd9a0a45Sdanielk1977 ){ 210b3bf556eSdanielk1977 CollSeq *pColl; 211b3bf556eSdanielk1977 if( zName ){ 212c4a64facSdrh pColl = findCollSeqEntry(db, zName, create); 213b3bf556eSdanielk1977 }else{ 214b3bf556eSdanielk1977 pColl = db->pDfltColl; 215b3bf556eSdanielk1977 } 216fd9a0a45Sdanielk1977 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); 217fd9a0a45Sdanielk1977 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE ); 218fd9a0a45Sdanielk1977 if( pColl ) pColl += enc-1; 219fd9a0a45Sdanielk1977 return pColl; 220fd9a0a45Sdanielk1977 } 221fd9a0a45Sdanielk1977 2228c0a791aSdanielk1977 /* During the search for the best function definition, this procedure 2238c0a791aSdanielk1977 ** is called to test how well the function passed as the first argument 2248c0a791aSdanielk1977 ** matches the request for a function with nArg arguments in a system 2258c0a791aSdanielk1977 ** that uses encoding enc. The value returned indicates how well the 2268c0a791aSdanielk1977 ** request is matched. A higher value indicates a better match. 2278c0a791aSdanielk1977 ** 22889d5d6a2Sdrh ** If nArg is -1 that means to only return a match (non-zero) if p->nArg 22989d5d6a2Sdrh ** is also -1. In other words, we are searching for a function that 23089d5d6a2Sdrh ** takes a variable number of arguments. 23189d5d6a2Sdrh ** 23289d5d6a2Sdrh ** If nArg is -2 that means that we are searching for any function 23389d5d6a2Sdrh ** regardless of the number of arguments it uses, so return a positive 23489d5d6a2Sdrh ** match score for any 23589d5d6a2Sdrh ** 236dfbc3a8aSdrh ** The returned value is always between 0 and 6, as follows: 2378c0a791aSdanielk1977 ** 23889d5d6a2Sdrh ** 0: Not a match. 23989d5d6a2Sdrh ** 1: UTF8/16 conversion required and function takes any number of arguments. 24089d5d6a2Sdrh ** 2: UTF16 byte order change required and function takes any number of args. 24189d5d6a2Sdrh ** 3: encoding matches and function takes any number of arguments 24289d5d6a2Sdrh ** 4: UTF8/16 conversion required - argument count matches exactly 24389d5d6a2Sdrh ** 5: UTF16 byte order conversion required - argument count matches exactly 24489d5d6a2Sdrh ** 6: Perfect match: encoding and argument count match exactly. 2458c0a791aSdanielk1977 ** 24689d5d6a2Sdrh ** If nArg==(-2) then any function with a non-null xStep or xFunc is 24789d5d6a2Sdrh ** a perfect match and any function with both xStep and xFunc NULL is 24889d5d6a2Sdrh ** a non-match. 2498c0a791aSdanielk1977 */ 25089d5d6a2Sdrh #define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */ 25189d5d6a2Sdrh static int matchQuality( 25289d5d6a2Sdrh FuncDef *p, /* The function we are evaluating for match quality */ 25389d5d6a2Sdrh int nArg, /* Desired number of arguments. (-1)==any */ 25489d5d6a2Sdrh u8 enc /* Desired text encoding */ 255dfbc3a8aSdrh ){ 25689d5d6a2Sdrh int match; 25789d5d6a2Sdrh 25889d5d6a2Sdrh /* nArg of -2 is a special case */ 25989d5d6a2Sdrh if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH; 26089d5d6a2Sdrh 26189d5d6a2Sdrh /* Wrong number of arguments means "no match" */ 26289d5d6a2Sdrh if( p->nArg!=nArg && p->nArg>=0 ) return 0; 26389d5d6a2Sdrh 26489d5d6a2Sdrh /* Give a better score to a function with a specific number of arguments 26589d5d6a2Sdrh ** than to function that accepts any number of arguments. */ 26689d5d6a2Sdrh if( p->nArg==nArg ){ 2678c0a791aSdanielk1977 match = 4; 26889d5d6a2Sdrh }else{ 26989d5d6a2Sdrh match = 1; 2708c0a791aSdanielk1977 } 27189d5d6a2Sdrh 27289d5d6a2Sdrh /* Bonus points if the text encoding matches */ 273d36e1041Sdrh if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){ 27489d5d6a2Sdrh match += 2; /* Exact encoding match */ 275d36e1041Sdrh }else if( (enc & p->funcFlags & 2)!=0 ){ 27689d5d6a2Sdrh match += 1; /* Both are UTF16, but with different byte orders */ 2778c0a791aSdanielk1977 } 27889d5d6a2Sdrh 2798c0a791aSdanielk1977 return match; 2808c0a791aSdanielk1977 } 2818c0a791aSdanielk1977 282fd9a0a45Sdanielk1977 /* 28370a8ca3cSdrh ** Search a FuncDefHash for a function with the given name. Return 28470a8ca3cSdrh ** a pointer to the matching FuncDef if found, or 0 if there is no match. 28570a8ca3cSdrh */ 28670a8ca3cSdrh static FuncDef *functionSearch( 28770a8ca3cSdrh FuncDefHash *pHash, /* Hash table to search */ 28870a8ca3cSdrh int h, /* Hash of the name */ 28970a8ca3cSdrh const char *zFunc, /* Name of function */ 29070a8ca3cSdrh int nFunc /* Number of bytes in zFunc */ 29170a8ca3cSdrh ){ 29270a8ca3cSdrh FuncDef *p; 29370a8ca3cSdrh for(p=pHash->a[h]; p; p=p->pHash){ 29470a8ca3cSdrh if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){ 29570a8ca3cSdrh return p; 29670a8ca3cSdrh } 29770a8ca3cSdrh } 29870a8ca3cSdrh return 0; 29970a8ca3cSdrh } 30070a8ca3cSdrh 30170a8ca3cSdrh /* 30270a8ca3cSdrh ** Insert a new FuncDef into a FuncDefHash hash table. 30370a8ca3cSdrh */ 30470a8ca3cSdrh void sqlite3FuncDefInsert( 30570a8ca3cSdrh FuncDefHash *pHash, /* The hash table into which to insert */ 30670a8ca3cSdrh FuncDef *pDef /* The function definition to insert */ 30770a8ca3cSdrh ){ 30870a8ca3cSdrh FuncDef *pOther; 309ea678832Sdrh int nName = sqlite3Strlen30(pDef->zName); 31070a8ca3cSdrh u8 c1 = (u8)pDef->zName[0]; 31170a8ca3cSdrh int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a); 31270a8ca3cSdrh pOther = functionSearch(pHash, h, pDef->zName, nName); 31370a8ca3cSdrh if( pOther ){ 3147aaa8786Sdrh assert( pOther!=pDef && pOther->pNext!=pDef ); 31570a8ca3cSdrh pDef->pNext = pOther->pNext; 31670a8ca3cSdrh pOther->pNext = pDef; 31770a8ca3cSdrh }else{ 31870a8ca3cSdrh pDef->pNext = 0; 31970a8ca3cSdrh pDef->pHash = pHash->a[h]; 32070a8ca3cSdrh pHash->a[h] = pDef; 32170a8ca3cSdrh } 32270a8ca3cSdrh } 32370a8ca3cSdrh 32470a8ca3cSdrh 32570a8ca3cSdrh 32670a8ca3cSdrh /* 327fd9a0a45Sdanielk1977 ** Locate a user function given a name, a number of arguments and a flag 328fd9a0a45Sdanielk1977 ** indicating whether the function prefers UTF-16 over UTF-8. Return a 329fd9a0a45Sdanielk1977 ** pointer to the FuncDef structure that defines that function, or return 330fd9a0a45Sdanielk1977 ** NULL if the function does not exist. 331fd9a0a45Sdanielk1977 ** 332fd9a0a45Sdanielk1977 ** If the createFlag argument is true, then a new (blank) FuncDef 333fd9a0a45Sdanielk1977 ** structure is created and liked into the "db" structure if a 33489d5d6a2Sdrh ** no matching function previously existed. 335fd9a0a45Sdanielk1977 ** 33689d5d6a2Sdrh ** If nArg is -2, then the first valid function found is returned. A 33789d5d6a2Sdrh ** function is valid if either xFunc or xStep is non-zero. The nArg==(-2) 33889d5d6a2Sdrh ** case is used to see if zName is a valid function name for some number 33989d5d6a2Sdrh ** of arguments. If nArg is -2, then createFlag must be 0. 340fd9a0a45Sdanielk1977 ** 341fd9a0a45Sdanielk1977 ** If createFlag is false, then a function with the required name and 342fd9a0a45Sdanielk1977 ** number of arguments may be returned even if the eTextRep flag does not 343fd9a0a45Sdanielk1977 ** match that requested. 344fd9a0a45Sdanielk1977 */ 345fd9a0a45Sdanielk1977 FuncDef *sqlite3FindFunction( 346fd9a0a45Sdanielk1977 sqlite3 *db, /* An open database */ 347fd9a0a45Sdanielk1977 const char *zName, /* Name of the function. Not null-terminated */ 348fd9a0a45Sdanielk1977 int nName, /* Number of characters in the name */ 349fd9a0a45Sdanielk1977 int nArg, /* Number of arguments. -1 means any number */ 350fd9a0a45Sdanielk1977 u8 enc, /* Preferred text encoding */ 35189d5d6a2Sdrh u8 createFlag /* Create new entry if true and does not otherwise exist */ 352fd9a0a45Sdanielk1977 ){ 353fd9a0a45Sdanielk1977 FuncDef *p; /* Iterator variable */ 354fd9a0a45Sdanielk1977 FuncDef *pBest = 0; /* Best match found so far */ 35570a8ca3cSdrh int bestScore = 0; /* Score of best match */ 35670a8ca3cSdrh int h; /* Hash value */ 357fd9a0a45Sdanielk1977 35889d5d6a2Sdrh assert( nArg>=(-2) ); 35989d5d6a2Sdrh assert( nArg>=(-1) || createFlag==0 ); 36070a8ca3cSdrh h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a); 361fd9a0a45Sdanielk1977 362e3602be8Sdrh /* First search for a match amongst the application-defined functions. 363e3602be8Sdrh */ 36470a8ca3cSdrh p = functionSearch(&db->aFunc, h, zName, nName); 36570a8ca3cSdrh while( p ){ 36670a8ca3cSdrh int score = matchQuality(p, nArg, enc); 36770a8ca3cSdrh if( score>bestScore ){ 368fd9a0a45Sdanielk1977 pBest = p; 36970a8ca3cSdrh bestScore = score; 370fd9a0a45Sdanielk1977 } 37170a8ca3cSdrh p = p->pNext; 372fd9a0a45Sdanielk1977 } 3738c0a791aSdanielk1977 374e3602be8Sdrh /* If no match is found, search the built-in functions. 375e3602be8Sdrh ** 376545f587fSdrh ** If the SQLITE_PreferBuiltin flag is set, then search the built-in 377545f587fSdrh ** functions even if a prior app-defined function was found. And give 378545f587fSdrh ** priority to built-in functions. 379545f587fSdrh ** 380e3602be8Sdrh ** Except, if createFlag is true, that means that we are trying to 3816c5cecb6Sdrh ** install a new function. Whatever FuncDef structure is returned it will 382e3602be8Sdrh ** have fields overwritten with new information appropriate for the 383e3602be8Sdrh ** new function. But the FuncDefs for built-in functions are read-only. 384e3602be8Sdrh ** So we must not search for built-ins when creating a new function. 3858c0a791aSdanielk1977 */ 386545f587fSdrh if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){ 387075c23afSdanielk1977 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); 388545f587fSdrh bestScore = 0; 389075c23afSdanielk1977 p = functionSearch(pHash, h, zName, nName); 39070a8ca3cSdrh while( p ){ 39170a8ca3cSdrh int score = matchQuality(p, nArg, enc); 39270a8ca3cSdrh if( score>bestScore ){ 39370a8ca3cSdrh pBest = p; 39470a8ca3cSdrh bestScore = score; 3958c0a791aSdanielk1977 } 39670a8ca3cSdrh p = p->pNext; 3978c0a791aSdanielk1977 } 398fd9a0a45Sdanielk1977 } 399fd9a0a45Sdanielk1977 400e3602be8Sdrh /* If the createFlag parameter is true and the search did not reveal an 401fd9a0a45Sdanielk1977 ** exact match for the name, number of arguments and encoding, then add a 402fd9a0a45Sdanielk1977 ** new entry to the hash table and return it. 403fd9a0a45Sdanielk1977 */ 40489d5d6a2Sdrh if( createFlag && bestScore<FUNC_PERFECT_MATCH && 4058c0a791aSdanielk1977 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){ 4068c0a791aSdanielk1977 pBest->zName = (char *)&pBest[1]; 4071bd10f8aSdrh pBest->nArg = (u16)nArg; 408d36e1041Sdrh pBest->funcFlags = enc; 409fd9a0a45Sdanielk1977 memcpy(pBest->zName, zName, nName); 410fd9a0a45Sdanielk1977 pBest->zName[nName] = 0; 41170a8ca3cSdrh sqlite3FuncDefInsert(&db->aFunc, pBest); 412fd9a0a45Sdanielk1977 } 413fd9a0a45Sdanielk1977 414fd9a0a45Sdanielk1977 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){ 415fd9a0a45Sdanielk1977 return pBest; 416fd9a0a45Sdanielk1977 } 417fd9a0a45Sdanielk1977 return 0; 418fd9a0a45Sdanielk1977 } 41903b808a6Sdrh 42003b808a6Sdrh /* 42103b808a6Sdrh ** Free all resources held by the schema structure. The void* argument points 422633e6d57Sdrh ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the 423b6ee6607Sdrh ** pointer itself, it just cleans up subsidiary resources (i.e. the contents 42403b808a6Sdrh ** of the schema hash tables). 4258cf6c554Sdanielk1977 ** 4268cf6c554Sdanielk1977 ** The Schema.cache_size variable is not cleared. 42703b808a6Sdrh */ 428b6ee6607Sdrh void sqlite3SchemaClear(void *p){ 42903b808a6Sdrh Hash temp1; 43003b808a6Sdrh Hash temp2; 43103b808a6Sdrh HashElem *pElem; 43203b808a6Sdrh Schema *pSchema = (Schema *)p; 43303b808a6Sdrh 43403b808a6Sdrh temp1 = pSchema->tblHash; 43503b808a6Sdrh temp2 = pSchema->trigHash; 436e61922a6Sdrh sqlite3HashInit(&pSchema->trigHash); 43703b808a6Sdrh sqlite3HashClear(&pSchema->idxHash); 43803b808a6Sdrh for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){ 439633e6d57Sdrh sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem)); 44003b808a6Sdrh } 44103b808a6Sdrh sqlite3HashClear(&temp2); 442e61922a6Sdrh sqlite3HashInit(&pSchema->tblHash); 44303b808a6Sdrh for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){ 44403b808a6Sdrh Table *pTab = sqliteHashData(pElem); 4451feeaed2Sdan sqlite3DeleteTable(0, pTab); 44603b808a6Sdrh } 44703b808a6Sdrh sqlite3HashClear(&temp1); 4481da40a38Sdan sqlite3HashClear(&pSchema->fkeyHash); 44903b808a6Sdrh pSchema->pSeqTab = 0; 450*2c5e35ffSdrh if( pSchema->schemaFlags & DB_SchemaLoaded ){ 451c2a75551Sdrh pSchema->iGeneration++; 452*2c5e35ffSdrh pSchema->schemaFlags &= ~DB_SchemaLoaded; 45303b808a6Sdrh } 454c2a75551Sdrh } 45503b808a6Sdrh 45603b808a6Sdrh /* 45703b808a6Sdrh ** Find and return the schema associated with a BTree. Create 45803b808a6Sdrh ** a new one if necessary. 45903b808a6Sdrh */ 46017435752Sdrh Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){ 46103b808a6Sdrh Schema * p; 46203b808a6Sdrh if( pBt ){ 463b6ee6607Sdrh p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear); 46403b808a6Sdrh }else{ 465b975598eSdrh p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema)); 46603b808a6Sdrh } 467a1644fd8Sdanielk1977 if( !p ){ 468a1644fd8Sdanielk1977 db->mallocFailed = 1; 469a1644fd8Sdanielk1977 }else if ( 0==p->file_format ){ 470e61922a6Sdrh sqlite3HashInit(&p->tblHash); 471e61922a6Sdrh sqlite3HashInit(&p->idxHash); 472e61922a6Sdrh sqlite3HashInit(&p->trigHash); 4731da40a38Sdan sqlite3HashInit(&p->fkeyHash); 474f012ea3bSdrh p->enc = SQLITE_UTF8; 47503b808a6Sdrh } 47603b808a6Sdrh return p; 47703b808a6Sdrh } 478