GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
driver_state.c
Go to the documentation of this file.
1 /*!
2  * \file db/dbmi_driver/driver_state.c
3  *
4  * \brief DBMI Library (driver) - drivers state
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 <stdlib.h>
16 #include <grass/dbmi.h>
17 #include "dbstubs.h"
18 
19 
20 static dbDriverState state;
21 
22 /*!
23  \brief Initialize driver state
24 */
26 {
27  db_zero((void *)&state, sizeof(state));
28 }
29 
30 /*!
31  \brief Get driver state
32 
33  \return pointer to dbDriverState
34 */
36 {
37  return &state;
38 }
39 
40 /*!
41  \brief Test database connection
42 
43  \return 1 opened
44  \return 0 closed
45 */
47 {
48  return state.open ? 1 : 0;
49 }
50 
51 /*!
52  \brief Mark database as opened
53 
54  \param dbname database name
55  \param dbschema database schema name
56 */
57 void db__mark_database_open(const char *dbname, const char *dbschema)
58 {
59  state.dbname = db_store(dbname);
60  state.dbschema = db_store(dbschema);
61  state.open = 1;
62 }
63 
64 /*!
65  \brief Mark database as closed
66 */
68 {
69  db_free(state.dbname);
70  db_free(state.dbschema);
71  state.open = 0;
72 }
73 
74 /*!
75  \brief Add cursor do driver state
76 
77  \param cursor db cursor to be added
78 */
80 {
81  dbCursor **list;
82  int i;
83 
84  /* find an empty slot in the cursor list */
85  list = state.cursor_list;
86  for (i = 0; i < state.ncursors; i++)
87  if (list[i] == NULL)
88  break;
89 
90  /* if not found, extend list */
91  if (i >= state.ncursors) {
92  list =
93  (dbCursor **) db_realloc((void *)list,
94  (i + 1) * sizeof(dbCursor *));
95  if (list == NULL)
96  return;
97  state.cursor_list = list;
98  state.ncursors = i + 1;
99  }
100 
101  /* add it in */
102  list[i] = cursor;
103 }
104 
105 /*!
106  \brief Drop cursor from driver state
107 
108  \param cursor db cursor to be dropped
109 */
111 {
112  int i;
113 
114  for (i = 0; i < state.ncursors; i++)
115  if (state.cursor_list[i] == cursor)
116  state.cursor_list[i] = NULL;
117 }
118 
119 /*!
120  \brief Close all cursors
121 */
123 {
124  int i;
125 
126  for (i = 0; i < state.ncursors; i++)
127  if (state.cursor_list[i])
129 
130  if (state.cursor_list)
131  db_free(state.cursor_list);
132 
133  state.ncursors = 0;
134  state.cursor_list = NULL;
135 }
void * db_realloc(void *, int)
Reallocate memory.
char * db_store(const char *)
Make a copy of string buffer.
void db__add_cursor_to_driver_state(dbCursor *cursor)
Add cursor do driver state.
Definition: driver_state.c:79
void db_zero(void *, int)
Zero allocated space.
void db_free(void *)
Free allocated memory.
#define NULL
Definition: ccmath.h:32
void db__close_all_cursors(void)
Close all cursors.
Definition: driver_state.c:122
int ncursors
Definition: dbmi.h:254
struct list * list
Definition: read_list.c:24
char * dbschema
Definition: dbmi.h:252
dbCursor ** cursor_list
Definition: dbmi.h:255
void db__mark_database_closed(void)
Mark database as closed.
Definition: driver_state.c:67
void db__mark_database_open(const char *dbname, const char *dbschema)
Mark database as opened.
Definition: driver_state.c:57
char * dbname
Definition: dbmi.h:251
int db__test_database_open(void)
Test database connection.
Definition: driver_state.c:46
void db__drop_cursor_from_driver_state(dbCursor *cursor)
Drop cursor from driver state.
Definition: driver_state.c:110
int(* db_driver_close_cursor)(dbCursor *)
struct state state
Definition: parser.c:103
void db__init_driver_state(void)
Initialize driver state.
Definition: driver_state.c:25
dbDriverState * db__get_driver_state(void)
Get driver state.
Definition: driver_state.c:35