Snapshot of upstream SQLite 3.31.0
[sqlcipher.git] / ext / misc / completion.c
blobb624b6d4768e523f6265e0c864fb751ee4942c1d
1 /*
2 ** 2017-07-10
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.
11 *************************************************************************
13 ** This file implements an eponymous virtual table that returns suggested
14 ** completions for a partial SQL input.
16 ** Suggested usage:
18 ** SELECT DISTINCT candidate COLLATE nocase
19 ** FROM completion($prefix,$wholeline)
20 ** ORDER BY 1;
22 ** The two query parameters are optional. $prefix is the text of the
23 ** current word being typed and that is to be completed. $wholeline is
24 ** the complete input line, used for context.
26 ** The raw completion() table might return the same candidate multiple
27 ** times, for example if the same column name is used to two or more
28 ** tables. And the candidates are returned in an arbitrary order. Hence,
29 ** the DISTINCT and ORDER BY are recommended.
31 ** This virtual table operates at the speed of human typing, and so there
32 ** is no attempt to make it fast. Even a slow implementation will be much
33 ** faster than any human can type.
36 #include "sqlite3ext.h"
37 SQLITE_EXTENSION_INIT1
38 #include <assert.h>
39 #include <string.h>
40 #include <ctype.h>
42 #ifndef SQLITE_OMIT_VIRTUALTABLE
44 /* completion_vtab is a subclass of sqlite3_vtab which will
45 ** serve as the underlying representation of a completion virtual table
47 typedef struct completion_vtab completion_vtab;
48 struct completion_vtab {
49 sqlite3_vtab base; /* Base class - must be first */
50 sqlite3 *db; /* Database connection for this completion vtab */
53 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
54 ** serve as the underlying representation of a cursor that scans
55 ** over rows of the result
57 typedef struct completion_cursor completion_cursor;
58 struct completion_cursor {
59 sqlite3_vtab_cursor base; /* Base class - must be first */
60 sqlite3 *db; /* Database connection for this cursor */
61 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
62 char *zPrefix; /* The prefix for the word we want to complete */
63 char *zLine; /* The whole that we want to complete */
64 const char *zCurrentRow; /* Current output row */
65 int szRow; /* Length of the zCurrentRow string */
66 sqlite3_stmt *pStmt; /* Current statement */
67 sqlite3_int64 iRowid; /* The rowid */
68 int ePhase; /* Current phase */
69 int j; /* inter-phase counter */
72 /* Values for ePhase:
74 #define COMPLETION_FIRST_PHASE 1
75 #define COMPLETION_KEYWORDS 1
76 #define COMPLETION_PRAGMAS 2
77 #define COMPLETION_FUNCTIONS 3
78 #define COMPLETION_COLLATIONS 4
79 #define COMPLETION_INDEXES 5
80 #define COMPLETION_TRIGGERS 6
81 #define COMPLETION_DATABASES 7
82 #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
83 #define COMPLETION_COLUMNS 9
84 #define COMPLETION_MODULES 10
85 #define COMPLETION_EOF 11
88 ** The completionConnect() method is invoked to create a new
89 ** completion_vtab that describes the completion virtual table.
91 ** Think of this routine as the constructor for completion_vtab objects.
93 ** All this routine needs to do is:
95 ** (1) Allocate the completion_vtab object and initialize all fields.
97 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
98 ** result set of queries against completion will look like.
100 static int completionConnect(
101 sqlite3 *db,
102 void *pAux,
103 int argc, const char *const*argv,
104 sqlite3_vtab **ppVtab,
105 char **pzErr
107 completion_vtab *pNew;
108 int rc;
110 (void)(pAux); /* Unused parameter */
111 (void)(argc); /* Unused parameter */
112 (void)(argv); /* Unused parameter */
113 (void)(pzErr); /* Unused parameter */
115 /* Column numbers */
116 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
117 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
118 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
119 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
121 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
122 rc = sqlite3_declare_vtab(db,
123 "CREATE TABLE x("
124 " candidate TEXT,"
125 " prefix TEXT HIDDEN,"
126 " wholeline TEXT HIDDEN,"
127 " phase INT HIDDEN" /* Used for debugging only */
128 ")");
129 if( rc==SQLITE_OK ){
130 pNew = sqlite3_malloc( sizeof(*pNew) );
131 *ppVtab = (sqlite3_vtab*)pNew;
132 if( pNew==0 ) return SQLITE_NOMEM;
133 memset(pNew, 0, sizeof(*pNew));
134 pNew->db = db;
136 return rc;
140 ** This method is the destructor for completion_cursor objects.
142 static int completionDisconnect(sqlite3_vtab *pVtab){
143 sqlite3_free(pVtab);
144 return SQLITE_OK;
148 ** Constructor for a new completion_cursor object.
150 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
151 completion_cursor *pCur;
152 pCur = sqlite3_malloc( sizeof(*pCur) );
153 if( pCur==0 ) return SQLITE_NOMEM;
154 memset(pCur, 0, sizeof(*pCur));
155 pCur->db = ((completion_vtab*)p)->db;
156 *ppCursor = &pCur->base;
157 return SQLITE_OK;
161 ** Reset the completion_cursor.
163 static void completionCursorReset(completion_cursor *pCur){
164 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
165 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
166 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
167 pCur->j = 0;
171 ** Destructor for a completion_cursor.
173 static int completionClose(sqlite3_vtab_cursor *cur){
174 completionCursorReset((completion_cursor*)cur);
175 sqlite3_free(cur);
176 return SQLITE_OK;
180 ** Advance a completion_cursor to its next row of output.
182 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
183 ** record the current state of the scan. This routine sets ->zCurrentRow
184 ** to the current row of output and then returns. If no more rows remain,
185 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
186 ** table that has reached the end of its scan.
188 ** The current implementation just lists potential identifiers and
189 ** keywords and filters them by zPrefix. Future enhancements should
190 ** take zLine into account to try to restrict the set of identifiers and
191 ** keywords based on what would be legal at the current point of input.
193 static int completionNext(sqlite3_vtab_cursor *cur){
194 completion_cursor *pCur = (completion_cursor*)cur;
195 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
196 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
197 pCur->iRowid++;
198 while( pCur->ePhase!=COMPLETION_EOF ){
199 switch( pCur->ePhase ){
200 case COMPLETION_KEYWORDS: {
201 if( pCur->j >= sqlite3_keyword_count() ){
202 pCur->zCurrentRow = 0;
203 pCur->ePhase = COMPLETION_DATABASES;
204 }else{
205 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
207 iCol = -1;
208 break;
210 case COMPLETION_DATABASES: {
211 if( pCur->pStmt==0 ){
212 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
213 &pCur->pStmt, 0);
215 iCol = 1;
216 eNextPhase = COMPLETION_TABLES;
217 break;
219 case COMPLETION_TABLES: {
220 if( pCur->pStmt==0 ){
221 sqlite3_stmt *pS2;
222 char *zSql = 0;
223 const char *zSep = "";
224 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
225 while( sqlite3_step(pS2)==SQLITE_ROW ){
226 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
227 zSql = sqlite3_mprintf(
228 "%z%s"
229 "SELECT name FROM \"%w\".sqlite_master",
230 zSql, zSep, zDb
232 if( zSql==0 ) return SQLITE_NOMEM;
233 zSep = " UNION ";
235 sqlite3_finalize(pS2);
236 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
237 sqlite3_free(zSql);
239 iCol = 0;
240 eNextPhase = COMPLETION_COLUMNS;
241 break;
243 case COMPLETION_COLUMNS: {
244 if( pCur->pStmt==0 ){
245 sqlite3_stmt *pS2;
246 char *zSql = 0;
247 const char *zSep = "";
248 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
249 while( sqlite3_step(pS2)==SQLITE_ROW ){
250 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
251 zSql = sqlite3_mprintf(
252 "%z%s"
253 "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
254 " JOIN pragma_table_info(sm.name,%Q) AS pti"
255 " WHERE sm.type='table'",
256 zSql, zSep, zDb, zDb
258 if( zSql==0 ) return SQLITE_NOMEM;
259 zSep = " UNION ";
261 sqlite3_finalize(pS2);
262 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
263 sqlite3_free(zSql);
265 iCol = 0;
266 eNextPhase = COMPLETION_EOF;
267 break;
270 if( iCol<0 ){
271 /* This case is when the phase presets zCurrentRow */
272 if( pCur->zCurrentRow==0 ) continue;
273 }else{
274 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
275 /* Extract the next row of content */
276 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
277 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
278 }else{
279 /* When all rows are finished, advance to the next phase */
280 sqlite3_finalize(pCur->pStmt);
281 pCur->pStmt = 0;
282 pCur->ePhase = eNextPhase;
283 continue;
286 if( pCur->nPrefix==0 ) break;
287 if( pCur->nPrefix<=pCur->szRow
288 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
290 break;
294 return SQLITE_OK;
298 ** Return values of columns for the row at which the completion_cursor
299 ** is currently pointing.
301 static int completionColumn(
302 sqlite3_vtab_cursor *cur, /* The cursor */
303 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
304 int i /* Which column to return */
306 completion_cursor *pCur = (completion_cursor*)cur;
307 switch( i ){
308 case COMPLETION_COLUMN_CANDIDATE: {
309 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
310 break;
312 case COMPLETION_COLUMN_PREFIX: {
313 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
314 break;
316 case COMPLETION_COLUMN_WHOLELINE: {
317 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
318 break;
320 case COMPLETION_COLUMN_PHASE: {
321 sqlite3_result_int(ctx, pCur->ePhase);
322 break;
325 return SQLITE_OK;
329 ** Return the rowid for the current row. In this implementation, the
330 ** rowid is the same as the output value.
332 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
333 completion_cursor *pCur = (completion_cursor*)cur;
334 *pRowid = pCur->iRowid;
335 return SQLITE_OK;
339 ** Return TRUE if the cursor has been moved off of the last
340 ** row of output.
342 static int completionEof(sqlite3_vtab_cursor *cur){
343 completion_cursor *pCur = (completion_cursor*)cur;
344 return pCur->ePhase >= COMPLETION_EOF;
348 ** This method is called to "rewind" the completion_cursor object back
349 ** to the first row of output. This method is always called at least
350 ** once prior to any call to completionColumn() or completionRowid() or
351 ** completionEof().
353 static int completionFilter(
354 sqlite3_vtab_cursor *pVtabCursor,
355 int idxNum, const char *idxStr,
356 int argc, sqlite3_value **argv
358 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
359 int iArg = 0;
360 (void)(idxStr); /* Unused parameter */
361 (void)(argc); /* Unused parameter */
362 completionCursorReset(pCur);
363 if( idxNum & 1 ){
364 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
365 if( pCur->nPrefix>0 ){
366 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
367 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
369 iArg = 1;
371 if( idxNum & 2 ){
372 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
373 if( pCur->nLine>0 ){
374 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
375 if( pCur->zLine==0 ) return SQLITE_NOMEM;
378 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
379 int i = pCur->nLine;
380 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
381 i--;
383 pCur->nPrefix = pCur->nLine - i;
384 if( pCur->nPrefix>0 ){
385 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
386 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
389 pCur->iRowid = 0;
390 pCur->ePhase = COMPLETION_FIRST_PHASE;
391 return completionNext(pVtabCursor);
395 ** SQLite will invoke this method one or more times while planning a query
396 ** that uses the completion virtual table. This routine needs to create
397 ** a query plan for each invocation and compute an estimated cost for that
398 ** plan.
400 ** There are two hidden parameters that act as arguments to the table-valued
401 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
402 ** is available and bit 1 is set if "wholeline" is available.
404 static int completionBestIndex(
405 sqlite3_vtab *tab,
406 sqlite3_index_info *pIdxInfo
408 int i; /* Loop over constraints */
409 int idxNum = 0; /* The query plan bitmask */
410 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
411 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
412 int nArg = 0; /* Number of arguments that completeFilter() expects */
413 const struct sqlite3_index_constraint *pConstraint;
415 (void)(tab); /* Unused parameter */
416 pConstraint = pIdxInfo->aConstraint;
417 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
418 if( pConstraint->usable==0 ) continue;
419 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
420 switch( pConstraint->iColumn ){
421 case COMPLETION_COLUMN_PREFIX:
422 prefixIdx = i;
423 idxNum |= 1;
424 break;
425 case COMPLETION_COLUMN_WHOLELINE:
426 wholelineIdx = i;
427 idxNum |= 2;
428 break;
431 if( prefixIdx>=0 ){
432 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
433 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
435 if( wholelineIdx>=0 ){
436 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
437 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
439 pIdxInfo->idxNum = idxNum;
440 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
441 pIdxInfo->estimatedRows = 500 - 100*nArg;
442 return SQLITE_OK;
446 ** This following structure defines all the methods for the
447 ** completion virtual table.
449 static sqlite3_module completionModule = {
450 0, /* iVersion */
451 0, /* xCreate */
452 completionConnect, /* xConnect */
453 completionBestIndex, /* xBestIndex */
454 completionDisconnect, /* xDisconnect */
455 0, /* xDestroy */
456 completionOpen, /* xOpen - open a cursor */
457 completionClose, /* xClose - close a cursor */
458 completionFilter, /* xFilter - configure scan constraints */
459 completionNext, /* xNext - advance a cursor */
460 completionEof, /* xEof - check for end of scan */
461 completionColumn, /* xColumn - read data */
462 completionRowid, /* xRowid - read data */
463 0, /* xUpdate */
464 0, /* xBegin */
465 0, /* xSync */
466 0, /* xCommit */
467 0, /* xRollback */
468 0, /* xFindMethod */
469 0, /* xRename */
470 0, /* xSavepoint */
471 0, /* xRelease */
472 0, /* xRollbackTo */
473 0 /* xShadowName */
476 #endif /* SQLITE_OMIT_VIRTUALTABLE */
478 int sqlite3CompletionVtabInit(sqlite3 *db){
479 int rc = SQLITE_OK;
480 #ifndef SQLITE_OMIT_VIRTUALTABLE
481 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
482 #endif
483 return rc;
486 #ifdef _WIN32
487 __declspec(dllexport)
488 #endif
489 int sqlite3_completion_init(
490 sqlite3 *db,
491 char **pzErrMsg,
492 const sqlite3_api_routines *pApi
494 int rc = SQLITE_OK;
495 SQLITE_EXTENSION_INIT2(pApi);
496 (void)(pzErrMsg); /* Unused parameter */
497 #ifndef SQLITE_OMIT_VIRTUALTABLE
498 rc = sqlite3CompletionVtabInit(db);
499 #endif
500 return rc;