Debian Codegraph @

ext/intck/sqlite3intck.c

raw
1 /*
2 ** 2024-02-08
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
14 #include "sqlite3intck.h"
15 #include <string.h>
16 #include <assert.h>
17
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 /*
22 ** nKeyVal:
23 ** The number of values that make up the 'key' for the current pCheck
24 ** statement.
25 **
26 ** rc:
27 ** Error code returned by most recent sqlite3_intck_step() or
28 ** sqlite3_intck_unlock() call. This is set to SQLITE_DONE when
29 ** the integrity-check operation is finished.
30 **
31 ** zErr:
32 ** If the object has entered the error state, this is the error message.
33 ** Is freed using sqlite3_free() when the object is deleted.
34 **
35 ** zTestSql:
36 ** The value returned by the most recent call to sqlite3_intck_testsql().
37 ** Each call to testsql() frees the previous zTestSql value (using
38 ** sqlite3_free()) and replaces it with the new value it will return.
39 */
40 struct sqlite3_intck {
41 sqlite3 *db;
42 const char *zDb; /* Copy of zDb parameter to _open() */
43 char *zObj; /* Current object. Or NULL. */
44
45 sqlite3_stmt *pCheck; /* Current check statement */
46 char *zKey;
47 int nKeyVal;
48
49 char *zMessage;
50 int bCorruptSchema;
51
52 int rc; /* Error code */
53 char *zErr; /* Error message */
54 char *zTestSql; /* Returned by sqlite3_intck_test_sql() */
55 };
56
57
58 /*
59 ** Some error has occurred while using database p->db. Save the error message
60 ** and error code currently held by the database handle in p->rc and p->zErr.
61 */
62 static void intckSaveErrmsg(sqlite3_intck *p){
63 p->rc = sqlite3_errcode(p->db);
64 sqlite3_free(p->zErr);
65 p->zErr = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
66 }
67
68 /*
69 ** If the handle passed as the first argument is already in the error state,
70 ** then this function is a no-op (returns NULL immediately). Otherwise, if an
71 ** error occurs within this function, it leaves an error in said handle.
72 **
73 ** Otherwise, this function attempts to prepare SQL statement zSql and
74 ** return the resulting statement handle to the user.
75 */
76 static sqlite3_stmt *intckPrepare(sqlite3_intck *p, const char *zSql){
77 sqlite3_stmt *pRet = 0;
78 if( p->rc==SQLITE_OK ){
79 p->rc = sqlite3_prepare_v2(p->db, zSql, -1, &pRet, 0);
80 if( p->rc!=SQLITE_OK ){
81 intckSaveErrmsg(p);
82 assert( pRet==0 );
83 }
84 }
85 return pRet;
86 }
87
88 /*
89 ** If the handle passed as the first argument is already in the error state,
90 ** then this function is a no-op (returns NULL immediately). Otherwise, if an
91 ** error occurs within this function, it leaves an error in said handle.
92 **
93 ** Otherwise, this function treats argument zFmt as a printf() style format
94 ** string. It formats it according to the trailing arguments and then
95 ** attempts to prepare the results and return the resulting prepared
96 ** statement.
97 */
98 static sqlite3_stmt *intckPrepareFmt(sqlite3_intck *p, const char *zFmt, ...){
99 sqlite3_stmt *pRet = 0;
100 va_list ap;
101 char *zSql = 0;
102 va_start(ap, zFmt);
103 zSql = sqlite3_vmprintf(zFmt, ap);
104 if( p->rc==SQLITE_OK && zSql==0 ){
105 p->rc = SQLITE_NOMEM;
106 }
107 pRet = intckPrepare(p, zSql);
108 sqlite3_free(zSql);
109 va_end(ap);
110 return pRet;
111 }
112
113 /*
114 ** Finalize SQL statement pStmt. If an error occurs and the handle passed
115 ** as the first argument does not already contain an error, store the
116 ** error in the handle.
117 */
118 static void intckFinalize(sqlite3_intck *p, sqlite3_stmt *pStmt){
119 int rc = sqlite3_finalize(pStmt);
120 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
121 intckSaveErrmsg(p);
122 }
123 }
124
125 /*
126 ** If there is already an error in handle p, return it. Otherwise, call
127 ** sqlite3_step() on the statement handle and return that value.
128 */
129 static int intckStep(sqlite3_intck *p, sqlite3_stmt *pStmt){
130 if( p->rc ) return p->rc;
131 return sqlite3_step(pStmt);
132 }
133
134 /*
135 ** Execute SQL statement zSql. There is no way to obtain any results
136 ** returned by the statement. This function uses the sqlite3_intck error
137 ** code convention.
138 */
139 static void intckExec(sqlite3_intck *p, const char *zSql){
140 sqlite3_stmt *pStmt = 0;
141 pStmt = intckPrepare(p, zSql);
142 intckStep(p, pStmt);
143 intckFinalize(p, pStmt);
144 }
145
146 /*
147 ** A wrapper around sqlite3_mprintf() that uses the sqlite3_intck error
148 ** code convention.
149 */
150 static char *intckMprintf(sqlite3_intck *p, const char *zFmt, ...){
151 va_list ap;
152 char *zRet = 0;
153 va_start(ap, zFmt);
154 zRet = sqlite3_vmprintf(zFmt, ap);
155 if( p->rc==SQLITE_OK ){
156 if( zRet==0 ){
157 p->rc = SQLITE_NOMEM;
158 }
159 }else{
160 sqlite3_free(zRet);
161 zRet = 0;
162 }
163 va_end(ap);
164 return zRet;
165 }
166
167 /*
168 ** This is used by sqlite3_intck_unlock() to save the vector key value
169 ** required to restart the current pCheck query as a nul-terminated string
170 ** in p->zKey.
171 */
172 static void intckSaveKey(sqlite3_intck *p){
173 int ii;
174 char *zSql = 0;
175 sqlite3_stmt *pStmt = 0;
176 sqlite3_stmt *pXinfo = 0;
177 const char *zDir = 0;
178
179 assert( p->pCheck );
180 assert( p->zKey==0 );
181
182 pXinfo = intckPrepareFmt(p,
183 "SELECT group_concat(desc, '') FROM %Q.sqlite_schema s, "
184 "pragma_index_xinfo(%Q, %Q) "
185 "WHERE s.type='index' AND s.name=%Q",
186 p->zDb, p->zObj, p->zDb, p->zObj
187 );
188 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXinfo) ){
189 zDir = (const char*)sqlite3_column_text(pXinfo, 0);
190 }
191
192 if( zDir==0 ){
193 /* Object is a table, not an index. This is the easy case,as there are
194 ** no DESC columns or NULL values in a primary key. */
195 const char *zSep = "SELECT '(' || ";
196 for(ii=0; ii<p->nKeyVal; ii++){
197 zSql = intckMprintf(p, "%z%squote(?)", zSql, zSep);
198 zSep = " || ', ' || ";
199 }
200 zSql = intckMprintf(p, "%z || ')'", zSql);
201 }else{
202
203 /* Object is an index. */
204 assert( p->nKeyVal>1 );
205 for(ii=p->nKeyVal; ii>0; ii--){
206 int bLastIsDesc = zDir[ii-1]=='1';
207 int bLastIsNull = sqlite3_column_type(p->pCheck, ii)==SQLITE_NULL;
208 const char *zLast = sqlite3_column_name(p->pCheck, ii);
209 char *zLhs = 0;
210 char *zRhs = 0;
211 char *zWhere = 0;
212
213 if( bLastIsNull ){
214 if( bLastIsDesc ) continue;
215 zWhere = intckMprintf(p, "'%s IS NOT NULL'", zLast);
216 }else{
217 const char *zOp = bLastIsDesc ? "<" : ">";
218 zWhere = intckMprintf(p, "'%s %s ' || quote(?%d)", zLast, zOp, ii);
219 }
220
221 if( ii>1 ){
222 const char *zLhsSep = "";
223 const char *zRhsSep = "";
224 int jj;
225 for(jj=0; jj<ii-1; jj++){
226 const char *zAlias = (const char*)sqlite3_column_name(p->pCheck,jj+1);
227 zLhs = intckMprintf(p, "%z%s%s", zLhs, zLhsSep, zAlias);
228 zRhs = intckMprintf(p, "%z%squote(?%d)", zRhs, zRhsSep, jj+1);
229 zLhsSep = ",";
230 zRhsSep = " || ',' || ";
231 }
232
233 zWhere = intckMprintf(p,
234 "'(%z) IS (' || %z || ') AND ' || %z",
235 zLhs, zRhs, zWhere);
236 }
237 zWhere = intckMprintf(p, "'WHERE ' || %z", zWhere);
238
239 zSql = intckMprintf(p, "%z%s(quote( %z ) )",
240 zSql,
241 (zSql==0 ? "VALUES" : ",\n "),
242 zWhere
243 );
244 }
245 zSql = intckMprintf(p,
246 "WITH wc(q) AS (\n%z\n)"
247 "SELECT 'VALUES' || group_concat('(' || q || ')', ',\n ') FROM wc"
248 , zSql
249 );
250 }
251
252 pStmt = intckPrepare(p, zSql);
253 if( p->rc==SQLITE_OK ){
254 for(ii=0; ii<p->nKeyVal; ii++){
255 sqlite3_bind_value(pStmt, ii+1, sqlite3_column_value(p->pCheck, ii+1));
256 }
257 if( SQLITE_ROW==sqlite3_step(pStmt) ){
258 p->zKey = intckMprintf(p,"%s",(const char*)sqlite3_column_text(pStmt, 0));
259 }
260 intckFinalize(p, pStmt);
261 }
262
263 sqlite3_free(zSql);
264 intckFinalize(p, pXinfo);
265 }
266
267 /*
268 ** Find the next database object (table or index) to check. If successful,
269 ** set sqlite3_intck.zObj to point to a nul-terminated buffer containing
270 ** the object's name before returning.
271 */
272 static void intckFindObject(sqlite3_intck *p){
273 sqlite3_stmt *pStmt = 0;
274 char *zPrev = p->zObj;
275 p->zObj = 0;
276
277 assert( p->rc==SQLITE_OK );
278 assert( p->pCheck==0 );
279
280 pStmt = intckPrepareFmt(p,
281 "WITH tables(table_name) AS ("
282 " SELECT name"
283 " FROM %Q.sqlite_schema WHERE (type='table' OR type='index') AND rootpage"
284 " UNION ALL "
285 " SELECT 'sqlite_schema'"
286 ")"
287 "SELECT table_name FROM tables "
288 "WHERE ?1 IS NULL OR table_name%s?1 "
289 "ORDER BY 1"
290 , p->zDb, (p->zKey ? ">=" : ">")
291 );
292
293 if( p->rc==SQLITE_OK ){
294 sqlite3_bind_text(pStmt, 1, zPrev, -1, SQLITE_TRANSIENT);
295 if( sqlite3_step(pStmt)==SQLITE_ROW ){
296 p->zObj = intckMprintf(p,"%s",(const char*)sqlite3_column_text(pStmt, 0));
297 }
298 }
299 intckFinalize(p, pStmt);
300
301 /* If this is a new object, ensure the previous key value is cleared. */
302 if( sqlite3_stricmp(p->zObj, zPrev) ){
303 sqlite3_free(p->zKey);
304 p->zKey = 0;
305 }
306
307 sqlite3_free(zPrev);
308 }
309
310 /*
311 ** Return the size in bytes of the first token in nul-terminated buffer z.
312 ** For the purposes of this call, a token is either:
313 **
314 ** * a quoted SQL string,
315 * * a contiguous series of ascii alphabet characters, or
316 * * any other single byte.
317 */
318 static int intckGetToken(const char *z){
319 char c = z[0];
320 int iRet = 1;
321 if( c=='\'' || c=='"' || c=='`' ){
322 while( z[iRet] ){
323 if( z[iRet]==c ){
324 iRet++;
325 if( z[iRet]!=c ) break;
326 }
327 iRet++;
328 }
329 }
330 else if( c=='[' ){
331 while( z[iRet++]!=']' && z[iRet] );
332 }
333 else if( (c>='A' && c<='Z') || (c>='a' && c<='z') ){
334 while( (z[iRet]>='A' && z[iRet]<='Z') || (z[iRet]>='a' && z[iRet]<='z') ){
335 iRet++;
336 }
337 }
338
339 return iRet;
340 }
341
342 /*
343 ** Return true if argument c is an ascii whitespace character.
344 */
345 static int intckIsSpace(char c){
346 return (c==' ' || c=='\t' || c=='\n' || c=='\r');
347 }
348
349 /*
350 ** Argument z points to the text of a CREATE INDEX statement. This function
351 ** identifies the part of the text that contains either the index WHERE
352 ** clause (if iCol<0) or the iCol'th column of the index.
353 **
354 ** If (iCol<0), the identified fragment does not include the "WHERE" keyword,
355 ** only the expression that follows it. If (iCol>=0) then the identified
356 ** fragment does not include any trailing sort-order keywords - "ASC" or
357 ** "DESC".
358 **
359 ** If the CREATE INDEX statement does not contain the requested field or
360 ** clause, NULL is returned and (*pnByte) is set to 0. Otherwise, a pointer to
361 ** the identified fragment is returned and output parameter (*pnByte) set
362 ** to its size in bytes.
363 */
364 static const char *intckParseCreateIndex(const char *z, int iCol, int *pnByte){
365 int iOff = 0;
366 int iThisCol = 0;
367 int iStart = 0;
368 int nOpen = 0;
369
370 const char *zRet = 0;
371 int nRet = 0;
372
373 int iEndOfCol = 0;
374
375 /* Skip forward until the first "(" token */
376 while( z[iOff]!='(' ){
377 iOff += intckGetToken(&z[iOff]);
378 if( z[iOff]=='\0' ) return 0;
379 }
380 assert( z[iOff]=='(' );
381
382 nOpen = 1;
383 iOff++;
384 iStart = iOff;
385 while( z[iOff] ){
386 const char *zToken = &z[iOff];
387 int nToken = 0;
388
389 /* Check if this is the end of the current column - either a "," or ")"
390 ** when nOpen==1. */
391 if( nOpen==1 ){
392 if( z[iOff]==',' || z[iOff]==')' ){
393 if( iCol==iThisCol ){
394 int iEnd = iEndOfCol ? iEndOfCol : iOff;
395 nRet = (iEnd - iStart);
396 zRet = &z[iStart];
397 break;
398 }
399 iStart = iOff+1;
400 while( intckIsSpace(z[iStart]) ) iStart++;
401 iThisCol++;
402 }
403 if( z[iOff]==')' ) break;
404 }
405 if( z[iOff]=='(' ) nOpen++;
406 if( z[iOff]==')' ) nOpen--;
407 nToken = intckGetToken(zToken);
408
409 if( (nToken==3 && 0==sqlite3_strnicmp(zToken, "ASC", nToken))
410 || (nToken==4 && 0==sqlite3_strnicmp(zToken, "DESC", nToken))
411 ){
412 iEndOfCol = iOff;
413 }else if( 0==intckIsSpace(zToken[0]) ){
414 iEndOfCol = 0;
415 }
416
417 iOff += nToken;
418 }
419
420 /* iStart is now the byte offset of 1 byte passed the final ')' in the
421 ** CREATE INDEX statement. Try to find a WHERE clause to return. */
422 while( zRet==0 && z[iOff] ){
423 int n = intckGetToken(&z[iOff]);
424 if( n==5 && 0==sqlite3_strnicmp(&z[iOff], "where", 5) ){
425 zRet = &z[iOff+5];
426 nRet = (int)strlen(zRet);
427 }
428 iOff += n;
429 }
430
431 /* Trim any whitespace from the start and end of the returned string. */
432 if( zRet ){
433 while( intckIsSpace(zRet[0]) ){
434 nRet--;
435 zRet++;
436 }
437 while( nRet>0 && intckIsSpace(zRet[nRet-1]) ) nRet--;
438 }
439
440 *pnByte = nRet;
441 return zRet;
442 }
443
444 /*
445 ** User-defined SQL function wrapper for intckParseCreateIndex():
446 **
447 ** SELECT parse_create_index(<sql>, <icol>);
448 */
449 static void intckParseCreateIndexFunc(
450 sqlite3_context *pCtx,
451 int nVal,
452 sqlite3_value **apVal
453 ){
454 const char *zSql = (const char*)sqlite3_value_text(apVal[0]);
455 int idx = sqlite3_value_int(apVal[1]);
456 const char *zRes = 0;
457 int nRes = 0;
458
459 assert( nVal==2 );
460 if( zSql ){
461 zRes = intckParseCreateIndex(zSql, idx, &nRes);
462 }
463 sqlite3_result_text(pCtx, zRes, nRes, SQLITE_TRANSIENT);
464 }
465
466 /*
467 ** Return true if sqlite3_intck.db has automatic indexes enabled, false
468 ** otherwise.
469 */
470 static int intckGetAutoIndex(sqlite3_intck *p){
471 int bRet = 0;
472 sqlite3_stmt *pStmt = 0;
473 pStmt = intckPrepare(p, "PRAGMA automatic_index");
474 if( SQLITE_ROW==intckStep(p, pStmt) ){
475 bRet = sqlite3_column_int(pStmt, 0);
476 }
477 intckFinalize(p, pStmt);
478 return bRet;
479 }
480
481 /*
482 ** Return true if zObj is an index, or false otherwise.
483 */
484 static int intckIsIndex(sqlite3_intck *p, const char *zObj){
485 int bRet = 0;
486 sqlite3_stmt *pStmt = 0;
487 pStmt = intckPrepareFmt(p,
488 "SELECT 1 FROM %Q.sqlite_schema WHERE name=%Q AND type='index'",
489 p->zDb, zObj
490 );
491 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
492 bRet = 1;
493 }
494 intckFinalize(p, pStmt);
495 return bRet;
496 }
497
498 /*
499 ** Return a pointer to a nul-terminated buffer containing the SQL statement
500 ** used to check database object zObj (a table or index) for corruption.
501 ** If parameter zPrev is not NULL, then it must be a string containing the
502 ** vector key required to restart the check where it left off last time.
503 ** If pnKeyVal is not NULL, then (*pnKeyVal) is set to the number of
504 ** columns in the vector key value for the specified object.
505 **
506 ** This function uses the sqlite3_intck error code convention.
507 */
508 static char *intckCheckObjectSql(
509 sqlite3_intck *p, /* Integrity check object */
510 const char *zObj, /* Object (table or index) to scan */
511 const char *zPrev, /* Restart key vector, if any */
512 int *pnKeyVal /* OUT: Number of key-values for this scan */
513 ){
514 char *zRet = 0;
515 sqlite3_stmt *pStmt = 0;
516 int bAutoIndex = 0;
517 int bIsIndex = 0;
518
519 const char *zCommon =
520 /* Relation without_rowid also contains just one row. Column "b" is
521 ** set to true if the table being examined is a WITHOUT ROWID table,
522 ** or false otherwise. */
523 ", without_rowid(b) AS ("
524 " SELECT EXISTS ("
525 " SELECT 1 FROM tabname, pragma_index_list(tab, db) AS l"
526 " WHERE origin='pk' "
527 " AND NOT EXISTS (SELECT 1 FROM sqlite_schema WHERE name=l.name)"
528 " )"
529 ")"
530 ""
531 /* Table idx_cols contains 1 row for each column in each index on the
532 ** table being checked. Columns are:
533 **
534 ** idx_name: Name of the index.
535 ** idx_ispk: True if this index is the PK of a WITHOUT ROWID table.
536 ** col_name: Name of indexed column, or NULL for index on expression.
537 ** col_expr: Indexed expression, including COLLATE clause.
538 ** col_alias: Alias used for column in 'intck_wrapper' table.
539 */
540 ", idx_cols(idx_name, idx_ispk, col_name, col_expr, col_alias) AS ("
541 " SELECT l.name, (l.origin=='pk' AND w.b), i.name, COALESCE(("
542 " SELECT parse_create_index(sql, i.seqno) FROM "
543 " sqlite_schema WHERE name = l.name"
544 " ), format('\"%w\"', i.name) || ' COLLATE ' || quote(i.coll)),"
545 " 'c' || row_number() OVER ()"
546 " FROM "
547 " tabname t,"
548 " without_rowid w,"
549 " pragma_index_list(t.tab, t.db) l,"
550 " pragma_index_xinfo(l.name) i"
551 " WHERE i.key"
552 " UNION ALL"
553 " SELECT '', 1, '_rowid_', '_rowid_', 'r1' FROM without_rowid WHERE b=0"
554 ")"
555 ""
556 ""
557 /*
558 ** For a PK declared as "PRIMARY KEY(a, b) ... WITHOUT ROWID", where
559 ** the intck_wrapper aliases of "a" and "b" are "c1" and "c2":
560 **
561 ** o_pk: "o.c1, o.c2"
562 ** i_pk: "i.'a', i.'b'"
563 ** ...
564 ** n_pk: 2
565 */
566 ", tabpk(db, tab, idx, o_pk, i_pk, q_pk, eq_pk, ps_pk, pk_pk, n_pk) AS ("
567 " WITH pkfields(f, a) AS ("
568 " SELECT i.col_name, i.col_alias FROM idx_cols i WHERE i.idx_ispk"
569 " )"
570 " SELECT t.db, t.tab, t.idx, "
571 " group_concat(a, ', '), "
572 " group_concat('i.'||quote(f), ', '), "
573 " group_concat('quote(o.'||a||')', ' || '','' || '), "
574 " format('(%s)==(%s)',"
575 " group_concat('o.'||a, ', '), "
576 " group_concat(format('\"%w\"', f), ', ')"
577 " ),"
578 " group_concat('%s', ','),"
579 " group_concat('quote('||a||')', ', '), "
580 " count(*)"
581 " FROM tabname t, pkfields"
582 ")"
583 ""
584 ", idx(name, match_expr, partial, partial_alias, idx_ps, idx_idx) AS ("
585 " SELECT idx_name,"
586 " format('(%s,%s) IS (%s,%s)', "
587 " group_concat(i.col_expr, ', '), i_pk,"
588 " group_concat('o.'||i.col_alias, ', '), o_pk"
589 " ), "
590 " parse_create_index("
591 " (SELECT sql FROM sqlite_schema WHERE name=idx_name), -1"
592 " ),"
593 " 'cond' || row_number() OVER ()"
594 " , group_concat('%s', ',')"
595 " , group_concat('quote('||i.col_alias||')', ', ')"
596 " FROM tabpk t, "
597 " without_rowid w,"
598 " idx_cols i"
599 " WHERE i.idx_ispk==0 "
600 " GROUP BY idx_name"
601 ")"
602 ""
603 ", wrapper_with(s) AS ("
604 " SELECT 'intck_wrapper AS (\n SELECT\n ' || ("
605 " WITH f(a, b) AS ("
606 " SELECT col_expr, col_alias FROM idx_cols"
607 " UNION ALL "
608 " SELECT partial, partial_alias FROM idx WHERE partial IS NOT NULL"
609 " )"
610 " SELECT group_concat(format('%s AS %s', a, b), ',\n ') FROM f"
611 " )"
612 " || format('\n FROM %Q.%Q ', t.db, t.tab)"
613 /* If the object being checked is a table, append "NOT INDEXED".
614 ** Otherwise, append "INDEXED BY <index>", and then, if the index
615 ** is a partial index " WHERE <condition>". */
616 " || CASE WHEN t.idx IS NULL THEN "
617 " 'NOT INDEXED'"
618 " ELSE"
619 " format('INDEXED BY %Q%s', t.idx, ' WHERE '||i.partial)"
620 " END"
621 " || '\n)'"
622 " FROM tabname t LEFT JOIN idx i ON (i.name=t.idx)"
623 ")"
624 ""
625 ;
626
627 bAutoIndex = intckGetAutoIndex(p);
628 if( bAutoIndex ) intckExec(p, "PRAGMA automatic_index = 0");
629
630 bIsIndex = intckIsIndex(p, zObj);
631 if( bIsIndex ){
632 pStmt = intckPrepareFmt(p,
633 /* Table idxname contains a single row. The first column, "db", contains
634 ** the name of the db containing the table (e.g. "main") and the second,
635 ** "tab", the name of the table itself. */
636 "WITH tabname(db, tab, idx) AS ("
637 " SELECT %Q, (SELECT tbl_name FROM %Q.sqlite_schema WHERE name=%Q), %Q "
638 ")"
639 ""
640 ", whereclause(w_c) AS (%s)"
641 ""
642 "%s" /* zCommon */
643 ""
644 ", case_statement(c) AS ("
645 " SELECT "
646 " 'CASE WHEN (' || group_concat(col_alias, ', ') || ', 1) IS (\n' "
647 " || ' SELECT ' || group_concat(col_expr, ', ') || ', 1 FROM '"
648 " || format('%%Q.%%Q NOT INDEXED WHERE %%s\n', t.db, t.tab, p.eq_pk)"
649 " || ' )\n THEN NULL\n '"
650 " || 'ELSE format(''surplus entry ('"
651 " || group_concat('%%s', ',') || ',' || p.ps_pk"
652 " || ') in index ' || t.idx || ''', ' "
653 " || group_concat('quote('||i.col_alias||')', ', ') || ', ' || p.pk_pk"
654 " || ')'"
655 " || '\n END AS error_message'"
656 " FROM tabname t, tabpk p, idx_cols i WHERE i.idx_name=t.idx"
657 ")"
658 ""
659 ", thiskey(k, n) AS ("
660 " SELECT group_concat(i.col_alias, ', ') || ', ' || p.o_pk, "
661 " count(*) + p.n_pk "
662 " FROM tabpk p, idx_cols i WHERE i.idx_name=p.idx"
663 ")"
664 ""
665 ", main_select(m, n) AS ("
666 " SELECT format("
667 " 'WITH %%s\n' ||"
668 " ', idx_checker AS (\n' ||"
669 " ' SELECT %%s,\n' ||"
670 " ' %%s\n' || "
671 " ' FROM intck_wrapper AS o\n' ||"
672 " ')\n',"
673 " ww.s, c, t.k"
674 " ), t.n"
675 " FROM case_statement, wrapper_with ww, thiskey t"
676 ")"
677
678 "SELECT m || "
679 " group_concat('SELECT * FROM idx_checker ' || w_c, ' UNION ALL '), n"
680 " FROM "
681 "main_select, whereclause "
682 , p->zDb, p->zDb, zObj, zObj
683 , zPrev ? zPrev : "VALUES('')", zCommon
684 );
685 }else{
686 pStmt = intckPrepareFmt(p,
687 /* Table tabname contains a single row. The first column, "db", contains
688 ** the name of the db containing the table (e.g. "main") and the second,
689 ** "tab", the name of the table itself. */
690 "WITH tabname(db, tab, idx, prev) AS (SELECT %Q, %Q, NULL, %Q)"
691 ""
692 "%s" /* zCommon */
693
694 /* expr(e) contains one row for each index on table zObj. Value e
695 ** is set to an expression that evaluates to NULL if the required
696 ** entry is present in the index, or an error message otherwise. */
697 ", expr(e, p) AS ("
698 " SELECT format('CASE WHEN EXISTS \n"
699 " (SELECT 1 FROM %%Q.%%Q AS i INDEXED BY %%Q WHERE %%s%%s)\n"
700 " THEN NULL\n"
701 " ELSE format(''entry (%%s,%%s) missing from index %%s'', %%s, %%s)\n"
702 " END\n'"
703 " , t.db, t.tab, i.name, i.match_expr, ' AND (' || partial || ')',"
704 " i.idx_ps, t.ps_pk, i.name, i.idx_idx, t.pk_pk),"
705 " CASE WHEN partial IS NULL THEN NULL ELSE i.partial_alias END"
706 " FROM tabpk t, idx i"
707 ")"
708
709 ", numbered(ii, cond, e) AS ("
710 " SELECT 0, 'n.ii=0', 'NULL'"
711 " UNION ALL "
712 " SELECT row_number() OVER (),"
713 " '(n.ii='||row_number() OVER ()||COALESCE(' AND '||p||')', ')'), e"
714 " FROM expr"
715 ")"
716
717 ", counter_with(w) AS ("
718 " SELECT 'WITH intck_counter(ii) AS (\n ' || "
719 " group_concat('SELECT '||ii, ' UNION ALL\n ') "
720 " || '\n)' FROM numbered"
721 ")"
722 ""
723 ", case_statement(c) AS ("
724 " SELECT 'CASE ' || "
725 " group_concat(format('\n WHEN %%s THEN (%%s)', cond, e), '') ||"
726 " '\nEND AS error_message'"
727 " FROM numbered"
728 ")"
729 ""
730
731 /* This table contains a single row consisting of a single value -
732 ** the text of an SQL expression that may be used by the main SQL
733 ** statement to output an SQL literal that can be used to resume
734 ** the scan if it is suspended. e.g. for a rowid table, an expression
735 ** like:
736 **
737 ** format('(%d,%d)', _rowid_, n.ii)
738 */
739 ", thiskey(k, n) AS ("
740 " SELECT o_pk || ', ii', n_pk+1 FROM tabpk"
741 ")"
742 ""
743 ", whereclause(w_c) AS ("
744 " SELECT CASE WHEN prev!='' THEN "
745 " '\nWHERE (' || o_pk ||', n.ii) > ' || prev"
746 " ELSE ''"
747 " END"
748 " FROM tabpk, tabname"
749 ")"
750 ""
751 ", main_select(m, n) AS ("
752 " SELECT format("
753 " '%%s, %%s\nSELECT %%s,\n%%s\nFROM intck_wrapper AS o"
754 ", intck_counter AS n%%s\nORDER BY %%s', "
755 " w, ww.s, c, thiskey.k, whereclause.w_c, t.o_pk"
756 " ), thiskey.n"
757 " FROM case_statement, tabpk t, counter_with, "
758 " wrapper_with ww, thiskey, whereclause"
759 ")"
760
761 "SELECT m, n FROM main_select",
762 p->zDb, zObj, zPrev, zCommon
763 );
764 }
765
766 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
767 zRet = intckMprintf(p, "%s", (const char*)sqlite3_column_text(pStmt, 0));
768 if( pnKeyVal ){
769 *pnKeyVal = sqlite3_column_int(pStmt, 1);
770 }
771 }
772 intckFinalize(p, pStmt);
773
774 if( bAutoIndex ) intckExec(p, "PRAGMA automatic_index = 1");
775 return zRet;
776 }
777
778 /*
779 ** Open a new integrity-check object.
780 */
781 int sqlite3_intck_open(
782 sqlite3 *db, /* Database handle to operate on */
783 const char *zDbArg, /* "main", "temp" etc. */
784 sqlite3_intck **ppOut /* OUT: New integrity-check handle */
785 ){
786 sqlite3_intck *pNew = 0;
787 int rc = SQLITE_OK;
788 const char *zDb = zDbArg ? zDbArg : "main";
789 int nDb = (int)strlen(zDb);
790
791 pNew = (sqlite3_intck*)sqlite3_malloc(sizeof(*pNew) + nDb + 1);
792 if( pNew==0 ){
793 rc = SQLITE_NOMEM;
794 }else{
795 memset(pNew, 0, sizeof(*pNew));
796 pNew->db = db;
797 pNew->zDb = (const char*)&pNew[1];
798 memcpy(&pNew[1], zDb, nDb+1);
799 rc = sqlite3_create_function(db, "parse_create_index",
800 2, SQLITE_UTF8, 0, intckParseCreateIndexFunc, 0, 0
801 );
802 if( rc!=SQLITE_OK ){
803 sqlite3_intck_close(pNew);
804 pNew = 0;
805 }
806 }
807
808 *ppOut = pNew;
809 return rc;
810 }
811
812 /*
813 ** Free the integrity-check object.
814 */
815 void sqlite3_intck_close(sqlite3_intck *p){
816 if( p ){
817 sqlite3_finalize(p->pCheck);
818 sqlite3_create_function(
819 p->db, "parse_create_index", 1, SQLITE_UTF8, 0, 0, 0, 0
820 );
821 sqlite3_free(p->zObj);
822 sqlite3_free(p->zKey);
823 sqlite3_free(p->zTestSql);
824 sqlite3_free(p->zErr);
825 sqlite3_free(p->zMessage);
826 sqlite3_free(p);
827 }
828 }
829
830 /*
831 ** Step the integrity-check object.
832 */
833 int sqlite3_intck_step(sqlite3_intck *p){
834 if( p->rc==SQLITE_OK ){
835
836 if( p->zMessage ){
837 sqlite3_free(p->zMessage);
838 p->zMessage = 0;
839 }
840
841 if( p->bCorruptSchema ){
842 p->rc = SQLITE_DONE;
843 }else
844 if( p->pCheck==0 ){
845 intckFindObject(p);
846 if( p->rc==SQLITE_OK ){
847 if( p->zObj ){
848 char *zSql = 0;
849 zSql = intckCheckObjectSql(p, p->zObj, p->zKey, &p->nKeyVal);
850 p->pCheck = intckPrepare(p, zSql);
851 sqlite3_free(zSql);
852 sqlite3_free(p->zKey);
853 p->zKey = 0;
854 }else{
855 p->rc = SQLITE_DONE;
856 }
857 }else if( p->rc==SQLITE_CORRUPT ){
858 p->rc = SQLITE_OK;
859 p->zMessage = intckMprintf(p, "%s",
860 "corruption found while reading database schema"
861 );
862 p->bCorruptSchema = 1;
863 }
864 }
865
866 if( p->pCheck ){
867 assert( p->rc==SQLITE_OK );
868 if( sqlite3_step(p->pCheck)==SQLITE_ROW ){
869 /* Normal case, do nothing. */
870 }else{
871 intckFinalize(p, p->pCheck);
872 p->pCheck = 0;
873 p->nKeyVal = 0;
874 if( p->rc==SQLITE_CORRUPT ){
875 p->rc = SQLITE_OK;
876 p->zMessage = intckMprintf(p,
877 "corruption found while scanning database object %s", p->zObj
878 );
879 }
880 }
881 }
882 }
883
884 return p->rc;
885 }
886
887 /*
888 ** Return a message describing the corruption encountered by the most recent
889 ** call to sqlite3_intck_step(), or NULL if no corruption was encountered.
890 */
891 const char *sqlite3_intck_message(sqlite3_intck *p){
892 assert( p->pCheck==0 || p->zMessage==0 );
893 if( p->zMessage ){
894 return p->zMessage;
895 }
896 if( p->pCheck ){
897 return (const char*)sqlite3_column_text(p->pCheck, 0);
898 }
899 return 0;
900 }
901
902 /*
903 ** Return the error code and message.
904 */
905 int sqlite3_intck_error(sqlite3_intck *p, const char **pzErr){
906 if( pzErr ) *pzErr = p->zErr;
907 return (p->rc==SQLITE_DONE ? SQLITE_OK : p->rc);
908 }
909
910 /*
911 ** Close any read transaction the integrity-check object is holding open
912 ** on the database.
913 */
914 int sqlite3_intck_unlock(sqlite3_intck *p){
915 if( p->rc==SQLITE_OK && p->pCheck ){
916 assert( p->zKey==0 && p->nKeyVal>0 );
917 intckSaveKey(p);
918 intckFinalize(p, p->pCheck);
919 p->pCheck = 0;
920 }
921 return p->rc;
922 }
923
924 /*
925 ** Return the SQL statement used to check object zObj. Or, if zObj is
926 ** NULL, the current SQL statement.
927 */
928 const char *sqlite3_intck_test_sql(sqlite3_intck *p, const char *zObj){
929 sqlite3_free(p->zTestSql);
930 if( zObj ){
931 p->zTestSql = intckCheckObjectSql(p, zObj, 0, 0);
932 }else{
933 if( p->zObj ){
934 p->zTestSql = intckCheckObjectSql(p, p->zObj, p->zKey, 0);
935 }else{
936 sqlite3_free(p->zTestSql);
937 p->zTestSql = 0;
938 }
939 }
940 return p->zTestSql;
941 }