dbuniqueid.c

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

C
685
字号
 * verifies sql statements for nice postgresql databases  */static gchar *db_id_verifysql_pg(gchar * sql)   {   int i, isokay = 0, isselect = 0;   gchar *retstr;   g_assert(sql);   retstr = (gchar *) mem_alloc(strlen(sql) + 6);   /* Is this a select statement anyway? */   for (i = 0; i < strlen(sql)-4; i++)      {      if (sql[i] == 'f' || sql[i] == 'F')         if (sql[i + 1] == 'r' || sql[i + 1] == 'R')            if (sql[i + 2] == 'o' || sql[i + 2] == 'O')               if (sql[i + 3] == 'm' || sql[i + 3] == 'M')                  {                  isselect = 1;                  break;                  }      retstr[i] = sql[i];      }   /* checks for ,oid adds it if not there */   if (isselect == 1)      {      if (sql[i - 4] == 'o' || sql[i - 4] == 'O')         if (sql[i - 3] == 'i' || sql[i - 3] == 'I')            if (sql[i - 2] == 'd' || sql[i - 2] == 'D')               isokay = 1;      if (isokay == 1)         {			mem_free(retstr);         /* debugmsg("This sql string should already work with postgresql"); */         return mem_strdup(sql);         }      else         {         retstr[i - 1] = ',';         retstr[i] = 'o';         retstr[i + 1] = 'i';         retstr[i + 2] = 'd';         retstr[i + 3] = ' ';         for (; i < strlen(sql) + 1; i++)            retstr[i + 4] = sql[i];			retstr[strlen(sql)+4] = 0;         }      }   else      {      errormsg("Not a select statement");		mem_free(retstr);      return NULL;      }   return retstr;   }/** * db_id_verifysql_other: * * Verifies other non postgresql databases that dont have pgoids, damn them  * As you can see this is a scary function written by baz. * * Returns: a static */static gchar *db_id_verifysql_other(gchar * sql, DbUniqueId * uid)   {   int i, j, k, isselect = 0, found = 0, placeholder;   int foundat[uid->num];   gchar *retstr = NULL, *firstbit = NULL, *endbit = NULL;   GList *fieldlist = NULL, *listrunner = NULL;   g_assert(sql);   g_assert(uid);   /* Ok I know I'm mallocing too much but I cant help myself */   firstbit = (gchar *) mem_alloc(strlen(sql) + 4);   endbit = (gchar *) mem_alloc(strlen(sql) + 4);   /* makes sure its a select statement */   for (i = 0; i < strlen(sql); i++)      {      if (sql[i] == 'f' || sql[i] == 'F')         if (sql[i + 1] == 'r' || sql[i + 1] == 'R')            if (sql[i + 2] == 'o' || sql[i + 2] == 'O')               if (sql[i + 3] == 'm' || sql[i + 3] == 'M')                  {                  isselect = 1;                  break;                  }      firstbit[i] = sql[i];      }   if (isselect == 0)      {      errormsg("This is not an select statement, so I'm returning NULL");      return NULL;      }   placeholder = i - 1;   fieldlist = db_sqlparse_getselectfields(sql);   listrunner = g_list_first(fieldlist);	if (fieldlist == NULL)		warningmsg("No fields found in sql query.");   while (listrunner != NULL)      {      for (j = 0; j < uid->num; j++)         {         if (strcmp((gchar *) listrunner->data, (gchar *) uid->field[j]->field) == 0)            {            foundat[j] = 1;            found++;            }         else            {            firstbit = (gchar *) mem_realloc(firstbit, strlen(firstbit) + strlen(uid->field[j]->field) + 2);            }         }      listrunner = g_list_next(listrunner);      }	/* free up the fieldlist */	db_sqlparse_freeselectfields(fieldlist);   if (found != uid->num)      {      for (j = 0; j < uid->num; j++)         {         if (foundat[j] != 1)            {            firstbit[placeholder] = ',';            placeholder++;            for (k = 0; k < strlen(uid->field[j]->field); k++)               {               firstbit[placeholder] = uid->field[j]->field[k];               placeholder++;               }            }         }      firstbit[placeholder] = 0;      for (i = 0, j = 0; i < strlen(sql); i++, j++)         endbit[j] = sql[i];      endbit[j] = 0;      }   else      {      debugmsg("The sql statement is fine just as it is");      mem_free(firstbit);      mem_free(endbit);      mem_free(retstr);      return sql;		/* and now excute some fancy arse stuff that no one will see cause its on the end of this line */      }   retstr = mem_strdup_printf("%s %s", firstbit, endbit);   mem_free(endbit);   mem_free(firstbit);   return retstr;   }/** * db_id_createblank: * @tablename: The base table to work out what are the primary keys * * Will generate a uniqueid structure with no data in it, ready to be assigned values. * The id will still be unique but it would change as soon as you wrote it back to the database, * this attempts to be a tempory use id. * * Returns: freshly allocated uniqueid. */DbUniqueId *db_id_createblank(gchar * tablename)   {   DbUniqueId *id;   id = (DbUniqueId *) mem_alloc(sizeof(DbUniqueId));   globaltemporaryids--;   id->pg_oid = globaltemporaryids;   id->primarykeys = NULL;   id->num = 0;   return id;   }#ifdef __OBSOLETE /** * db_id_adduniqueid: * @obj: Database Object * @id: Id record to insert * @row: Row at which to extract key. * * Inserts @id into the object uniqueid index set.  Do not free @id after use. * * Returns: non-zero on error */gintdb_id_adduniqueid(Object * obj, DbUniqueId * id, gint row)   {   gint i;   obj->idindex = (DbIdIndex **) mem_realloc(obj->idindex, sizeof(DbIdIndex *) * (obj->numid + 1));   obj->idindex[obj->numid] = (DbIdIndex *) mem_alloc(sizeof(DbIdIndex));   obj->idindex[obj->numid]->origrow = row;   obj->idindex[obj->numid]->id = id;   obj->idindex[obj->numid]->cache = NULL;   for (i = 0; i < obj->numcache; i++)      if (obj->cache[i]->origrow == row)         {         obj->idindex[obj->numid]->cache = obj->cache[i];         break;         }	obj->numid++;   return 0;   }#endif/** * db_id_extractid: * @obj: Database Object * @id: Id record to record results into * @row: Row at which to extract key. * * Read from your recordset to find the uniqueid for @row. * * Returns: non-zero on error */gintdb_id_extractid(Object * obj, DbUniqueId * id, gint row)   {   gint pos;   g_assert(id);   if (id == NULL)      {      errormsg("DbUniqueId is NULL, can not proceed for %s", obj->query);      return -3;      }   if (obj->res == NULL)      {      debugmsg("Obj->res is NULL so i'm just making something up here.");      return -1;      }	if (row >= obj->num)		{		errormsg("Your trying to read more than you are allowed...");		return -4;		}   pos = db_field_getpos(obj, "oid", NULL);   if (pos >= 0)      id->pg_oid = atoi(db_dbgetvalue(obj->res, row, pos));	   if (id->pg_oid < 0 || pos < 0)      {      errormsg("Unable to extract the unique id for this record. Aborting here. pos:%d pg_oid:%d row:%d", pos, id->pg_oid, row);		g_assert(NULL);      return -2;      }   /* m.essage ("Extracted id %d from %s at %d pos %d",id->pg_oid,obj->name,obj->row, pos); */   return 0;   }/** * db_id_isnewrecord: * @id: Id record to check * * Check to see if @id is a new record that has yet to be inserted into the database, meaning * we manage the uniqueid until the database can take it over then.  The id will typically be  * invalid like negitive but still unique. * * Returns: TRUE if it is a new record, otherwise FALSE */gbooleandb_id_isnewrecord(DbUniqueId * id)   {   g_assert(id);   if (id->pg_oid < 0)      return TRUE;   return FALSE;   }/** * db_id_isvalid: * @id: Id record to check * Check to see if @id a valid record to index on. * * Returns: TRUE if it is record ok, otherwise FALSE if it isn't good. */gbooleandb_id_isvalid(DbUniqueId * id)   {   if (id == NULL)      return FALSE;   if (db_id_isnewrecord(id) == TRUE)      return FALSE;   return TRUE;   }/** * db_id_syncid: * @obj: Database object * * Set the object id which it is pointing to, to whatever it should be based on the row you have * selected.  Sounds complex i know. * * Returns: non-zero on failure */gintdb_id_syncid(Object * obj)   {	if (obj->currentcache != NULL)		{      obj->id = obj->currentcache->id;		return 0;		}   return -1;   }/** * db_uniqueid: * @result: Result from a insert * @fieldpos: Field position *  * Kinda a wrapper for PQoidValue(@result); * Assuming you run an insert query with db_dbexec(), this will return the unique id assocated with * that record.  Note this works on just getting the oid value from postgresql, on other databases * a bit of a work around will be needed. * * Returns: the pg OID value. %NULL is returned if PQoidValue fails. */DbUniqueId*db_uniqueid(DbRecordSet * result)   {	gint oid;	DbUniqueId *id;	oid = db_dbuniqueid(result);	if (oid == 0)		return NULL;   id = db_id_createblank(result->basetable);   id->pg_oid = oid;   return id;   }

⌨️ 快捷键说明

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