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

📄 dbtoliet.c

📁 bonddb 是一个源于PostgreSQL封装包的对象。它是一个由C/C++编写的快速数据提取层应用软件
💻 C
字号:
#include <glib.h>#include "debug.h"#include "dbobject.h"#include "dbtoliet.h"#include "dbcache.h"#include "db.h"#include "dbclient.h"#include "dbwrapper.h"#include "dbuniqueid.h"#include "dbbureaucrat.h"#include "dbfield.h"#include "dbsqlparse.h"#include "dbfilter.h"/** * db_toliet_flushall: * * Write all objects to the database that have changed.   * * Returns: non-zero on failure */gintdb_toliet_flushall()	{	DbFlush *f;	DbCache *c = NULL;	GList *cachewalk;	GList *walk, *flushlist;	//debugwin ("db_toliet_flushall() was called");	flushlist = db_bureaucrat_changedobjects();	walk = g_list_first(flushlist);	while (walk != NULL)		{		f = walk->data;		cachewalk = g_list_first(f->cachelist);		while (cachewalk != NULL)			{			c = cachewalk->data;			/*debugwrap(f->obj, c, "db_toliet_flushall");*/			db_toliet_flush(f->obj, c);			c->changed = FALSE;			cachewalk = cachewalk->next;			}		f->obj->changed = FALSE;		walk = walk->next;		}	db_bureaucrat_freeflush(flushlist);	return 0;	}/** * db_toliet_flush: * * Flushes an individual cache item * * Returns: non-zero on failure */gintdb_toliet_flush(Object * obj, DbCache * cache)	{	gint cont = 0;	gchar *query = NULL;	DbUniqueId *id;	debugwin ("db_toliet_flush() called by obj\n"			"\tname: %s\n"			"\tcache value: %s\n"			"\tcache pg_oid: %d\n"			"\tcache id: ?\n",		obj->name, cache->value[0], cache->id->pg_oid);	debugwin ("obj->changed: %d\n"			"cache->changed: %d\n"			"db_iscachechanged(cache): %d\n", obj->changed, cache->changed, db_iscachechanged(cache));	if (obj->changed == FALSE || cache->changed == FALSE)		{		errormsg("Your trying to flush something not chagned.?");		g_assert(NULL);		}	if (db_iscachechanged(cache) == FALSE)		return 1;	db_toliet_checkfordefaults(obj, cache);	/* db_toliet_debug(obj); */	/* create the sql for adding the string to db. These statements will create a query which can be run against sql */	if (db_iscachenewrecord(obj) == TRUE)		{		cont = db_toliet_sqlinsert(obj, cache, &query);		}	else		{		cont = db_toliet_sqlupdate(obj, cache, &query);		}	if (cont == 1)		{		if (db_obj_sqlwrite(query, obj->name, &id) == 1)			{			/* Re-adjust the idindex and possible obj index cause the id just changed */			db_cache_updateid(obj, id, cache);			}		else {			return -1;		}		mem_free(query);		}	/* clear cache */	db_toliet_setwrote(cache);	debugmsg("i've just done an insert on %s, with %d %d items there", obj->name, db_numrecord(obj), obj->num);	return 0;	}/** * db_toliet_flushobject: * * Write all objects to the database that have changed.   * * Returns: non-zero on failure */gboolean dbtolietdebug = TRUE;gintdb_toliet_flushobject(Object * obj)	{	DbFlush *f;	DbCache *c;	GList *cachewalk;	//if (dbtolietdebug) {debugwin ("db_toliet_flushobject() was called");}	f = db_bureaucrat_createflush(obj);	if (f == NULL)		return -1;	cachewalk = g_list_first(f->cachelist);	while (cachewalk != NULL)		{		c = cachewalk->data;		/*db_toliet_flush*/debugwrap(obj, c, "db_toliet_flushobject");		c->changed = FALSE;		cachewalk = cachewalk->next;		}	obj->changed = FALSE;	g_list_free(f->cachelist);	mem_free(f);	return 0;	}/** * db_toliet_sqlinsert: * @obj: * @c: * @query: * * Creates an SQL statement which is saved into @query.  Make sure you free it up after  * your done.  The sql statement is based on the contents of @c and @obj. * * Returns: non-zero on error */gintdb_toliet_sqlinsert(Object * obj, DbCache * c, gchar ** query)	{	gchar *vstr = NULL, *str = NULL, *cval = NULL, *fval = NULL, *fieldparts = NULL, *vfieldparts = NULL;	gchar *rubbish;	gchar *fieldstr, *tablestr;	gint cont = 0, i;	*query = NULL;	/* run some checks on the object */	/* Renable this code for object fillins. It means if a value is invalid in a record set it will guess whats suppose	   to go in there before writing it back to the database */	/* db_makerecordvalid(c->obj); */	/* build up insert string */	for (i = 0; i < c->num; i++)		{		if (c->value[i] == NULL)			{			/* this is a null value so dont need to do anything */			continue;			}		else			{			db_field_get(obj, i, &fieldstr, &tablestr);			if (fieldstr != NULL)				if (tablestr == NULL || strcmp(tablestr, obj->name) == 0)					{					if (cont == 0)						{						fval = mem_strdup_printf("%s", fieldstr);						rubbish = db_sqlparse_dropquation(c->value[i]);						cval = mem_strdup_printf("'%s'", rubbish);						mem_free(rubbish);						}					else						{						fval = mem_strdup_printf(",%s", fieldstr);						rubbish = db_sqlparse_dropquation(c->value[i]);						cval = mem_strdup_printf(",'%s'", rubbish);						mem_free(rubbish);						}					cont = 1;					}			}		/* begginning part of string with the field names */		if (fieldparts != NULL)			{			vfieldparts = g_strconcat(fieldparts, fval, NULL);			g_free(fieldparts);			}		else			vfieldparts = g_strdup(fval);		mem_free(fval);		fieldparts = vfieldparts;		/* second part of string with individual values */		if (str != NULL)			{			vstr = g_strconcat(str, cval, NULL);			g_free(str);			}		else			vstr = g_strdup(cval);		mem_free(cval);		str = vstr;		}	if (cont == 1)		{		/* join string together */		*query = mem_strdup_printf("INSERT INTO %s (%s) VALUES (%s)", obj->name, vfieldparts, vstr);		g_free(vstr);		g_free(vfieldparts);		}	return cont;	}/** * db_toliet_sqlupdate: * * create an update query to play with  */gintdb_toliet_sqlupdate(Object * obj, DbCache * c, gchar ** query)	{	gchar *vstr = NULL, *str = NULL, *cval = NULL;	gchar *rubbish, *fieldstr, *tablestr;	gint cont = 0, i;	*query = NULL;	/* Renable this code for object fillins. It means if a value is invalid in a record set it will guess whats suppose	   to go in there before writing it back to the database */	/* db_makerecordvalid(c->obj); */	/* build up update string */	for (i = 0; i < c->num; i++)		{		if (c->value[i] == NULL)			continue;		rubbish = db_sqlparse_dropquation(c->value[i]);		db_field_get(obj, i, &fieldstr, &tablestr);		if (fieldstr != NULL)			if (tablestr == NULL || strcmp(tablestr, obj->name) == 0)				{				if (cont == 0)					cval = mem_strdup_printf("%s='%s'", fieldstr, rubbish);				else					cval = mem_strdup_printf(",%s='%s'", fieldstr, rubbish);				}		mem_free(rubbish);		cont = 1;		if (str != NULL)			{			vstr = g_strconcat(str, cval, NULL);			g_free(str);			}		else			vstr = g_strdup(cval);		mem_free(cval);		str = vstr;		}	/* things good so far so do final step of building the query */	if (cont == 1)		{		if (db_id_isvalid(c->id) == FALSE)			{			errormsg("Record uniqueid is invalid");			g_assert(NULL);			return -1;			}		rubbish = db_id_createsql(obj, c->id);		*query = mem_strdup_printf("UPDATE %s SET %s WHERE %s", obj->name, vstr, rubbish);		mem_free(rubbish);		g_free(vstr);		}	return cont;	}/* I removed some good comments here *//** * db_toliet_setwrote:  * @cache: Database cache object * * Set the current cache object to say that it has been written to the database. * * Returns: non-zero on failure */gintdb_toliet_setwrote(DbCache * cache)	{	if (cache->state == OBJ_NEW)		cache->state = OBJ_EDITNEW;	cache->changed = FALSE;	/* Todo: return based on success or failure */	return 1;	}/** * db_toliet_checkfordefaults: * @obj: database object it came from * @cache: cache to check * * Look to see if anything uses the name EXECFUNCTION and then excute a function * if it exists *  * Returns: non-zero on error */gintdb_toliet_checkfordefaults(Object * obj, DbCache * cache)	{	gint i;	DbField *field;	if (obj->numfield != cache->num)		errormsg("I'm going to segfault");	for (i = 0; i < cache->num; i++)		{		/* its a function, run it */		if (cache->value[i] != NULL)			if (strcmp(cache->value[i], "EXECFUNCTION") == 0)				{				field = obj->field[i];				if (field->fielddef->defaultvalue != NULL && strlen(field->fielddef->defaultvalue) > 0)					{					mem_free(cache->value[i]);					cache->value[i] = db_default_execfunction(field->fielddef->defaultvalue);					/* debugmsg("replacing %s with value %s",field->field,cache->value[i]); */					}				}		}	return 0;	}gintdebugwrap (Object * obj, DbCache * cache, const char *func){	static int count = 0;	const char *msg;	msg = g_strdup_printf ("From caller %s(): , count: %d", func, count++);	debugwin (msg);	g_free (msg);	db_toliet_flush (obj, cache);}

⌨️ 快捷键说明

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