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

📄 db.c

📁 bonddb 是一个源于PostgreSQL封装包的对象。它是一个由C/C++编写的快速数据提取层应用软件
💻 C
📖 第 1 页 / 共 2 页
字号:
         break;         }      }   if (row == -1)      {      db_moveto(obj, oldrow);      debugmsg("Unable to find a record with the value selected");      return -3;      }   /* set to the new postion like */   db_moveto(obj, row);   return 0;   }/** * db_deleteobject: * @obj: database object * * Deletes the record in obj, and all records which follow this one.  It only deletes the row your in, not the entire record set. * Needs to be tested still, as it has the potential to cause huge damage, especially with * things like circular relationships. * * Returns: non-zero on error */gintdb_deleteobject(Object * obj){	g_assert(obj);	/* Run recusive delete, and move that code below into something else like	 * db_obj_dodelete() */	db_del_objectrecinit();	db_del_objectrec(obj, db_id_get(obj));	db_del_objectreccleanup();	debugmsg("This is going to delete recusively.  TEST WELL BEFORE USE!");	return 0;   }/** * db_clearobject: * @obj: database object * * Clear the contents of an object making everything blank.  This will free up any * record sets associated with @obj and setup the object as a new record.  The * table it points to will be the same as its existing table. * * Returns: non-zero on error */gintdb_clearobject(Object * obj)   {   if (obj == NULL)      return -3;   return db_obj_clear(obj);   }/* CODE GRAVE YARD .... from db_getvalue*//* check to see if num is out of range. If so return "" string *//*	if (obj->append == TRUE)		{		if (obj->row == obj->num-1)			{			warningmsg("Trying to extract a value from a not added object (in process of append");			*retval = emptystring;			return 0;			}		} *//* data has changed so reload query. I disabled this cause i dont know why i have it *//*   if (obj->changed == TRUE)// && obj->append != TRUE)	db_refreshobject(obj);  *//*   if ((fieldpos = db_getfieldpos(obj, field)) == -1)      {      errormsg("field %s not found in %s", field, obj->table->name);      g_assert(NULL);	      return -1;      } *//*	printf(":%d:%d  %s\n",obj->row,fieldpos,*retval); *//* message("Got value %s for row %d, %d items in total, This is all strange and lovely and i think we ar egoing to be hit by a giant rock from outta space",*retval,obj->row,obj->num); *//* new record, get what you just wrote *//* if (obj->pg_oid > 0 && obj->res == NULL) { warningmsg("This is untested code. I dont know why i am here. HELP");   db_refreshwrittenobject(obj); } 10 PRINT "HELLO WORLD"20 GOTO 10	*//* check the local pointer *//*   if (db_isnewrecord(objlocal) == TRUE && db_ischanged(objlocal) == FALSE)      {      errormsg("%s is a new record, I can't load values from it into the other object.\n"					"You need to add some text",objlocal->name);      return -1;      } *//*   if (db_isvalidforread(objlocal) == FALSE)      {      errormsg("Invalid oid for current record");      return -4;      } *//*   desttable = objdest->table;   g_assert(desttable);   if (desttable->cref == NULL)      {      errormsg("Unable to find any relationships for this destionation table, can not proceed");      return -5;      }    walk = g_list_first(desttable->cref);   while (walk != NULL)      {      c = walk->data;      g_assert(c);      localfield = NULL;      destfield = NULL;      if (strcasecmp(c->table[0], objlocal->table->name) == 0 && c->casual == 0)         {         localfield = c->column[0];         destfield = c->column[1];         }      else if (strcasecmp(c->table[1], objlocal->table->name) == 0 && c->casual == 0)         {         localfield = c->column[1];         destfield = c->column[0];         }      if (localfield != NULL && destfield != NULL)         {         db_getfieldstr(objlocal, localfield, &value);         db_addwrite(objdest, destfield, value);         objdest->changed = FALSE;         }      walk = walk->next;      }   objdest->changed = FALSE;    return 0;   }*//* Check to work if refreshnwrittenrecord worked. *//* if (obj->res == NULL) { errormsg("Object result is null, meaning you trying to read stuff not yet written.\n"   "This occured for %s.%s", obj->table->name, field); g_assert(NULL); } *//* this code died somewhere else but we bury him here just the same *//* if (c->obj != NULL) if (obj != c->obj || (obj == c->obj && c->pg_oid != obj->pg_oid)) db_flushobjects(); *//* gchar *str; gint fieldpos; extern DbWriteCache *globaldbwritecache;   errormsg("Code is obsolete. Do not use"); g_assert(NULL);   g_assert(obj); if (globaldbwritecache != NULL) if (globaldbwritecache->obj != NULL) if (obj ==   globaldbwritecache->obj) db_flushobjects();   g_assert(obj->res); g_assert(obj->table); if (obj->res == NULL || obj->row < 0) { errormsg("obj->res is null or   obj->row is < 0, i can't do this. arr"); return -3; }   if (obj->append == TRUE) { if (obj->row == obj->num - 1) { (*retval) = 0; return 0; } }   if ((fieldpos = db_getfieldpos(obj, field)) == -1) { errormsg("field not found"); return -1; } str = (gchar *)   db_dbgetvalue(obj->res, obj->row, fieldpos); if (str == NULL) { errormsg("returned null"); return -2; } *retval =   atoi(str); return 0; *//**  * db_getfieldint:  * @obj: Database object  * @field: database field name  * @retval: int to return  * Retreives an int from the database based on a object and field.  * db_getfieldstr().  * Returns: <b>error code</B> on how successful it was.   *//*gintdb_getfieldint(Object * obj, gchar * field, gint * retval)   {		   gchar *str = NULL;	*retval = 0;   db_getfieldstr(obj, field, &str);	if(str==NULL)		{		errormsg("Unable to get int value for field %s in %s",field,obj->name);		if (db_isnewrecord(obj)==TRUE)			g_assert(NULL);		g_assert(NULL);		return -1;		}   *retval = atoi(str);   return 0;   } *//* Note, dont free the returned string from this, as its been used   by something else. Soo NOOO YOUR NOT ALLOWED TO!. Hands off or else   I'll poke you with nasty bits.*//** * db_lookthroughobject: * @objlocal: * @othertable: * HMMM I"M DUMPING THIS CODE CAUSE IT AN"T USED MUCH  * Returns: %NULL on unable to find anything else the field that links it. */ /*   gchar *   db_lookthroughobject(Object * objlocal, gchar * othertable)   {   gchar *destfield, *localfield;   GList *walk;   DbTableDef *db;   DbConstraintDef *c;    g_assert(objlocal);   g_assert(othertable);    if (db == NULL)   {   debugmsg("No object found");   return NULL;   }   db = db_findtable(othertable);   walk = g_list_first(db->cref);   while (walk != NULL)   {   c = walk->data;   g_assert(c);   if (strcasecmp(c->table[0], objlocal->table->name) == 0) *//* othertable has a ref to local object *//*         {         localfield = c->column[0];         destfield = c->column[1];         return destfield;         }      else if (strcasecmp(c->table[1], objlocal->table->name) == 0)         {         localfield = c->column[1];         destfield = c->column[0];         return destfield;         }      walk = walk->next;      }   return NULL;   } */ /*   * db_getfieldstr:   * obj: Data *//*gintdb_getfieldstr(Object * obj, gchar * field, gchar ** retval)   {   gint fieldpos;    *retval = NULL;   g_assert(obj);*//* get field position in the data set *//* if ((fieldpos = db_getfieldpos(obj, field)) == -1) { errormsg("field %s not found in %s", field, obj->name);   return -1; } *//* is the value sitting in the cache ready to be used ?? *//* if (db_isincache(obj) == TRUE) if (globaldbwritecache->value[fieldpos] != NULL) { *retval =   globaldbwritecache->value[fieldpos]; return 0; } *//* do i need to flush the cache? *//*   if (db_isflushneeded(obj) == TRUE)      {      debugmsg("Flushing cache cause its repeating stuff for query %s", obj->query);      debugmsg("Gathering field %s, %d", field, obj->row);      db_flushobjects();      } *//* am i allowed to read from this object ??. ie is not a blank record *//*   if (db_isvalidforread(obj) == FALSE)      {      debugmsg("empty record, can not retrieve a value %s at postion %d of %d", field, obj->row, obj->num);      return 1;      } *//* assumes row and field is set right, and res is correct *//* The string is also stripped to not contain end trailing or beginning spaces. This shoulnd't create a memory leak,   but you never know. I'll check for this later on *//*	db_field_read(obj, field, retval);   (*retval) = g_strstrip((gchar *)(*retval)); *//* Something went straigly wrong *//*   if (*retval == NULL)      {      errormsg("returned null");      return -2;      }   return 0;   }*//* Let there souls rest in peace ... *//** * db_getobjectbyreference: * @obj: Database object * @tablename: Name of table to load object by * * This function will create a new database object based on an existing object * and a table name.  Constraint information will be used to work out how the * two tables relate to each other. */Object *db_getobjectbyreference(Object * obj, gchar * tablename)   {   g_assert(obj);   g_assert(tablename);   return db_mapme_findobjectbytable(obj, tablename);   }/** * db_numrows: * @obj: Database object * * Returns the number of rows there are in a object. * * Returns: interger number of rows in a object dataset */gintdb_numrows(Object * obj)   {   g_assert(obj);	return db_numrecord(obj);   }/** * db_addobjecttoobject: * @objdest: the new database object which will have information added to it * @objlocal: the existing base database object. *  * This is not a major api call, db_loadbyobject() will automatically call this * function. * * Elements of @objlocal will be used to create elements in @objdest.  This will * find all the links between the two objects and populate @objdest with  * values corresponding from those links. * * Returns: non-zero on error */gintdb_addobjecttoobject(Object * destobj, Object * obj)   {   DbConstraintDef *cref;   DbTableDef *desttabledef;   DbBirth *birth;   /* loading yourself by yourself. hmmm */   if (strcmp(destobj->name, obj->name) == 0)      {      errormsg("Your trying to add yourself from yourself to yourself. confusing huh? well what about me");      return -1;      }   desttabledef = db_findtable(destobj->name);   g_assert(desttabledef);   /* TODO: expand support to have more than one table source */   cref = db_constraint_getonmatch(desttabledef->cref, obj->name, destobj->name, 0);   if (cref == NULL)      {      errormsg("Unable to find a relationship between %s and %s, make sure a cref exists", obj->name, destobj->name);      return -3;      }   /* now the sensible stuff */   birth = db_birth_create(NULL, destobj->name);   db_birth_applycref(obj, birth, cref);   db_birth_save(destobj, birth);   debugmsg("Spring Chickens!");   db_birth_springchicken(destobj);   return 0;   }/** * db_add: * @obj: Database object * * Adds another record onto the end of a database object.  For example if you * performaned an sql query to create a database object you can then use * this function to append a new record onto the end of that record set. * Default values will be applied and if it wsa an object loaded by * db_loadobjectbyobject() then other nessesary values will also * be added. * * Returns: non-zero on failure */gintdb_add(Object * obj)   {   gint num, retval = 0;   g_assert(obj);   if (db_isabletoadd(obj) == FALSE || (db_isabletogetid(obj) == FALSE && obj->num > 0))      {      debugmsg("You can't add a record without finishing this record");      return -1;      }   num = obj->num;   /* add a new item to the cache */   db_setforinsert(obj);   retval -= db_moveto(obj, obj->num - 1);   retval -= db_obj_applydefaults(obj);   if (obj->birth != NULL)		{		debugmsg("Applying default sprintchicken values.");      retval -= db_birth_springchicken(obj);		}   if (obj->num != num + 1)      {      errormsg("unable to add new record, orginally there was %d now there is %d.\n"               "There should be one more", num, obj->num);      }   return retval;   }/*** db_insert:* @obj: Database object** Not yet implimented** Returns: non-zero on failure*/gintdb_insert(Object * obj)   {   g_assert(obj);   errormsg("NOT WRITTEN. Please code this. : TODO");   g_assert(NULL);   if (db_isabletoadd(obj) == FALSE)      {      warningmsg("You can't add a record without finishing this record");      return -1;      }   /* add a new item to the cache */   /*TODO c = db_toliet_addtocache(obj);    c->origrow = obj->row + 1;   obj->row = obj->row + 1;   obj->num++;   db_moveto(obj, obj->row + 1); */   return 0;   }/** * db_createdeadobject: * @tablename: Name of a table where the record will be inserted * * Will create a non-usuable dead object.  This object can be used * to indicate what fields etc exist on the datasource @tablename. * * Returns: A newly created database object, or %NULL on error. */Object*db_createdeadobject(gchar *tablename)	{	Object *obj;   obj = db_obj_create(tablename);   obj->numfield = db_numfields(obj);   db_obj_handle_empty_recordset(obj);   return obj;	}

⌨️ 快捷键说明

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