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

📄 db.c

📁 bonddb 是一个源于PostgreSQL封装包的对象。它是一个由C/C++编写的快速数据提取层应用软件
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <glib.h>#include "debug.h"#include "dbwrapper.h"#include "db.h"#include "dbclient.h"#include "dbgather.h"#include "dbobject.h"#include "dbconstraint.h"#include "dbtoliet.h"#include "dbuniqueid.h"#include "dbbureaucrat.h"#include "dbsuggest.h"#include "dbbirth.h"#include "dbobjectnav.h"#include "dbobjectdel.h"#include "dbsqlparse.h"/** * db_getvalue: * @obj: Database object * @field: Field name * @table: a class to look up in, or NULL if the current class. * @retval: Where to set the string after i've got the value. * * Toplevel function.  Retreives information from the database.  * * Returns: non-zero on failure. */gintdb_getvalue(Object * obj, gchar * field, gchar * table, gchar ** retval)   {   gint rval;   DbSuggest *suggest;   g_assert(obj);   *retval = NULL;	if (obj->row >= obj->num || obj->row < 0)		{		debugmsg("Unable to read, your outside of range for your current row. (%d of %d)",obj->row,obj->num);		return -2;		}   /* Establish if field is a remote location. Null will be returned if it cna't be done */   suggest = db_suggest_all(obj, field, table);   if (suggest->err != 0)      {      errormsg("Unable to find values for %s.%s. Aborting.", table, field);		db_suggest_free(suggest);      return -1;      }   rval = db_obj_doread(suggest->obj, suggest->fieldname, suggest->tablename, retval);   if (rval < 0)      warningmsg("error was encounted");		db_suggest_free(suggest);   return rval;   }/* create an empty object ready for stuff to be done to it *//** * db_createobject: * @tablename: Name of a table where the record will be inserted * * Will create a fresh new database object ready for use.  If %NULL is passed into @tablename * then this record isn't used for updating back to the database, just reading. * * Returns: A newly created database object, or %NULL on error. */Object *db_createobject(gchar * tablename)   {   Object *obj;   obj = db_obj_create(tablename);   obj->numfield = db_numfields(obj);   db_obj_handle_new_recordset(obj);   /* Arr ok this should do stuff now.. . */   return obj;   }/** * db_freeobject: * @obj: Database object * * Free up a database object, both clean up recordsets and all allocated memory. It will also * be removed from the bureaucrats records. */voiddb_freeobject(Object * obj)   {   db_obj_free(obj);   }/** * db_sql: * @obj: * @sql: * * Run a SQL statement on the backend database and save the results into a Object * * Returns: returns @obj or a newly created object if @obj is %NULL *//* note, request the oid field last in the list, ie select * from ... */Object *db_sql(Object * obj, gchar * sql)   {   gint typeofsql;   gchar *targettable;   DbRecordSet *res;   DbUniqueId *id;   extern DbConnection *globaldbconn;   /* if you pass in NULL it'll create a brand new spanking object and then do some fancy stuff on it to make it safe */   if (obj == NULL)      obj = db_obj_create(NULL);   typeofsql = db_sqlparse_typeofsql(sql);   /* run the query */   if (typeofsql == 0)      {      db_obj_clear(obj);      db_obj_sqlread(obj, sql);      }   else      {      /* do a write */      if (obj == NULL)         {         res = db_dbexec(globaldbconn, sql);         db_dbclear(res);         return 0;         }      targettable = db_sqlparse_getfirsttable(sql);      db_obj_sqlwrite(sql, targettable, &id);      /* db_id_updateidindex(obj, id, cache->origrow); */      }   /* return object */   return obj;   }/* load everything in a table. oh joy *//** * db_loadall: * @obj: Database object to load results into *  * Load all fields in a table into an object *  * Returns: non-zero on failure */gintdb_loadall(Object * obj)   {   gchar *query;   gint retval;   g_assert(obj);   query = mem_strdup_printf("SELECT * FROM %s", obj->name);   db_obj_clear(obj);   retval = db_obj_sqlread(obj, query);   mem_free(query);   return retval;   }/* marika needs to get ... supermarket, i from from supermarket, ...   bazmarty price.   tomota sauce   pipped prunes   potato pomps - frozen ones   sascuages   deviled sausages sauce in a can   chicken stock. - real liquid stock.*//** * db_loadobjectbyobject: * @obj: database object, source object * @destobj: database object, the returned object list * @desttable: destination table to load by *  * Destination object needs to be already created on your part.  What this will * do is run the nessary sql statements on the @destobj to make it a subset * of the @obj database object. * * Returns: non-zero on failure */gintdb_loadobjectbyobject(Object * obj, Object * destobj, gchar * desttable)   {   gint retval = 0;   gchar *query;   DbConstraintDef *cref;   DbTableDef *desttabledef;   DbBirth *birth;	/* You can't display a object from a non-saved object. */   if (db_isnewrecord(obj) == TRUE)      {		if (db_isrecordchanged(obj) == FALSE)			{			debugmsg("Record has not changed so i will abort");			return retval;				}      /* flush the buffer if its waiting to be writtened */		db_toliet_flushobject(obj);      debugmsg("%s is a new record, I am flushing it so i can use it properly.", obj->name);		retval = 1;      }   /* loading yourself by yourself. hmmm */   if (strcmp(desttable, obj->name) == 0)      {      birth = db_birth_create(obj->id, desttable);      query = db_birth_createsql(birth);      db_birth_save(destobj, birth);      retval = db_obj_sqlread(destobj, query);		mem_free(query);		return retval;      }   desttabledef = db_findtable(desttable);   g_assert(desttabledef);   /* TODO: expand support to have more than one table source */   cref = db_constraint_getonmatch(desttabledef->cref, obj->name, desttable, 0);   if (cref == NULL)      cref = db_constraint_getonmatch(desttabledef->cref, desttable, obj->name, 0);   if (cref == NULL)      {      errormsg("Unable to find a relationship between %s and %s, make sure a cref exists", obj->name, desttable);      return -3;      }   /* now the sensible stuff */   db_obj_clear(destobj);   birth = db_birth_create(NULL, desttable);   if(db_birth_applycref(obj, birth, cref)!=0)		{		db_birth_free(birth);		return -1;		}   query = db_birth_createsql(birth);   g_assert(query);   db_birth_save(destobj, birth);   retval = db_obj_sqlread(destobj, query);	mem_free(query);   /* Was there no results from this load? */   /*if (retval == 1)      {      debugmsg("Spring Chickens!");      db_birth_springchicken(destobj);      } */   /*debugmsg("Finishing running loadobjectbyobject, %d records",obj->num);*/   return retval;   }/** * db_refreshobject: * @obj: database object * * Re-run the query on an object. This will refresh all its data and dump anything sitting * in the cache waiting to be flushed. * * Returns: non-zero on error */gintdb_refreshobject(Object * obj)   {   return db_obj_refresh(obj);   }/* Prepare for random comment unattached to any specific code *//* a bit more of the tricky stuff. load an object based on whatsomeone has selected in the first object. hmmm, yes the row doesplay a part in this *//** * db_debugobject: * @obj: database object to show information for *  * Show lots of intersting things about this object. Actually no, no it just prints a list * of the values for every field. boring huh?. * db_obj_debug() differs from thsi function in that it prints out the cache values. */voiddb_debugobject(Object * obj)   {   gchar *tmpstr;   gint x, y;   fprintf(debug_stream, "====== OBJECT CONTENTS =======\n");   for (y = 0; y < obj->num; y++)      {      for (x = 0; x < obj->numfield; x++)         {         tmpstr = db_dbgetvalue(obj->res, y, x);         fprintf(debug_stream, "%s \t", tmpstr);         }      fprintf(debug_stream, "\n");      }   fprintf(debug_stream, "==============================\n");   }/** * db_setvalue: * @obj: database object * @field * @table: * @value: * * Majical function that writes stuff to a database.  You can edit an existing recordset, create * a new record, and modifed a new record fields with this function. * * Returns: non-zero on failure */gintdb_setvalue(Object * obj, gchar * field, gchar * table, gchar * value)   {   gint retval = 0;   DbSuggest *suggest;	if (obj->row < 0 || obj->row >= obj->num)		{		debugmsg("Out of range for row, will not add write");		return -1;		}   suggest = db_suggest_all(obj, field, table);   /* Establish if field is a remote location. Null will be returned if it cna't be done */   if (suggest->err != 0)      {      errormsg("Unable to find values for %s.%s. Aborting.", table, field);      return -1;      }   /* run man, just do it */   retval = db_obj_addwrite(suggest->obj, suggest->fieldname, suggest->tablename, value, TRUE);	db_suggest_free(suggest);   return retval;   }/** * db_setvalue_nomark: * @obj: database object * @field: field to set value into * @table: table to set this into, leave as NULL if main table * @value: string to set the field to * * Majical function that writes stuff to a database.  You can edit an existing recordset, create * a new record, and modifed a new record fields with this function.  The difference * between this function doesn't record the change as perment in the object, so if nothing * else changes in teh object then the changes wont be saved, else they will be. * * Returns: non-zero on failure */gintdb_setvalue_nomark(Object * obj, gchar * field, gchar * table, gchar * value)   {   gint retval = 0;   DbSuggest *suggest;	if (obj->row < 0 || obj->row >= obj->num)		{		debugmsg("Out of range for row, will not add write");		return -1;		}   suggest = db_suggest_all(obj, field, table);   /* Establish if field is a remote location. Null will be returned if it cna't be done */   if (suggest->err != 0)      {      errormsg("Unable to find values for %s.%s. Aborting.", table, field);      return -1;      }   /* run man, just do it */   debugmsg("adding in some default values of %s to %s",value,suggest->fieldname);   retval = db_obj_addwrite(suggest->obj, suggest->fieldname, suggest->tablename, value, FALSE);	db_suggest_free(suggest);   return retval;   }/** * db_getobjectbyfield: * @obj: database object * @field: field to load by * @value: value you want field to equal * * This is equivilant to a SELECT * FROM obj->name WHERE field=value.  All records that * match the required results will be returned. whiippi. * * Returns: non-zero on error */gintdb_getobjectbyfield(Object * obj, gchar * field, gchar * value)   {   gint retval = 0;   gchar *query;   DbBirth *birth;   g_assert(obj);   /* clear the contents of the object before creating a new isntance of it */   db_obj_clear(obj);   if (obj->name == NULL)      {      errormsg("I have no table associated with this object, I can not proceeed as i do not know what to filter on");      return -1;      }   birth = db_birth_create(NULL, obj->name);   db_birth_addvalue(birth, field, value);   query = db_birth_createsql(birth);   db_birth_save(obj, birth);   g_assert(obj->birth);   retval = db_obj_sqlread(obj, query);	mem_free(query);   return retval;   }/** * db_getobjectid: * @obj: database object * * Get uniqueid for a object, this just returns the obj->id value.  * * Returns: %NULL on error else the unique id */DbUniqueId *db_getobjectid(Object * obj)   {   g_assert(obj);	if (obj->id == NULL)		{		errormsg("Returning NULL objectid. This is bad");		g_assert(obj->id);		}   return obj->id;   }/** * db_setrowbyfield: * @obj: database object * @fieldname: fieldname to set by * @value: value to equate by * * Assumes object list has already been loaded. Go though records and find one that * matchs criteria specified and sets row to that field.  * * Returns: non-zero on error */gintdb_setrowbyfield(Object * obj, gchar * fieldname, gchar *tablename, gchar * value)   {   gchar *tmpstr;   gint row = -1;   gint i, pos = -1;   gint oldrow;   g_assert(obj);   g_assert(value);   if (obj->num == 0)      {      errormsg("Empty list, can't set row");      return -2;      }   oldrow = obj->row;   pos = db_field_getpos(obj, fieldname,tablename);   if (pos < 0)      {      errormsg(" arr shit we here troubles. cna't find %s", fieldname);      return -3;      }   for (i = 0; i < obj->num; i++)      {      /* move along and get the value */      db_moveto(obj, i);      db_obj_doread(obj, fieldname, tablename, &tmpstr);      /* do the compare */      if (strcmp(value, tmpstr) == 0)         {         row = i;

⌨️ 快捷键说明

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