GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 
20 static int valid_cursor(dbCursor * cursor, int position);
21 
22 /*!
23  \brief Fetch data
24 
25  \return DB_OK on success
26  \return DB_FAILED on failure
27  */
28 int db_d_fetch(void)
29 {
30  dbToken token;
31  dbCursor *cursor;
32  int stat;
33  int more;
34  int position;
35 
36  /* get the arg(s) */
37  DB_RECV_TOKEN(&token);
38  DB_RECV_INT(&position);
39  cursor = (dbCursor *) db_find_token(token);
40  if (!valid_cursor(cursor, position)) {
42  return DB_FAILED;
43  }
44 
45  /* call the procedure */
46  stat = db_driver_fetch(cursor, position, &more);
47 
48  /* send the return code */
49  if (stat != DB_OK) {
51  return DB_OK;
52  }
54 
55  /* results */
56  DB_SEND_INT(more);
57  if (more) {
58  DB_SEND_TABLE_DATA(cursor->table);
59  }
60 
61  return DB_OK;
62 }
63 
64 
65 static int valid_cursor(dbCursor * cursor, int position)
66 {
67  if (cursor == NULL)
68  return 0;
69 
70  if (!db_test_cursor_type_fetch(cursor)) {
71  db_error("not a fetchable cursor");
72  return 0;
73  }
74 
75  if (position != DB_NEXT && !db_test_cursor_mode_scroll(cursor)) {
76  db_error("not a scrollable cursor");
77  return 0;
78  }
79 
80  return 1;
81 }
#define DB_SEND_TABLE_DATA(x)
Definition: macros.h:67
int db_test_cursor_type_fetch(dbCursor *)
Check cursor type.
Definition: cursor.c:144
#define DB_SEND_INT(x)
Definition: macros.h:37
#define DB_RECV_INT(x)
Definition: macros.h:39
#define DB_RECV_TOKEN(x)
Definition: macros.h:99
int dbToken
Definition: dbmi.h:145
#define NULL
Definition: ccmath.h:32
#define DB_NEXT
Definition: dbmi.h:114
#define DB_SEND_FAILURE()
Definition: macros.h:9
void db_error(const char *)
Report error message.
dbAddress db_find_token(dbToken)
Find token.
#define DB_FAILED
Definition: dbmi.h:72
int db_test_cursor_mode_scroll(dbCursor *)
Check if cursor mode is &#39;scroll&#39;.
Definition: cursor.c:250
#define DB_SEND_SUCCESS()
Definition: macros.h:7
int(* db_driver_fetch)(dbCursor *, int, int *)
dbTable * table
Definition: dbmi.h:234
int db_d_fetch(void)
Fetch data.
Definition: d_fetch.c:28
#define DB_OK
Definition: dbmi.h:71