dbuniqueid.c

来自「bonddb 是一个源于PostgreSQL封装包的对象。它是一个由C/C++编」· C语言 代码 · 共 685 行 · 第 1/2 页

C
685
字号
#include <stdio.h>#include <stdlib.h>#include <glib.h>#include "dbuniqueid.h"#include "db.h"#include "dbwrapper.h"#include "debug.h"#include "dbobject.h"#include "dbfield.h"#include "dbtoliet.h"#include "dbsqlparse.h"static gchar *db_id_verifysql_pg(gchar * sql);static gchar *db_id_verifysql_other(gchar * sql, DbUniqueId * uid);gint globaltemporaryids;#ifdef __OBSOLETE/** * db_id_createall: * @obj: Database Object * * Creates an array for every unique-ids/primary keys in the @obj.  * This will not attempt to read all the records.  If your refreshing the object then * db_id_createall will just restructure the object. * * Returns: non-zero no failure */gintdb_id_createall(Object * obj)   {   g_assert(obj);   if (obj->idindex != NULL)      {      warningmsg("IdIndex != NULL");      db_id_freeall(obj);      }   obj->idindex = (DbIdIndex **) mem_alloc(sizeof(DbIdIndex *));   obj->numid = 0;   return 0;   }/** * db_id_createall: * @obj: Database Object * * Frees up the array created by db_id_createall(). This function is called automatically * by db_freeobject(). * * Returns: non-zero no failure */gintdb_id_freeall(Object * obj)   {   gint i;   g_assert(obj);	   for (i = 0; i < obj->numid; i++)      {      if (obj->idindex[i] != NULL)         db_id_free(obj->idindex[i]->id);      mem_free(obj->idindex[i]);      }   mem_free(obj->idindex);   obj->idindex = NULL;   obj->id = NULL;   obj->posinidindex = -1;   obj->numid = 0;	/* ohh this feels crazzyzyy.  CRZZYYYY CODE! */   return 0;   }#endif/** * db_id_free: * @id: Unique id * * Frees up the invidual @id, which is allcoed by a db_id_createblank. * This needs support for primary key cleanup at some point, you go baz go write that code * * Returns: non-zero no failure */gintdb_id_free(DbUniqueId * id)   {   mem_free(id);   /* TODO: Have to add in support for primary key freeing at some point */   return 0;   }/** * db_id_remeber: * @obj: Database Object * * Because all the id's are not read in straight away, this function will set the unique ids * in the array to what they are suppose to based on the object->row. * * Returns: NULL on zero or newly created id. */DbUniqueId *db_id_remeber(Object * obj)   {   return db_id_remeberbyrow(obj, obj->row);   }/** * db_id_remeberbyrow: * @obj: Database Object * @row: Row to remeber by. * @idindexpos: A returned interger of what position in the array this record is in.  * * Similar to db_id_remeber() but allows you to specify the row you want to remeber the unique id * for.  This will give a unique temporary id if one is not availiable for use. * * Returns: NULL on failure else the uniqueid for the newly created row. */DbUniqueId *db_id_remeberbyrow(Object * obj, gint row)   {   DbUniqueId *id;   g_assert(obj);   /* See if you already have this position is set already */   if ((id = db_id_getbyrow(obj, row)) != NULL)      return id;	   if (db_isabletogetid(obj) == FALSE)      {      /* create that id */      id = db_id_createblank(obj->name);		/* m.essage("Adding temporary unique id of %d to object %s at row %d",id->pg_oid,obj->name,row); */      /* db_id_adduniqueid(obj, id, row); */		obj->unknownid = FALSE;      /* Object is new and yet to be written */      }   else      {      /* Just so we dont run out of intergers */      globaltemporaryids++;		/* message("getting id for %s at row %d",obj->name,row); */      /* Load value if not. */      id = db_id_createblank(obj->name);      if (db_id_extractid(obj, id, row) == 0)         {			/* The extractid function will write to cache the value of the id */			/* m.essage("Extracting unique id of %d to object %s at row %d",id->pg_oid,obj->name,row); */			;         }      else         {         errormsg("Unable to extract uniqueid for record");         db_id_free(id);         return NULL;         }      }   return id;   }/** * db_id_get: * @obj: Database Object * * Based on what row your currently on, it will return the unique identifier for that  * record.   * * Returns: DbUniqueId value, or %NULL on failure */DbUniqueId *db_id_get(Object * obj)   {   return db_id_getbyrow(obj, obj->row);   }/** * db_id_getbyrow: * @obj: Database Object * @row: Row to get unique id for. * * Based on what row your currently on, it will return the unique identifier for that  * record. * * Returns: DbUniqueId value, or %NULL on failure */DbUniqueId *db_id_getbyrow(Object * obj, gint row)   {	if (obj->row < obj->numcache && obj->row >= 0)		if (obj->cache[obj->row] != NULL)			return obj->cache[obj->row]->id;	return NULL;   }/** * db_id_moveto: * @obj: Database Object * @id: Unique Id * * Move to a row in the recordset based on the @id. * * Returns: negitive number on failure, else positive number reflecting position */gintdb_id_moveto(Object * obj, DbUniqueId * id)   {   gint i;   g_assert(obj);   for (i = 0; i < obj->numcache; i++)		if (obj->cache[i] != NULL)			if (db_id_compare(id, obj->cache[i]->id) == 0)				return obj->cache[i]->origrow;   return -1;   }/** * db_id_compare: * @id1: Unique id * @id2: Unique id * * Compare two uniqueids to see if they are the same. * * Returns: non-zero if there isn't a match. */gintdb_id_compare(DbUniqueId * id1, DbUniqueId * id2)   {   gint i, fail;	if (id1->pg_oid != 0 && id2->pg_oid != 0)		{		if (id1->pg_oid == id2->pg_oid)			return 0;		return 1;		}	fail = 1;   /* This relies on the primary keys been kept in the same order */   for (i = 0; i < id1->num; i++)      if (strcmp(id1->primarykeys[i], id2->primarykeys[i]) != 0)         {         fail = 1;         break;         }   if (fail == 0)      return 0;   return 1;   }/* SELECT * FROM table WHERE primarykeynames = oid *//** * db_id_createsql: * @obj: Database Object * @id: Unique id * * Allocates a string that you can put at the end of an SQL statement to get the record * specified by @id.  You will need to put a WHERE in your SQL statement prior * to appending this string onto it.   * * Unlike most functions, the returned string here needs to be freed.  Use mem_free(retstr), * on successful running of this function. * * Returns: newly created sql string, or %NULL on failure. */gchar *db_id_createsql(Object * obj, DbUniqueId * id)   {   gchar *retstr, *tmpstr;   gchar **strlist;   gint i;   g_assert(id);   if (id->pg_oid > 0)      {      retstr = mem_strdup_printf("%s.oid=%d", obj->name, id->pg_oid);      return retstr;      }   else if (id->num > 0)      {      strlist = (gchar **) mem_alloc(sizeof(gchar *) * (id->num + 1));      for (i = 0; i < id->num; i++)         strlist[i] = mem_strdup_printf("%s.%s=%s", id->field[i]->table, id->field[i]->field, id->primarykeys[i]);      tmpstr = g_strjoinv(" AND ", strlist);		retstr = mem_strdup(tmpstr);		g_free(tmpstr);      for (i = 0; i < id->num; i++)         mem_free(strlist[i]);      mem_free(strlist);      return retstr;      }   return NULL;   }/** * db_id_verifysql: * @sql: SQL Statement to be verified   * * Checks to see if a sql select statement has its unique identifies listed last, if not it adds them and returns * a new string based on @sql.  This assumes you already have the object name set as this is a reference * point to extract the unique id. * * Returns: Newly created SQL string, NULL on error */gchar *db_id_verifysql(gchar * sql)   {   DbUniqueId *uid;   gchar *retstr, *tablename;   tablename = db_sqlparse_getfirsttable(sql);   uid = db_id_createblank(tablename);   mem_free(tablename);   /* Checks to see if it is a pg Unique ID */   if (db_dbgetserver() == DB_PGSQL)      {      retstr = db_id_verifysql_pg(sql);		db_id_free(uid);      return retstr;      }   /* Checks to see if it has some primary key fields */   else      {      retstr = db_id_verifysql_other(sql, uid);		db_id_free(uid);      return retstr;      }   warningmsg("Unable to determine the type of uniqueid this is");   /* couldn't find a unique identifier so return NULL */   return NULL;   }/** * db_id_verifysql_pg: *

⌨️ 快捷键说明

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