| 1 |
/********************************************************************\
|
| 2 |
* database.c -- manage database connection info. *
|
| 3 |
* Copyright (C) 2002 Linas Vepstas <linas@linas.org> *
|
| 4 |
* http://dwi.sourceforge.net *
|
| 5 |
* *
|
| 6 |
* This library is free software; you can redistribute it and/or *
|
| 7 |
* modify it under the terms of the GNU Lesser General Public *
|
| 8 |
* License as published by the Free Software Foundation; either *
|
| 9 |
* version 2.1 of the License, or (at your option) any later version.
|
| 10 |
* *
|
| 11 |
* This library is distributed in the hope that it will be useful, *
|
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
| 14 |
* GNU Lesser General Public License for more details. *
|
| 15 |
* *
|
| 16 |
* You should have received a copy of the GNU Lesser General Public *
|
| 17 |
* License along with this program; if not, contact: *
|
| 18 |
* *
|
| 19 |
* Free Software Foundation Voice: +1-617-542-5942 *
|
| 20 |
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
|
| 21 |
* Boston, MA 02111-1307, USA gnu@gnu.org *
|
| 22 |
\********************************************************************/
|
| 23 |
|
| 24 |
/*
|
| 25 |
* HISTORY:
|
| 26 |
* Linas Vepstas March 2002
|
| 27 |
*/
|
| 28 |
|
| 29 |
#include <string.h>
|
| 30 |
#include <stdlib.h>
|
| 31 |
|
| 32 |
#include <glib.h>
|
| 33 |
#include <glib-object.h>
|
| 34 |
|
| 35 |
#include "perr.h"
|
| 36 |
#include "database.h"
|
| 37 |
#include "dui-initdb.h"
|
| 38 |
|
| 39 |
struct DuiDatabase_s
|
| 40 |
{
|
| 41 |
GObject gobject;
|
| 42 |
char * name;
|
| 43 |
|
| 44 |
/* database info */
|
| 45 |
char * provider;
|
| 46 |
char * dbname;
|
| 47 |
char * hostname;
|
| 48 |
char * username;
|
| 49 |
char * authent;
|
| 50 |
|
| 51 |
/* current database connection that is being used */
|
| 52 |
DuiDBConnection *db_conn;
|
| 53 |
};
|
| 54 |
|
| 55 |
/* properties */
|
| 56 |
enum
|
| 57 |
{
|
| 58 |
DB_NAME=1,
|
| 59 |
DB_PROVIDER,
|
| 60 |
DB_DBNAME,
|
| 61 |
DB_HOSTNAME,
|
| 62 |
DB_USERNAME,
|
| 63 |
DB_AUTH,
|
| 64 |
};
|
| 65 |
|
| 66 |
typedef struct DuiDatabaseClass_s
|
| 67 |
{
|
| 68 |
GObjectClass parent_class;
|
| 69 |
} DuiDatabaseClass;
|
| 70 |
|
| 71 |
/* =================================================================== */
|
| 72 |
|
| 73 |
static void
|
| 74 |
db_get_property (GObject *object, guint property_id,
|
| 75 |
GValue *value, GParamSpec *pspec)
|
| 76 |
{
|
| 77 |
char * str = NULL;
|
| 78 |
GValue val;
|
| 79 |
DuiDatabase *db = DUI_DATABASE(object);
|
| 80 |
|
| 81 |
g_value_init (&val, G_TYPE_STRING);
|
| 82 |
switch (property_id)
|
| 83 |
{
|
| 84 |
case DB_NAME: str=db->name; break;
|
| 85 |
case DB_PROVIDER: str=db->provider; break;
|
| 86 |
case DB_DBNAME: str=db->dbname; break;
|
| 87 |
case DB_HOSTNAME: str=db->hostname; break;
|
| 88 |
case DB_USERNAME: str=db->username; break;
|
| 89 |
case DB_AUTH: str=db->authent; break;
|
| 90 |
|
| 91 |
}
|
| 92 |
g_value_set_string (&val, str);
|
| 93 |
g_value_copy (&val, value);
|
| 94 |
}
|
| 95 |
|
| 96 |
|
| 97 |
static void
|
| 98 |
db_set_property (GObject *object, guint property_id,
|
| 99 |
const GValue *value, GParamSpec *pspec)
|
| 100 |
{
|
| 101 |
char * str;
|
| 102 |
DuiDatabase *db = DUI_DATABASE(object);
|
| 103 |
|
| 104 |
str = g_strdup (g_value_get_string (value));
|
| 105 |
switch (property_id)
|
| 106 |
{
|
| 107 |
case DB_NAME: g_free (db->name); db->name = str; break;
|
| 108 |
case DB_PROVIDER: g_free (db->provider); db->provider = str; break;
|
| 109 |
case DB_DBNAME: g_free (db->dbname); db->dbname = str; break;
|
| 110 |
case DB_HOSTNAME: g_free (db->hostname); db->hostname = str; break;
|
| 111 |
case DB_USERNAME: g_free (db->username); db->username = str; break;
|
| 112 |
case DB_AUTH: g_free (db->authent); db->authent = str; break;
|
| 113 |
}
|
| 114 |
}
|
| 115 |
|
| 116 |
|
| 117 |
/* =================================================================== */
|
| 118 |
|
| 119 |
#define INSTALL(PROP,NAME,DESC) { \
|
| 120 |
GParamSpec *pspec; \
|
| 121 |
pspec = g_param_spec_string (NAME, NULL, \
|
| 122 |
DESC, NULL, G_PARAM_READWRITE); \
|
| 123 |
g_object_class_install_property (goc, PROP, pspec); \
|
| 124 |
}
|
| 125 |
|
| 126 |
|
| 127 |
static void
|
| 128 |
db_class_init (DuiDatabaseClass *dbclass, gpointer class_data)
|
| 129 |
{
|
| 130 |
GObjectClass *goc;
|
| 131 |
|
| 132 |
dbclass->parent_class.set_property = db_set_property;
|
| 133 |
dbclass->parent_class.get_property = db_get_property;
|
| 134 |
|
| 135 |
goc = G_OBJECT_CLASS (dbclass);
|
| 136 |
|
| 137 |
INSTALL (DB_NAME, "name", "DWI Database term name");
|
| 138 |
INSTALL (DB_PROVIDER, "provider", "DWI database driver name");
|
| 139 |
INSTALL (DB_DBNAME, "dbname", "Database name");
|
| 140 |
INSTALL (DB_HOSTNAME, "hostname", "TCP/IP host name");
|
| 141 |
INSTALL (DB_USERNAME, "username", "User Login");
|
| 142 |
INSTALL (DB_AUTH, "authentication", "Password or Other Authentication");
|
| 143 |
}
|
| 144 |
|
| 145 |
|
| 146 |
/* =================================================================== */
|
| 147 |
|
| 148 |
static void
|
| 149 |
db_init (DuiDatabase *db, DuiDatabaseClass *dbclass)
|
| 150 |
{
|
| 151 |
db->name = NULL;
|
| 152 |
db->provider = NULL;
|
| 153 |
db->dbname = NULL;
|
| 154 |
db->hostname = NULL;
|
| 155 |
db->username = NULL;
|
| 156 |
db->authent = NULL;
|
| 157 |
|
| 158 |
db->db_conn = NULL;
|
| 159 |
}
|
| 160 |
|
| 161 |
/* =================================================================== */
|
| 162 |
|
| 163 |
GType
|
| 164 |
dui_database_get_type (void)
|
| 165 |
{
|
| 166 |
static GType db_type = 0;
|
| 167 |
|
| 168 |
if (!db_type)
|
| 169 |
{
|
| 170 |
GTypeInfo db_info =
|
| 171 |
{
|
| 172 |
sizeof (DuiDatabaseClass),
|
| 173 |
NULL,
|
| 174 |
NULL,
|
| 175 |
(GClassInitFunc) db_class_init,
|
| 176 |
NULL, NULL,
|
| 177 |
sizeof (DuiDatabase),
|
| 178 |
0,
|
| 179 |
(GInstanceInitFunc) db_init,
|
| 180 |
NULL
|
| 181 |
};
|
| 182 |
|
| 183 |
db_type = g_type_register_static (G_TYPE_OBJECT,
|
| 184 |
"DuiDatabase", &db_info, 0);
|
| 185 |
}
|
| 186 |
return db_type;
|
| 187 |
}
|
| 188 |
|
| 189 |
/* =================================================================== */
|
| 190 |
|
| 191 |
DuiDatabase *
|
| 192 |
dui_database_new (const char * name,
|
| 193 |
const char * provider, const char * dbname,
|
| 194 |
const char * hostname, const char * username,
|
| 195 |
const char * passwd)
|
| 196 |
{
|
| 197 |
DuiDatabase *db;
|
| 198 |
|
| 199 |
g_type_init(); /* assume no one else has don this yet */
|
| 200 |
|
| 201 |
db = g_object_new (DUI_DATABASE_TYPE, NULL);
|
| 202 |
|
| 203 |
/* Any of these values might be set later */
|
| 204 |
if (!name) name = "";
|
| 205 |
if (!provider) provider = "";
|
| 206 |
if (!dbname) dbname = "";
|
| 207 |
if (!hostname) hostname = "";
|
| 208 |
if (!username) username = "";
|
| 209 |
if (!passwd) passwd = "";
|
| 210 |
|
| 211 |
db->name = g_strdup (name);
|
| 212 |
db->provider = g_strdup (provider);
|
| 213 |
db->dbname = g_strdup (dbname);
|
| 214 |
db->hostname = g_strdup (hostname);
|
| 215 |
db->username = g_strdup (username);
|
| 216 |
db->authent = g_strdup (passwd);
|
| 217 |
|
| 218 |
db->db_conn = NULL;
|
| 219 |
|
| 220 |
return db;
|
| 221 |
}
|
| 222 |
|
| 223 |
/* =================================================================== */
|
| 224 |
|
| 225 |
void
|
| 226 |
dui_database_destroy (DuiDatabase *db)
|
| 227 |
{
|
| 228 |
dui_connection_free (db->db_conn);
|
| 229 |
g_free (db->name);
|
| 230 |
g_free (db->provider);
|
| 231 |
g_free (db->hostname);
|
| 232 |
g_free (db->dbname);
|
| 233 |
g_free (db->username);
|
| 234 |
g_free (db->authent);
|
| 235 |
|
| 236 |
/* hack alert XXX need to free the object itself. Not sure how ... */
|
| 237 |
}
|
| 238 |
|
| 239 |
/* =================================================================== */
|
| 240 |
|
| 241 |
DuiDBConnection *
|
| 242 |
dui_database_do_realize (DuiDatabase *db)
|
| 243 |
{
|
| 244 |
|
| 245 |
if (!db) return NULL;
|
| 246 |
if (db->db_conn) return db->db_conn;
|
| 247 |
|
| 248 |
/* now actually open the database */
|
| 249 |
db->db_conn = dui_connection_new (db->provider,
|
| 250 |
db->dbname, db->username, db->authent);
|
| 251 |
if (NULL == db->db_conn)
|
| 252 |
{
|
| 253 |
PWARN ("Can't open connection for provider=\"%s\", "
|
| 254 |
"database=\"%s\", username=\"%s\"",
|
| 255 |
db->provider, db->dbname, db->username);
|
| 256 |
}
|
| 257 |
return db->db_conn;
|
| 258 |
}
|
| 259 |
|
| 260 |
/* =================================================================== */
|
| 261 |
|
| 262 |
const char *
|
| 263 |
dui_database_get_name (DuiDatabase *db)
|
| 264 |
{
|
| 265 |
if (!db) return NULL;
|
| 266 |
return db->name;
|
| 267 |
}
|
| 268 |
|
| 269 |
/* ========================== END OF FILE ============================ */
|