blob: 1b1963177276175302d998e14b2de3088a0634bf [file] [log] [blame]
drh724b1892016-05-31 16:22:48 +00001/*
2** 2016-05-28
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 the implementation of an SQLite virtual table for
14** reading CSV files.
15**
16** Usage:
17**
18** .load ./csv
19** CREATE VIRTUAL TABLE temp.csv USING csv(filename=FILENAME);
20** SELECT * FROM csv;
21**
22** The columns are named "c1", "c2", "c3", ... by default. But the
23** application can define its own CREATE TABLE statement as an additional
24** parameter. For example:
25**
26** CREATE VIRTUAL TABLE temp.csv2 USING csv(
27** filename = "../http.log",
28** schema = "CREATE TABLE x(date,ipaddr,url,referrer,userAgent)"
29** );
30*/
31#include <sqlite3ext.h>
32SQLITE_EXTENSION_INIT1
33#include <string.h>
34#include <stdlib.h>
35#include <assert.h>
36#include <stdarg.h>
37#include <ctype.h>
38#include <stdio.h>
39
40/*
41** A macro to hint to the compiler that a function should not be
42** inlined.
43*/
44#if defined(__GNUC__)
45# define CSV_NOINLINE __attribute__((noinline))
46#elif defined(_MSC_VER) && _MSC_VER>=1310
47# define CSV_NOINLINE __declspec(noinline)
48#else
49# define CSV_NOINLINE
50#endif
51
52
53/* Max size of the error message in a CsvReader */
54#define CSV_MXERR 200
55
drhadcba642016-06-02 17:44:24 +000056/* Size of the CsvReader input buffer */
57#define CSV_INBUFSZ 1024
58
drh724b1892016-05-31 16:22:48 +000059/* A context object used when read a CSV file. */
60typedef struct CsvReader CsvReader;
61struct CsvReader {
62 FILE *in; /* Read the CSV text from this input stream */
63 char *z; /* Accumulated text for a field */
64 int n; /* Number of bytes in z */
65 int nAlloc; /* Space allocated for z[] */
66 int nLine; /* Current line number */
drhadcba642016-06-02 17:44:24 +000067 char cTerm; /* Character that terminated the most recent field */
68 size_t iIn; /* Next unread character in the input buffer */
69 size_t nIn; /* Number of characters in the input buffer */
70 char *zIn; /* The input buffer */
drh724b1892016-05-31 16:22:48 +000071 char zErr[CSV_MXERR]; /* Error message */
72};
73
74/* Initialize a CsvReader object */
75static void csv_reader_init(CsvReader *p){
drhadcba642016-06-02 17:44:24 +000076 p->in = 0;
77 p->z = 0;
78 p->n = 0;
79 p->nAlloc = 0;
80 p->nLine = 0;
81 p->nIn = 0;
82 p->zIn = 0;
83 p->zErr[0] = 0;
drh724b1892016-05-31 16:22:48 +000084}
85
86/* Close and reset a CsvReader object */
87static void csv_reader_reset(CsvReader *p){
drhadcba642016-06-02 17:44:24 +000088 if( p->in ){
89 fclose(p->in);
90 sqlite3_free(p->zIn);
91 }
drh724b1892016-05-31 16:22:48 +000092 sqlite3_free(p->z);
93 csv_reader_init(p);
94}
95
96/* Report an error on a CsvReader */
97static void csv_errmsg(CsvReader *p, const char *zFormat, ...){
98 va_list ap;
99 va_start(ap, zFormat);
100 sqlite3_vsnprintf(CSV_MXERR, p->zErr, zFormat, ap);
101 va_end(ap);
102}
103
104/* Open the file associated with a CsvReader
105** Return the number of errors.
106*/
drhadcba642016-06-02 17:44:24 +0000107static int csv_reader_open(
108 CsvReader *p, /* The reader to open */
109 const char *zFilename, /* Read from this filename */
110 const char *zData /* ... or use this data */
111){
112 if( zFilename ){
113 p->zIn = sqlite3_malloc( CSV_INBUFSZ );
114 if( p->zIn==0 ){
115 csv_errmsg(p, "out of memory");
116 return 1;
117 }
118 p->in = fopen(zFilename, "rb");
119 if( p->in==0 ){
120 csv_reader_reset(p);
121 csv_errmsg(p, "cannot open '%s' for reading", zFilename);
122 return 1;
123 }
124 }else{
125 assert( p->in==0 );
126 p->zIn = (char*)zData;
127 p->nIn = strlen(zData);
drh724b1892016-05-31 16:22:48 +0000128 }
129 return 0;
130}
131
drhadcba642016-06-02 17:44:24 +0000132/* The input buffer has overflowed. Refill the input buffer, then
133** return the next character
134*/
135static CSV_NOINLINE int csv_getc_refill(CsvReader *p){
136 size_t got;
137
138 assert( p->iIn>=p->nIn ); /* Only called on an empty input buffer */
139 assert( p->in!=0 ); /* Only called if reading froma file */
140
141 got = fread(p->zIn, 1, CSV_INBUFSZ, p->in);
142 if( got==0 ) return EOF;
143 p->nIn = got;
144 p->iIn = 1;
145 return p->zIn[0];
146}
147
148/* Return the next character of input. Return EOF at end of input. */
149static int csv_getc(CsvReader *p){
150 if( p->iIn >= p->nIn ){
151 if( p->in!=0 ) return csv_getc_refill(p);
152 return EOF;
153 }
154 return p->zIn[p->iIn++];
155}
156
drh724b1892016-05-31 16:22:48 +0000157/* Increase the size of p->z and append character c to the end.
158** Return 0 on success and non-zero if there is an OOM error */
159static CSV_NOINLINE int csv_resize_and_append(CsvReader *p, char c){
160 char *zNew;
161 int nNew = p->nAlloc*2 + 100;
162 zNew = sqlite3_realloc64(p->z, nNew);
163 if( zNew ){
164 p->z = zNew;
165 p->nAlloc = nNew;
166 p->z[p->n++] = c;
167 return 0;
168 }else{
169 csv_errmsg(p, "out of memory");
170 return 1;
171 }
172}
173
174/* Append a single character to the CsvReader.z[] array.
175** Return 0 on success and non-zero if there is an OOM error */
176static int csv_append(CsvReader *p, char c){
177 if( p->n>=p->nAlloc-1 ) return csv_resize_and_append(p, c);
178 p->z[p->n++] = c;
179 return 0;
180}
181
182/* Read a single field of CSV text. Compatible with rfc4180 and extended
183** with the option of having a separator other than ",".
184**
185** + Input comes from p->in.
186** + Store results in p->z of length p->n. Space to hold p->z comes
187** from sqlite3_malloc64().
188** + Keep track of the line number in p->nLine.
189** + Store the character that terminates the field in p->cTerm. Store
190** EOF on end-of-file.
191**
192** Return "" at EOF. Return 0 on an OOM error.
193*/
194static char *csv_read_one_field(CsvReader *p){
195 int c;
196 p->n = 0;
drhadcba642016-06-02 17:44:24 +0000197 c = csv_getc(p);
drh724b1892016-05-31 16:22:48 +0000198 if( c==EOF ){
199 p->cTerm = EOF;
200 return "";
201 }
202 if( c=='"' ){
203 int pc, ppc;
204 int startLine = p->nLine;
205 int cQuote = c;
206 pc = ppc = 0;
207 while( 1 ){
drhadcba642016-06-02 17:44:24 +0000208 c = csv_getc(p);
drh724b1892016-05-31 16:22:48 +0000209 if( c=='\n' ) p->nLine++;
210 if( c==cQuote ){
211 if( pc==cQuote ){
212 pc = 0;
213 continue;
214 }
215 }
216 if( (c==',' && pc==cQuote)
217 || (c=='\n' && pc==cQuote)
218 || (c=='\n' && pc=='\r' && ppc==cQuote)
219 || (c==EOF && pc==cQuote)
220 ){
221 do{ p->n--; }while( p->z[p->n]!=cQuote );
222 p->cTerm = c;
223 break;
224 }
225 if( pc==cQuote && c!='\r' ){
226 csv_errmsg(p, "line %d: unescaped %c character", p->nLine, cQuote);
227 break;
228 }
229 if( c==EOF ){
230 csv_errmsg(p, "line %d: unterminated %c-quoted field\n",
231 startLine, cQuote);
232 p->cTerm = c;
233 break;
234 }
235 if( csv_append(p, (char)c) ) return 0;
236 ppc = pc;
237 pc = c;
238 }
239 }else{
240 while( c!=EOF && c!=',' && c!='\n' ){
241 if( csv_append(p, (char)c) ) return 0;
drhadcba642016-06-02 17:44:24 +0000242 c = csv_getc(p);
drh724b1892016-05-31 16:22:48 +0000243 }
244 if( c=='\n' ){
245 p->nLine++;
246 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
247 }
248 p->cTerm = c;
249 }
250 if( p->z ) p->z[p->n] = 0;
251 return p->z;
252}
253
254
255/* Forward references to the various virtual table methods implemented
256** in this file. */
257static int csvtabCreate(sqlite3*, void*, int, const char*const*,
258 sqlite3_vtab**,char**);
259static int csvtabConnect(sqlite3*, void*, int, const char*const*,
260 sqlite3_vtab**,char**);
261static int csvtabBestIndex(sqlite3_vtab*,sqlite3_index_info*);
262static int csvtabDisconnect(sqlite3_vtab*);
263static int csvtabOpen(sqlite3_vtab*, sqlite3_vtab_cursor**);
264static int csvtabClose(sqlite3_vtab_cursor*);
265static int csvtabFilter(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
266 int argc, sqlite3_value **argv);
267static int csvtabNext(sqlite3_vtab_cursor*);
268static int csvtabEof(sqlite3_vtab_cursor*);
269static int csvtabColumn(sqlite3_vtab_cursor*,sqlite3_context*,int);
270static int csvtabRowid(sqlite3_vtab_cursor*,sqlite3_int64*);
271
272/* An instance of the CSV virtual table */
273typedef struct CsvTable {
274 sqlite3_vtab base; /* Base class. Must be first */
275 char *zFilename; /* Name of the CSV file */
drhadcba642016-06-02 17:44:24 +0000276 char *zData; /* Raw CSV data in lieu of zFilename */
drh724b1892016-05-31 16:22:48 +0000277 long iStart; /* Offset to start of data in zFilename */
278 int nCol; /* Number of columns in the CSV file */
drhabfd2722016-05-31 18:08:35 +0000279 unsigned int tstFlags; /* Bit values used for testing */
drh724b1892016-05-31 16:22:48 +0000280} CsvTable;
281
drhabfd2722016-05-31 18:08:35 +0000282/* Allowed values for tstFlags */
283#define CSVTEST_FIDX 0x0001 /* Pretend that constrained searchs cost less*/
284
drh724b1892016-05-31 16:22:48 +0000285/* A cursor for the CSV virtual table */
286typedef struct CsvCursor {
287 sqlite3_vtab_cursor base; /* Base class. Must be first */
288 CsvReader rdr; /* The CsvReader object */
289 char **azVal; /* Value of the current row */
290 sqlite3_int64 iRowid; /* The current rowid. Negative for EOF */
291} CsvCursor;
292
293/* Transfer error message text from a reader into a CsvTable */
294static void csv_xfer_error(CsvTable *pTab, CsvReader *pRdr){
295 sqlite3_free(pTab->base.zErrMsg);
296 pTab->base.zErrMsg = sqlite3_mprintf("%s", pRdr->zErr);
297}
298
299/*
300** This method is the destructor fo a CsvTable object.
301*/
302static int csvtabDisconnect(sqlite3_vtab *pVtab){
303 CsvTable *p = (CsvTable*)pVtab;
304 sqlite3_free(p->zFilename);
305 sqlite3_free(p);
306 return SQLITE_OK;
307}
308
309/* Skip leading whitespace. Return a pointer to the first non-whitespace
310** character, or to the zero terminator if the string has only whitespace */
311static const char *csv_skip_whitespace(const char *z){
312 while( isspace((unsigned char)z[0]) ) z++;
313 return z;
314}
315
316/* Remove trailing whitespace from the end of string z[] */
317static void csv_trim_whitespace(char *z){
318 size_t n = strlen(z);
319 while( n>0 && isspace((unsigned char)z[n]) ) n--;
320 z[n] = 0;
321}
322
323/* Dequote the string */
324static void csv_dequote(char *z){
325 int i, j;
326 char cQuote = z[0];
327 size_t n;
328
329 if( cQuote!='\'' && cQuote!='"' ) return;
330 n = strlen(z);
331 if( n<2 || z[n-1]!=z[0] ) return;
332 for(i=1, j=0; i<n-1; i++){
333 if( z[i]==cQuote && z[i+1]==cQuote ) i++;
334 z[j++] = z[i];
335 }
336 z[j] = 0;
337}
338
339/* Check to see if the string is of the form: "TAG = VALUE" with optional
340** whitespace before and around tokens. If it is, return a pointer to the
341** first character of VALUE. If it is not, return NULL.
342*/
343static const char *csv_parameter(const char *zTag, int nTag, const char *z){
344 z = csv_skip_whitespace(z);
345 if( strncmp(zTag, z, nTag)!=0 ) return 0;
346 z = csv_skip_whitespace(z+nTag);
347 if( z[0]!='=' ) return 0;
348 return csv_skip_whitespace(z+1);
349}
350
drhadcba642016-06-02 17:44:24 +0000351/* Decode a parameter that requires a dequoted string.
352**
353** Return 1 if the parameter is seen, or 0 if not. 1 is returned
354** even if there is an error. If an error occurs, then an error message
355** is left in p->zErr. If there are no errors, p->zErr[0]==0.
356*/
357static int csv_string_parameter(
358 CsvReader *p, /* Leave the error message here, if there is one */
359 const char *zParam, /* Parameter we are checking for */
360 const char *zArg, /* Raw text of the virtual table argment */
361 char **pzVal /* Write the dequoted string value here */
362){
363 const char *zValue;
364 zValue = csv_parameter(zParam,strlen(zParam),zArg);
365 if( zValue==0 ) return 0;
366 p->zErr[0] = 0;
367 if( *pzVal ){
368 csv_errmsg(p, "more than one '%s' parameter", zParam);
369 return 1;
370 }
371 *pzVal = sqlite3_mprintf("%s", zValue);
372 if( *pzVal==0 ){
373 csv_errmsg(p, "out of memory");
374 return 1;
375 }
376 csv_trim_whitespace(*pzVal);
377 csv_dequote(*pzVal);
378 return 1;
379}
380
381
drh724b1892016-05-31 16:22:48 +0000382/* Return 0 if the argument is false and 1 if it is true. Return -1 if
383** we cannot really tell.
384*/
385static int csv_boolean(const char *z){
386 if( sqlite3_stricmp("yes",z)==0
387 || sqlite3_stricmp("on",z)==0
388 || sqlite3_stricmp("true",z)==0
389 || (z[0]=='1' && z[0]==0)
390 ){
391 return 1;
392 }
393 if( sqlite3_stricmp("no",z)==0
394 || sqlite3_stricmp("off",z)==0
395 || sqlite3_stricmp("false",z)==0
396 || (z[0]=='0' && z[1]==0)
397 ){
398 return 0;
399 }
400 return -1;
401}
402
403
404/*
405** Parameters:
drhadcba642016-06-02 17:44:24 +0000406** filename=FILENAME Name of file containing CSV content
407** data=TEXT Direct CSV content.
drh1fc1a0f2016-05-31 18:44:33 +0000408** schema=SCHEMA Alternative CSV schema.
drh724b1892016-05-31 16:22:48 +0000409** header=YES|NO First row of CSV defines the names of
410** columns if "yes". Default "no".
drhadcba642016-06-02 17:44:24 +0000411** columns=N Assume the CSV file contains N columns.
drhabfd2722016-05-31 18:08:35 +0000412** testflags=N Bitmask of test flags. Optional
drh724b1892016-05-31 16:22:48 +0000413**
drh1fc1a0f2016-05-31 18:44:33 +0000414** If schema= is omitted, then the columns are named "c0", "c1", "c2",
415** and so forth. If columns=N is omitted, then the file is opened and
416** the number of columns in the first row is counted to determine the
417** column count. If header=YES, then the first row is skipped.
drh724b1892016-05-31 16:22:48 +0000418*/
419static int csvtabConnect(
420 sqlite3 *db,
421 void *pAux,
422 int argc, const char *const*argv,
423 sqlite3_vtab **ppVtab,
424 char **pzErr
425){
drh1fc1a0f2016-05-31 18:44:33 +0000426 CsvTable *pNew = 0; /* The CsvTable object to construct */
427 int bHeader = -1; /* header= flags. -1 means not seen yet */
428 int rc = SQLITE_OK; /* Result code from this routine */
drhadcba642016-06-02 17:44:24 +0000429 int i, j; /* Loop counters */
430 int tstFlags = 0; /* Value for testflags=N parameter */
drh1fc1a0f2016-05-31 18:44:33 +0000431 int nCol = -99; /* Value of the columns= parameter */
432 CsvReader sRdr; /* A CSV file reader used to store an error
433 ** message and/or to count the number of columns */
drhadcba642016-06-02 17:44:24 +0000434 static const char *azParam[] = {
435 "filename", "data", "schema",
436 };
437 char *azPValue[3]; /* Parameter values */
438# define CSV_FILENAME (azPValue[0])
439# define CSV_DATA (azPValue[1])
440# define CSV_SCHEMA (azPValue[2])
drh724b1892016-05-31 16:22:48 +0000441
drhadcba642016-06-02 17:44:24 +0000442
443 assert( sizeof(azPValue)==sizeof(azParam) );
drh724b1892016-05-31 16:22:48 +0000444 memset(&sRdr, 0, sizeof(sRdr));
drhadcba642016-06-02 17:44:24 +0000445 memset(azPValue, 0, sizeof(azPValue));
drh724b1892016-05-31 16:22:48 +0000446 for(i=3; i<argc; i++){
447 const char *z = argv[i];
448 const char *zValue;
drhadcba642016-06-02 17:44:24 +0000449 for(j=0; j<sizeof(azParam)/sizeof(azParam[0]); j++){
450 if( csv_string_parameter(&sRdr, azParam[j], z, &azPValue[j]) ) break;
451 }
452 if( j<sizeof(azParam)/sizeof(azParam[0]) ){
453 if( sRdr.zErr[0] ) goto csvtab_connect_error;
drh724b1892016-05-31 16:22:48 +0000454 }else
455 if( (zValue = csv_parameter("header",6,z))!=0 ){
456 int x;
457 if( bHeader>=0 ){
458 csv_errmsg(&sRdr, "more than one 'header' parameter");
459 goto csvtab_connect_error;
460 }
461 x = csv_boolean(zValue);
462 if( x==1 ){
463 bHeader = 1;
464 }else if( x==0 ){
465 bHeader = 0;
466 }else{
467 csv_errmsg(&sRdr, "unrecognized argument to 'header': %s", zValue);
468 goto csvtab_connect_error;
469 }
470 }else
drhabfd2722016-05-31 18:08:35 +0000471 if( (zValue = csv_parameter("testflags",9,z))!=0 ){
472 tstFlags = (unsigned int)atoi(zValue);
473 }else
drh1fc1a0f2016-05-31 18:44:33 +0000474 if( (zValue = csv_parameter("columns",7,z))!=0 ){
475 if( nCol>0 ){
476 csv_errmsg(&sRdr, "more than one 'columns' parameter");
477 goto csvtab_connect_error;
478 }
479 nCol = atoi(zValue);
480 if( nCol<=0 ){
481 csv_errmsg(&sRdr, "must have at least one column");
482 goto csvtab_connect_error;
483 }
484 }else
drh724b1892016-05-31 16:22:48 +0000485 {
486 csv_errmsg(&sRdr, "unrecognized parameter '%s'", z);
487 goto csvtab_connect_error;
488 }
489 }
drhadcba642016-06-02 17:44:24 +0000490 if( (CSV_FILENAME==0)==(CSV_DATA==0) ){
491 csv_errmsg(&sRdr, "must either filename= or data= but not both");
drh724b1892016-05-31 16:22:48 +0000492 goto csvtab_connect_error;
493 }
drhadcba642016-06-02 17:44:24 +0000494 if( nCol<=0 && csv_reader_open(&sRdr, CSV_FILENAME, CSV_DATA) ){
drh724b1892016-05-31 16:22:48 +0000495 goto csvtab_connect_error;
496 }
497 pNew = sqlite3_malloc( sizeof(*pNew) );
498 *ppVtab = (sqlite3_vtab*)pNew;
499 if( pNew==0 ) goto csvtab_connect_oom;
500 memset(pNew, 0, sizeof(*pNew));
drh1fc1a0f2016-05-31 18:44:33 +0000501 if( nCol>0 ){
502 pNew->nCol = nCol;
503 }else{
504 do{
505 const char *z = csv_read_one_field(&sRdr);
506 if( z==0 ) goto csvtab_connect_oom;
507 pNew->nCol++;
508 }while( sRdr.cTerm==',' );
509 }
drhadcba642016-06-02 17:44:24 +0000510 pNew->zFilename = CSV_FILENAME; CSV_FILENAME = 0;
511 pNew->zData = CSV_DATA; CSV_DATA = 0;
drhabfd2722016-05-31 18:08:35 +0000512 pNew->tstFlags = tstFlags;
drh724b1892016-05-31 16:22:48 +0000513 pNew->iStart = bHeader==1 ? ftell(sRdr.in) : 0;
514 csv_reader_reset(&sRdr);
drhadcba642016-06-02 17:44:24 +0000515 if( CSV_SCHEMA==0 ){
drh724b1892016-05-31 16:22:48 +0000516 char *zSep = "";
drhadcba642016-06-02 17:44:24 +0000517 CSV_SCHEMA = sqlite3_mprintf("CREATE TABLE x(");
518 if( CSV_SCHEMA==0 ) goto csvtab_connect_oom;
drh724b1892016-05-31 16:22:48 +0000519 for(i=0; i<pNew->nCol; i++){
drhadcba642016-06-02 17:44:24 +0000520 CSV_SCHEMA = sqlite3_mprintf("%z%sc%d TEXT",CSV_SCHEMA, zSep, i);
drh724b1892016-05-31 16:22:48 +0000521 zSep = ",";
522 }
drhadcba642016-06-02 17:44:24 +0000523 CSV_SCHEMA = sqlite3_mprintf("%z);", CSV_SCHEMA);
drh724b1892016-05-31 16:22:48 +0000524 }
drhadcba642016-06-02 17:44:24 +0000525 rc = sqlite3_declare_vtab(db, CSV_SCHEMA);
drh724b1892016-05-31 16:22:48 +0000526 if( rc ) goto csvtab_connect_error;
drhadcba642016-06-02 17:44:24 +0000527 for(i=0; i<sizeof(azPValue)/sizeof(azPValue[0]); i++){
528 sqlite3_free(azPValue[i]);
529 }
drh724b1892016-05-31 16:22:48 +0000530 return SQLITE_OK;
531
532csvtab_connect_oom:
533 rc = SQLITE_NOMEM;
534 csv_errmsg(&sRdr, "out of memory");
535
536csvtab_connect_error:
537 if( pNew ) csvtabDisconnect(&pNew->base);
drhadcba642016-06-02 17:44:24 +0000538 for(i=0; i<sizeof(azPValue)/sizeof(azPValue[0]); i++){
539 sqlite3_free(azPValue[i]);
540 }
drh724b1892016-05-31 16:22:48 +0000541 if( sRdr.zErr[0] ){
542 sqlite3_free(*pzErr);
543 *pzErr = sqlite3_mprintf("%s", sRdr.zErr);
544 }
545 csv_reader_reset(&sRdr);
drhabfd2722016-05-31 18:08:35 +0000546 if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
drh724b1892016-05-31 16:22:48 +0000547 return rc;
548}
549
550/*
551** Reset the current row content held by a CsvCursor.
552*/
553static void csvtabCursorRowReset(CsvCursor *pCur){
554 CsvTable *pTab = (CsvTable*)pCur->base.pVtab;
555 int i;
556 for(i=0; i<pTab->nCol; i++){
557 sqlite3_free(pCur->azVal[i]);
558 pCur->azVal[i] = 0;
559 }
560}
561
562/*
563** The xConnect and xCreate methods do the same thing, but they must be
564** different so that the virtual table is not an eponymous virtual table.
565*/
566static int csvtabCreate(
567 sqlite3 *db,
568 void *pAux,
569 int argc, const char *const*argv,
570 sqlite3_vtab **ppVtab,
571 char **pzErr
572){
573 return csvtabConnect(db, pAux, argc, argv, ppVtab, pzErr);
574}
575
576/*
577** Destructor for a CsvCursor.
578*/
579static int csvtabClose(sqlite3_vtab_cursor *cur){
580 CsvCursor *pCur = (CsvCursor*)cur;
581 csvtabCursorRowReset(pCur);
582 csv_reader_reset(&pCur->rdr);
583 sqlite3_free(cur);
584 return SQLITE_OK;
585}
586
587/*
588** Constructor for a new CsvTable cursor object.
589*/
590static int csvtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
591 CsvTable *pTab = (CsvTable*)p;
592 CsvCursor *pCur;
593 pCur = sqlite3_malloc( sizeof(*pCur) * sizeof(char*)*pTab->nCol );
594 if( pCur==0 ) return SQLITE_NOMEM;
595 memset(pCur, 0, sizeof(*pCur) + sizeof(char*)*pTab->nCol );
596 pCur->azVal = (char**)&pCur[1];
597 *ppCursor = &pCur->base;
drhadcba642016-06-02 17:44:24 +0000598 if( csv_reader_open(&pCur->rdr, pTab->zFilename, pTab->zData) ){
drh724b1892016-05-31 16:22:48 +0000599 csv_xfer_error(pTab, &pCur->rdr);
600 return SQLITE_ERROR;
601 }
602 return SQLITE_OK;
603}
604
605
606/*
607** Advance a CsvCursor to its next row of input.
608** Set the EOF marker if we reach the end of input.
609*/
610static int csvtabNext(sqlite3_vtab_cursor *cur){
611 CsvCursor *pCur = (CsvCursor*)cur;
612 CsvTable *pTab = (CsvTable*)cur->pVtab;
613 int i = 0;
614 char *z;
615 csvtabCursorRowReset(pCur);
616 do{
617 z = csv_read_one_field(&pCur->rdr);
618 if( z==0 ){
619 csv_xfer_error(pTab, &pCur->rdr);
620 break;
621 }
622 z = sqlite3_mprintf("%s", z);
623 if( z==0 ){
624 csv_errmsg(&pCur->rdr, "out of memory");
625 csv_xfer_error(pTab, &pCur->rdr);
626 break;
627 }
628 if( i<pTab->nCol ){
629 pCur->azVal[i++] = z;
630 }
631 }while( z!=0 && pCur->rdr.cTerm==',' );
632 if( z==0 || pCur->rdr.cTerm==EOF ){
633 pCur->iRowid = -1;
634 }else{
635 pCur->iRowid++;
636 }
637 return SQLITE_OK;
638}
639
640/*
641** Return values of columns for the row at which the CsvCursor
642** is currently pointing.
643*/
644static int csvtabColumn(
645 sqlite3_vtab_cursor *cur, /* The cursor */
646 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
647 int i /* Which column to return */
648){
649 CsvCursor *pCur = (CsvCursor*)cur;
650 CsvTable *pTab = (CsvTable*)cur->pVtab;
651 if( i>=0 && i<pTab->nCol && pCur->azVal[i]!=0 ){
652 sqlite3_result_text(ctx, pCur->azVal[i], -1, SQLITE_STATIC);
653 }
654 return SQLITE_OK;
655}
656
657/*
658** Return the rowid for the current row.
659*/
660static int csvtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
661 CsvCursor *pCur = (CsvCursor*)cur;
662 *pRowid = pCur->iRowid;
663 return SQLITE_OK;
664}
665
666/*
667** Return TRUE if the cursor has been moved off of the last
668** row of output.
669*/
670static int csvtabEof(sqlite3_vtab_cursor *cur){
671 CsvCursor *pCur = (CsvCursor*)cur;
672 return pCur->iRowid<0;
673}
674
675/*
676** Only a full table scan is supported. So xFilter simply rewinds to
677** the beginning.
678*/
679static int csvtabFilter(
680 sqlite3_vtab_cursor *pVtabCursor,
681 int idxNum, const char *idxStr,
682 int argc, sqlite3_value **argv
683){
684 CsvCursor *pCur = (CsvCursor*)pVtabCursor;
685 CsvTable *pTab = (CsvTable*)pVtabCursor->pVtab;
686 pCur->iRowid = 0;
drhadcba642016-06-02 17:44:24 +0000687 if( pCur->rdr.in==0 ){
688 assert( pCur->rdr.zIn==pTab->zData );
689 assert( pTab->iStart<=pCur->rdr.nIn );
690 pCur->rdr.iIn = pTab->iStart;
691 }else{
692 fseek(pCur->rdr.in, pTab->iStart, SEEK_SET);
693 pCur->rdr.iIn = 0;
694 pCur->rdr.nIn = 0;
695 }
drh724b1892016-05-31 16:22:48 +0000696 return csvtabNext(pVtabCursor);
697}
698
699/*
drhadcba642016-06-02 17:44:24 +0000700** Only a forward full table scan is supported. xBestIndex is mostly
drhabfd2722016-05-31 18:08:35 +0000701** a no-op. If CSVTEST_FIDX is set, then the presence of equality
702** constraints lowers the estimated cost, which is fiction, but is useful
703** for testing certain kinds of virtual table behavior.
drh724b1892016-05-31 16:22:48 +0000704*/
705static int csvtabBestIndex(
706 sqlite3_vtab *tab,
707 sqlite3_index_info *pIdxInfo
708){
drhabfd2722016-05-31 18:08:35 +0000709 CsvTable *pTab = (CsvTable*)tab;
710 int i;
drh1fc1a0f2016-05-31 18:44:33 +0000711 int nConst = 0;
drhabfd2722016-05-31 18:08:35 +0000712 pIdxInfo->estimatedCost = 1000000;
713 if( (pTab->tstFlags & CSVTEST_FIDX)==0 ){
714 return SQLITE_OK;
715 }
drhadcba642016-06-02 17:44:24 +0000716 /* The usual (and sensible) case is to take the "return SQLITE_OK" above.
717 ** The code below only runs when testflags=1. The code below
drh1fc1a0f2016-05-31 18:44:33 +0000718 ** generates an artifical and unrealistic plan which is useful
drhadcba642016-06-02 17:44:24 +0000719 ** for testing virtual table logic but is not helpfulto real applications.
720 **
721 ** Any ==, LIKE, or GLOB constraint is marked as usable by the virtual
722 ** table (even though it is not) and the cost of running the virtual table
723 ** is reduced from 1 million to just 10. The constraints are *not* marked
724 ** as omittable, however, so the query planner should still generate a
725 ** plan that gives a correct answer, even if they plan is not optimal.
726 */
drhabfd2722016-05-31 18:08:35 +0000727 for(i=0; i<pIdxInfo->nConstraint; i++){
drh1fc1a0f2016-05-31 18:44:33 +0000728 unsigned char op;
drhabfd2722016-05-31 18:08:35 +0000729 if( pIdxInfo->aConstraint[i].usable==0 ) continue;
drh1fc1a0f2016-05-31 18:44:33 +0000730 op = pIdxInfo->aConstraint[i].op;
731 if( op==SQLITE_INDEX_CONSTRAINT_EQ
732 || op==SQLITE_INDEX_CONSTRAINT_LIKE
733 || op==SQLITE_INDEX_CONSTRAINT_GLOB
734 ){
drhabfd2722016-05-31 18:08:35 +0000735 pIdxInfo->estimatedCost = 10;
drh1fc1a0f2016-05-31 18:44:33 +0000736 pIdxInfo->aConstraintUsage[nConst].argvIndex = nConst+1;
737 nConst++;
drhabfd2722016-05-31 18:08:35 +0000738 }
739 }
drh724b1892016-05-31 16:22:48 +0000740 return SQLITE_OK;
741}
742
743
744static sqlite3_module CsvModule = {
745 0, /* iVersion */
746 csvtabCreate, /* xCreate */
747 csvtabConnect, /* xConnect */
748 csvtabBestIndex, /* xBestIndex */
749 csvtabDisconnect, /* xDisconnect */
750 csvtabDisconnect, /* xDestroy */
751 csvtabOpen, /* xOpen - open a cursor */
752 csvtabClose, /* xClose - close a cursor */
753 csvtabFilter, /* xFilter - configure scan constraints */
754 csvtabNext, /* xNext - advance a cursor */
755 csvtabEof, /* xEof - check for end of scan */
756 csvtabColumn, /* xColumn - read data */
757 csvtabRowid, /* xRowid - read data */
758 0, /* xUpdate */
759 0, /* xBegin */
760 0, /* xSync */
761 0, /* xCommit */
762 0, /* xRollback */
763 0, /* xFindMethod */
764 0, /* xRename */
765};
766
767#ifdef _WIN32
768__declspec(dllexport)
769#endif
770/*
771** This routine is called when the extension is loaded. The new
772** CSV virtual table module is registered with the calling database
773** connection.
774*/
775int sqlite3_csv_init(
776 sqlite3 *db,
777 char **pzErrMsg,
778 const sqlite3_api_routines *pApi
779){
780 SQLITE_EXTENSION_INIT2(pApi);
781 return sqlite3_create_module(db, "csv", &CsvModule, 0);
782}