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 */
db__init_driver_state(void)25 void db__init_driver_state(void)
26 {
27     db_zero((void *)&state, sizeof(state));
28 }
29 
30 /*!
31   \brief Get driver state
32 
33   \return pointer to dbDriverState
34 */
db__get_driver_state(void)35 dbDriverState *db__get_driver_state(void)
36 {
37     return &state;
38 }
39 
40 /*!
41   \brief Test database connection
42 
43   \return 1 opened
44   \return 0 closed
45 */
db__test_database_open(void)46 int db__test_database_open(void)
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 */
db__mark_database_open(const char * dbname,const char * dbschema)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 */
db__mark_database_closed(void)67 void db__mark_database_closed(void)
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 */
db__add_cursor_to_driver_state(dbCursor * cursor)79 void db__add_cursor_to_driver_state(dbCursor * cursor)
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 */
db__drop_cursor_from_driver_state(dbCursor * cursor)110 void db__drop_cursor_from_driver_state(dbCursor * cursor)
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 */
db__close_all_cursors(void)122 void db__close_all_cursors(void)
123 {
124     int i;
125 
126     for (i = 0; i < state.ncursors; i++)
127 	if (state.cursor_list[i])
128 	    db_driver_close_cursor(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 }
136