GRASS 8 Programmer's Manual 8.6.0dev(2026)-55de52a352
Loading...
Searching...
No Matches
cursor.c
Go to the documentation of this file.
1/*!
2 \file lib/db/dbmi_base/cursor.c
3
4 \brief DBMI Library (base) - cursors management
5
6 SPDX-FileCopyrightText: 1999-2009 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
15/*!
16 \brief Initialize dbCursor
17
18 \param cursor pointer to dbCursor to be initialized
19 */
21{
22 G_zero(cursor, sizeof(dbCursor));
23 cursor->token = -1;
24}
25
26/*!
27 \brief Allocate table for cursor
28
29 \param cursor pointer to dbCursor
30 \param ncols number of column in table
31
32 \return DB_OK on success
33 \return error code on error
34 */
36{
37 cursor->table = db_alloc_table(ncols);
38 if (cursor->table == NULL)
39 return db_get_error_code();
40 return DB_OK;
41}
42
43/*!
44 \brief Free allocated dbCursor
45
46 \param cursor pointer to dbCursor
47 */
49{
50 if (cursor->table)
51 db_free_table(cursor->table);
52 if (cursor->column_flags)
55}
56
57/*!
58 \brief Get table allocated by cursor
59
60 \param cursor pointer to dbCursor
61
62 \return pointer to dbTable
63 */
65{
66 return cursor->table;
67}
68
69/*!
70 \brief Set table for given cursor
71
72 \param cursor pointer to dbCursor
73 \param table pointer to dbTable
74 */
76{
77 cursor->table = table;
78}
79
80/*!
81 \brief Get cursor token
82
83 \param cursor pointer to dbCursor
84
85 \return pointer to dbToken
86 */
88{
89 return cursor->token;
90}
91
92/*!
93 \brief Set cursor token
94
95 \param cursor pointer to dbCursor
96 \param token pointer to dbToken
97 */
99{
100 cursor->token = token;
101}
102
103/*!
104 \brief Set cursor to be read-only (select)
105
106 \param cursor pointer to dbCursor
107 */
112
113/*!
114 \brief Set cursor to be writable (update)
115
116 \param cursor pointer to dbCursor
117 */
122
123/*!
124 \brief Set cursor to be writable (insert)
125
126 \param cursor pointer to dbCursor
127 */
132
133/*!
134 \brief Check cursor type
135
136 \param cursor pointer to dbCursor
137
138 \return 1 for known cursor type
139 \return 0 for unknown cursor type
140 */
142{
143 return (cursor->type == DB_READONLY || cursor->type == DB_UPDATE ||
144 cursor->type == DB_INSERT);
145}
146
147/*!
148 \brief Check if cursor type is 'update'
149
150 \param cursor pointer to dbCursor
151
152 \return 1 if cursor type is 'update'
153 \return 0 otherwise
154 */
156{
157 return (cursor->type == DB_UPDATE);
158}
159
160/*!
161 \brief Check if cursor type is 'insert'
162
163 \param cursor pointer to dbCursor
164
165 \return 1 if cursor type is 'insert'
166 \return 0 otherwise
167 */
169{
170 return (cursor->type == DB_INSERT);
171}
172
173/*!
174 \brief Set cursor mode
175
176 Modes:
177 - DB_SCROLL
178 - DB_INSENSITIVE
179
180 \param cursor pointer to dbCursor
181 \param mode cursor mode
182 */
184{
185 cursor->mode = mode;
186}
187
188/*!
189 \brief Set 'scroll' cursor mode
190
191 \param cursor pointer to dbCursor
192 */
197
198/*!
199 \brief Unset 'scroll' cursor mode
200
201 \param cursor pointer to dbCursor
202 */
204{
205 cursor->mode &= ~DB_SCROLL;
206}
207
208/*!
209 \brief Unset cursor mode
210
211 \param cursor pointer to dbCursor
212 */
214{
215 cursor->mode = 0;
216}
217
218/*!
219 \brief Set 'intensive' cursor mode
220
221 \param cursor pointer to dbCursor
222 */
227
228/*!
229 \brief Unset 'intensive' cursor mode
230
231 \param cursor pointer to dbCursor
232 */
234{
235 cursor->mode &= ~DB_INSENSITIVE;
236}
237
238/*!
239 \brief Check if cursor mode is 'scroll'
240
241 \param cursor pointer to dbCursor
242
243 \return 1 if true
244 \return 0 if false
245 */
247{
248 return (cursor->mode & DB_SCROLL);
249}
250
251/*!
252 \brief Check if cursor mode is 'intensive'
253
254 \param cursor pointer to dbCursor
255
256 \return 1 if true
257 \return 0 if false
258 */
263
264/*!
265 \brief Allocate columns' flags for cursor
266
267 \param cursor pointer to dbCursor
268
269 \return DB_OK on success
270 \return error code on failure
271 */
273{
274 int ncols;
275 int col;
276
278 cursor->column_flags = (short *)db_calloc(ncols, sizeof(short));
279 if (cursor->column_flags == NULL)
280 return db_get_error_code();
281 for (col = 0; col < ncols; col++)
283 return DB_OK;
284}
285
286/*!
287 \brief Free columns' flags of cursor
288
289 \param cursor pointer to dbCursor
290 */
292{
293 if (cursor->column_flags)
294 db_free(cursor->column_flags);
295 cursor->column_flags = NULL;
296}
297
298/*!
299 \brief Set Column flag to 'update'
300
301 \param cursor pointer to dbCursor
302 \param col column index (starting with '0')
303 */
308
309/*!
310 \brief Unset 'update' column flag
311
312 \param cursor pointer to dbCursor
313 \param col column index (starting with '0')
314 */
319
320/*!
321 \brief Check if column flag is 'update'
322
323 \param cursor pointer to dbCursor
324 \param col column index (starting with '0')
325
326 \return 1 if true
327 \return 0 if false
328 */
333
334/*!
335 \brief Check if columns' flag is 'update'
336
337 \param cursor pointer to dbCursor
338
339 \return 1 if true
340 \return 0 if false
341 */
346
347/*!
348 \brief Set column's flag
349
350 \param cursor pointer to dbCursor
351 \param col column index (starting with '0')
352 */
354{
355 if (cursor->column_flags)
356 cursor->column_flags[col] = 1;
357}
358
359/*!
360 \brief Unset column's flag
361
362 \param cursor pointer to dbCursor
363 \param col column index (starting with '0')
364 */
366{
367 if (cursor->column_flags)
368 cursor->column_flags[col] = 0;
369}
370
371/*!
372 \brief Checks column's flag
373
374 \param cursor pointer to dbCursor
375 \param col column index (starting with '0')
376
377 \return 1 if flag is defined
378 \return 0 otherwise
379 */
381{
382 return cursor->column_flags && cursor->column_flags[col] ? 1 : 0;
383}
384
385/*!
386 \brief Get number of columns
387
388 \param cursor pointer to dbCursor
389 */
391{
392 dbTable *table;
393
395 if (table)
396 return db_get_table_number_of_columns(table);
397 return 0;
398}
399
400/*!
401 \brief Checks columns' flag
402
403 Is any cursor column flag set?
404
405 \param cursor pointer to dbCursor
406
407 \return 1 if true
408 \return 0 if false
409 */
411{
412 int ncols, col;
413
415 for (col = 0; col < ncols; col++)
417 return 1;
418 return 0;
419}
#define NULL
Definition ccmath.h:32
void db_set_cursor_mode_scroll(dbCursor *cursor)
Set 'scroll' cursor mode.
Definition cursor.c:193
void db_set_cursor_token(dbCursor *cursor, dbToken token)
Set cursor token.
Definition cursor.c:98
int db_test_cursor_any_column_for_update(dbCursor *cursor)
Check if columns' flag is 'update'.
Definition cursor.c:342
int db_alloc_cursor_table(dbCursor *cursor, int ncols)
Allocate table for cursor.
Definition cursor.c:35
dbTable * db_get_cursor_table(dbCursor *cursor)
Get table allocated by cursor.
Definition cursor.c:64
void db_free_cursor(dbCursor *cursor)
Free allocated dbCursor.
Definition cursor.c:48
void db_free_cursor_column_flags(dbCursor *cursor)
Free columns' flags of cursor.
Definition cursor.c:291
dbToken db_get_cursor_token(dbCursor *cursor)
Get cursor token.
Definition cursor.c:87
int db_test_cursor_type_fetch(dbCursor *cursor)
Check cursor type.
Definition cursor.c:141
int db_test_cursor_mode_scroll(dbCursor *cursor)
Check if cursor mode is 'scroll'.
Definition cursor.c:246
void db_unset_cursor_mode_insensitive(dbCursor *cursor)
Unset 'intensive' cursor mode.
Definition cursor.c:233
void db_unset_cursor_column_for_update(dbCursor *cursor, int col)
Unset 'update' column flag.
Definition cursor.c:315
void db_set_cursor_table(dbCursor *cursor, dbTable *table)
Set table for given cursor.
Definition cursor.c:75
int db_test_cursor_column_flag(dbCursor *cursor, int col)
Checks column's flag.
Definition cursor.c:380
void db_set_cursor_type_readonly(dbCursor *cursor)
Set cursor to be read-only (select)
Definition cursor.c:108
void db_init_cursor(dbCursor *cursor)
Initialize dbCursor.
Definition cursor.c:20
int db_test_cursor_type_update(dbCursor *cursor)
Check if cursor type is 'update'.
Definition cursor.c:155
int db_alloc_cursor_column_flags(dbCursor *cursor)
Allocate columns' flags for cursor.
Definition cursor.c:272
int db_test_cursor_column_for_update(dbCursor *cursor, int col)
Check if column flag is 'update'.
Definition cursor.c:329
void db_set_cursor_mode_insensitive(dbCursor *cursor)
Set 'intensive' cursor mode.
Definition cursor.c:223
void db_set_cursor_type_insert(dbCursor *cursor)
Set cursor to be writable (insert)
Definition cursor.c:128
void db_set_cursor_column_for_update(dbCursor *cursor, int col)
Set Column flag to 'update'.
Definition cursor.c:304
void db_unset_cursor_mode_scroll(dbCursor *cursor)
Unset 'scroll' cursor mode.
Definition cursor.c:203
int db_get_cursor_number_of_columns(dbCursor *cursor)
Get number of columns.
Definition cursor.c:390
void db_unset_cursor_mode(dbCursor *cursor)
Unset cursor mode.
Definition cursor.c:213
void db_set_cursor_type_update(dbCursor *cursor)
Set cursor to be writable (update)
Definition cursor.c:118
int db_test_cursor_type_insert(dbCursor *cursor)
Check if cursor type is 'insert'.
Definition cursor.c:168
int db_test_cursor_any_column_flag(dbCursor *cursor)
Checks columns' flag.
Definition cursor.c:410
void db_set_cursor_mode(dbCursor *cursor, int mode)
Set cursor mode.
Definition cursor.c:183
int db_test_cursor_mode_insensitive(dbCursor *cursor)
Check if cursor mode is 'intensive'.
Definition cursor.c:259
void db_unset_cursor_column_flag(dbCursor *cursor, int col)
Unset column's flag.
Definition cursor.c:365
void db_set_cursor_column_flag(dbCursor *cursor, int col)
Set column's flag.
Definition cursor.c:353
Main header of GRASS DataBase Management Interface.
int dbToken
Definition dbmi.h:143
#define DB_INSENSITIVE
Definition dbmi.h:123
#define DB_SCROLL
Definition dbmi.h:122
#define DB_UPDATE
Definition dbmi.h:120
#define DB_OK
Definition dbmi.h:69
#define DB_INSERT
Definition dbmi.h:119
#define DB_READONLY
Definition dbmi.h:118
void db_free_table(dbTable *)
Free the table.
dbTable * db_alloc_table(int)
Allocate a table with a specific number of columns.
void * db_calloc(int, int)
Allocate memory.
int db_get_error_code(void)
Get error code.
void db_free(void *)
Free allocated memory.
int db_get_table_number_of_columns(dbTable *)
Return the number of columns of the table.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition gis/zero.c:21