GRASS GIS 8 Programmer's Manual  8.4.0dev(2023)-bfef3d3961
d_fetch.c
Go to the documentation of this file.
1 /*!
2  * \file db/dbmi_driver/d_fetch.c
3  *
4  * \brief DBMI Library (driver) - fetch data
5  *
6  * (C) 1999-2008 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public
9  * License (>=v2). Read the file COPYING that comes with GRASS
10  * for details.
11  *
12  * \author Joel Jones (CERL/UIUC), Radim Blazek
13  */
14 
15 #include <grass/dbmi.h>
16 #include "macros.h"
17 #include "dbstubs.h"
18 
19 static int valid_cursor(dbCursor *cursor, int position);
20 
21 /*!
22  \brief Fetch data
23 
24  \return DB_OK on success
25  \return DB_FAILED on failure
26  */
27 int db_d_fetch(void)
28 {
29  dbToken token;
30  dbCursor *cursor;
31  int stat;
32  int more;
33  int position;
34 
35  /* get the arg(s) */
36  DB_RECV_TOKEN(&token);
37  DB_RECV_INT(&position);
38  cursor = (dbCursor *)db_find_token(token);
39  if (!valid_cursor(cursor, position)) {
41  return DB_FAILED;
42  }
43 
44  /* call the procedure */
45  stat = db_driver_fetch(cursor, position, &more);
46 
47  /* send the return code */
48  if (stat != DB_OK) {
50  return DB_OK;
51  }
53 
54  /* results */
55  DB_SEND_INT(more);
56  if (more) {
57  DB_SEND_TABLE_DATA(cursor->table);
58  }
59 
60  return DB_OK;
61 }
62 
63 static int valid_cursor(dbCursor *cursor, int position)
64 {
65  if (cursor == NULL)
66  return 0;
67 
68  if (!db_test_cursor_type_fetch(cursor)) {
69  db_error("not a fetchable cursor");
70  return 0;
71  }
72 
73  if (position != DB_NEXT && !db_test_cursor_mode_scroll(cursor)) {
74  db_error("not a scrollable cursor");
75  return 0;
76  }
77 
78  return 1;
79 }
#define NULL
Definition: ccmath.h:32
int db_d_fetch(void)
Fetch data.
Definition: d_fetch.c:27
int dbToken
Definition: dbmi.h:145
#define DB_FAILED
Definition: dbmi.h:72
#define DB_OK
Definition: dbmi.h:71
#define DB_NEXT
Definition: dbmi.h:114
int(* db_driver_fetch)(dbCursor *, int, int *)
int db_test_cursor_type_fetch(dbCursor *)
Check cursor type.
Definition: cursor.c:144
dbAddress db_find_token(dbToken)
Find token.
void db_error(const char *)
Report error message.
int db_test_cursor_mode_scroll(dbCursor *)
Check if cursor mode is 'scroll'.
Definition: cursor.c:249
#define DB_SEND_INT(x)
Definition: macros.h:82
#define DB_RECV_INT(x)
Definition: macros.h:87
#define DB_SEND_SUCCESS()
Definition: macros.h:13
#define DB_SEND_TABLE_DATA(x)
Definition: macros.h:148
#define DB_RECV_TOKEN(x)
Definition: macros.h:219
#define DB_SEND_FAILURE()
Definition: macros.h:18
dbTable * table
Definition: dbmi.h:224