callback.c 12 KB

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