SQLite

Check-in [ec08b15f77]
Login

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

Overview
Comment:Removed line limit on rows.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | csv_ext
Files: files | file ages | folders
SHA1: ec08b15f77741efd38dadd6aff492ccbb841471e
User & Date: shaneh 2009-11-05 04:01:04.000
Context
2009-11-05
04:14
Update maxRow size after shrinking the row buffer (CSV). (Closed-Leaf check-in: d474195a99 user: shaneh tags: csv_ext)
04:01
Removed line limit on rows. (check-in: ec08b15f77 user: shaneh tags: csv_ext)
02:34
Initial implementation of a virtual table for CSV files. (check-in: 90e63b7d84 user: shaneh tags: csv_ext)
Changes
Unified Diff Ignore Whitespace Patch
Changes to ext/csv/csv.c.
52
53
54
55
56
57
58

59
60
61
62
63
64
65
66
  char *zDb;                   /* Name of database containing CSV table */
  char *zName;                 /* Name of CSV table */ 
  char *zFile;                 /* Name of CSV file */ 
  int nBusy;                   /* Current number of users of this structure */
  FILE *f;                     /* File pointer for source CSV file */
  long offsetFirstRow;         /* ftell position of first row */
  int eof;                     /* True when at end of file */

  char zRow[4096];             /* Buffer for current CSV row */
  char cDelim;                 /* Character to use for delimiting columns */
  int nCol;                    /* Number of columns in current row */
  int maxCol;                  /* Size of aCols array */
  char **aCols;                /* Array of parsed columns */
};









>
|







52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  char *zDb;                   /* Name of database containing CSV table */
  char *zName;                 /* Name of CSV table */ 
  char *zFile;                 /* Name of CSV file */ 
  int nBusy;                   /* Current number of users of this structure */
  FILE *f;                     /* File pointer for source CSV file */
  long offsetFirstRow;         /* ftell position of first row */
  int eof;                     /* True when at end of file */
  int maxRow;                  /* Size of zRow buffer */
  char *zRow;                  /* Buffer for current CSV row */
  char cDelim;                 /* Character to use for delimiting columns */
  int nCol;                    /* Number of columns in current row */
  int maxCol;                  /* Size of aCols array */
  char **aCols;                /* Array of parsed columns */
};


100
101
102
103
104
105
106













107



108






































109
110
111
112
113
114
115
}
static int csv_seek( CSV *pCSV, long pos ){
  return fseek( pCSV->f, pos, SEEK_SET );
}
static long csv_tell( CSV *pCSV ){
  return ftell( pCSV->f );
}













static char *csv_gets( CSV *pCSV ){



  return fgets( pCSV->zRow, sizeof(pCSV->zRow), pCSV->f );






































}


/* 
** CSV virtual table module xCreate method.
*/
static int csvCreate(







>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
}
static int csv_seek( CSV *pCSV, long pos ){
  return fseek( pCSV->f, pos, SEEK_SET );
}
static long csv_tell( CSV *pCSV ){
  return ftell( pCSV->f );
}


/*
** This routine reads a line of text from FILE in, stores
** the text in memory obtained from malloc() and returns a pointer
** to the text.  NULL is returned at end of file, or if malloc()
** fails.
**
** The interface is like "readline" but no command-line editing
** is done.
**
** This code was modified from existing code in shell.c of the sqlite3 CLI.
*/
static char *csv_getline( CSV *pCSV ){
  int n = 0;
  int bEol = 0;
  int bShrink = 0;

  /* allocate initial row buffer */
  if( pCSV->maxRow < 1 ){
    pCSV->zRow = sqlite3_malloc( 100 );
    if( pCSV->zRow ){
      pCSV->maxRow = 100;
    }
  }
  if( !pCSV->zRow ) return 0;

  /* read until eol */
  while( !bEol ){
    /* grow row buffer as needed */
    if( n+100>pCSV->maxRow ){
      int newSize = pCSV->maxRow*2 + 100;
      char *p = sqlite3_realloc(pCSV->zRow, newSize);
      if( !p ) return 0;
      pCSV->maxRow = newSize;
      pCSV->zRow = p;
      bShrink = -1;
    }
    if( fgets(&pCSV->zRow[n], pCSV->maxRow-n, pCSV->f)==0 ){
      if( n==0 ){
        break;
      }
      pCSV->zRow[n] = '\0';
      bEol = -1;
      break;
    }
    /* look for line delimiter */
    while( pCSV->zRow[n] ){ n++; }
    if( (n>0) && ((pCSV->zRow[n-1]=='\n') || (pCSV->zRow[n-1]=='\r')) ){
      pCSV->zRow[n-1] = '\n'; /* uniform line ending */
      pCSV->zRow[n] = '\0';
      bEol = -1;
    }
  }
  if( bShrink ){ pCSV->zRow = realloc( pCSV->zRow, n+1 ); }
  return bEol ? pCSV->zRow : 0;
}


