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 */
db_init_handle(dbHandle * handle)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 */
db_set_handle(dbHandle * handle,const char * dbName,const char * dbSchema)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 */
db_get_handle_dbname(dbHandle * handle)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 */
db_get_handle_dbschema(dbHandle * handle)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 */
db_free_handle(dbHandle * handle)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 */
db_free_handle_array(dbHandle * handle,int count)91 void db_free_handle_array(dbHandle * handle, int count)
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 */
db_alloc_handle_array(int count)109 dbHandle *db_alloc_handle_array(int count)
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 }
120