⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbclient.c

📁 bonddb 是一个源于PostgreSQL封装包的对象。它是一个由C/C++编写的快速数据提取层应用软件
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <glib.h>#include <glob.h>#include <dlfcn.h>#include "dbclient.h"#include "debug.h"#include "dbgather.h"#include "dbobject.h"#include "dbwrapper.h"#include "dbtoliet.h"#include "dbbureaucrat.h"#include "dblog.h"#define NUMPLUGINPATHS 4#define NUMBONDCONFSEARCH 5static gchar *plugin_search_path[NUMPLUGINPATHS] = { "./.libs/", "./", "/usr/local/lib/",      "/usr/lib/"                                            };gchar *bondconffilesearch[NUMBONDCONFSEARCH] = { "../etc/bond/", "/usr/local/etc/bond/",      "/etc/bond/", "/etc/", "/usr/local/etc/"                                               };/* startup database connections. This is principle init function for bonddb *//** * * Look for @searchforstr in @parentstr, return 0 if true. */static gintdb_strcontain(gchar *parentstr, gchar *searchforstr)	{	gint i, pass=0;	if (searchforstr == NULL || parentstr == NULL)		return 0;	for (i=0;i<strlen(parentstr);i++)		{		if (parentstr[i] == searchforstr[pass])			pass++;		else			pass = 0;		if (pass == strlen(searchforstr))			return 0;		}		return 1;	}/** * db_init: * @conninfo: standard postgresql database connection string or the name of the application * * Establishes database connection to postgresql, and extracts all the  * information postgresql needs to work with the database.  If @conninfo  * is NULL then the bond.conf configuration files will  * be read to get the database initialising string. * * @conninfo can be either the name of the config file (ie appname.conf) * or a database connection string. It looks for the string dbname * in @conninfo and if present it assumes its a database connection * string else it assumes its not and is a filename. *  * Returns: non-zero on failure. */gintdb_init(gchar * conninfo)   {   gchar *c;   /*	if (conninfo == NULL)   		{   		errormsg("The API for db_init() has changed. You can no longer pass in NULL\n"   					"as a valid input for db_init().  You need to pass in a database\n"   					"connection string or the name of the application (argv[0]).\n");   		return -2;   		}   */	/* debugmsg("passing in conninfo of %s",conninfo); */	/* this is a tad complex and can cause a few bugs. If conninfo is a db	 * string then initialise it else do a read on a config file 	 */   if (conninfo == NULL || db_strcontain(conninfo,"dbname") != 0)      c = db_readconfigfile(conninfo);   else      c = mem_strdup_printf("%s", conninfo);	if (c == NULL)		{		errormsg("Error creating connection string from %s. Aborting",conninfo);		}   if (globaldbconn != NULL)      db_cleanup();   debug_init();   /* dont you hate it when code you write goes missing? */   globaldbconn = db_dbconnect(c);   if (db_dbstatus(globaldbconn) != 0)      {		 db_dbfinish(globaldbconn);		 errormsg("Connection to database failed with %s argument.\nerror msg %s", c, db_dberrormsg(globaldbconn));		 mem_free(c);		 exit(-1);      }   globaldb = db_builddef();   globaldb->connstr = mem_strdup(c);   mem_free(c);   return 0;   }static gchar *db_findbondconffile_ensurefine(gchar * appfile)   {   gint i, num;   g_assert(appfile);   num = strlen(appfile);   for (i = num - 1; i >= 0; i--)      {      if (appfile[i] == '/')         {         debugmsg("returning %s", appfile + (i + 1));         return appfile + (i + 1);         }      }   return appfile;   }/** * db_findbondconffile: * @appfile: name of the application been run (argc[0]) *  * Search for the configuration files in a number of different locations before * running.  Please note, this is hardcoded to the local directory, /usr/local/etc * and /etc. */FILE *db_findbondconffile(gchar * appfile)   {   FILE *file;   gint i, count;   glob_t g;   gchar *pattern;	gchar *filename = NULL;	if (db_strcontain(appfile,".conf") != 0) 		filename = mem_strdup_printf("%s.conf",appfile);	else		filename = mem_strdup(appfile);   if ((file = fopen(filename, "r")) != NULL)		{		mem_free(filename);      return file;		}	mem_free(filename);   if ((file = fopen(DEFAULTCONFIGFILE, "r")) != NULL)      return file;   if ((file = fopen(".bond", "r")) != NULL)      return file;	debugmsg("i still can't find it.");   if (appfile == NULL)      errormsg("No arguments were passed into db_init() or bond_init() and application was unable"               "to work out its roots. Arr i'm lost.  Try copying bond.conf to your working directory, or ensure\n"	       	       "/etc/bond/appname.conf exists.");   g_assert(appfile);   appfile = db_findbondconffile_ensurefine(appfile);   pattern = mem_strdup_printf("%s", appfile);   if ((file = fopen(pattern, "r")) != NULL)      {      mem_free(pattern);      return file;      }   mem_free(pattern);   for (i = 0; i < NUMBONDCONFSEARCH; i++)      {      pattern = mem_strdup_printf("%s%s", bondconffilesearch[i], appfile);      debugmsg("looking for a match on %s", pattern);      if (glob(pattern, 0, NULL, &g) == 0)         for (count = 0; count < g.gl_pathc; count++)            if ((file = fopen(g.gl_pathv[count], "r")) != NULL)               {               globfree(&g);               mem_free(pattern);               return file;               }      mem_free(pattern);      }   globfree(&g);   return NULL;   }/* shutdown database connection *//** * db_cleanup: * * Frees up all memory used by db_init(). Will flush all write ahead caches to make sure data is  * saved and then close database connections.  Make sure you call this else your last record entered * maybe lost. * * Returns: non-zero on failure. */gintdb_cleanup()   {   extern DbDatabaseDef *globaldb;   /* flush everything outstanding */   db_toliet_flushall();	/* clear out the bureaucrat */	db_bureaucrat_cleanup();	/* empty the logs */	db_lomem_free();   /* free tables up */   db_freedatabasedef(globaldb);   /* close connections */   db_dbfinish(globaldbconn);   globaldbconn = NULL;   debug_cleanup();   return 0;   }/** * db_restart: *  * Restart database connection, incase it either dies. * * Returns: non-zero on failure. */gintdb_restart()   {   db_dbreset(globaldbconn);   return 0;   }/** * db_readconfigfile: * @appfile: Name of application to load * * Read a configuration file for database etc settings.  This may need readapting at some * point to a more powerful and standard configuration method. * * Returns: NULL on failure, else the db configuration connection string. *//** \todo{ look for code from someone else possible. read config files} */gchar *db_readconfigfile(gchar * appfile)   {   gchar *gladexmlfile, *dbstring = NULL, *uiplugin;   gint debuginfo, debuglineinfo;   db_readallconfig(appfile, &gladexmlfile, &dbstring, &uiplugin, &debuginfo, &debuglineinfo);   if (gladexmlfile != NULL)      mem_free(gladexmlfile);   if (uiplugin != NULL)      mem_free(uiplugin);   return dbstring;   }/** * db_readallconfig: * @appfile: Name of application been run * @uiplugin: User interface widget set plugin to be used. (uigtk, uihtml, uiwx etc), returned string * @dbstring: Database connection string, returned string * @gladexmlfile: Glade xml file to use. * * Liam write me */voiddb_readallconfig(gchar * appfile, gchar ** gladexmlfile, gchar ** dbstring, gchar ** uiplugin , gint * debuginfo, gint * debuglineinfo)   {   FILE *configfile = NULL;   gchar *buffer = (gchar *) mem_alloc(1000);   gchar *value;   gint slen;   *gladexmlfile = NULL;   *dbstring = NULL;	*uiplugin = NULL;   *debuginfo = 1;   *debuglineinfo = 0;   configfile = db_findbondconffile(appfile);   if (configfile == NULL)      {      errormsg("Unable to find a config file for %s\nEnsure there is a %s.conf file"               "in your /etc/bond/ directory or /usr/local/etc/bond directory.", appfile, appfile);      exit( -1);      }   fgets(buffer, 1000, configfile);   value = strstr(buffer, "=");   if (value && strcasecmp(value + 1, "yes\n") == 0)      *debuginfo = 1;   else      *debuginfo = 0;   fgets(buffer, 1000, configfile);   value = strstr(buffer, "=");   if (value && strcasecmp(value + 1, "yes\n") == 0)      *debuglineinfo = 1;   else      *debuglineinfo = 0;   fgets(buffer, 1000, configfile);   value = strstr(buffer, "=");   if (!value)      value = buffer;							 /* support old-style configs */   if (value)      {      *gladexmlfile = mem_strdup(value + 1);      slen = strlen(*gladexmlfile);      (*gladexmlfile)[slen - 1] = 0;	 /* strip off CR ??? */      }   else      {      mem_free(buffer);      return ;      }   fgets(buffer, 1000, configfile);	if (buffer == NULL || strlen(buffer) == 0)		return ;   *dbstring = mem_strdup(strstr(buffer, "=") + 1);   slen = strlen(*dbstring);   (*dbstring)[slen - 1] = 0;   /* printf("%s\n", *dbstring); */   mem_free(buffer);	if (feof(configfile))		{		fgets(buffer, 1000, configfile);		*uiplugin = mem_strdup(strstr(buffer, "=") + 1);		slen = strlen(*uiplugin);		(*uiplugin)[slen - 1] = 0;		/* printf("%s\n", *uiplugin); */		}	   }/* load that library eh */static void *check_shared_library(char *so)   {   void *handle;   void *sym;   if ((handle = dlopen(so, RTLD_LAZY)) == 0)      {      errormsg("An error occured while reading the shared library %s\n%s", so, dlerror());      return NULL;      }   if ((sym = dlsym(handle, "plugin_init")) == 0)      {      dlclose(handle);      return NULL;      }   return handle;   }gintdb_loadbindings(gchar * driver)   {   return 0;   }/*static void*db_load_library(gchar *so);	{	gint i, count;	glob_t g;	gchar *pattern; 	g_assert(library);		plugin = mem_alloc(sizeof(Plugin));	if (args != NULL) 		args = mem_strdup(args);	if (library!= NULL) 		library = mem_strdup(library);		plugin->name = library;	plugin->args = args;	plugin->func = NULL;	plugin->numfunctions = 0;  	for (i = 0; i < NUMPLUGINPATHS; i++)		{		pattern = mem_strdup_printf("%s*.so", plugin_search_path [i]);				if (glob (pattern, 0, NULL, &g) == 0)			{			for (count = 0; count < g.gl_pathc; count++)				{				debugmsg ("Checking library: %s", g.gl_pathv[count]);								plugin->dlhandle = check_shared_library (g.gl_pathv[count]);								if (plugin->dlhandle!=NULL)					{					plugin->sym = dlsym (plugin->dlhandle, "plugin_init");					g_assert (plugin->sym);					if (plugin->sym (plugin) == 0)						{						globfree (&g);						debugmsg("Plugin successfully loaded.");						return plugin;						}					}				}			}		mem_free (pattern);		}	warningmsg("Failed to load plugin\n");	mem_free (plugin->name);	mem_free (plugin->args);	mem_free (plugin);	} */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -