xref: /sqlite-3.40.0/src/callback.c (revision 41ce47c4)
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 */
callCollNeeded(sqlite3 * db,int enc,const char * zName)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 */
synthCollSeq(sqlite3 * db,CollSeq * pColl)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 routine is called on a collation sequence before it is used to
704dade037Sdanielk1977 ** check that it is defined. An undefined collation sequence exists when
714dade037Sdanielk1977 ** a database is loaded that contains references to collation sequences
724dade037Sdanielk1977 ** that have not been defined by sqlite3_create_collation() etc.
734dade037Sdanielk1977 **
744dade037Sdanielk1977 ** If required, this routine calls the 'collation needed' callback to
754dade037Sdanielk1977 ** request a definition of the collating sequence. If this doesn't work,
764dade037Sdanielk1977 ** an equivalent collating sequence that uses a text encoding different
774dade037Sdanielk1977 ** from the main database is substituted, if one is available.
784dade037Sdanielk1977 */
sqlite3CheckCollSeq(Parse * pParse,CollSeq * pColl)794dade037Sdanielk1977 int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
80666e8662Sdrh   if( pColl && pColl->xCmp==0 ){
814dade037Sdanielk1977     const char *zName = pColl->zName;
829aeda79cSdrh     sqlite3 *db = pParse->db;
8379e72a50Sdrh     CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
844dade037Sdanielk1977     if( !p ){
854dade037Sdanielk1977       return SQLITE_ERROR;
864dade037Sdanielk1977     }
87b3bf556eSdanielk1977     assert( p==pColl );
884dade037Sdanielk1977   }
894dade037Sdanielk1977   return SQLITE_OK;
904dade037Sdanielk1977 }
914dade037Sdanielk1977 
924dade037Sdanielk1977 
934dade037Sdanielk1977 
944dade037Sdanielk1977 /*
95fd9a0a45Sdanielk1977 ** Locate and return an entry from the db.aCollSeq hash table. If the entry
96fd9a0a45Sdanielk1977 ** specified by zName and nName is not found and parameter 'create' is
97fd9a0a45Sdanielk1977 ** true, then create a new entry. Otherwise return NULL.
98fd9a0a45Sdanielk1977 **
99fd9a0a45Sdanielk1977 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
100fd9a0a45Sdanielk1977 ** array of three CollSeq structures. The first is the collation sequence
10160ec914cSpeter.d.reid ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
102fd9a0a45Sdanielk1977 **
103fd9a0a45Sdanielk1977 ** Stored immediately after the three collation sequences is a copy of
104fd9a0a45Sdanielk1977 ** the collation sequence name. A pointer to this string is stored in
105fd9a0a45Sdanielk1977 ** each collation sequence structure.
106fd9a0a45Sdanielk1977 */
findCollSeqEntry(sqlite3 * db,const char * zName,int create)107fd9a0a45Sdanielk1977 static CollSeq *findCollSeqEntry(
108c4a64facSdrh   sqlite3 *db,          /* Database connection */
109c4a64facSdrh   const char *zName,    /* Name of the collating sequence */
110c4a64facSdrh   int create            /* Create a new entry if true */
111fd9a0a45Sdanielk1977 ){
112fd9a0a45Sdanielk1977   CollSeq *pColl;
113acbcb7e0Sdrh   pColl = sqlite3HashFind(&db->aCollSeq, zName);
114fd9a0a45Sdanielk1977 
115fd9a0a45Sdanielk1977   if( 0==pColl && create ){
1163b02e0f6Sdrh     int nName = sqlite3Strlen30(zName) + 1;
1173b02e0f6Sdrh     pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
118fd9a0a45Sdanielk1977     if( pColl ){
119fd9a0a45Sdanielk1977       CollSeq *pDel = 0;
120fd9a0a45Sdanielk1977       pColl[0].zName = (char*)&pColl[3];
121fd9a0a45Sdanielk1977       pColl[0].enc = SQLITE_UTF8;
122fd9a0a45Sdanielk1977       pColl[1].zName = (char*)&pColl[3];
123fd9a0a45Sdanielk1977       pColl[1].enc = SQLITE_UTF16LE;
124fd9a0a45Sdanielk1977       pColl[2].zName = (char*)&pColl[3];
125fd9a0a45Sdanielk1977       pColl[2].enc = SQLITE_UTF16BE;
126fd9a0a45Sdanielk1977       memcpy(pColl[0].zName, zName, nName);
127acbcb7e0Sdrh       pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
128fd9a0a45Sdanielk1977 
129be217793Sshane       /* If a malloc() failure occurred in sqlite3HashInsert(), it will
130fd9a0a45Sdanielk1977       ** return the pColl pointer to be deleted (because it wasn't added
131fd9a0a45Sdanielk1977       ** to the hash table).
132fd9a0a45Sdanielk1977       */
133f3a65f7eSdrh       assert( pDel==0 || pDel==pColl );
134f3a65f7eSdrh       if( pDel!=0 ){
1354a642b60Sdrh         sqlite3OomFault(db);
136633e6d57Sdrh         sqlite3DbFree(db, pDel);
13791171cdeSdrh         pColl = 0;
13891171cdeSdrh       }
139fd9a0a45Sdanielk1977     }
140fd9a0a45Sdanielk1977   }
141fd9a0a45Sdanielk1977   return pColl;
142fd9a0a45Sdanielk1977 }
143fd9a0a45Sdanielk1977 
144fd9a0a45Sdanielk1977 /*
145fd9a0a45Sdanielk1977 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
146fd9a0a45Sdanielk1977 ** Return the CollSeq* pointer for the collation sequence named zName
147fd9a0a45Sdanielk1977 ** for the encoding 'enc' from the database 'db'.
148fd9a0a45Sdanielk1977 **
149fd9a0a45Sdanielk1977 ** If the entry specified is not found and 'create' is true, then create a
150fd9a0a45Sdanielk1977 ** new entry.  Otherwise return NULL.
151a34001c9Sdrh **
152a34001c9Sdrh ** A separate function sqlite3LocateCollSeq() is a wrapper around
153a34001c9Sdrh ** this routine.  sqlite3LocateCollSeq() invokes the collation factory
154a34001c9Sdrh ** if necessary and generates an error message if the collating sequence
155a34001c9Sdrh ** cannot be found.
156c4a64facSdrh **
157c4a64facSdrh ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
158fd9a0a45Sdanielk1977 */
sqlite3FindCollSeq(sqlite3 * db,u8 enc,const char * zName,int create)159fd9a0a45Sdanielk1977 CollSeq *sqlite3FindCollSeq(
160a5d09115Sdrh   sqlite3 *db,          /* Database connection to search */
161a5d09115Sdrh   u8 enc,               /* Desired text encoding */
162a5d09115Sdrh   const char *zName,    /* Name of the collating sequence.  Might be NULL */
163a5d09115Sdrh   int create            /* True to create CollSeq if doesn't already exist */
164fd9a0a45Sdanielk1977 ){
165b3bf556eSdanielk1977   CollSeq *pColl;
16642a630b1Sdrh   assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
16742a630b1Sdrh   assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
168b3bf556eSdanielk1977   if( zName ){
169c4a64facSdrh     pColl = findCollSeqEntry(db, zName, create);
17042a630b1Sdrh     if( pColl ) pColl += enc-1;
171b3bf556eSdanielk1977   }else{
172b3bf556eSdanielk1977     pColl = db->pDfltColl;
173b3bf556eSdanielk1977   }
174fd9a0a45Sdanielk1977   return pColl;
175fd9a0a45Sdanielk1977 }
176fd9a0a45Sdanielk1977 
177a5d09115Sdrh /*
17842a630b1Sdrh ** Change the text encoding for a database connection. This means that
17942a630b1Sdrh ** the pDfltColl must change as well.
18042a630b1Sdrh */
sqlite3SetTextEncoding(sqlite3 * db,u8 enc)18142a630b1Sdrh void sqlite3SetTextEncoding(sqlite3 *db, u8 enc){
18242a630b1Sdrh   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
18342a630b1Sdrh   db->enc = enc;
18442a630b1Sdrh   /* EVIDENCE-OF: R-08308-17224 The default collating function for all
18542a630b1Sdrh   ** strings is BINARY.
18642a630b1Sdrh   */
18742a630b1Sdrh   db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
18842a630b1Sdrh }
18942a630b1Sdrh 
19042a630b1Sdrh /*
191a5d09115Sdrh ** This function is responsible for invoking the collation factory callback
192a5d09115Sdrh ** or substituting a collation sequence of a different encoding when the
193a5d09115Sdrh ** requested collation sequence is not available in the desired encoding.
194a5d09115Sdrh **
195a5d09115Sdrh ** If it is not NULL, then pColl must point to the database native encoding
196a5d09115Sdrh ** collation sequence with name zName, length nName.
197a5d09115Sdrh **
198a5d09115Sdrh ** The return value is either the collation sequence to be used in database
199a5d09115Sdrh ** db for collation type name zName, length nName, or NULL, if no collation
200a5d09115Sdrh ** sequence can be found.  If no collation is found, leave an error message.
201a5d09115Sdrh **
202a5d09115Sdrh ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
203a5d09115Sdrh */
sqlite3GetCollSeq(Parse * pParse,u8 enc,CollSeq * pColl,const char * zName)204a5d09115Sdrh CollSeq *sqlite3GetCollSeq(
205a5d09115Sdrh   Parse *pParse,        /* Parsing context */
206a5d09115Sdrh   u8 enc,               /* The desired encoding for the collating sequence */
207a5d09115Sdrh   CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
208a5d09115Sdrh   const char *zName     /* Collating sequence name */
209a5d09115Sdrh ){
210a5d09115Sdrh   CollSeq *p;
211a5d09115Sdrh   sqlite3 *db = pParse->db;
212a5d09115Sdrh 
213a5d09115Sdrh   p = pColl;
214a5d09115Sdrh   if( !p ){
215a5d09115Sdrh     p = sqlite3FindCollSeq(db, enc, zName, 0);
216a5d09115Sdrh   }
217a5d09115Sdrh   if( !p || !p->xCmp ){
218a5d09115Sdrh     /* No collation sequence of this type for this encoding is registered.
219a5d09115Sdrh     ** Call the collation factory to see if it can supply us with one.
220a5d09115Sdrh     */
221a5d09115Sdrh     callCollNeeded(db, enc, zName);
222a5d09115Sdrh     p = sqlite3FindCollSeq(db, enc, zName, 0);
223a5d09115Sdrh   }
224a5d09115Sdrh   if( p && !p->xCmp && synthCollSeq(db, p) ){
225a5d09115Sdrh     p = 0;
226a5d09115Sdrh   }
227a5d09115Sdrh   assert( !p || p->xCmp );
228a5d09115Sdrh   if( p==0 ){
229a5d09115Sdrh     sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
230a5d09115Sdrh     pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
231a5d09115Sdrh   }
232a5d09115Sdrh   return p;
233a5d09115Sdrh }
234a5d09115Sdrh 
235a5d09115Sdrh /*
236a5d09115Sdrh ** This function returns the collation sequence for database native text
237a5d09115Sdrh ** encoding identified by the string zName.
238a5d09115Sdrh **
239a5d09115Sdrh ** If the requested collation sequence is not available, or not available
240a5d09115Sdrh ** in the database native encoding, the collation factory is invoked to
241a5d09115Sdrh ** request it. If the collation factory does not supply such a sequence,
242a5d09115Sdrh ** and the sequence is available in another text encoding, then that is
243a5d09115Sdrh ** returned instead.
244a5d09115Sdrh **
245a5d09115Sdrh ** If no versions of the requested collations sequence are available, or
246a5d09115Sdrh ** another error occurs, NULL is returned and an error message written into
247a5d09115Sdrh ** pParse.
248a5d09115Sdrh **
249a5d09115Sdrh ** This routine is a wrapper around sqlite3FindCollSeq().  This routine
250a5d09115Sdrh ** invokes the collation factory if the named collation cannot be found
251a5d09115Sdrh ** and generates an error message.
252a5d09115Sdrh **
253a5d09115Sdrh ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
254a5d09115Sdrh */
sqlite3LocateCollSeq(Parse * pParse,const char * zName)255a5d09115Sdrh CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
256a5d09115Sdrh   sqlite3 *db = pParse->db;
257a5d09115Sdrh   u8 enc = ENC(db);
258a5d09115Sdrh   u8 initbusy = db->init.busy;
259a5d09115Sdrh   CollSeq *pColl;
260a5d09115Sdrh 
261a5d09115Sdrh   pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
262a5d09115Sdrh   if( !initbusy && (!pColl || !pColl->xCmp) ){
263a5d09115Sdrh     pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
264a5d09115Sdrh   }
265a5d09115Sdrh 
266a5d09115Sdrh   return pColl;
267a5d09115Sdrh }
268a5d09115Sdrh 
2698c0a791aSdanielk1977 /* During the search for the best function definition, this procedure
2708c0a791aSdanielk1977 ** is called to test how well the function passed as the first argument
2718c0a791aSdanielk1977 ** matches the request for a function with nArg arguments in a system
2728c0a791aSdanielk1977 ** that uses encoding enc. The value returned indicates how well the
2738c0a791aSdanielk1977 ** request is matched. A higher value indicates a better match.
2748c0a791aSdanielk1977 **
27589d5d6a2Sdrh ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
27689d5d6a2Sdrh ** is also -1.  In other words, we are searching for a function that
27789d5d6a2Sdrh ** takes a variable number of arguments.
27889d5d6a2Sdrh **
27989d5d6a2Sdrh ** If nArg is -2 that means that we are searching for any function
28089d5d6a2Sdrh ** regardless of the number of arguments it uses, so return a positive
28189d5d6a2Sdrh ** match score for any
28289d5d6a2Sdrh **
283dfbc3a8aSdrh ** The returned value is always between 0 and 6, as follows:
2848c0a791aSdanielk1977 **
28589d5d6a2Sdrh ** 0: Not a match.
28689d5d6a2Sdrh ** 1: UTF8/16 conversion required and function takes any number of arguments.
28789d5d6a2Sdrh ** 2: UTF16 byte order change required and function takes any number of args.
28889d5d6a2Sdrh ** 3: encoding matches and function takes any number of arguments
28989d5d6a2Sdrh ** 4: UTF8/16 conversion required - argument count matches exactly
29089d5d6a2Sdrh ** 5: UTF16 byte order conversion required - argument count matches exactly
29189d5d6a2Sdrh ** 6: Perfect match:  encoding and argument count match exactly.
2928c0a791aSdanielk1977 **
2932d80151fSdrh ** If nArg==(-2) then any function with a non-null xSFunc is
2942d80151fSdrh ** a perfect match and any function with xSFunc NULL is
29589d5d6a2Sdrh ** a non-match.
2968c0a791aSdanielk1977 */
29789d5d6a2Sdrh #define FUNC_PERFECT_MATCH 6  /* The score for a perfect match */
matchQuality(FuncDef * p,int nArg,u8 enc)29889d5d6a2Sdrh static int matchQuality(
29989d5d6a2Sdrh   FuncDef *p,     /* The function we are evaluating for match quality */
30089d5d6a2Sdrh   int nArg,       /* Desired number of arguments.  (-1)==any */
30189d5d6a2Sdrh   u8 enc          /* Desired text encoding */
302dfbc3a8aSdrh ){
30389d5d6a2Sdrh   int match;
3042eeca204Sdrh   assert( p->nArg>=-1 );
30589d5d6a2Sdrh 
30689d5d6a2Sdrh   /* Wrong number of arguments means "no match" */
3072eeca204Sdrh   if( p->nArg!=nArg ){
3082eeca204Sdrh     if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
3092eeca204Sdrh     if( p->nArg>=0 ) return 0;
3102eeca204Sdrh   }
31189d5d6a2Sdrh 
31289d5d6a2Sdrh   /* Give a better score to a function with a specific number of arguments
31389d5d6a2Sdrh   ** than to function that accepts any number of arguments. */
31489d5d6a2Sdrh   if( p->nArg==nArg ){
3158c0a791aSdanielk1977     match = 4;
31689d5d6a2Sdrh   }else{
31789d5d6a2Sdrh     match = 1;
3188c0a791aSdanielk1977   }
31989d5d6a2Sdrh 
32089d5d6a2Sdrh   /* Bonus points if the text encoding matches */
321d36e1041Sdrh   if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
32289d5d6a2Sdrh     match += 2;  /* Exact encoding match */
323d36e1041Sdrh   }else if( (enc & p->funcFlags & 2)!=0 ){
32489d5d6a2Sdrh     match += 1;  /* Both are UTF16, but with different byte orders */
3258c0a791aSdanielk1977   }
32689d5d6a2Sdrh 
3278c0a791aSdanielk1977   return match;
3288c0a791aSdanielk1977 }
3298c0a791aSdanielk1977 
330fd9a0a45Sdanielk1977 /*
33170a8ca3cSdrh ** Search a FuncDefHash for a function with the given name.  Return
33270a8ca3cSdrh ** a pointer to the matching FuncDef if found, or 0 if there is no match.
33370a8ca3cSdrh */
sqlite3FunctionSearch(int h,const char * zFunc)33419efd0dbSdrh FuncDef *sqlite3FunctionSearch(
33570a8ca3cSdrh   int h,               /* Hash of the name */
33680738d9cSdrh   const char *zFunc    /* Name of function */
33770a8ca3cSdrh ){
33870a8ca3cSdrh   FuncDef *p;
33980738d9cSdrh   for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
340f9751074Sdrh     assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
34180738d9cSdrh     if( sqlite3StrICmp(p->zName, zFunc)==0 ){
34270a8ca3cSdrh       return p;
34370a8ca3cSdrh     }
34470a8ca3cSdrh   }
34570a8ca3cSdrh   return 0;
34670a8ca3cSdrh }
34770a8ca3cSdrh 
34870a8ca3cSdrh /*
34970a8ca3cSdrh ** Insert a new FuncDef into a FuncDefHash hash table.
35070a8ca3cSdrh */
sqlite3InsertBuiltinFuncs(FuncDef * aDef,int nDef)35180738d9cSdrh void sqlite3InsertBuiltinFuncs(
35280738d9cSdrh   FuncDef *aDef,      /* List of global functions to be inserted */
35380738d9cSdrh   int nDef            /* Length of the apDef[] list */
35470a8ca3cSdrh ){
35580738d9cSdrh   int i;
35680738d9cSdrh   for(i=0; i<nDef; i++){
35770a8ca3cSdrh     FuncDef *pOther;
35880738d9cSdrh     const char *zName = aDef[i].zName;
35980738d9cSdrh     int nName = sqlite3Strlen30(zName);
3608bee11a4Smistachkin     int h = SQLITE_FUNC_HASH(zName[0], nName);
361f9751074Sdrh     assert( aDef[i].funcFlags & SQLITE_FUNC_BUILTIN );
36219efd0dbSdrh     pOther = sqlite3FunctionSearch(h, zName);
36370a8ca3cSdrh     if( pOther ){
36480738d9cSdrh       assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
36580738d9cSdrh       aDef[i].pNext = pOther->pNext;
36680738d9cSdrh       pOther->pNext = &aDef[i];
36770a8ca3cSdrh     }else{
36880738d9cSdrh       aDef[i].pNext = 0;
36980738d9cSdrh       aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
37080738d9cSdrh       sqlite3BuiltinFunctions.a[h] = &aDef[i];
37180738d9cSdrh     }
37270a8ca3cSdrh   }
37370a8ca3cSdrh }
37470a8ca3cSdrh 
37570a8ca3cSdrh 
37670a8ca3cSdrh 
37770a8ca3cSdrh /*
378fd9a0a45Sdanielk1977 ** Locate a user function given a name, a number of arguments and a flag
379fd9a0a45Sdanielk1977 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
380fd9a0a45Sdanielk1977 ** pointer to the FuncDef structure that defines that function, or return
381fd9a0a45Sdanielk1977 ** NULL if the function does not exist.
382fd9a0a45Sdanielk1977 **
383fd9a0a45Sdanielk1977 ** If the createFlag argument is true, then a new (blank) FuncDef
384fd9a0a45Sdanielk1977 ** structure is created and liked into the "db" structure if a
38589d5d6a2Sdrh ** no matching function previously existed.
386fd9a0a45Sdanielk1977 **
38789d5d6a2Sdrh ** If nArg is -2, then the first valid function found is returned.  A
3882d80151fSdrh ** function is valid if xSFunc is non-zero.  The nArg==(-2)
38989d5d6a2Sdrh ** case is used to see if zName is a valid function name for some number
39089d5d6a2Sdrh ** of arguments.  If nArg is -2, then createFlag must be 0.
391fd9a0a45Sdanielk1977 **
392fd9a0a45Sdanielk1977 ** If createFlag is false, then a function with the required name and
393fd9a0a45Sdanielk1977 ** number of arguments may be returned even if the eTextRep flag does not
394fd9a0a45Sdanielk1977 ** match that requested.
395fd9a0a45Sdanielk1977 */
sqlite3FindFunction(sqlite3 * db,const char * zName,int nArg,u8 enc,u8 createFlag)396fd9a0a45Sdanielk1977 FuncDef *sqlite3FindFunction(
397fd9a0a45Sdanielk1977   sqlite3 *db,       /* An open database */
39880738d9cSdrh   const char *zName, /* Name of the function.  zero-terminated */
399fd9a0a45Sdanielk1977   int nArg,          /* Number of arguments.  -1 means any number */
400fd9a0a45Sdanielk1977   u8 enc,            /* Preferred text encoding */
40189d5d6a2Sdrh   u8 createFlag      /* Create new entry if true and does not otherwise exist */
402fd9a0a45Sdanielk1977 ){
403fd9a0a45Sdanielk1977   FuncDef *p;         /* Iterator variable */
404fd9a0a45Sdanielk1977   FuncDef *pBest = 0; /* Best match found so far */
40570a8ca3cSdrh   int bestScore = 0;  /* Score of best match */
40670a8ca3cSdrh   int h;              /* Hash value */
40780738d9cSdrh   int nName;          /* Length of the name */
408fd9a0a45Sdanielk1977 
40989d5d6a2Sdrh   assert( nArg>=(-2) );
41089d5d6a2Sdrh   assert( nArg>=(-1) || createFlag==0 );
41180738d9cSdrh   nName = sqlite3Strlen30(zName);
412fd9a0a45Sdanielk1977 
413e3602be8Sdrh   /* First search for a match amongst the application-defined functions.
414e3602be8Sdrh   */
41580738d9cSdrh   p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
41670a8ca3cSdrh   while( p ){
41770a8ca3cSdrh     int score = matchQuality(p, nArg, enc);
41870a8ca3cSdrh     if( score>bestScore ){
419fd9a0a45Sdanielk1977       pBest = p;
42070a8ca3cSdrh       bestScore = score;
421fd9a0a45Sdanielk1977     }
42270a8ca3cSdrh     p = p->pNext;
423fd9a0a45Sdanielk1977   }
4248c0a791aSdanielk1977 
425e3602be8Sdrh   /* If no match is found, search the built-in functions.
426e3602be8Sdrh   **
4278257aa8dSdrh   ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
428545f587fSdrh   ** functions even if a prior app-defined function was found.  And give
429545f587fSdrh   ** priority to built-in functions.
430545f587fSdrh   **
431e3602be8Sdrh   ** Except, if createFlag is true, that means that we are trying to
4326c5cecb6Sdrh   ** install a new function.  Whatever FuncDef structure is returned it will
433e3602be8Sdrh   ** have fields overwritten with new information appropriate for the
434e3602be8Sdrh   ** new function.  But the FuncDefs for built-in functions are read-only.
435e3602be8Sdrh   ** So we must not search for built-ins when creating a new function.
4368c0a791aSdanielk1977   */
4378257aa8dSdrh   if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
438545f587fSdrh     bestScore = 0;
4398bee11a4Smistachkin     h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
44019efd0dbSdrh     p = sqlite3FunctionSearch(h, zName);
44170a8ca3cSdrh     while( p ){
44270a8ca3cSdrh       int score = matchQuality(p, nArg, enc);
44370a8ca3cSdrh       if( score>bestScore ){
44470a8ca3cSdrh         pBest = p;
44570a8ca3cSdrh         bestScore = score;
4468c0a791aSdanielk1977       }
44770a8ca3cSdrh       p = p->pNext;
4488c0a791aSdanielk1977     }
449fd9a0a45Sdanielk1977   }
450fd9a0a45Sdanielk1977 
451e3602be8Sdrh   /* If the createFlag parameter is true and the search did not reveal an
452fd9a0a45Sdanielk1977   ** exact match for the name, number of arguments and encoding, then add a
453fd9a0a45Sdanielk1977   ** new entry to the hash table and return it.
454fd9a0a45Sdanielk1977   */
45589d5d6a2Sdrh   if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
4568c0a791aSdanielk1977       (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
45780738d9cSdrh     FuncDef *pOther;
45892011847Sdrh     u8 *z;
4596ad224e9Sdrh     pBest->zName = (const char*)&pBest[1];
4601bd10f8aSdrh     pBest->nArg = (u16)nArg;
461d36e1041Sdrh     pBest->funcFlags = enc;
4626ad224e9Sdrh     memcpy((char*)&pBest[1], zName, nName+1);
46392011847Sdrh     for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
46480738d9cSdrh     pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
46580738d9cSdrh     if( pOther==pBest ){
46680738d9cSdrh       sqlite3DbFree(db, pBest);
46780738d9cSdrh       sqlite3OomFault(db);
46880738d9cSdrh       return 0;
46980738d9cSdrh     }else{
47080738d9cSdrh       pBest->pNext = pOther;
47180738d9cSdrh     }
472fd9a0a45Sdanielk1977   }
473fd9a0a45Sdanielk1977 
4742d80151fSdrh   if( pBest && (pBest->xSFunc || createFlag) ){
475fd9a0a45Sdanielk1977     return pBest;
476fd9a0a45Sdanielk1977   }
477fd9a0a45Sdanielk1977   return 0;
478fd9a0a45Sdanielk1977 }
47903b808a6Sdrh 
48003b808a6Sdrh /*
48103b808a6Sdrh ** Free all resources held by the schema structure. The void* argument points
482633e6d57Sdrh ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
483b6ee6607Sdrh ** pointer itself, it just cleans up subsidiary resources (i.e. the contents
48403b808a6Sdrh ** of the schema hash tables).
4858cf6c554Sdanielk1977 **
4868cf6c554Sdanielk1977 ** The Schema.cache_size variable is not cleared.
48703b808a6Sdrh */
sqlite3SchemaClear(void * p)488b6ee6607Sdrh void sqlite3SchemaClear(void *p){
48903b808a6Sdrh   Hash temp1;
49003b808a6Sdrh   Hash temp2;
49103b808a6Sdrh   HashElem *pElem;
49203b808a6Sdrh   Schema *pSchema = (Schema *)p;
493*41ce47c4Sdrh   sqlite3 xdb;
49403b808a6Sdrh 
495*41ce47c4Sdrh   memset(&xdb, 0, sizeof(xdb));
49603b808a6Sdrh   temp1 = pSchema->tblHash;
49703b808a6Sdrh   temp2 = pSchema->trigHash;
498e61922a6Sdrh   sqlite3HashInit(&pSchema->trigHash);
49903b808a6Sdrh   sqlite3HashClear(&pSchema->idxHash);
50003b808a6Sdrh   for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
501*41ce47c4Sdrh     sqlite3DeleteTrigger(&xdb, (Trigger*)sqliteHashData(pElem));
50203b808a6Sdrh   }
50303b808a6Sdrh   sqlite3HashClear(&temp2);
504e61922a6Sdrh   sqlite3HashInit(&pSchema->tblHash);
50503b808a6Sdrh   for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
50603b808a6Sdrh     Table *pTab = sqliteHashData(pElem);
507*41ce47c4Sdrh     sqlite3DeleteTable(&xdb, pTab);
50803b808a6Sdrh   }
50903b808a6Sdrh   sqlite3HashClear(&temp1);
5101da40a38Sdan   sqlite3HashClear(&pSchema->fkeyHash);
51103b808a6Sdrh   pSchema->pSeqTab = 0;
5122c5e35ffSdrh   if( pSchema->schemaFlags & DB_SchemaLoaded ){
513c2a75551Sdrh     pSchema->iGeneration++;
51403b808a6Sdrh   }
515dc6b41edSdrh   pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
516c2a75551Sdrh }
51703b808a6Sdrh 
51803b808a6Sdrh /*
51903b808a6Sdrh ** Find and return the schema associated with a BTree.  Create
52003b808a6Sdrh ** a new one if necessary.
52103b808a6Sdrh */
sqlite3SchemaGet(sqlite3 * db,Btree * pBt)52217435752Sdrh Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
52303b808a6Sdrh   Schema * p;
52403b808a6Sdrh   if( pBt ){
525b6ee6607Sdrh     p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
52603b808a6Sdrh   }else{
527b975598eSdrh     p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
52803b808a6Sdrh   }
529a1644fd8Sdanielk1977   if( !p ){
5304a642b60Sdrh     sqlite3OomFault(db);
531a1644fd8Sdanielk1977   }else if ( 0==p->file_format ){
532e61922a6Sdrh     sqlite3HashInit(&p->tblHash);
533e61922a6Sdrh     sqlite3HashInit(&p->idxHash);
534e61922a6Sdrh     sqlite3HashInit(&p->trigHash);
5351da40a38Sdan     sqlite3HashInit(&p->fkeyHash);
536f012ea3bSdrh     p->enc = SQLITE_UTF8;
53703b808a6Sdrh   }
53803b808a6Sdrh   return p;
53903b808a6Sdrh }
540