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