#ifndef __DB_UTILS_H #define __DB_UTILS_H /*------------------------------------------------------------------------- * Copyright (c) 1999-2004 Kenneth W. Sodemann (stuffle@mac.com) *------------------------------------------------------------------------- * db_utils * * Synopsis: * Basic utility functions to generalize access to the database. * * $Id: db_utils.h,v 1.12 2004/11/25 23:44:07 stuffle Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * *------------------------------------------------------------------------- */ #include #include #include /* * transaction handlers * * The finalize_transaction function is just an easy way to do either * a commit or a rollback depending on the status of the result. */ #define START_TRANSACTION(conn) PQclear (PQexec ((conn), "BEGIN")) #define COMMIT(conn) PQclear (PQexec ((conn), "COMMIT")) #define ROLLBACK(conn) PQclear (PQexec ((conn), "ROLLBACK")) gboolean finalize_transaction (PGconn *conn, PGresult *res); /* * DbLoginData * * This structure contains the common data that may be needed to * log in to a database. */ typedef struct DbLoginData { GList *db_name_list; GString *username; GString *password; GString *db_name; GString *host; GString *port; } DbLoginData; /*------------------------------------------------------------------------- * Function: get_db_access_lvl * * Synopsis: * Return the preps access level of the current database user. Note * this is PRepS level access. We would not even be this far * if the user did not have PostgreSQL level access to the database. * * Returns: * -1 : No PRepS access to this database * 0 : Regular PRepS user * 1 : PRepS admin user *------------------------------------------------------------------------- */ gint get_db_access_lvl (PGconn *conn); /*------------------------------------------------------------------------- * Function: init_db_data * * Synopsis: * Initialize a DbLoginData structure. db_name is set according * to the following priority: * o last accessed database if def_db is NULL (Gnome only) * o default database name ("myprs") if no other db info is * available. * Other fields are set as appropriate based on how the db_name * gets set. *------------------------------------------------------------------------- */ void init_db_data (GConfClient *client, DbLoginData *db_data); /*------------------------------------------------------------------------- * Function: commit_db_data * * Synopsis: * *------------------------------------------------------------------------- */ void commit_db_data (GConfClient *client, const DbLoginData *db_data); /*------------------------------------------------------------------------- * Function: set_db_username * * Synopsis: * Set the username field to the given user name. *------------------------------------------------------------------------- */ void set_db_username (DbLoginData *db_data, const char *username); /*------------------------------------------------------------------------- * Function: clear_db_username * * Synopsis: * Free up the space for the current user name (if any), set to * NULL. *------------------------------------------------------------------------- */ void clear_db_username (DbLoginData *db_data); /*------------------------------------------------------------------------- * Function: set_db_password * * Synopsis: * Set the password field to the given password. *------------------------------------------------------------------------- */ void set_db_password (DbLoginData *db_data, const char *user_name); /*------------------------------------------------------------------------- * Function: clear_db_password * * Synopsis: * Free up the space for the current password (if any), set to * NULL. *------------------------------------------------------------------------- */ void clear_db_password (DbLoginData *db_data); /*------------------------------------------------------------------------- * Function: set_db_name * * Synopsis: * Set the database name field to the given database name. * * If "default_others" is TRUE, default the username, host, and port * data based on the last time this db name was used, (defaults are * used if this is the first time with this database). * *------------------------------------------------------------------------- */ void set_db_name (GConfClient *client, DbLoginData *db_data, const char *db_name, gboolean default_others); /*------------------------------------------------------------------------- * Function: clear_db_name * * Synopsis: * Free up the space for the current database name (if any), set to * NULL. *------------------------------------------------------------------------- */ void clear_db_name (DbLoginData *db_data); /*------------------------------------------------------------------------- * Function: set_db_host * * Synopsis: * Set the host field to the given host ID. *------------------------------------------------------------------------- */ void set_db_host (DbLoginData *db_data, const char *user_name); /*------------------------------------------------------------------------- * Function: clear_db_host * * Synopsis: * Free up the space for the current host ID (if any), set to * NULL. *------------------------------------------------------------------------- */ void clear_db_host (DbLoginData *db_data); /*------------------------------------------------------------------------- * Function: set_db_port * * Synopsis: * Set the port field to the given port. *------------------------------------------------------------------------- */ void set_db_port (DbLoginData *db_data, const char *user_name); /*------------------------------------------------------------------------- * Function: clear_db_port * * Synopsis: * Free up the space for the current port (if any), set to * NULL. *------------------------------------------------------------------------- */ void clear_db_port (DbLoginData *db_data); /*------------------------------------------------------------------------- * Function: clear_db_data * * Synopsis: * Free up the space held by all the fields, and set them to NULL. *------------------------------------------------------------------------- */ void clear_db_data (DbLoginData *db_data); /*------------------------------------------------------------------------- * Function: db_login * * Synopsis: * Login to the database. * * Inputs: * db_data : const pointer to the db data to use for the login * * Outputs: * None. * * Return Value: * Returns a pointer to the connection. It is the callers * responsibility to check the connection pointer for validity. * *------------------------------------------------------------------------- */ PGconn *db_login (const DbLoginData *db_data); /*------------------------------------------------------------------------- * Function: db_shutdown * * Synopsis: * Logoff the database, and perform general shutdown routines. * * Inputs: * None. * * Outputs: * None. * * Return Value: * None. * *------------------------------------------------------------------------- */ void db_shutdown (PGconn *conn); /*------------------------------------------------------------------------- * Function: db_stat_string * * Synopsis: * Given an exec status, return a status message that makes sense. * * Inputs: * The exec status code. * * Outputs: * None. * * Return Value: * The status message. * *------------------------------------------------------------------------- */ const char *db_stat_string (ExecStatusType stat); #endif