-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
driver_state.c
135 lines (112 loc) · 2.44 KB
/
driver_state.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*!
* \file db/dbmi_driver/driver_state.c
*
* \brief DBMI Library (driver) - drivers state
*
* (C) 1999-2008 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
* \author Joel Jones (CERL/UIUC), Radim Blazek
*/
#include <stdlib.h>
#include <grass/dbmi.h>
#include "dbstubs.h"
static dbDriverState state;
/*!
\brief Initialize driver state
*/
void db__init_driver_state(void)
{
db_zero((void *)&state, sizeof(state));
}
/*!
\brief Get driver state
\return pointer to dbDriverState
*/
dbDriverState *db__get_driver_state(void)
{
return &state;
}
/*!
\brief Test database connection
\return 1 opened
\return 0 closed
*/
int db__test_database_open(void)
{
return state.open ? 1 : 0;
}
/*!
\brief Mark database as opened
\param dbname database name
\param dbschema database schema name
*/
void db__mark_database_open(const char *dbname, const char *dbschema)
{
state.dbname = db_store(dbname);
state.dbschema = db_store(dbschema);
state.open = 1;
}
/*!
\brief Mark database as closed
*/
void db__mark_database_closed(void)
{
db_free(state.dbname);
db_free(state.dbschema);
state.open = 0;
}
/*!
\brief Add cursor do driver state
\param cursor db cursor to be added
*/
void db__add_cursor_to_driver_state(dbCursor * cursor)
{
dbCursor **list;
int i;
/* find an empty slot in the cursor list */
list = state.cursor_list;
for (i = 0; i < state.ncursors; i++)
if (list[i] == NULL)
break;
/* if not found, extend list */
if (i >= state.ncursors) {
list =
(dbCursor **) db_realloc((void *)list,
(i + 1) * sizeof(dbCursor *));
if (list == NULL)
return;
state.cursor_list = list;
state.ncursors = i + 1;
}
/* add it in */
list[i] = cursor;
}
/*!
\brief Drop cursor from driver state
\param cursor db cursor to be dropped
*/
void db__drop_cursor_from_driver_state(dbCursor * cursor)
{
int i;
for (i = 0; i < state.ncursors; i++)
if (state.cursor_list[i] == cursor)
state.cursor_list[i] = NULL;
}
/*!
\brief Close all cursors
*/
void db__close_all_cursors(void)
{
int i;
for (i = 0; i < state.ncursors; i++)
if (state.cursor_list[i])
db_driver_close_cursor(state.cursor_list[i]);
if (state.cursor_list)
db_free(state.cursor_list);
state.ncursors = 0;
state.cursor_list = NULL;
}