GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
db.c
Go to the documentation of this file.
1 /*!
2  * \file db/dbmi_client/db.c
3  *
4  * \brief DBMI Library (client) - open/close driver/database connection
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 <grass/dbmi.h>
16 #include <grass/glocale.h>
17 #include "macros.h"
18 
19 /*!
20  \brief Open driver/database connection
21 
22  \param drvname driver name
23  \param dbname database name
24 
25  \return pointer to dbDriver structure
26  \return NULL on failure
27  */
29  const char *dbname)
30 {
31  dbHandle handle;
33 
34  G_debug(3, "db_start_driver_open_database(): drvname='%s', dbname='%s'",
35  drvname, dbname);
36 
37  db_init_handle(&handle);
38 
39  driver = db_start_driver(drvname);
40  if (driver == NULL) {
41  G_warning(_("Unable to start driver <%s>"), drvname);
42  return NULL;
43  }
44  db_set_handle(&handle, dbname, NULL);
45  if (db_open_database(driver, &handle) != DB_OK) {
46  G_warning(_("Unable to open database <%s> by driver <%s>"),
47  dbname, drvname);
48  db_shutdown_driver(driver);
49  return NULL;
50  }
51 
52  return driver;
53 }
54 
55 /*!
56  \brief Close driver/database connection
57 
58  \param driver db driver
59 
60  \return DB_OK or DB_FAILED
61 */
63 {
64  int status;
65 
66  status = db_close_database(driver);
67  G_debug(2, "db_close_database() result: %d (%d means success)",
68  status, DB_OK);
69 
70  if (db_shutdown_driver(driver) != 0) {
71  status = DB_FAILED;
72  G_debug(2, "db_shutdown_driver() failed");
73  }
74 
75  return status;
76 }
dbDriver * db_start_driver_open_database(const char *drvname, const char *dbname)
Open driver/database connection.
Definition: db.c:28
dbDriver * db_start_driver(const char *)
Initialize a new dbDriver for db transaction.
Definition: start.c:50
int db_close_database_shutdown_driver(dbDriver *driver)
Close driver/database connection.
Definition: db.c:62
int db_set_handle(dbHandle *, const char *, const char *)
Set handle (database and schema name)
Definition: handle.c:39
#define NULL
Definition: ccmath.h:32
int db_close_database(dbDriver *)
Close database connection.
Definition: c_closedb.c:26
const struct driver * driver
Definition: driver/init.c:25
int db_shutdown_driver(dbDriver *)
Closedown the driver, and free the driver structure.
Definition: shutdown.c:36
void db_init_handle(dbHandle *)
Initialize handle (i.e database/schema)
Definition: handle.c:23
int db_open_database(dbDriver *, dbHandle *)
Open database connection.
Definition: c_opendb.c:27
Definition: driver.h:22
#define DB_FAILED
Definition: dbmi.h:72
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
int G_debug(int, const char *,...) __attribute__((format(printf
#define DB_OK
Definition: dbmi.h:71