Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add the testflags parameter to the csv extension. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
b93fb2fe0df1b3bea2bc2a4e1528da74 |
User & Date: | drh 2016-05-31 18:08:35.263 |
Context
2016-05-31
| ||
18:44 | Add the columns=N parameter to the CSV extension. (check-in: 28ebeadd6a user: drh tags: trunk) | |
18:08 | Add the testflags parameter to the csv extension. (check-in: b93fb2fe0d user: drh tags: trunk) | |
16:22 | Add the "csv" virtual table for reading CSV files, as an extension in the ext/misc/ subfolder. (check-in: 00d3570c8b user: drh tags: trunk) | |
Changes
Changes to ext/misc/csv.c.
︙ | ︙ | |||
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | /* An instance of the CSV virtual table */ typedef struct CsvTable { sqlite3_vtab base; /* Base class. Must be first */ char *zFilename; /* Name of the CSV file */ long iStart; /* Offset to start of data in zFilename */ int nCol; /* Number of columns in the CSV file */ } CsvTable; /* A cursor for the CSV virtual table */ typedef struct CsvCursor { sqlite3_vtab_cursor base; /* Base class. Must be first */ CsvReader rdr; /* The CsvReader object */ char **azVal; /* Value of the current row */ sqlite3_int64 iRowid; /* The current rowid. Negative for EOF */ } CsvCursor; | > > > > | 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | /* An instance of the CSV virtual table */ typedef struct CsvTable { sqlite3_vtab base; /* Base class. Must be first */ char *zFilename; /* Name of the CSV file */ long iStart; /* Offset to start of data in zFilename */ int nCol; /* Number of columns in the CSV file */ unsigned int tstFlags; /* Bit values used for testing */ } CsvTable; /* Allowed values for tstFlags */ #define CSVTEST_FIDX 0x0001 /* Pretend that constrained searchs cost less*/ /* A cursor for the CSV virtual table */ typedef struct CsvCursor { sqlite3_vtab_cursor base; /* Base class. Must be first */ CsvReader rdr; /* The CsvReader object */ char **azVal; /* Value of the current row */ sqlite3_int64 iRowid; /* The current rowid. Negative for EOF */ } CsvCursor; |
︙ | ︙ | |||
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | /* ** Parameters: ** filename=FILENAME Required ** schema=SCHEMA Optional ** header=YES|NO First row of CSV defines the names of ** columns if "yes". Default "no". ** ** If header=no and not columns are listed, then the columns are named ** "c0", "c1", "c2", and so forth. */ static int csvtabConnect( sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVtab, char **pzErr ){ CsvTable *pNew = 0; int bHeader = -1; int rc = SQLITE_OK; int i; char *zFilename = 0; char *zSchema = 0; CsvReader sRdr; memset(&sRdr, 0, sizeof(sRdr)); for(i=3; i<argc; i++){ const char *z = argv[i]; const char *zValue; if( (zValue = csv_parameter("filename",8,z))!=0 ){ | > > | 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | /* ** Parameters: ** filename=FILENAME Required ** schema=SCHEMA Optional ** header=YES|NO First row of CSV defines the names of ** columns if "yes". Default "no". ** testflags=N Bitmask of test flags. Optional ** ** If header=no and not columns are listed, then the columns are named ** "c0", "c1", "c2", and so forth. */ static int csvtabConnect( sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVtab, char **pzErr ){ CsvTable *pNew = 0; int bHeader = -1; int rc = SQLITE_OK; int i; char *zFilename = 0; char *zSchema = 0; int tstFlags = 0; CsvReader sRdr; memset(&sRdr, 0, sizeof(sRdr)); for(i=3; i<argc; i++){ const char *z = argv[i]; const char *zValue; if( (zValue = csv_parameter("filename",8,z))!=0 ){ |
︙ | ︙ | |||
369 370 371 372 373 374 375 376 377 378 379 380 381 382 | }else if( x==0 ){ bHeader = 0; }else{ csv_errmsg(&sRdr, "unrecognized argument to 'header': %s", zValue); goto csvtab_connect_error; } }else { csv_errmsg(&sRdr, "unrecognized parameter '%s'", z); goto csvtab_connect_error; } } if( zFilename==0 ){ csv_errmsg(&sRdr, "missing 'filename' parameter"); | > > > | 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | }else if( x==0 ){ bHeader = 0; }else{ csv_errmsg(&sRdr, "unrecognized argument to 'header': %s", zValue); goto csvtab_connect_error; } }else if( (zValue = csv_parameter("testflags",9,z))!=0 ){ tstFlags = (unsigned int)atoi(zValue); }else { csv_errmsg(&sRdr, "unrecognized parameter '%s'", z); goto csvtab_connect_error; } } if( zFilename==0 ){ csv_errmsg(&sRdr, "missing 'filename' parameter"); |
︙ | ︙ | |||
391 392 393 394 395 396 397 398 399 400 401 402 403 404 | memset(pNew, 0, sizeof(*pNew)); do{ const char *z = csv_read_one_field(&sRdr); if( z==0 ) goto csvtab_connect_oom; pNew->nCol++; }while( sRdr.cTerm==',' ); pNew->zFilename = zFilename; zFilename = 0; pNew->iStart = bHeader==1 ? ftell(sRdr.in) : 0; csv_reader_reset(&sRdr); if( zSchema==0 ){ char *zSep = ""; zSchema = sqlite3_mprintf("CREATE TABLE x("); if( zSchema==0 ) goto csvtab_connect_oom; | > | 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | memset(pNew, 0, sizeof(*pNew)); do{ const char *z = csv_read_one_field(&sRdr); if( z==0 ) goto csvtab_connect_oom; pNew->nCol++; }while( sRdr.cTerm==',' ); pNew->zFilename = zFilename; pNew->tstFlags = tstFlags; zFilename = 0; pNew->iStart = bHeader==1 ? ftell(sRdr.in) : 0; csv_reader_reset(&sRdr); if( zSchema==0 ){ char *zSep = ""; zSchema = sqlite3_mprintf("CREATE TABLE x("); if( zSchema==0 ) goto csvtab_connect_oom; |
︙ | ︙ | |||
422 423 424 425 426 427 428 429 430 431 432 433 434 435 | sqlite3_free(zFilename); sqlite3_free(zSchema); if( sRdr.zErr[0] ){ sqlite3_free(*pzErr); *pzErr = sqlite3_mprintf("%s", sRdr.zErr); } csv_reader_reset(&sRdr); return rc; } /* ** Reset the current row content held by a CsvCursor. */ static void csvtabCursorRowReset(CsvCursor *pCur){ | > | 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | sqlite3_free(zFilename); sqlite3_free(zSchema); if( sRdr.zErr[0] ){ sqlite3_free(*pzErr); *pzErr = sqlite3_mprintf("%s", sRdr.zErr); } csv_reader_reset(&sRdr); if( rc==SQLITE_OK ) rc = SQLITE_ERROR; return rc; } /* ** Reset the current row content held by a CsvCursor. */ static void csvtabCursorRowReset(CsvCursor *pCur){ |
︙ | ︙ | |||
567 568 569 570 571 572 573 | CsvTable *pTab = (CsvTable*)pVtabCursor->pVtab; pCur->iRowid = 0; fseek(pCur->rdr.in, pTab->iStart, SEEK_SET); return csvtabNext(pVtabCursor); } /* | | > > > > > > > > > > > > > > > > | 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 | CsvTable *pTab = (CsvTable*)pVtabCursor->pVtab; pCur->iRowid = 0; fseek(pCur->rdr.in, pTab->iStart, SEEK_SET); return csvtabNext(pVtabCursor); } /* ** Only a forwards full table scan is supported. xBestIndex is mostly ** a no-op. If CSVTEST_FIDX is set, then the presence of equality ** constraints lowers the estimated cost, which is fiction, but is useful ** for testing certain kinds of virtual table behavior. */ static int csvtabBestIndex( sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo ){ CsvTable *pTab = (CsvTable*)tab; int i; pIdxInfo->estimatedCost = 1000000; if( (pTab->tstFlags & CSVTEST_FIDX)==0 ){ return SQLITE_OK; } for(i=0; i<pIdxInfo->nConstraint; i++){ if( pIdxInfo->aConstraint[i].usable==0 ) continue; if( pIdxInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ ){ pIdxInfo->estimatedCost = 10; break; } } return SQLITE_OK; } static sqlite3_module CsvModule = { 0, /* iVersion */ csvtabCreate, /* xCreate */ |
︙ | ︙ |