GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-602118adcc
handle.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_base/handle.c
3 
4  \brief DBMI Library (base) - handle management
5 
6  (C) 1999-2009 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 
18 /*!
19  \brief Initialize handle (i.e database/schema)
20 
21  \param handle pointer to dbHandle to be initialized
22  */
23 void db_init_handle(dbHandle *handle)
24 {
25  db_init_string(&handle->dbName);
26  db_init_string(&handle->dbSchema);
27 }
28 
29 /*!
30  \brief Set handle (database and schema name)
31 
32  \param handle pointer to dbHandle
33  \param dbName database name
34  \param dbSchema schema name
35 
36  \return DB_OK on success
37  \return DB_FAILED on failure
38  */
39 int db_set_handle(dbHandle *handle, const char *dbName, const char *dbSchema)
40 {
41  int stat;
42 
43  stat = db_set_string(&handle->dbName, dbName);
44  if (stat != DB_OK)
45  return stat;
46  stat = db_set_string(&handle->dbSchema, dbSchema);
47  return stat;
48 }
49 
50 /*!
51  \brief Get handle database name
52 
53  \param handle pointer to dbHandle
54 
55  \return pointer to string with database name
56  */
57 const char *db_get_handle_dbname(dbHandle *handle)
58 {
59  return db_get_string(&handle->dbName);
60 }
61 
62 /*!
63  \brief Get handle schema name
64 
65  \param handle pointer to dbHandle
66 
67  \return pointer to string with schema name
68  */
69 const char *db_get_handle_dbschema(dbHandle *handle)
70 {
71  return db_get_string(&handle->dbSchema);
72 }
73 
74 /*!
75  \brief Free dbHandle structure
76 
77  \param handle pointer to dbHandle
78  */
79 void db_free_handle(dbHandle *handle)
80 {
81  db_free_string(&handle->dbName);
82  db_free_string(&handle->dbSchema);
83 }
84 
85 /*!
86  \brief Free array of handles
87 
88  \param handle pointer to first dbHandle in the array
89  \param count number of handles in the array
90  */
92 {
93  int i;
94 
95  if (handle) {
96  for (i = 0; i < count; i++)
97  db_free_handle(&handle[i]);
98  db_free((void *)handle);
99  }
100 }
101 
102 /*!
103  \brief Allocate array of handles
104 
105  \param count number of handles in the array
106 
107  \return pointer to first dbHandle in the array
108  */
110 {
111  int i;
112  dbHandle *handle;
113 
114  handle = (dbHandle *)db_calloc(count, sizeof(dbHandle));
115  if (handle)
116  for (i = 0; i < count; i++)
117  db_init_handle(&handle[i]);
118  return handle;
119 }
#define DB_OK
Definition: dbmi.h:71
void * db_calloc(int, int)
Allocate memory.
void db_free_string(dbString *)
Free allocated space for dbString.
Definition: string.c:150
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition: string.c:41
void db_free(void *)
Free allocated memory.
void db_init_string(dbString *)
Initialize dbString.
Definition: string.c:25
char * db_get_string(const dbString *)
Get string.
Definition: string.c:140
dbHandle * db_alloc_handle_array(int count)
Allocate array of handles.
Definition: handle.c:109
const char * db_get_handle_dbschema(dbHandle *handle)
Get handle schema name.
Definition: handle.c:69
const char * db_get_handle_dbname(dbHandle *handle)
Get handle database name.
Definition: handle.c:57
void db_free_handle_array(dbHandle *handle, int count)
Free array of handles.
Definition: handle.c:91
void db_init_handle(dbHandle *handle)
Initialize handle (i.e database/schema)
Definition: handle.c:23
void db_free_handle(dbHandle *handle)
Free dbHandle structure.
Definition: handle.c:79
int db_set_handle(dbHandle *handle, const char *dbName, const char *dbSchema)
Set handle (database and schema name)
Definition: handle.c:39
int count
dbString dbSchema
Definition: dbmi.h:174
dbString dbName
Definition: dbmi.h:172