4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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 a table-valued function:
15 ** prefixes('abcdefg')
17 ** The function has a single (non-HIDDEN) column named prefix that takes
18 ** on all prefixes of the string in its argument, including an empty string
19 ** and the input string itself. The order of prefixes is from longest
22 #if !defined(SQLITE_CORE) || !defined(SQLITE_OMIT_VIRTUALTABLE)
23 #if !defined(SQLITEINT_H)
24 #include "sqlite3ext.h"
26 SQLITE_EXTENSION_INIT1
30 /* prefixes_vtab is a subclass of sqlite3_vtab which is
31 ** underlying representation of the virtual table
33 typedef struct prefixes_vtab prefixes_vtab
;
34 struct prefixes_vtab
{
35 sqlite3_vtab base
; /* Base class - must be first */
36 /* No additional fields are necessary */
39 /* prefixes_cursor is a subclass of sqlite3_vtab_cursor which will
40 ** serve as the underlying representation of a cursor that scans
41 ** over rows of the result
43 typedef struct prefixes_cursor prefixes_cursor
;
44 struct prefixes_cursor
{
45 sqlite3_vtab_cursor base
; /* Base class - must be first */
46 sqlite3_int64 iRowid
; /* The rowid */
47 char *zStr
; /* Original string to be prefixed */
48 int nStr
; /* Length of the string in bytes */
52 ** The prefixesConnect() method is invoked to create a new
53 ** template virtual table.
55 ** Think of this routine as the constructor for prefixes_vtab objects.
57 ** All this routine needs to do is:
59 ** (1) Allocate the prefixes_vtab object and initialize all fields.
61 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
62 ** result set of queries against the virtual table will look like.
64 static int prefixesConnect(
67 int argc
, const char *const*argv
,
68 sqlite3_vtab
**ppVtab
,
74 rc
= sqlite3_declare_vtab(db
,
75 "CREATE TABLE prefixes(prefix TEXT, original_string TEXT HIDDEN)"
78 pNew
= sqlite3_malloc( sizeof(*pNew
) );
79 *ppVtab
= (sqlite3_vtab
*)pNew
;
80 if( pNew
==0 ) return SQLITE_NOMEM
;
81 memset(pNew
, 0, sizeof(*pNew
));
82 sqlite3_vtab_config(db
, SQLITE_VTAB_INNOCUOUS
);
88 ** This method is the destructor for prefixes_vtab objects.
90 static int prefixesDisconnect(sqlite3_vtab
*pVtab
){
91 prefixes_vtab
*p
= (prefixes_vtab
*)pVtab
;
97 ** Constructor for a new prefixes_cursor object.
99 static int prefixesOpen(sqlite3_vtab
*p
, sqlite3_vtab_cursor
**ppCursor
){
100 prefixes_cursor
*pCur
;
101 pCur
= sqlite3_malloc( sizeof(*pCur
) );
102 if( pCur
==0 ) return SQLITE_NOMEM
;
103 memset(pCur
, 0, sizeof(*pCur
));
104 *ppCursor
= &pCur
->base
;
109 ** Destructor for a prefixes_cursor.
111 static int prefixesClose(sqlite3_vtab_cursor
*cur
){
112 prefixes_cursor
*pCur
= (prefixes_cursor
*)cur
;
113 sqlite3_free(pCur
->zStr
);
120 ** Advance a prefixes_cursor to its next row of output.
122 static int prefixesNext(sqlite3_vtab_cursor
*cur
){
123 prefixes_cursor
*pCur
= (prefixes_cursor
*)cur
;
129 ** Return values of columns for the row at which the prefixes_cursor
130 ** is currently pointing.
132 static int prefixesColumn(
133 sqlite3_vtab_cursor
*cur
, /* The cursor */
134 sqlite3_context
*ctx
, /* First argument to sqlite3_result_...() */
135 int i
/* Which column to return */
137 prefixes_cursor
*pCur
= (prefixes_cursor
*)cur
;
140 sqlite3_result_text(ctx
, pCur
->zStr
, pCur
->nStr
- (int)pCur
->iRowid
,
144 sqlite3_result_text(ctx
, pCur
->zStr
, pCur
->nStr
, 0);
151 ** Return the rowid for the current row. In this implementation, the
152 ** rowid is the same as the output value.
154 static int prefixesRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
155 prefixes_cursor
*pCur
= (prefixes_cursor
*)cur
;
156 *pRowid
= pCur
->iRowid
;
161 ** Return TRUE if the cursor has been moved off of the last
164 static int prefixesEof(sqlite3_vtab_cursor
*cur
){
165 prefixes_cursor
*pCur
= (prefixes_cursor
*)cur
;
166 return pCur
->iRowid
>pCur
->nStr
;
170 ** This method is called to "rewind" the prefixes_cursor object back
171 ** to the first row of output. This method is always called at least
172 ** once prior to any call to prefixesColumn() or prefixesRowid() or
175 static int prefixesFilter(
176 sqlite3_vtab_cursor
*pVtabCursor
,
177 int idxNum
, const char *idxStr
,
178 int argc
, sqlite3_value
**argv
180 prefixes_cursor
*pCur
= (prefixes_cursor
*)pVtabCursor
;
181 sqlite3_free(pCur
->zStr
);
183 pCur
->zStr
= sqlite3_mprintf("%s", sqlite3_value_text(argv
[0]));
184 pCur
->nStr
= pCur
->zStr
? (int)strlen(pCur
->zStr
) : 0;
194 ** SQLite will invoke this method one or more times while planning a query
195 ** that uses the virtual table. This routine needs to create
196 ** a query plan for each invocation and compute an estimated cost for that
199 static int prefixesBestIndex(
201 sqlite3_index_info
*pIdxInfo
203 /* Search for a usable equality constraint against column 1
204 ** (original_string) and use it if at all possible */
206 const struct sqlite3_index_constraint
*p
;
208 for(i
=0, p
=pIdxInfo
->aConstraint
; i
<pIdxInfo
->nConstraint
; i
++, p
++){
209 if( p
->iColumn
!=1 ) continue;
210 if( p
->op
!=SQLITE_INDEX_CONSTRAINT_EQ
) continue;
211 if( !p
->usable
) continue;
212 pIdxInfo
->aConstraintUsage
[i
].argvIndex
= 1;
213 pIdxInfo
->aConstraintUsage
[i
].omit
= 1;
214 pIdxInfo
->estimatedCost
= (double)10;
215 pIdxInfo
->estimatedRows
= 10;
218 pIdxInfo
->estimatedCost
= (double)1000000000;
219 pIdxInfo
->estimatedRows
= 1000000000;
224 ** This following structure defines all the methods for the
227 static sqlite3_module prefixesModule
= {
230 /* xConnect */ prefixesConnect
,
231 /* xBestIndex */ prefixesBestIndex
,
232 /* xDisconnect */ prefixesDisconnect
,
234 /* xOpen */ prefixesOpen
,
235 /* xClose */ prefixesClose
,
236 /* xFilter */ prefixesFilter
,
237 /* xNext */ prefixesNext
,
238 /* xEof */ prefixesEof
,
239 /* xColumn */ prefixesColumn
,
240 /* xRowid */ prefixesRowid
,
256 ** This is a copy of the SQLITE_SKIP_UTF8(zIn) macro in sqliteInt.h.
258 ** Assuming zIn points to the first byte of a UTF-8 character,
259 ** advance zIn to point to the first byte of the next UTF-8 character.
261 #define PREFIX_SKIP_UTF8(zIn) { \
262 if( (*(zIn++))>=0xc0 ){ \
263 while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
268 ** Implementation of function prefix_length(). This function accepts two
269 ** strings as arguments and returns the length in characters (not bytes),
270 ** of the longest prefix shared by the two strings. For example:
272 ** prefix_length('abcdxxx', 'abcyy') == 3
273 ** prefix_length('abcdxxx', 'bcyyy') == 0
274 ** prefix_length('abcdxxx', 'ab') == 2
275 ** prefix_length('ab', 'abcd') == 2
277 ** This function assumes the input is well-formed utf-8. If it is not,
278 ** it is possible for this function to return -1.
280 static void prefixLengthFunc(
281 sqlite3_context
*ctx
,
283 sqlite3_value
**apVal
285 int nByte
; /* Number of bytes to compare */
286 int nRet
= 0; /* Return value */
287 const unsigned char *zL
= sqlite3_value_text(apVal
[0]);
288 const unsigned char *zR
= sqlite3_value_text(apVal
[1]);
289 int nL
= sqlite3_value_bytes(apVal
[0]);
290 int nR
= sqlite3_value_bytes(apVal
[1]);
293 nByte
= (nL
> nR
? nL
: nR
);
294 for(i
=0; i
<nByte
; i
++){
295 if( zL
[i
]!=zR
[i
] ) break;
296 if( (zL
[i
] & 0xC0)!=0x80 ) nRet
++;
299 if( (zL
[i
] & 0xC0)==0x80 ) nRet
--;
300 sqlite3_result_int(ctx
, nRet
);
304 __declspec(dllexport
)
306 int sqlite3_prefixes_init(
309 const sqlite3_api_routines
*pApi
312 SQLITE_EXTENSION_INIT2(pApi
);
313 rc
= sqlite3_create_module(db
, "prefixes", &prefixesModule
, 0);
315 rc
= sqlite3_create_function(
316 db
, "prefix_length", 2, SQLITE_UTF8
, 0, prefixLengthFunc
, 0, 0
321 #endif /* !defined(SQLITE_CORE) || !defined(SQLITE_OMIT_VIRTUALTABLE) */