2012 July 27
The author disclaims copyright to this source code. In place of a legal notice, here is a blessing:
May you do good and not evil. May you find forgiveness for yourself and forgive others. May you share freely, never taking more than you give.
Definition in file csvtable.c.#include <sqlite3ext.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
Go to the source code of this file.
| Data Structures | |
| struct | csv_cursor | 
| Structure to describe CSV virtual table cursor. More... | |
| struct | csv_file | 
| Structure to implement CSV file handle. More... | |
| struct | csv_guess_fmt | 
| Info to guess CSV layout. More... | |
| struct | csv_vtab | 
| Structure to describe a CSV virtual table. More... | |
| Typedefs | |
| typedef csv_file | csv_file | 
| typedef csv_guess_fmt | csv_guess_fmt | 
| typedef csv_vtab | csv_vtab | 
| Functions | |
| void | append_free (char **in) | 
| Free dynamically allocated string buffer. | |
| char * | append (char **in, char const *append, char quote) | 
| Append a string to dynamically allocated string buffer with optional quoting. | |
| char * | unquote (char const *in) | 
| Strip off quotes given string. | |
| int | maptype (char const *type) | 
| Map string to SQLite data type. | |
| void | conv_names (char **names, int ncols) | 
| Convert and collapse white space in column names to underscore. | |
| void | result_or_bind (sqlite3_context *ctx, sqlite3_stmt *stmt, int idx, char *data, int len, int type) | 
| Make result data or parameter binding accoring to type. | |
| int | process_col (sqlite3_context *ctx, sqlite3_stmt *stmt, int idx, char *data, int type, int conv) | 
| Process one column of the current row. | |
| csv_file * | csv_open (const char *filename, const char *sep, const char *quot) | 
| Open CSV file for reading and return handle to it. | |
| void | csv_close (csv_file *csv) | 
| Close CSV file handle. | |
| int | csv_eof (csv_file *csv) | 
| Test EOF on CSV file handle. | |
| long | csv_seek (csv_file *csv, long pos) | 
| Position CSV file handle. | |
| void | csv_rewind (csv_file *csv) | 
| Rewind CSV file handle. | |
| long | csv_tell (csv_file *csv) | 
| Return current position of CSV file handle. | |
| int | csv_getline (csv_file *csv, csv_guess_fmt *guess) | 
| Read and process one line of CSV file handle. | |
| int | csv_ncols (csv_file *csv) | 
| Return number of columns of current row in CSV file. | |
| char * | csv_coldata (csv_file *csv, int n) | 
| Return nth column of current row in CSV file. | |
| int | csv_guess (csv_file *csv) | 
| Guess CSV layout of CSV file handle. | |
| int | csv_vtab_connect (sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp) | 
| Connect to virtual table. | |
| int | csv_vtab_create (sqlite3 *db, void *aux, int argc, const char *const *argv, sqlite3_vtab **vtabp, char **errp) | 
| Create virtual table. | |
| int | csv_vtab_disconnect (sqlite3_vtab *vtab) | 
| Disconnect virtual table. | |
| int | csv_vtab_destroy (sqlite3_vtab *vtab) | 
| Destroy virtual table. | |
| int | csv_vtab_bestindex (sqlite3_vtab *vtab, sqlite3_index_info *info) | 
| Determines information for filter function according to constraints. | |
| int | csv_vtab_open (sqlite3_vtab *vtab, sqlite3_vtab_cursor **cursorp) | 
| Open virtual table and return cursor. | |
| int | csv_vtab_close (sqlite3_vtab_cursor *cursor) | 
| Close virtual table cursor. | |
| int | csv_vtab_next (sqlite3_vtab_cursor *cursor) | 
| Retrieve next row from virtual table cursor. | |
| int | csv_vtab_filter (sqlite3_vtab_cursor *cursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv) | 
| Filter function for virtual table. | |
| int | csv_vtab_eof (sqlite3_vtab_cursor *cursor) | 
| Return end of table state of virtual table cursor. | |
| int | csv_vtab_column (sqlite3_vtab_cursor *cursor, sqlite3_context *ctx, int n) | 
| Return column data of virtual table. | |
| int | csv_vtab_rowid (sqlite3_vtab_cursor *cursor, sqlite_int64 *rowidp) | 
| Return current rowid of virtual table cursor. | |
| void | csv_import_func (sqlite3_context *ctx, int argc, sqlite3_value **argv) | 
| Import CSV file as table into database. | |
| int | csv_vtab_init (sqlite3 *db) | 
| Module initializer creating SQLite functions and modules. | |
| int | sqlite3_extension_init (sqlite3 *db, char **errmsg, const sqlite3_api_routines *api) | 
| Initializer for SQLite extension load mechanism. | |
| Variables | |
| const sqlite3_module | csv_vtab_mod | 
| SQLite module descriptor. | |
| 
 | 
