| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2005 May 23 | 2 ** 2005 May 23 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 memcpy(pColl[0].zName, zName, nName); | 170 memcpy(pColl[0].zName, zName, nName); |
| 171 pColl[0].zName[nName] = 0; | 171 pColl[0].zName[nName] = 0; |
| 172 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl); | 172 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl); |
| 173 | 173 |
| 174 /* If a malloc() failure occurred in sqlite3HashInsert(), it will | 174 /* If a malloc() failure occurred in sqlite3HashInsert(), it will |
| 175 ** return the pColl pointer to be deleted (because it wasn't added | 175 ** return the pColl pointer to be deleted (because it wasn't added |
| 176 ** to the hash table). | 176 ** to the hash table). |
| 177 */ | 177 */ |
| 178 assert( pDel==0 || pDel==pColl ); | 178 assert( pDel==0 || pDel==pColl ); |
| 179 if( pDel!=0 ){ | 179 if( pDel!=0 ){ |
| 180 db->mallocFailed = 1; | 180 sqlite3OomFault(db); |
| 181 sqlite3DbFree(db, pDel); | 181 sqlite3DbFree(db, pDel); |
| 182 pColl = 0; | 182 pColl = 0; |
| 183 } | 183 } |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 return pColl; | 186 return pColl; |
| 187 } | 187 } |
| 188 | 188 |
| 189 /* | 189 /* |
| 190 ** Parameter zName points to a UTF-8 encoded string nName bytes long. | 190 ** Parameter zName points to a UTF-8 encoded string nName bytes long. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 ** The returned value is always between 0 and 6, as follows: | 236 ** The returned value is always between 0 and 6, as follows: |
| 237 ** | 237 ** |
| 238 ** 0: Not a match. | 238 ** 0: Not a match. |
| 239 ** 1: UTF8/16 conversion required and function takes any number of arguments. | 239 ** 1: UTF8/16 conversion required and function takes any number of arguments. |
| 240 ** 2: UTF16 byte order change required and function takes any number of args. | 240 ** 2: UTF16 byte order change required and function takes any number of args. |
| 241 ** 3: encoding matches and function takes any number of arguments | 241 ** 3: encoding matches and function takes any number of arguments |
| 242 ** 4: UTF8/16 conversion required - argument count matches exactly | 242 ** 4: UTF8/16 conversion required - argument count matches exactly |
| 243 ** 5: UTF16 byte order conversion required - argument count matches exactly | 243 ** 5: UTF16 byte order conversion required - argument count matches exactly |
| 244 ** 6: Perfect match: encoding and argument count match exactly. | 244 ** 6: Perfect match: encoding and argument count match exactly. |
| 245 ** | 245 ** |
| 246 ** If nArg==(-2) then any function with a non-null xStep or xFunc is | 246 ** If nArg==(-2) then any function with a non-null xSFunc is |
| 247 ** a perfect match and any function with both xStep and xFunc NULL is | 247 ** a perfect match and any function with xSFunc NULL is |
| 248 ** a non-match. | 248 ** a non-match. |
| 249 */ | 249 */ |
| 250 #define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */ | 250 #define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */ |
| 251 static int matchQuality( | 251 static int matchQuality( |
| 252 FuncDef *p, /* The function we are evaluating for match quality */ | 252 FuncDef *p, /* The function we are evaluating for match quality */ |
| 253 int nArg, /* Desired number of arguments. (-1)==any */ | 253 int nArg, /* Desired number of arguments. (-1)==any */ |
| 254 u8 enc /* Desired text encoding */ | 254 u8 enc /* Desired text encoding */ |
| 255 ){ | 255 ){ |
| 256 int match; | 256 int match; |
| 257 | 257 |
| 258 /* nArg of -2 is a special case */ | 258 /* nArg of -2 is a special case */ |
| 259 if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH; | 259 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH; |
| 260 | 260 |
| 261 /* Wrong number of arguments means "no match" */ | 261 /* Wrong number of arguments means "no match" */ |
| 262 if( p->nArg!=nArg && p->nArg>=0 ) return 0; | 262 if( p->nArg!=nArg && p->nArg>=0 ) return 0; |
| 263 | 263 |
| 264 /* Give a better score to a function with a specific number of arguments | 264 /* Give a better score to a function with a specific number of arguments |
| 265 ** than to function that accepts any number of arguments. */ | 265 ** than to function that accepts any number of arguments. */ |
| 266 if( p->nArg==nArg ){ | 266 if( p->nArg==nArg ){ |
| 267 match = 4; | 267 match = 4; |
| 268 }else{ | 268 }else{ |
| 269 match = 1; | 269 match = 1; |
| 270 } | 270 } |
| 271 | 271 |
| 272 /* Bonus points if the text encoding matches */ | 272 /* Bonus points if the text encoding matches */ |
| 273 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){ | 273 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){ |
| 274 match += 2; /* Exact encoding match */ | 274 match += 2; /* Exact encoding match */ |
| 275 }else if( (enc & p->funcFlags & 2)!=0 ){ | 275 }else if( (enc & p->funcFlags & 2)!=0 ){ |
| 276 match += 1; /* Both are UTF16, but with different byte orders */ | 276 match += 1; /* Both are UTF16, but with different byte orders */ |
| 277 } | 277 } |
| 278 | 278 |
| 279 return match; | 279 return match; |
| 280 } | 280 } |
| 281 | 281 |
| 282 /* | 282 /* |
| 283 ** Search a FuncDefHash for a function with the given name. Return | 283 ** Search a FuncDefHash for a function with the given name. Return |
| 284 ** a pointer to the matching FuncDef if found, or 0 if there is no match. | 284 ** a pointer to the matching FuncDef if found, or 0 if there is no match. |
| 285 */ | 285 */ |
| 286 static FuncDef *functionSearch( | 286 static FuncDef *functionSearch( |
| 287 FuncDefHash *pHash, /* Hash table to search */ | |
| 288 int h, /* Hash of the name */ | 287 int h, /* Hash of the name */ |
| 289 const char *zFunc, /* Name of function */ | 288 const char *zFunc /* Name of function */ |
| 290 int nFunc /* Number of bytes in zFunc */ | |
| 291 ){ | 289 ){ |
| 292 FuncDef *p; | 290 FuncDef *p; |
| 293 for(p=pHash->a[h]; p; p=p->pHash){ | 291 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){ |
| 294 if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){ | 292 if( sqlite3StrICmp(p->zName, zFunc)==0 ){ |
| 295 return p; | 293 return p; |
| 296 } | 294 } |
| 297 } | 295 } |
| 298 return 0; | 296 return 0; |
| 299 } | 297 } |
| 300 | 298 |
| 301 /* | 299 /* |
| 302 ** Insert a new FuncDef into a FuncDefHash hash table. | 300 ** Insert a new FuncDef into a FuncDefHash hash table. |
| 303 */ | 301 */ |
| 304 void sqlite3FuncDefInsert( | 302 void sqlite3InsertBuiltinFuncs( |
| 305 FuncDefHash *pHash, /* The hash table into which to insert */ | 303 FuncDef *aDef, /* List of global functions to be inserted */ |
| 306 FuncDef *pDef /* The function definition to insert */ | 304 int nDef /* Length of the apDef[] list */ |
| 307 ){ | 305 ){ |
| 308 FuncDef *pOther; | 306 int i; |
| 309 int nName = sqlite3Strlen30(pDef->zName); | 307 for(i=0; i<nDef; i++){ |
| 310 u8 c1 = (u8)pDef->zName[0]; | 308 FuncDef *pOther; |
| 311 int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a); | 309 const char *zName = aDef[i].zName; |
| 312 pOther = functionSearch(pHash, h, pDef->zName, nName); | 310 int nName = sqlite3Strlen30(zName); |
| 313 if( pOther ){ | 311 int h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % SQLITE_FUNC_HASH_SZ; |
| 314 assert( pOther!=pDef && pOther->pNext!=pDef ); | 312 pOther = functionSearch(h, zName); |
| 315 pDef->pNext = pOther->pNext; | 313 if( pOther ){ |
| 316 pOther->pNext = pDef; | 314 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] ); |
| 317 }else{ | 315 aDef[i].pNext = pOther->pNext; |
| 318 pDef->pNext = 0; | 316 pOther->pNext = &aDef[i]; |
| 319 pDef->pHash = pHash->a[h]; | 317 }else{ |
| 320 pHash->a[h] = pDef; | 318 aDef[i].pNext = 0; |
| 319 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h]; |
| 320 sqlite3BuiltinFunctions.a[h] = &aDef[i]; |
| 321 } |
| 321 } | 322 } |
| 322 } | 323 } |
| 323 | 324 |
| 324 | 325 |
| 325 | 326 |
| 326 /* | 327 /* |
| 327 ** Locate a user function given a name, a number of arguments and a flag | 328 ** Locate a user function given a name, a number of arguments and a flag |
| 328 ** indicating whether the function prefers UTF-16 over UTF-8. Return a | 329 ** indicating whether the function prefers UTF-16 over UTF-8. Return a |
| 329 ** pointer to the FuncDef structure that defines that function, or return | 330 ** pointer to the FuncDef structure that defines that function, or return |
| 330 ** NULL if the function does not exist. | 331 ** NULL if the function does not exist. |
| 331 ** | 332 ** |
| 332 ** If the createFlag argument is true, then a new (blank) FuncDef | 333 ** If the createFlag argument is true, then a new (blank) FuncDef |
| 333 ** structure is created and liked into the "db" structure if a | 334 ** structure is created and liked into the "db" structure if a |
| 334 ** no matching function previously existed. | 335 ** no matching function previously existed. |
| 335 ** | 336 ** |
| 336 ** If nArg is -2, then the first valid function found is returned. A | 337 ** If nArg is -2, then the first valid function found is returned. A |
| 337 ** function is valid if either xFunc or xStep is non-zero. The nArg==(-2) | 338 ** function is valid if xSFunc is non-zero. The nArg==(-2) |
| 338 ** case is used to see if zName is a valid function name for some number | 339 ** case is used to see if zName is a valid function name for some number |
| 339 ** of arguments. If nArg is -2, then createFlag must be 0. | 340 ** of arguments. If nArg is -2, then createFlag must be 0. |
| 340 ** | 341 ** |
| 341 ** If createFlag is false, then a function with the required name and | 342 ** If createFlag is false, then a function with the required name and |
| 342 ** number of arguments may be returned even if the eTextRep flag does not | 343 ** number of arguments may be returned even if the eTextRep flag does not |
| 343 ** match that requested. | 344 ** match that requested. |
| 344 */ | 345 */ |
| 345 FuncDef *sqlite3FindFunction( | 346 FuncDef *sqlite3FindFunction( |
| 346 sqlite3 *db, /* An open database */ | 347 sqlite3 *db, /* An open database */ |
| 347 const char *zName, /* Name of the function. Not null-terminated */ | 348 const char *zName, /* Name of the function. zero-terminated */ |
| 348 int nName, /* Number of characters in the name */ | |
| 349 int nArg, /* Number of arguments. -1 means any number */ | 349 int nArg, /* Number of arguments. -1 means any number */ |
| 350 u8 enc, /* Preferred text encoding */ | 350 u8 enc, /* Preferred text encoding */ |
| 351 u8 createFlag /* Create new entry if true and does not otherwise exist */ | 351 u8 createFlag /* Create new entry if true and does not otherwise exist */ |
| 352 ){ | 352 ){ |
| 353 FuncDef *p; /* Iterator variable */ | 353 FuncDef *p; /* Iterator variable */ |
| 354 FuncDef *pBest = 0; /* Best match found so far */ | 354 FuncDef *pBest = 0; /* Best match found so far */ |
| 355 int bestScore = 0; /* Score of best match */ | 355 int bestScore = 0; /* Score of best match */ |
| 356 int h; /* Hash value */ | 356 int h; /* Hash value */ |
| 357 int nName; /* Length of the name */ |
| 357 | 358 |
| 358 assert( nArg>=(-2) ); | 359 assert( nArg>=(-2) ); |
| 359 assert( nArg>=(-1) || createFlag==0 ); | 360 assert( nArg>=(-1) || createFlag==0 ); |
| 360 h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a); | 361 nName = sqlite3Strlen30(zName); |
| 361 | 362 |
| 362 /* First search for a match amongst the application-defined functions. | 363 /* First search for a match amongst the application-defined functions. |
| 363 */ | 364 */ |
| 364 p = functionSearch(&db->aFunc, h, zName, nName); | 365 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName); |
| 365 while( p ){ | 366 while( p ){ |
| 366 int score = matchQuality(p, nArg, enc); | 367 int score = matchQuality(p, nArg, enc); |
| 367 if( score>bestScore ){ | 368 if( score>bestScore ){ |
| 368 pBest = p; | 369 pBest = p; |
| 369 bestScore = score; | 370 bestScore = score; |
| 370 } | 371 } |
| 371 p = p->pNext; | 372 p = p->pNext; |
| 372 } | 373 } |
| 373 | 374 |
| 374 /* If no match is found, search the built-in functions. | 375 /* If no match is found, search the built-in functions. |
| 375 ** | 376 ** |
| 376 ** If the SQLITE_PreferBuiltin flag is set, then search the built-in | 377 ** If the SQLITE_PreferBuiltin flag is set, then search the built-in |
| 377 ** functions even if a prior app-defined function was found. And give | 378 ** functions even if a prior app-defined function was found. And give |
| 378 ** priority to built-in functions. | 379 ** priority to built-in functions. |
| 379 ** | 380 ** |
| 380 ** Except, if createFlag is true, that means that we are trying to | 381 ** Except, if createFlag is true, that means that we are trying to |
| 381 ** install a new function. Whatever FuncDef structure is returned it will | 382 ** install a new function. Whatever FuncDef structure is returned it will |
| 382 ** have fields overwritten with new information appropriate for the | 383 ** have fields overwritten with new information appropriate for the |
| 383 ** new function. But the FuncDefs for built-in functions are read-only. | 384 ** new function. But the FuncDefs for built-in functions are read-only. |
| 384 ** So we must not search for built-ins when creating a new function. | 385 ** So we must not search for built-ins when creating a new function. |
| 385 */ | 386 */ |
| 386 if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){ | 387 if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){ |
| 387 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); | |
| 388 bestScore = 0; | 388 bestScore = 0; |
| 389 p = functionSearch(pHash, h, zName, nName); | 389 h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % SQLITE_FUNC_HASH_SZ; |
| 390 p = functionSearch(h, zName); |
| 390 while( p ){ | 391 while( p ){ |
| 391 int score = matchQuality(p, nArg, enc); | 392 int score = matchQuality(p, nArg, enc); |
| 392 if( score>bestScore ){ | 393 if( score>bestScore ){ |
| 393 pBest = p; | 394 pBest = p; |
| 394 bestScore = score; | 395 bestScore = score; |
| 395 } | 396 } |
| 396 p = p->pNext; | 397 p = p->pNext; |
| 397 } | 398 } |
| 398 } | 399 } |
| 399 | 400 |
| 400 /* If the createFlag parameter is true and the search did not reveal an | 401 /* If the createFlag parameter is true and the search did not reveal an |
| 401 ** exact match for the name, number of arguments and encoding, then add a | 402 ** exact match for the name, number of arguments and encoding, then add a |
| 402 ** new entry to the hash table and return it. | 403 ** new entry to the hash table and return it. |
| 403 */ | 404 */ |
| 404 if( createFlag && bestScore<FUNC_PERFECT_MATCH && | 405 if( createFlag && bestScore<FUNC_PERFECT_MATCH && |
| 405 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){ | 406 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){ |
| 406 pBest->zName = (char *)&pBest[1]; | 407 FuncDef *pOther; |
| 408 pBest->zName = (const char*)&pBest[1]; |
| 407 pBest->nArg = (u16)nArg; | 409 pBest->nArg = (u16)nArg; |
| 408 pBest->funcFlags = enc; | 410 pBest->funcFlags = enc; |
| 409 memcpy(pBest->zName, zName, nName); | 411 memcpy((char*)&pBest[1], zName, nName+1); |
| 410 pBest->zName[nName] = 0; | 412 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest); |
| 411 sqlite3FuncDefInsert(&db->aFunc, pBest); | 413 if( pOther==pBest ){ |
| 414 sqlite3DbFree(db, pBest); |
| 415 sqlite3OomFault(db); |
| 416 return 0; |
| 417 }else{ |
| 418 pBest->pNext = pOther; |
| 419 } |
| 412 } | 420 } |
| 413 | 421 |
| 414 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){ | 422 if( pBest && (pBest->xSFunc || createFlag) ){ |
| 415 return pBest; | 423 return pBest; |
| 416 } | 424 } |
| 417 return 0; | 425 return 0; |
| 418 } | 426 } |
| 419 | 427 |
| 420 /* | 428 /* |
| 421 ** Free all resources held by the schema structure. The void* argument points | 429 ** Free all resources held by the schema structure. The void* argument points |
| 422 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the | 430 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the |
| 423 ** pointer itself, it just cleans up subsidiary resources (i.e. the contents | 431 ** pointer itself, it just cleans up subsidiary resources (i.e. the contents |
| 424 ** of the schema hash tables). | 432 ** of the schema hash tables). |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 ** a new one if necessary. | 466 ** a new one if necessary. |
| 459 */ | 467 */ |
| 460 Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){ | 468 Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){ |
| 461 Schema * p; | 469 Schema * p; |
| 462 if( pBt ){ | 470 if( pBt ){ |
| 463 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear); | 471 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear); |
| 464 }else{ | 472 }else{ |
| 465 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema)); | 473 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema)); |
| 466 } | 474 } |
| 467 if( !p ){ | 475 if( !p ){ |
| 468 db->mallocFailed = 1; | 476 sqlite3OomFault(db); |
| 469 }else if ( 0==p->file_format ){ | 477 }else if ( 0==p->file_format ){ |
| 470 sqlite3HashInit(&p->tblHash); | 478 sqlite3HashInit(&p->tblHash); |
| 471 sqlite3HashInit(&p->idxHash); | 479 sqlite3HashInit(&p->idxHash); |
| 472 sqlite3HashInit(&p->trigHash); | 480 sqlite3HashInit(&p->trigHash); |
| 473 sqlite3HashInit(&p->fkeyHash); | 481 sqlite3HashInit(&p->fkeyHash); |
| 474 p->enc = SQLITE_UTF8; | 482 p->enc = SQLITE_UTF8; |
| 475 } | 483 } |
| 476 return p; | 484 return p; |
| 477 } | 485 } |
| OLD | NEW |