GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 1999-2008 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Joel Jones (CERL/UIUC), Radim Blazek
10 */
11
12#include <stdlib.h>
13#include <grass/dbmi.h>
14#include "dbstubs.h"
15
16static dbDriverState state;
17
18/*!
19 \brief Initialize driver state
20 */
22{
23 db_zero((void *)&state, sizeof(state));
24}
25
26/*!
27 \brief Get driver state
28
29 \return pointer to dbDriverState
30 */
32{
33 return &state;
34}
35
36/*!
37 \brief Test database connection
38
39 \return 1 opened
40 \return 0 closed
41 */
43{
44 return state.open ? 1 : 0;
45}
46
47/*!
48 \brief Mark database as opened
49
50 \param dbname database name
51 \param dbschema database schema name
52 */
53void db__mark_database_open(const char *dbname, const char *dbschema)
54{
55 state.dbname = db_store(dbname);
56 state.dbschema = db_store(dbschema);
57 state.open = 1;
58}
59
60/*!
61 \brief Mark database as closed
62 */
64{
65 db_free(state.dbname);
66 db_free(state.dbschema);
67 state.open = 0;
68}
69
70/*!
71 \brief Add cursor do driver state
72
73 \param cursor db cursor to be added
74 */
76{
77 dbCursor **list;
78 int i;
79
80 /* find an empty slot in the cursor list */
81 list = state.cursor_list;
82 for (i = 0; i < state.ncursors; i++)
83 if (list[i] == NULL)
84 break;
85
86 /* if not found, extend list */
87 if (i >= state.ncursors) {
88 list =
89 (dbCursor **)db_realloc((void *)list, (i + 1) * sizeof(dbCursor *));
90 if (list == NULL)
91 return;
92 state.cursor_list = list;
93 state.ncursors = i + 1;
94 }
95
96 /* add it in */
97 list[i] = cursor;
98}
99
100/*!
101 \brief Drop cursor from driver state
102
103 \param cursor db cursor to be dropped
104 */
106{
107 int i;
108
109 for (i = 0; i < state.ncursors; i++)
110 if (state.cursor_list[i] == cursor)
111 state.cursor_list[i] = NULL;
112}
113
114/*!
115 \brief Close all cursors
116 */
118{
119 int i;
120
121 for (i = 0; i < state.ncursors; i++)
122 if (state.cursor_list[i])
123 db_driver_close_cursor(state.cursor_list[i]);
124
125 if (state.cursor_list)
126 db_free(state.cursor_list);
127
128 state.ncursors = 0;
129 state.cursor_list = NULL;
130}
#define NULL
Definition ccmath.h:32
Main header of GRASS DataBase Management Interface.
int(* db_driver_close_cursor)(dbCursor *)
void * db_realloc(void *, int)
Reallocate memory.
void db_free(void *)
Free allocated memory.
char * db_store(const char *)
Make a copy of string buffer.
void db_zero(void *, int)
Zero allocated space.
void db__close_all_cursors(void)
Close all cursors.
int db__test_database_open(void)
Test database connection.
dbDriverState * db__get_driver_state(void)
Get driver state.
void db__add_cursor_to_driver_state(dbCursor *cursor)
Add cursor do driver state.
void db__init_driver_state(void)
Initialize driver state.
void db__mark_database_closed(void)
Mark database as closed.
void db__mark_database_open(const char *dbname, const char *dbschema)
Mark database as opened.
void db__drop_cursor_from_driver_state(dbCursor *cursor)
Drop cursor from driver state.
Definition manage.h:4