| 
 | 
| 
 | 
| 
 | 
| 
 | 
| 
 | 
| 
 | ||||||||||||||||
| Append a string to dynamically allocated string buffer with optional quoting. 
 
 
 Definition at line 118 of file csvtable.c. References append(). Referenced by append(), csv_import_func(), csv_vtab_connect(), and dump_cb(). | 
| 
 | 
| Free dynamically allocated string buffer. 
 
 Definition at line 97 of file csvtable.c. Referenced by csv_import_func(), csv_vtab_connect(), and dump_cb(). | 
| 
 | ||||||||||||
| Convert and collapse white space in column names to underscore. 
 
 Definition at line 246 of file csvtable.c. Referenced by csv_import_func(), and csv_vtab_connect(). | 
| 
 | 
| Close CSV file handle. 
 
 Definition at line 555 of file csvtable.c. References csv_file::cols, csv_file::f, csv_file::line, csv_file::quot, and csv_file::sep. Referenced by csv_import_func(), csv_vtab_connect(), and csv_vtab_disconnect(). | 
| 
 | ||||||||||||
| Return nth column of current row in CSV file. 
 
 
 Definition at line 841 of file csvtable.c. References csv_file::cols, and csv_file::ncols. Referenced by csv_import_func(), and csv_vtab_column(). | 
| 
 | 
| Test EOF on CSV file handle. 
 
 
 Definition at line 584 of file csvtable.c. References csv_file::f. Referenced by csv_vtab_eof(). | 
| 
 | ||||||||||||
| Read and process one line of CSV file handle. 
 
 
 Definition at line 644 of file csvtable.c. References csv_file::cols, csv_file::f, csv_guess_fmt::hist, csv_file::isdos, csv_file::line, csv_file::maxc, csv_file::maxl, csv_file::ncols, csv_guess_fmt::nlines, csv_file::quot, and csv_file::sep. Referenced by csv_guess(), csv_import_func(), csv_vtab_connect(), and csv_vtab_next(). | 
| 
 | 
| Guess CSV layout of CSV file handle. 
 
 
 Definition at line 856 of file csvtable.c. References csv_getline(), csv_rewind(), csv_guess_fmt::hist, min, csv_guess_fmt::nlines, csv_file::pos0, csv_file::quot, and csv_file::sep. Referenced by csv_import_func(), and csv_vtab_connect(). | 
| 
 | ||||||||||||||||
| Import CSV file as table into database. 
 
 
argv[0] - name of table to create (required) Translation flags: 
1 - convert ISO-8859-1 to UTF-8 Definition at line 1390 of file csvtable.c. References append(), append_free(), csv_file::cols, conv_names(), csv_close(), csv_coldata(), csv_getline(), csv_guess(), csv_ncols(), csv_open(), csv_rewind(), csv_tell(), maptype(), csv_file::ncols, csv_file::pos0, process_col(), csv_file::quot, and csv_file::sep. Referenced by csv_vtab_init(). | 
| 
 | 
| Return number of columns of current row in CSV file. 
 
 
 Definition at line 825 of file csvtable.c. References csv_file::cols, and csv_file::ncols. Referenced by csv_import_func(), and csv_vtab_connect(). | 
| 
 | ||||||||||||||||
| Open CSV file for reading and return handle to it. 
 
 
 Definition at line 496 of file csvtable.c. References csv_file::cols, csv_file::f, csv_file::isdos, csv_file::line, csv_file::maxc, csv_file::maxl, csv_file::ncols, csv_file::pos0, csv_file::quot, and csv_file::sep. Referenced by csv_import_func(), and csv_vtab_connect(). | 
| 
 | 
| Rewind CSV file handle. 
 
 Definition at line 614 of file csvtable.c. References csv_seek(), csv_file::f, and csv_file::pos0. Referenced by csv_guess(), csv_import_func(), csv_vtab_connect(), csv_vtab_filter(), and csv_vtab_open(). | 
| 
 | ||||||||||||
| Position CSV file handle. 
 
 
 Definition at line 600 of file csvtable.c. References csv_file::f. Referenced by csv_rewind(). | 
| 
 | 
| Return current position of CSV file handle. 
 
 
 Definition at line 628 of file csvtable.c. References csv_file::f. Referenced by csv_import_func(), csv_vtab_connect(), csv_vtab_next(), and csv_vtab_open(). | 
| 
 | ||||||||||||
