📄 dbcache.c
字号:
#include <glib.h>#include <stdio.h>#include "dbcache.h"#include "dbtoliet.h"#include "dbuniqueid.h"#include "dbobject.h"#include "dbuniqueid.h"#include "dbbureaucrat.h"#include "dbfield.h"#include "dbsqlparse.h"#include "debug.h"#include "dbfilter.h"/*static void db_toliet_freecache_element(DbCache * c);static void db_toliet_blank_element(DbCache * c, gint num); */static gint db_cache_createall(Object *obj) { g_assert(obj); if (obj->cache != NULL) db_cache_cleanup(obj); obj->cache = mem_alloc(sizeof(DbCache*)*(obj->num+1)); memset(obj->cache,0,sizeof(DbCache*)*(obj->num+1)); obj->numcache = obj->num; return 0; }static gintdb_cache_expand(Object *obj) { gint ptrlen; void *ptr; if (obj->num < obj->numcache) return -1; obj->cache = mem_realloc(obj->cache,sizeof(DbCache*)*(obj->num+1)); ptr = obj->cache; (char*)ptr += obj->numcache*sizeof(DbCache*); ptrlen = (sizeof(DbCache*)*(obj->num)) - (sizeof(DbCache*)*(obj->numcache)); memset(ptr,0,ptrlen); obj->numcache = obj->num; return 0; }static voiddb_cache_freecache_element(DbCache * c) { gint i; for (i = 0; i < c->num; i++) { if (c->value[i] != NULL) mem_free(c->value[i]); } db_filter_free(c->filter); db_id_free(c->id); mem_free(c->value); mem_free(c->flags); mem_free(c); return ; }static voiddb_cache_blank_element(DbCache * c, gint num) { gint i; g_assert(c); c->num = num; if (num <= 0) num = 1; c->value = (gchar **) mem_alloc(sizeof(gchar *) * num); for (i = 0; i < num; i++) c->value[i] = NULL; c->origrow = -1; c->id = NULL; c->state = OBJ_NEW; c->changed = FALSE; }/** * db_cache_addtocache: * @obj: Database object * * Add to the cache index file a new entry, and return the newly created cache. Do not * free the variable returned. You need to make sure you add an id and update the idindex * file after using this function. * * Returns: Null on error else the newly created cache object. */static DbCache *db_cache_create(Object * obj) { DbCache *c; /* add an item to it */ c = (DbCache *) mem_alloc(sizeof(DbCache)); db_cache_blank_element(c, obj->numfield); /* create filter flags */ c->flags = (gchar *) mem_alloc(sizeof(gchar) * (obj->numfield+1)); memset(c->flags, 0, sizeof(gchar) * (obj->numfield+1)); return c; }/* ======================================================================================= * External Code. *//** * db_cache_moveto: * @obj: Database object * * Replaces remberid() function. * Moves to the current row in the @obj, and if there isn't a cache item existing * it will create one. If its a read source it will extract the UniqueIds (primary * keys or oid's). Referencing is done on a row basis. * * Returns: DbCache element for your current row. */DbCache *db_cache_moveto(Object *obj) { DbUniqueId *id; if (obj->cache == NULL) db_cache_createall(obj); else if (obj->numcache < obj->num) db_cache_expand(obj); if (obj->cache[obj->row] == NULL) { obj->cache [obj->row] = db_cache_create(obj); obj->cache [obj->row]->origrow = obj->row; id = db_id_remeberbyrow(obj, obj->row); obj->cache[obj->row]->filter = db_filter_create(TRUE, obj->row); obj->cache [obj->row]->id = id; if (db_id_isnewrecord(id) == FALSE) if (obj->cache [obj->row]->state == OBJ_NEW) obj->cache [obj->row]->state = OBJ_READ; } obj->currentcache = obj->cache[obj->row]; /* Align the id's */ db_id_syncid(obj); return obj->currentcache; }/** * db_cache_isincache: * @obj: Database object * * Works out if the record you have currently selected is in cache or not. This will take * the current UniqueId and row and see if there is data for this in cache. * * Returns: %NULL if not in cache, else returns cached record. */DbCache *db_cache_isincache(Object * obj) { /* i suspect a bug lies here within */ g_assert(obj); if (obj->cache == NULL) return NULL; /* the quick way... */ if (obj->row >= obj->numcache) { errormsg("Referce to %s is %d outside of range of %d",obj->name,obj->row,obj->numcache); return NULL; } return obj->cache[obj->row]; }/** * db_cache_getvalue: * @obj: Database object * @cache: Single cache element * @field: Field name * @value: Pointer to a string that will have the returned string assigned to it. * * Get the value of a value in the cache. * * Returns: non-zero on failure */gintdb_cache_getvalue(Object * obj, DbCache * cache, gchar * field, gchar * table, gchar ** value) { gint fieldpos; g_assert(cache); g_assert(field); g_assert(obj); *value = NULL; if (cache->value == NULL) { warningmsg("Nothing in here worth returning"); return -1; } fieldpos = db_field_getpos(obj, field, table); if (fieldpos >= obj->numfield || fieldpos < 0) { errormsg("Invalid field %s, can not find a postiion for this.", field); return -1; } *value = cache->value[fieldpos]; /* m.essage("got %s from cache. pos %d",*value,fieldpos); */ return 0; }/* Function for testing cache courption. *//*gintdbtestcache(Object *obj) { gint i, j; for(i=0;i<obj->numcache;i++) if (obj->cache[i] != NULL) for(j=0;j<obj->cache[i]->num;j++) if (obj->cache[i]->value[j] != NULL) if (obj->cache[i]->value[j][0] == 226 || obj->cache[i]->value[j][0] == '
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -