/ Check-in [0e213555]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add the "phase" output column on the COMPLETION table-valued function, for debugging. Improved comments on the implementation.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0e2135552757cc6566ac959475d3b87254b6d462425ee6c457c743001f3370e2
User & Date: drh 2017-07-11 02:05:21
Context
2017-07-11
02:53
Fix a backup2 test case so that it works on FreeBSD. check-in: 724819b4 user: drh tags: trunk
02:05
Add the "phase" output column on the COMPLETION table-valued function, for debugging. Improved comments on the implementation. check-in: 0e213555 user: drh tags: trunk
01:38
The COMPLETION virtual table now looks at the names of databases, tables, and columns in addition to SQL keywords. check-in: 1cc97711 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/misc/completion.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file implements an eponymous virtual table that returns suggested
    14     14   ** completions for a partial SQL input.
    15     15   **
           16  +** Suggested usage:
           17  +**
           18  +**     SELECT DISTINCT candidate COLLATE nocase
           19  +**       FROM completion($prefix,$wholeline)
           20  +**      ORDER BY 1;
           21  +**
           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.
           25  +**
           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.
           30  +**
           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.
           34  +**
    16     35   */
    17     36   #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_COMPLETIONVTAB)
    18     37   #if !defined(SQLITEINT_H)
    19     38   #include "sqlite3ext.h"
    20     39   #endif
    21     40   SQLITE_EXTENSION_INIT1
    22     41   #include <assert.h>
................................................................................
    90    109     completion_vtab *pNew;
    91    110     int rc;
    92    111   
    93    112   /* Column numbers */
    94    113   #define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
    95    114   #define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
    96    115   #define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
          116  +#define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
    97    117   
    98    118     rc = sqlite3_declare_vtab(db,
    99    119         "CREATE TABLE x("
   100    120         "  candidate TEXT,"
   101    121         "  prefix TEXT HIDDEN,"
   102         -      "  wholeline TEXT HIDDEN"
          122  +      "  wholeline TEXT HIDDEN,"
          123  +      "  phase INT HIDDEN"        /* Used for debugging only */
   103    124         ")");
   104    125     if( rc==SQLITE_OK ){
   105    126       pNew = sqlite3_malloc( sizeof(*pNew) );
   106    127       *ppVtab = (sqlite3_vtab*)pNew;
   107    128       if( pNew==0 ) return SQLITE_NOMEM;
   108    129       memset(pNew, 0, sizeof(*pNew));
   109    130       pNew->db = db;
................................................................................
   173    194     "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
   174    195     "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
   175    196     "WITH", "WITHOUT",
   176    197   };
   177    198   
   178    199   /*
   179    200   ** Advance a completion_cursor to its next row of output.
          201  +**
          202  +** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
          203  +** record the current state of the scan.  This routine sets ->zCurrentRow
          204  +** to the current row of output and then returns.  If no more rows remain,
          205  +** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
          206  +** table that has reached the end of its scan.
          207  +**
          208  +** The current implementation just lists potential identifiers and
          209  +** keywords and filters them by zPrefix.  Future enhancements should
          210  +** take zLine into account to try to restrict the set of identifiers and
          211  +** keywords based on what would be legal at the current point of input.
   180    212   */
   181    213   static int completionNext(sqlite3_vtab_cursor *cur){
   182    214     completion_cursor *pCur = (completion_cursor*)cur;
   183         -  int eNextPhase = 0;/* Next phase to try if current phase reaches end */
   184         -  int iCol = -1;     /* If >=0 then step pCur->pStmt and use the i-th column */
          215  +  int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
          216  +  int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
   185    217     pCur->iRowid++;
   186    218     while( pCur->ePhase!=COMPLETION_EOF ){
   187    219       switch( pCur->ePhase ){
   188    220         case COMPLETION_KEYWORDS: {
   189    221           if( pCur->j >=  sizeof(completionKwrds)/sizeof(completionKwrds[0]) ){
   190    222             pCur->zCurrentRow = 0;
   191    223             pCur->ePhase = COMPLETION_DATABASES;
................................................................................
   298    330       case COMPLETION_COLUMN_PREFIX: {
   299    331         sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
   300    332         break;
   301    333       }
   302    334       case COMPLETION_COLUMN_WHOLELINE: {
   303    335         sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
   304    336         break;
          337  +    }
          338  +    case COMPLETION_COLUMN_PHASE: {
          339  +      sqlite3_result_int(ctx, pCur->ePhase);
          340  +      break;
   305    341       }
   306    342     }
   307    343     return SQLITE_OK;
   308    344   }
   309    345   
   310    346   /*
   311    347   ** Return the rowid for the current row.  In this implementation, the