00001 #include <stdlib.h> 00002 #include <grass/dbmi.h> 00003 00010 void db_init_cursor(dbCursor * cursor) 00011 { 00012 cursor->driver = NULL; 00013 cursor->token = -1; 00014 cursor->type = 0; 00015 cursor->mode = 0; 00016 cursor->table = NULL; 00017 cursor->column_flags = NULL; 00018 } 00019 00026 int db_alloc_cursor_table(dbCursor * cursor, int ncols) 00027 { 00028 cursor->table = db_alloc_table(ncols); 00029 if (cursor->table == NULL) 00030 return db_get_error_code(); 00031 return DB_OK; 00032 } 00033 00040 void db_free_cursor(dbCursor * cursor) 00041 { 00042 if (cursor->table) 00043 db_free_table(cursor->table); 00044 if (cursor->column_flags) 00045 db_free_cursor_column_flags(cursor); 00046 db_init_cursor(cursor); 00047 } 00048 00055 dbTable *db_get_cursor_table(dbCursor * cursor) 00056 { 00057 return cursor->table; 00058 } 00059 00066 void db_set_cursor_table(dbCursor * cursor, dbTable * table) 00067 { 00068 cursor->table = table; 00069 } 00070 00077 dbToken db_get_cursor_token(dbCursor * cursor) 00078 { 00079 return cursor->token; 00080 } 00081 00088 void db_set_cursor_token(dbCursor * cursor, dbToken token) 00089 { 00090 cursor->token = token; 00091 } 00092 00099 void db_set_cursor_type_readonly(dbCursor * cursor) 00100 { 00101 cursor->type = DB_READONLY; 00102 } 00103 00110 void db_set_cursor_type_update(dbCursor * cursor) 00111 { 00112 cursor->type = DB_UPDATE; 00113 } 00114 00121 void db_set_cursor_type_insert(dbCursor * cursor) 00122 { 00123 cursor->type = DB_INSERT; 00124 } 00125 00132 int db_test_cursor_type_fetch(dbCursor * cursor) 00133 { 00134 return (cursor->type == DB_READONLY || cursor->type == DB_UPDATE); 00135 } 00136 00143 int db_test_cursor_type_update(dbCursor * cursor) 00144 { 00145 return (cursor->type == DB_UPDATE); 00146 } 00147 00154 int db_test_cursor_type_insert(dbCursor * cursor) 00155 { 00156 return (cursor->type == DB_INSERT); 00157 } 00158 00165 void db_set_cursor_mode(dbCursor * cursor, int mode) 00166 { 00167 cursor->mode = mode; 00168 } 00169 00176 void db_set_cursor_mode_scroll(dbCursor * cursor) 00177 { 00178 cursor->mode |= DB_SCROLL; 00179 } 00180 00187 void db_unset_cursor_mode_scroll(dbCursor * cursor) 00188 { 00189 cursor->mode &= ~DB_SCROLL; 00190 } 00191 00198 void db_unset_cursor_mode(dbCursor * cursor) 00199 { 00200 cursor->mode = 0; 00201 } 00202 00209 void db_set_cursor_mode_insensitive(dbCursor * cursor) 00210 { 00211 cursor->mode |= DB_INSENSITIVE; 00212 } 00213 00220 void db_unset_cursor_mode_insensitive(dbCursor * cursor) 00221 { 00222 cursor->mode &= ~DB_INSENSITIVE; 00223 } 00224 00231 int db_test_cursor_mode_scroll(dbCursor * cursor) 00232 { 00233 return (cursor->mode & DB_SCROLL); 00234 } 00235 00236 00243 int db_test_cursor_mode_insensitive(dbCursor * cursor) 00244 { 00245 return (cursor->mode & DB_INSENSITIVE); 00246 } 00247 00254 int db_alloc_cursor_column_flags(dbCursor * cursor) 00255 { 00256 int ncols; 00257 int col; 00258 00259 ncols = db_get_cursor_number_of_columns(cursor); 00260 cursor->column_flags = (short *)db_calloc(ncols, sizeof(short)); 00261 if (cursor->column_flags == NULL) 00262 return db_get_error_code(); 00263 for (col = 0; col < ncols; col++) 00264 db_unset_cursor_column_flag(cursor, col); 00265 return DB_OK; 00266 } 00267 00274 void db_free_cursor_column_flags(dbCursor * cursor) 00275 { 00276 if (cursor->column_flags) 00277 free(cursor->column_flags); 00278 cursor->column_flags = NULL; 00279 } 00280 00287 void db_set_cursor_column_for_update(dbCursor * cursor, int col) 00288 { 00289 db_set_cursor_column_flag(cursor, col); 00290 } 00291 00298 void db_unset_cursor_column_for_update(dbCursor * cursor, int col) 00299 { 00300 db_unset_cursor_column_flag(cursor, col); 00301 } 00302 00309 int db_test_cursor_column_for_update(dbCursor * cursor, int col) 00310 { 00311 return db_test_cursor_column_flag(cursor, col); 00312 } 00313 00320 int db_test_cursor_any_column_for_update(dbCursor * cursor) 00321 { 00322 return db_test_cursor_any_column_flag(cursor); 00323 } 00324 00331 void db_set_cursor_column_flag(dbCursor * cursor, int col) 00332 { 00333 if (cursor->column_flags) 00334 cursor->column_flags[col] = 1; 00335 } 00336 00343 void db_unset_cursor_column_flag(dbCursor * cursor, int col) 00344 { 00345 if (cursor->column_flags) 00346 cursor->column_flags[col] = 0; 00347 } 00348 00355 int db_test_cursor_column_flag(dbCursor * cursor, int col) 00356 { 00357 return cursor->column_flags && cursor->column_flags[col] ? 1 : 0; 00358 } 00359 00366 int db_get_cursor_number_of_columns(dbCursor * cursor) 00367 { 00368 dbTable *table; 00369 00370 table = db_get_cursor_table(cursor); 00371 if (table) 00372 return db_get_table_number_of_columns(table); 00373 return 0; 00374 } 00375 00382 /* is any cursor column flag set? */ 00383 int db_test_cursor_any_column_flag(dbCursor * cursor) 00384 { 00385 int ncols, col; 00386 00387 ncols = db_get_cursor_number_of_columns(cursor); 00388 for (col = 0; col < ncols; col++) 00389 if (db_test_cursor_column_flag(cursor, col)) 00390 return 1; 00391 return 0; 00392 }