/* 
** CSV virtual table module xCreate method.
*/
static int csvCreate(
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269

270
271
272
273
274
275
276
277
278
279

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
** CSV virtual table module xNext method.
*/
static int csvNext( sqlite3_vtab_cursor* pVtabCursor ){
  CSV *pCSV = (CSV *)pVtabCursor->pVtab;
  CSVCursor *pCsr = (CSVCursor *)pVtabCursor;
  int nCol = 0;
  char *s;
  char zDelims[4] = ",\r\n";
  char cDelim; /* char that delimited current col */

  if( pCSV->eof ){
    return SQLITE_ERROR;
  }

  /* update the cursor */
  pCsr->csvpos = csv_tell( pCSV );

  /* read the next row of data */
  s = csv_gets( pCSV );
  if( !s ){
    /* and error or eof occured */
    pCSV->eof = -1;
    return SQLITE_OK;
  }

  /* allocate initial space for the column pointers */
  if( pCSV->maxCol < 1 ){
    /* take a guess */
    pCSV->maxCol = (int)(strlen(pCSV->zRow) / 5 + 1);
    if( pCSV->aCols ) sqlite3_free( pCSV->aCols );
    pCSV->aCols = (char **)sqlite3_malloc( sizeof(char*) * pCSV->maxCol );
    if( !pCSV->aCols ){
      /* out of memory */
      return SQLITE_NOMEM;
    }
  }


  /* add custom delim character */
  zDelims[0] = pCSV->cDelim;

  /* parse the zRow into individual columns */
  do{
    /* if it begins with a quote, assume it's a quoted col */
    if( *s=='\"' ){
      s++;  /* skip quote */
      pCSV->aCols[nCol] = s; /* save pointer for this col */

      /* find closing quote */
#if 1
      s = strchr(s, '\"');
      if( !s ){
        /* no closing quote */
        pCSV->eof = -1;
        return SQLITE_ERROR;
      }
      *s = '\0'; /* null terminate this col */
      /* fall through and look for following ",\n\r" */
      s++;
#else
      /* TBD: handle escaped quotes "" */
      while( s[0] ){
        if( s[0]=='\"' ){
          if( s[1]=='\"' ){
          }
          break;
        }
        s++;
      }
#endif
    }else{
      pCSV->aCols[nCol] = s; /* save pointer for this col */
    }
    s = strpbrk(s, zDelims);
    if( !s ){
      /* no col delimiter */
      pCSV->eof = -1;
      return SQLITE_ERROR;
    }
    cDelim = *s;
    /* null terminate the column by overwriting the delimiter */
    *s = '\0';
    nCol++;
    /* if end of zRow, stop parsing cols */
    if( (cDelim == '\n') || (cDelim == '\r') ) break;
    /* move to start of next col */
    s++; /* skip delimiter */

    if(nCol >= pCSV->maxCol ){
      /* we need to grow our col pointer array */
      char **p = (char **)sqlite3_realloc( pCSV->aCols, sizeof(char*) * (nCol+5) );
      if( !p ){







|










|









|
<
|
|
|
<


>










>

<







|

<
<
<
<
<
<
<
<
<
<
<














|







289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
341
342
343
344











345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
** CSV virtual table module xNext method.
*/
static int csvNext( sqlite3_vtab_cursor* pVtabCursor ){
  CSV *pCSV = (CSV *)pVtabCursor->pVtab;
  CSVCursor *pCsr = (CSVCursor *)pVtabCursor;
  int nCol = 0;
  char *s;
  char zDelims[3] = ",\n";
  char cDelim; /* char that delimited current col */

  if( pCSV->eof ){
    return SQLITE_ERROR;
  }

  /* update the cursor */
  pCsr->csvpos = csv_tell( pCSV );

  /* read the next row of data */
  s = csv_getline( pCSV );
  if( !s ){
    /* and error or eof occured */
    pCSV->eof = -1;
    return SQLITE_OK;
  }

  /* allocate initial space for the column pointers */
  if( pCSV->maxCol < 1 ){
    /* take a guess */
    int maxCol = (int)(strlen(pCSV->zRow) / 5 + 1);

    pCSV->aCols = (char **)sqlite3_malloc( sizeof(char*) * maxCol );
    if( pCSV->aCols ){
      pCSV->maxCol = maxCol;

    }
  }
  if( !pCSV->aCols ) return SQLITE_NOMEM;

  /* add custom delim character */
  zDelims[0] = pCSV->cDelim;

  /* parse the zRow into individual columns */
  do{
    /* if it begins with a quote, assume it's a quoted col */
    if( *s=='\"' ){
      s++;  /* skip quote */
      pCSV->aCols[nCol] = s; /* save pointer for this col */
      /* TBD: handle escaped quotes "" */
      /* find closing quote */

      s = strchr(s, '\"');
      if( !s ){
        /* no closing quote */
        pCSV->eof = -1;
        return SQLITE_ERROR;
      }
      *s = '\0'; /* null terminate this col */
      /* fall through and look for following ",\n" */
      s++;











    }else{
      pCSV->aCols[nCol] = s; /* save pointer for this col */
    }
    s = strpbrk(s, zDelims);
    if( !s ){
      /* no col delimiter */
      pCSV->eof = -1;
      return SQLITE_ERROR;
    }
    cDelim = *s;
    /* null terminate the column by overwriting the delimiter */
    *s = '\0';
    nCol++;
    /* if end of zRow, stop parsing cols */
    if( cDelim == '\n' ) break;
    /* move to start of next col */
    s++; /* skip delimiter */

    if(nCol >= pCSV->maxCol ){
      /* we need to grow our col pointer array */
      char **p = (char **)sqlite3_realloc( pCSV->aCols, sizeof(char*) * (nCol+5) );
      if( !p ){
423
424
425
426
427
428
429

430
431
432
433
434
435
436
static int csvRelease( CSV *pCSV ){
  pCSV->nBusy--;
  if( pCSV->nBusy<1 ){

    /* finalize any prepared statements here */

    csv_close( pCSV );

    if( pCSV->aCols ) sqlite3_free( pCSV->aCols );
    sqlite3_free( pCSV );
  }
  return 0;
}









>







466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
static int csvRelease( CSV *pCSV ){
  pCSV->nBusy--;
  if( pCSV->nBusy<1 ){

    /* finalize any prepared statements here */

    csv_close( pCSV );
    if( pCSV->zRow ) sqlite3_free( pCSV->zRow );
    if( pCSV->aCols ) sqlite3_free( pCSV->aCols );
    sqlite3_free( pCSV );
  }
  return 0;
}