| Determines information for filter function according to constraints. 
 
 
 Definition at line 1189 of file csvtable.c. | 
| 
 | 
| Close virtual table cursor. 
 
 
 Definition at line 1224 of file csvtable.c. | 
| 
 | ||||||||||||||||
| Return column data of virtual table. 
 
 
 Definition at line 1292 of file csvtable.c. References csv_vtab::coltypes, csv_vtab::convert, csv_vtab::csv, csv_coldata(), csv_cursor::cursor, and process_col(). | 
| 
 | ||||||||||||||||||||||||||||
| Connect to virtual table. 
 
 
 
argv[0] - module name Translation flags: 
1 - convert ISO-8859-1 to UTF-8 Definition at line 966 of file csvtable.c. References append(), append_free(), csv_file::cols, csv_vtab::coltypes, conv_names(), csv_vtab::convert, csv_vtab::csv, csv_close(), csv_getline(), csv_guess(), csv_ncols(), csv_open(), csv_rewind(), csv_tell(), maptype(), csv_file::ncols, csv_file::pos0, csv_file::quot, csv_file::sep, unquote(), and csv_vtab::vtab. Referenced by csv_vtab_create(). | 
| 
 | ||||||||||||||||||||||||||||
| Create virtual table. 
 
 
 Definition at line 1146 of file csvtable.c. References csv_vtab_connect(). | 
| 
 | 
| Destroy virtual table. 
 
 
 Definition at line 1176 of file csvtable.c. References csv_vtab_disconnect(). | 
| 
 | 
| Disconnect virtual table. 
 
 
 Definition at line 1160 of file csvtable.c. References csv_vtab::csv, and csv_close(). Referenced by csv_vtab_destroy(). | 
| 
 | 
| Return end of table state of virtual table cursor. 
 
 
 Definition at line 1275 of file csvtable.c. References csv_vtab::csv, csv_eof(), and csv_cursor::cursor. | 
| 
 | ||||||||||||||||||||||||
| Filter function for virtual table. 
 
 
 Definition at line 1258 of file csvtable.c. References csv_vtab::csv, csv_rewind(), csv_vtab_next(), and csv_cursor::cursor. | 
| 
 | 
| Module initializer creating SQLite functions and modules. 
 
 
 Definition at line 1658 of file csvtable.c. References csv_import_func(), and csv_vtab_mod. Referenced by sqlite3_extension_init(). | 
| 
 | 
| Retrieve next row from virtual table cursor. 
 
 
 Definition at line 1237 of file csvtable.c. References csv_vtab::csv, csv_getline(), csv_tell(), csv_cursor::cursor, and csv_cursor::pos. Referenced by csv_vtab_filter(). | 
| 
 | ||||||||||||
| Open virtual table and return cursor. 
 
 
 Definition at line 1202 of file csvtable.c. References csv_vtab::csv, csv_rewind(), csv_tell(), csv_cursor::cursor, and csv_cursor::pos. | 
| 
 | ||||||||||||
| Return current rowid of virtual table cursor. 
 
 
 Definition at line 1309 of file csvtable.c. References csv_cursor::pos. | 
| 
 | 
| Map string to SQLite data type. 
 
 
 Definition at line 216 of file csvtable.c. Referenced by csv_import_func(), and csv_vtab_connect(). | 
| 
 | ||||||||||||||||||||||||||||
| Process one column of the current row. 
 
 Definition at line 360 of file csvtable.c. References result_or_bind(). Referenced by csv_import_func(), and csv_vtab_column(). | 
| 
 | ||||||||||||||||||||||||||||
| Make result data or parameter binding accoring to type. 
 
 Definition at line 286 of file csvtable.c. Referenced by process_col(). | 
| 
 | ||||||||||||||||
| Initializer for SQLite extension load mechanism. 
 
 
 Definition at line 1676 of file csvtable.c. References csv_vtab_init(). | 
| 
 | 
| Strip off quotes given string. 
 
 
 Definition at line 188 of file csvtable.c. | 
| 
 | 
| Initial value:  {
    1,                   
    csv_vtab_create,     
    csv_vtab_connect,    
    csv_vtab_bestindex,  
    csv_vtab_disconnect, 
    csv_vtab_destroy,    
    csv_vtab_open,       
    csv_vtab_close,      
    csv_vtab_filter,     
    csv_vtab_next,       
    csv_vtab_eof,        
    csv_vtab_column,     
    csv_vtab_rowid,      
    0,                   
    0,                   
    0,                   
    0,                   
    0,                   
    0,                   
}
 Definition at line 1337 of file csvtable.c. Referenced by csv_vtab_init(). |