danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2005 May 23 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** |
| 13 | ** This file contains functions used to access the internal hash tables |
| 14 | ** of user defined functions and collation sequences. |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "sqliteInt.h" |
| 18 | |
| 19 | /* |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 20 | ** Invoke the 'collation needed' callback to request a collation sequence |
drh | 9aeda79 | 2009-08-20 02:34:15 +0000 | [diff] [blame] | 21 | ** in the encoding enc of name zName, length nName. |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 22 | */ |
drh | 9aeda79 | 2009-08-20 02:34:15 +0000 | [diff] [blame] | 23 | static void callCollNeeded(sqlite3 *db, int enc, const char *zName){ |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 24 | assert( !db->xCollNeeded || !db->xCollNeeded16 ); |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 25 | if( db->xCollNeeded ){ |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 26 | char *zExternal = sqlite3DbStrDup(db, zName); |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 27 | if( !zExternal ) return; |
drh | 9aeda79 | 2009-08-20 02:34:15 +0000 | [diff] [blame] | 28 | db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 29 | sqlite3DbFree(db, zExternal); |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 30 | } |
| 31 | #ifndef SQLITE_OMIT_UTF16 |
| 32 | if( db->xCollNeeded16 ){ |
| 33 | char const *zExternal; |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 34 | sqlite3_value *pTmp = sqlite3ValueNew(db); |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 35 | sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 36 | zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE); |
drh | 26abcb1 | 2005-12-14 22:51:16 +0000 | [diff] [blame] | 37 | if( zExternal ){ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 38 | db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal); |
drh | 26abcb1 | 2005-12-14 22:51:16 +0000 | [diff] [blame] | 39 | } |
| 40 | sqlite3ValueFree(pTmp); |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 41 | } |
| 42 | #endif |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | ** This routine is called if the collation factory fails to deliver a |
| 47 | ** collation function in the best encoding but there may be other versions |
| 48 | ** of this collation function (for other text encodings) available. Use one |
| 49 | ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if |
| 50 | ** possible. |
| 51 | */ |
| 52 | static int synthCollSeq(sqlite3 *db, CollSeq *pColl){ |
| 53 | CollSeq *pColl2; |
| 54 | char *z = pColl->zName; |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 55 | int i; |
| 56 | static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 }; |
| 57 | for(i=0; i<3; i++){ |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 58 | pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0); |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 59 | if( pColl2->xCmp!=0 ){ |
| 60 | memcpy(pColl, pColl2, sizeof(CollSeq)); |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 61 | pColl->xDel = 0; /* Do not copy the destructor */ |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 62 | return SQLITE_OK; |
| 63 | } |
| 64 | } |
| 65 | return SQLITE_ERROR; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | ** This function is responsible for invoking the collation factory callback |
| 70 | ** or substituting a collation sequence of a different encoding when the |
drh | 9aeda79 | 2009-08-20 02:34:15 +0000 | [diff] [blame] | 71 | ** requested collation sequence is not available in the desired encoding. |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 72 | ** |
| 73 | ** If it is not NULL, then pColl must point to the database native encoding |
| 74 | ** collation sequence with name zName, length nName. |
| 75 | ** |
| 76 | ** The return value is either the collation sequence to be used in database |
| 77 | ** db for collation type name zName, length nName, or NULL, if no collation |
drh | 79e72a5 | 2012-10-05 14:43:40 +0000 | [diff] [blame] | 78 | ** sequence can be found. If no collation is found, leave an error message. |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 79 | ** |
| 80 | ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq() |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 81 | */ |
| 82 | CollSeq *sqlite3GetCollSeq( |
drh | 79e72a5 | 2012-10-05 14:43:40 +0000 | [diff] [blame] | 83 | Parse *pParse, /* Parsing context */ |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 84 | u8 enc, /* The desired encoding for the collating sequence */ |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 85 | CollSeq *pColl, /* Collating sequence with native encoding, or NULL */ |
| 86 | const char *zName /* Collating sequence name */ |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 87 | ){ |
| 88 | CollSeq *p; |
drh | 79e72a5 | 2012-10-05 14:43:40 +0000 | [diff] [blame] | 89 | sqlite3 *db = pParse->db; |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 90 | |
| 91 | p = pColl; |
| 92 | if( !p ){ |
drh | 9aeda79 | 2009-08-20 02:34:15 +0000 | [diff] [blame] | 93 | p = sqlite3FindCollSeq(db, enc, zName, 0); |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 94 | } |
| 95 | if( !p || !p->xCmp ){ |
| 96 | /* No collation sequence of this type for this encoding is registered. |
| 97 | ** Call the collation factory to see if it can supply us with one. |
| 98 | */ |
drh | 9aeda79 | 2009-08-20 02:34:15 +0000 | [diff] [blame] | 99 | callCollNeeded(db, enc, zName); |
| 100 | p = sqlite3FindCollSeq(db, enc, zName, 0); |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 101 | } |
| 102 | if( p && !p->xCmp && synthCollSeq(db, p) ){ |
| 103 | p = 0; |
| 104 | } |
drh | 464a2dd | 2014-12-05 14:36:15 +0000 | [diff] [blame] | 105 | assert( p==0 || (sqlite3ValidCollSeq(p) && p->xCmp!=0) ); |
drh | 79e72a5 | 2012-10-05 14:43:40 +0000 | [diff] [blame] | 106 | if( p==0 ){ |
| 107 | sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName); |
| 108 | } |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 109 | return p; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | ** This routine is called on a collation sequence before it is used to |
| 114 | ** check that it is defined. An undefined collation sequence exists when |
| 115 | ** a database is loaded that contains references to collation sequences |
| 116 | ** that have not been defined by sqlite3_create_collation() etc. |
| 117 | ** |
| 118 | ** If required, this routine calls the 'collation needed' callback to |
| 119 | ** request a definition of the collating sequence. If this doesn't work, |
| 120 | ** an equivalent collating sequence that uses a text encoding different |
| 121 | ** from the main database is substituted, if one is available. |
| 122 | */ |
| 123 | int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){ |
| 124 | if( pColl ){ |
| 125 | const char *zName = pColl->zName; |
drh | 9aeda79 | 2009-08-20 02:34:15 +0000 | [diff] [blame] | 126 | sqlite3 *db = pParse->db; |
drh | 79e72a5 | 2012-10-05 14:43:40 +0000 | [diff] [blame] | 127 | CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName); |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 128 | if( !p ){ |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 129 | return SQLITE_ERROR; |
| 130 | } |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 131 | assert( p==pColl ); |
danielk1977 | 4dade03 | 2005-05-25 10:45:10 +0000 | [diff] [blame] | 132 | } |
| 133 | return SQLITE_OK; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
| 138 | /* |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 139 | ** Locate and return an entry from the db.aCollSeq hash table. If the entry |
| 140 | ** specified by zName and nName is not found and parameter 'create' is |
| 141 | ** true, then create a new entry. Otherwise return NULL. |
| 142 | ** |
| 143 | ** Each pointer stored in the sqlite3.aCollSeq hash table contains an |
| 144 | ** array of three CollSeq structures. The first is the collation sequence |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 145 | ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be. |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 146 | ** |
| 147 | ** Stored immediately after the three collation sequences is a copy of |
| 148 | ** the collation sequence name. A pointer to this string is stored in |
| 149 | ** each collation sequence structure. |
| 150 | */ |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 151 | static CollSeq *findCollSeqEntry( |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 152 | sqlite3 *db, /* Database connection */ |
| 153 | const char *zName, /* Name of the collating sequence */ |
| 154 | int create /* Create a new entry if true */ |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 155 | ){ |
| 156 | CollSeq *pColl; |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 157 | pColl = sqlite3HashFind(&db->aCollSeq, zName); |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 158 | |
| 159 | if( 0==pColl && create ){ |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 160 | int nName = sqlite3Strlen30(zName); |
| 161 | pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1); |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 162 | if( pColl ){ |
| 163 | CollSeq *pDel = 0; |
| 164 | pColl[0].zName = (char*)&pColl[3]; |
| 165 | pColl[0].enc = SQLITE_UTF8; |
| 166 | pColl[1].zName = (char*)&pColl[3]; |
| 167 | pColl[1].enc = SQLITE_UTF16LE; |
| 168 | pColl[2].zName = (char*)&pColl[3]; |
| 169 | pColl[2].enc = SQLITE_UTF16BE; |
| 170 | memcpy(pColl[0].zName, zName, nName); |
| 171 | pColl[0].zName[nName] = 0; |
drh | acbcb7e | 2014-08-21 20:26:37 +0000 | [diff] [blame] | 172 | pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl); |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 173 | |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 174 | /* If a malloc() failure occurred in sqlite3HashInsert(), it will |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 175 | ** return the pColl pointer to be deleted (because it wasn't added |
| 176 | ** to the hash table). |
| 177 | */ |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 178 | assert( pDel==0 || pDel==pColl ); |
| 179 | if( pDel!=0 ){ |
| 180 | db->mallocFailed = 1; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 181 | sqlite3DbFree(db, pDel); |
drh | 91171cd | 2006-03-14 11:08:27 +0000 | [diff] [blame] | 182 | pColl = 0; |
| 183 | } |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | return pColl; |
| 187 | } |
| 188 | |
drh | 464a2dd | 2014-12-05 14:36:15 +0000 | [diff] [blame] | 189 | #ifdef SQLITE_DEBUG |
| 190 | /* |
| 191 | ** The following routine does sanity checking on a CollSeq object and |
| 192 | ** returns 1 if everything looks ok and 0 if the CollSeq object appears |
| 193 | ** to be corrupt. This routine is used only inside of assert() statements. |
| 194 | */ |
| 195 | int sqlite3ValidCollSeq(const CollSeq *p){ |
| 196 | /* The CollSeq must be one of a triple and the zName field must |
| 197 | ** point to the first byte after that triple |
| 198 | */ |
| 199 | int n = (int)(p->zName - (char*)p)/sizeof(CollSeq); |
| 200 | if( n<=0 || n>3 ) return 0; |
| 201 | |
| 202 | /* Check for valid enc values */ |
| 203 | if( p->enc==SQLITE_UTF8 ) return 1; |
| 204 | if( p->enc==SQLITE_UTF16LE ) return 1; |
| 205 | if( p->enc==SQLITE_UTF16BE ) return 1; |
| 206 | if( p->enc==(SQLITE_UTF16LE|SQLITE_UTF16_ALIGNED) ) return 1; |
| 207 | if( p->enc==(SQLITE_UTF16BE|SQLITE_UTF16_ALIGNED) ) return 1; |
| 208 | |
| 209 | /* Otherwise, malformed */ |
| 210 | return 0; |
| 211 | } |
| 212 | #endif /* SQLITE_DEBUG */ |
| 213 | |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 214 | /* |
| 215 | ** Parameter zName points to a UTF-8 encoded string nName bytes long. |
| 216 | ** Return the CollSeq* pointer for the collation sequence named zName |
| 217 | ** for the encoding 'enc' from the database 'db'. |
| 218 | ** |
| 219 | ** If the entry specified is not found and 'create' is true, then create a |
| 220 | ** new entry. Otherwise return NULL. |
drh | a34001c | 2007-02-02 12:44:37 +0000 | [diff] [blame] | 221 | ** |
| 222 | ** A separate function sqlite3LocateCollSeq() is a wrapper around |
| 223 | ** this routine. sqlite3LocateCollSeq() invokes the collation factory |
| 224 | ** if necessary and generates an error message if the collating sequence |
| 225 | ** cannot be found. |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 226 | ** |
| 227 | ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq() |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 228 | */ |
| 229 | CollSeq *sqlite3FindCollSeq( |
| 230 | sqlite3 *db, |
| 231 | u8 enc, |
| 232 | const char *zName, |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 233 | int create |
| 234 | ){ |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 235 | CollSeq *pColl; |
| 236 | if( zName ){ |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 237 | pColl = findCollSeqEntry(db, zName, create); |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 238 | }else{ |
| 239 | pColl = db->pDfltColl; |
| 240 | } |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 241 | assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); |
| 242 | assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE ); |
drh | 464a2dd | 2014-12-05 14:36:15 +0000 | [diff] [blame] | 243 | if( pColl ){ |
| 244 | pColl += enc-1; |
| 245 | assert( sqlite3ValidCollSeq(pColl) ); |
| 246 | } |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 247 | return pColl; |
| 248 | } |
| 249 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 250 | /* During the search for the best function definition, this procedure |
| 251 | ** is called to test how well the function passed as the first argument |
| 252 | ** matches the request for a function with nArg arguments in a system |
| 253 | ** that uses encoding enc. The value returned indicates how well the |
| 254 | ** request is matched. A higher value indicates a better match. |
| 255 | ** |
drh | 89d5d6a | 2012-04-07 00:09:21 +0000 | [diff] [blame] | 256 | ** If nArg is -1 that means to only return a match (non-zero) if p->nArg |
| 257 | ** is also -1. In other words, we are searching for a function that |
| 258 | ** takes a variable number of arguments. |
| 259 | ** |
| 260 | ** If nArg is -2 that means that we are searching for any function |
| 261 | ** regardless of the number of arguments it uses, so return a positive |
| 262 | ** match score for any |
| 263 | ** |
drh | dfbc3a8 | 2009-01-31 22:28:48 +0000 | [diff] [blame] | 264 | ** The returned value is always between 0 and 6, as follows: |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 265 | ** |
drh | 89d5d6a | 2012-04-07 00:09:21 +0000 | [diff] [blame] | 266 | ** 0: Not a match. |
| 267 | ** 1: UTF8/16 conversion required and function takes any number of arguments. |
| 268 | ** 2: UTF16 byte order change required and function takes any number of args. |
| 269 | ** 3: encoding matches and function takes any number of arguments |
| 270 | ** 4: UTF8/16 conversion required - argument count matches exactly |
| 271 | ** 5: UTF16 byte order conversion required - argument count matches exactly |
| 272 | ** 6: Perfect match: encoding and argument count match exactly. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 273 | ** |
drh | 89d5d6a | 2012-04-07 00:09:21 +0000 | [diff] [blame] | 274 | ** If nArg==(-2) then any function with a non-null xStep or xFunc is |
| 275 | ** a perfect match and any function with both xStep and xFunc NULL is |
| 276 | ** a non-match. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 277 | */ |
drh | 89d5d6a | 2012-04-07 00:09:21 +0000 | [diff] [blame] | 278 | #define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */ |
| 279 | static int matchQuality( |
| 280 | FuncDef *p, /* The function we are evaluating for match quality */ |
| 281 | int nArg, /* Desired number of arguments. (-1)==any */ |
| 282 | u8 enc /* Desired text encoding */ |
| 283 | ){ |
| 284 | int match; |
| 285 | |
| 286 | /* nArg of -2 is a special case */ |
| 287 | if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH; |
| 288 | |
| 289 | /* Wrong number of arguments means "no match" */ |
| 290 | if( p->nArg!=nArg && p->nArg>=0 ) return 0; |
| 291 | |
| 292 | /* Give a better score to a function with a specific number of arguments |
| 293 | ** than to function that accepts any number of arguments. */ |
| 294 | if( p->nArg==nArg ){ |
| 295 | match = 4; |
| 296 | }else{ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 297 | match = 1; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 298 | } |
drh | 89d5d6a | 2012-04-07 00:09:21 +0000 | [diff] [blame] | 299 | |
| 300 | /* Bonus points if the text encoding matches */ |
drh | d36e104 | 2013-09-06 13:10:12 +0000 | [diff] [blame] | 301 | if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){ |
drh | 89d5d6a | 2012-04-07 00:09:21 +0000 | [diff] [blame] | 302 | match += 2; /* Exact encoding match */ |
drh | d36e104 | 2013-09-06 13:10:12 +0000 | [diff] [blame] | 303 | }else if( (enc & p->funcFlags & 2)!=0 ){ |
drh | 89d5d6a | 2012-04-07 00:09:21 +0000 | [diff] [blame] | 304 | match += 1; /* Both are UTF16, but with different byte orders */ |
| 305 | } |
| 306 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 307 | return match; |
| 308 | } |
| 309 | |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 310 | /* |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 311 | ** Search a FuncDefHash for a function with the given name. Return |
| 312 | ** a pointer to the matching FuncDef if found, or 0 if there is no match. |
| 313 | */ |
| 314 | static FuncDef *functionSearch( |
| 315 | FuncDefHash *pHash, /* Hash table to search */ |
| 316 | int h, /* Hash of the name */ |
| 317 | const char *zFunc, /* Name of function */ |
| 318 | int nFunc /* Number of bytes in zFunc */ |
| 319 | ){ |
| 320 | FuncDef *p; |
| 321 | for(p=pHash->a[h]; p; p=p->pHash){ |
| 322 | if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){ |
| 323 | return p; |
| 324 | } |
| 325 | } |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | ** Insert a new FuncDef into a FuncDefHash hash table. |
| 331 | */ |
| 332 | void sqlite3FuncDefInsert( |
| 333 | FuncDefHash *pHash, /* The hash table into which to insert */ |
| 334 | FuncDef *pDef /* The function definition to insert */ |
| 335 | ){ |
| 336 | FuncDef *pOther; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 337 | int nName = sqlite3Strlen30(pDef->zName); |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 338 | u8 c1 = (u8)pDef->zName[0]; |
| 339 | int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a); |
| 340 | pOther = functionSearch(pHash, h, pDef->zName, nName); |
| 341 | if( pOther ){ |
drh | 7aaa878 | 2009-05-20 02:40:45 +0000 | [diff] [blame] | 342 | assert( pOther!=pDef && pOther->pNext!=pDef ); |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 343 | pDef->pNext = pOther->pNext; |
| 344 | pOther->pNext = pDef; |
| 345 | }else{ |
| 346 | pDef->pNext = 0; |
| 347 | pDef->pHash = pHash->a[h]; |
| 348 | pHash->a[h] = pDef; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | |
| 353 | |
| 354 | /* |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 355 | ** Locate a user function given a name, a number of arguments and a flag |
| 356 | ** indicating whether the function prefers UTF-16 over UTF-8. Return a |
| 357 | ** pointer to the FuncDef structure that defines that function, or return |
| 358 | ** NULL if the function does not exist. |
| 359 | ** |
| 360 | ** If the createFlag argument is true, then a new (blank) FuncDef |
| 361 | ** structure is created and liked into the "db" structure if a |
drh | 89d5d6a | 2012-04-07 00:09:21 +0000 | [diff] [blame] | 362 | ** no matching function previously existed. |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 363 | ** |
drh | 89d5d6a | 2012-04-07 00:09:21 +0000 | [diff] [blame] | 364 | ** If nArg is -2, then the first valid function found is returned. A |
| 365 | ** function is valid if either xFunc or xStep is non-zero. The nArg==(-2) |
| 366 | ** case is used to see if zName is a valid function name for some number |
| 367 | ** of arguments. If nArg is -2, then createFlag must be 0. |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 368 | ** |
| 369 | ** If createFlag is false, then a function with the required name and |
| 370 | ** number of arguments may be returned even if the eTextRep flag does not |
| 371 | ** match that requested. |
| 372 | */ |
| 373 | FuncDef *sqlite3FindFunction( |
| 374 | sqlite3 *db, /* An open database */ |
| 375 | const char *zName, /* Name of the function. Not null-terminated */ |
| 376 | int nName, /* Number of characters in the name */ |
| 377 | int nArg, /* Number of arguments. -1 means any number */ |
| 378 | u8 enc, /* Preferred text encoding */ |
drh | 89d5d6a | 2012-04-07 00:09:21 +0000 | [diff] [blame] | 379 | u8 createFlag /* Create new entry if true and does not otherwise exist */ |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 380 | ){ |
| 381 | FuncDef *p; /* Iterator variable */ |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 382 | FuncDef *pBest = 0; /* Best match found so far */ |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 383 | int bestScore = 0; /* Score of best match */ |
| 384 | int h; /* Hash value */ |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 385 | |
drh | 89d5d6a | 2012-04-07 00:09:21 +0000 | [diff] [blame] | 386 | assert( nArg>=(-2) ); |
| 387 | assert( nArg>=(-1) || createFlag==0 ); |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 388 | h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a); |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 389 | |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 390 | /* First search for a match amongst the application-defined functions. |
| 391 | */ |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 392 | p = functionSearch(&db->aFunc, h, zName, nName); |
| 393 | while( p ){ |
| 394 | int score = matchQuality(p, nArg, enc); |
| 395 | if( score>bestScore ){ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 396 | pBest = p; |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 397 | bestScore = score; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 398 | } |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 399 | p = p->pNext; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 400 | } |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 401 | |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 402 | /* If no match is found, search the built-in functions. |
| 403 | ** |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 404 | ** If the SQLITE_PreferBuiltin flag is set, then search the built-in |
| 405 | ** functions even if a prior app-defined function was found. And give |
| 406 | ** priority to built-in functions. |
| 407 | ** |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 408 | ** Except, if createFlag is true, that means that we are trying to |
drh | 6c5cecb | 2010-09-16 19:49:22 +0000 | [diff] [blame] | 409 | ** install a new function. Whatever FuncDef structure is returned it will |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 410 | ** have fields overwritten with new information appropriate for the |
| 411 | ** new function. But the FuncDefs for built-in functions are read-only. |
| 412 | ** So we must not search for built-ins when creating a new function. |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 413 | */ |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 414 | if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 415 | FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 416 | bestScore = 0; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 417 | p = functionSearch(pHash, h, zName, nName); |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 418 | while( p ){ |
| 419 | int score = matchQuality(p, nArg, enc); |
| 420 | if( score>bestScore ){ |
| 421 | pBest = p; |
| 422 | bestScore = score; |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 423 | } |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 424 | p = p->pNext; |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 428 | /* If the createFlag parameter is true and the search did not reveal an |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 429 | ** exact match for the name, number of arguments and encoding, then add a |
| 430 | ** new entry to the hash table and return it. |
| 431 | */ |
drh | 89d5d6a | 2012-04-07 00:09:21 +0000 | [diff] [blame] | 432 | if( createFlag && bestScore<FUNC_PERFECT_MATCH && |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 433 | (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){ |
| 434 | pBest->zName = (char *)&pBest[1]; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 435 | pBest->nArg = (u16)nArg; |
drh | d36e104 | 2013-09-06 13:10:12 +0000 | [diff] [blame] | 436 | pBest->funcFlags = enc; |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 437 | memcpy(pBest->zName, zName, nName); |
| 438 | pBest->zName[nName] = 0; |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 439 | sqlite3FuncDefInsert(&db->aFunc, pBest); |
danielk1977 | fd9a0a4 | 2005-05-24 12:01:00 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){ |
| 443 | return pBest; |
| 444 | } |
| 445 | return 0; |
| 446 | } |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 447 | |
| 448 | /* |
| 449 | ** Free all resources held by the schema structure. The void* argument points |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 450 | ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the |
drh | b6ee660 | 2011-04-04 13:40:53 +0000 | [diff] [blame] | 451 | ** pointer itself, it just cleans up subsidiary resources (i.e. the contents |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 452 | ** of the schema hash tables). |
danielk1977 | 8cf6c55 | 2008-06-23 16:53:46 +0000 | [diff] [blame] | 453 | ** |
| 454 | ** The Schema.cache_size variable is not cleared. |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 455 | */ |
drh | b6ee660 | 2011-04-04 13:40:53 +0000 | [diff] [blame] | 456 | void sqlite3SchemaClear(void *p){ |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 457 | Hash temp1; |
| 458 | Hash temp2; |
| 459 | HashElem *pElem; |
| 460 | Schema *pSchema = (Schema *)p; |
| 461 | |
| 462 | temp1 = pSchema->tblHash; |
| 463 | temp2 = pSchema->trigHash; |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 464 | sqlite3HashInit(&pSchema->trigHash); |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 465 | sqlite3HashClear(&pSchema->idxHash); |
| 466 | for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 467 | sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem)); |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 468 | } |
| 469 | sqlite3HashClear(&temp2); |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 470 | sqlite3HashInit(&pSchema->tblHash); |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 471 | for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){ |
| 472 | Table *pTab = sqliteHashData(pElem); |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 473 | sqlite3DeleteTable(0, pTab); |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 474 | } |
| 475 | sqlite3HashClear(&temp1); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 476 | sqlite3HashClear(&pSchema->fkeyHash); |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 477 | pSchema->pSeqTab = 0; |
drh | 2c5e35f | 2014-08-05 11:04:21 +0000 | [diff] [blame] | 478 | if( pSchema->schemaFlags & DB_SchemaLoaded ){ |
drh | c2a7555 | 2011-03-18 21:55:46 +0000 | [diff] [blame] | 479 | pSchema->iGeneration++; |
drh | 2c5e35f | 2014-08-05 11:04:21 +0000 | [diff] [blame] | 480 | pSchema->schemaFlags &= ~DB_SchemaLoaded; |
drh | c2a7555 | 2011-03-18 21:55:46 +0000 | [diff] [blame] | 481 | } |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /* |
| 485 | ** Find and return the schema associated with a BTree. Create |
| 486 | ** a new one if necessary. |
| 487 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 488 | Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){ |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 489 | Schema * p; |
| 490 | if( pBt ){ |
drh | b6ee660 | 2011-04-04 13:40:53 +0000 | [diff] [blame] | 491 | p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear); |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 492 | }else{ |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 493 | p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema)); |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 494 | } |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 495 | if( !p ){ |
| 496 | db->mallocFailed = 1; |
| 497 | }else if ( 0==p->file_format ){ |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 498 | sqlite3HashInit(&p->tblHash); |
| 499 | sqlite3HashInit(&p->idxHash); |
| 500 | sqlite3HashInit(&p->trigHash); |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 501 | sqlite3HashInit(&p->fkeyHash); |
drh | f012ea3 | 2006-05-24 12:43:26 +0000 | [diff] [blame] | 502 | p->enc = SQLITE_UTF8; |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 503 | } |
| 504 | return p; |
| 505 | } |