1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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
|
/*****************************************************************************
*
* MODULE: DBF driver
*
* AUTHOR(S): Radim Blazek
*
* PURPOSE: Simple driver for reading and writing dbf files
*
* COPYRIGHT: (C) 2000 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*****************************************************************************/
#include <dbmi.h>
#include "globals.h"
#include "proto.h"
int
db__driver_fetch(cn, position, more)
dbCursor *cn;
int position;
int *more;
{
cursor *c;
dbToken token;
dbTable *table;
dbColumn *column;
dbValue *value;
int col, ncols;
int htype, sqltype, ctype;
int dbfrow, dbfcol;
/* get cursor token */
token = db_get_cursor_token(cn);
/* get the cursor by its token */
if (!(c = (cursor *) db_find_token(token)))
{
db_error("cursor not found");
return DB_FAILED;
}
/* fetch on position */
switch (position)
{
case DB_NEXT:
c->cur++;
break;
case DB_CURRENT:
break;
case DB_PREVIOUS:
c->cur--;
break;
case DB_FIRST:
c->cur = 0;
break;
case DB_LAST:
c->cur = c->nrows - 1;
break;
};
if ( (c->cur >= c->nrows) || (c->cur < 0) )
{
*more = 0;
return DB_OK;
}
*more = 1;
/* get the data out of the descriptor into the table */
table = db_get_cursor_table(cn);
ncols = db_get_table_number_of_columns (table);
dbfrow = c->set[c->cur];
for (col = 1; col <= ncols; col++)
{
dbfcol = c->cols[col-1];
column = db_get_table_column (table, col-1);
value = db_get_column_value (column);
db_free_string (&value->s);
sqltype = db_get_column_sqltype(column);
ctype = db_sqltype_to_Ctype(sqltype);
htype = db_get_column_host_type(column);
if ( db.tables[c->table].rows[dbfrow].values[dbfcol].is_null ) {
db_set_value_null ( value ) ;
} else {
db_set_value_not_null ( value ) ;
switch (ctype)
{
case DB_C_TYPE_STRING:
db_set_string ( &(value->s), db.tables[c->table].rows[dbfrow].values[dbfcol].c);
break;
case DB_C_TYPE_INT:
value->i = db.tables[c->table].rows[dbfrow].values[dbfcol].i;
break;
case DB_C_TYPE_DOUBLE:
value->d = db.tables[c->table].rows[dbfrow].values[dbfcol].d;
break;
}
}
}
return DB_OK;
}
int
db__driver_get_num_rows (cn )
dbCursor *cn;
{
cursor *c;
dbToken token;
/* get cursor token */
token = db_get_cursor_token(cn);
/* get the cursor by its token */
if (!(c = (cursor *) db_find_token(token)))
{
db_error("cursor not found");
return DB_FAILED;
}
return ( c->nrows );
}
|