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

📄 dbelement.c

📁 linux下的电话本的最底层
💻 C
📖 第 1 页 / 共 5 页
字号:
						case FIELD_TYPE_STRING:							pStmtExt = pStmtExts[DB_FIELDS_TEXT];							break;									case FIELD_TYPE_BINARY:						default:							pStmtExt = pStmtExts[DB_FIELDS_BLOB];							break;					}					value = record_get_field(record,																	 fid,																	 &size);					label = record_get_label(record,																	 fid);					desc = record_get_desc(record,																 fid);										bind_value_by_index(pStmtExt, 															1,															id,															prifdesc->type,															prisize);		/*				g_print("%s(): ftid = %3d, value = 0x%08x, size = %d\n",										__FUNCTION__,										fdesc->identifier,										(guint32)value, 										size);		*/							sqlite3_bind_int(pStmtExt,													 2,													 fid);					if (label)					{						sqlite3_bind_text(pStmtExt,															3,															record_get_label(record, fid),															-1,															SQLITE_TRANSIENT);					}					else					{						sqlite3_bind_null(pStmtExt, 3);					}										if (desc)					{						sqlite3_bind_text(pStmtExt,															4,															record_get_desc(record, fid),															-1,															SQLITE_TRANSIENT);					}					else					{						sqlite3_bind_null(pStmtExt, 4);					}										if (value)					{						bind_value_by_index(pStmtExt, 																5,																value,																fdesc->type,																size);					}					else					{						sqlite3_bind_null(pStmtExt, 5);					}				}								ret = sqlite3_step(pStmtExt);				if (ret == SQLITE_ROW)				{					goto failure;				}								if (ret != SQLITE_DONE)				{					g_print("%s(): sqlite3_step() WARNING: main, %d[%s].\n",									__FUNCTION__,									sqlite3_errcode(element->db->database),									sqlite3_errmsg(element->db->database));					if (ret == SQLITE_BUSY)					{						sqlite3_finalize(pStmt);												goto failure;					}				}								ret = sqlite3_reset(pStmtExt);			}							for (i = 0; i < DB_FIELDS_NTYPES; i++)			{				if (pStmtExts[i] != NULL)				{					ret = sqlite3_finalize(pStmtExts[i]);				}			}		} while (ret == SQLITE_SCHEMA);	}	record_db_inner_trans_commit(element->db);	g_string_free(sql_str, TRUE);	iterator_free(iter, TRUE);	return DB_ERROR_NONE;failure:	g_string_free(sql_str, TRUE);	iterator_free(iter, TRUE);	for (i = 0; i < DB_FIELDS_NTYPES; i++)	{		if (pStmtExts[i] != NULL)		{			ret = sqlite3_finalize(pStmtExts[i]);		}	}	g_print("%s(): failure leaving. %d\n",					__FUNCTION__,					ret);	if (ret == SQLITE_BUSY)	{		return DB_ERROR_BUSY;	}	return DB_ERROR_FAILED;}/** * @brief		Retrieve a specific #Record object from  * 					#RecordDBElement database. * * @param		element				the #RecordDBElement object * @param		record		the retrieved #Record object * @param		id	the value of primary field of #Record * 										to be retrieved. This value will be * 										unique identification of the #Record * 										object in database. *  * @return	DB_ERROR_NONE if operation was successfully. *  * @note		the retrieved #Record object should be freed * 					when no longer needed. */RecordDBErrorrecord_db_element_get_record (RecordDBElement		*element,															Record					 **record,															guint32						 id){	sqlite3_stmt * pStmt = NULL;	GString * sql_str = NULL;	gint ret;	g_return_val_if_fail(element, DB_ERROR_ARG_NULL);	g_return_val_if_fail(record, DB_ERROR_ARG_NULL);	sql_str = g_string_new("");	/*	 * read from fidmap table	 */		record_db_inner_trans_begin(element->db, 															DB_TRANS_DEFERRED);	g_string_append_printf(sql_str,												 "SELECT * FROM %s_fidsmap where ( ftid%d = :%03d );",												 element->db_elm_name,												 element->db_elm_id_ftid,												 element->db_elm_id_ftid);/*		g_print("%s(): %s\n",						__FUNCTION__,						sql_str->str);*/	do	{		const FieldDescriptor * prifdesc = NULL;						prifdesc = field_template_get(element->db_elm_template,																	element->db_elm_id_ftid);/*				if (element->db_elm_id_type == DB_FIELDS_BLOB)		{			g_print("%s(): BINARY id, use fixed size %d\n",							__FUNCTION__,							prifdesc->msize);		}*/						ret = sqlite3_prepare(element->db->database,													sql_str->str,													-1,													&pStmt,													0);		if (ret != SQLITE_OK)		{			g_print("%s(): sqlite3_step() WARNING: %d[%s].\n",							__FUNCTION__,							sqlite3_errcode(element->db->database),							sqlite3_errmsg(element->db->database));							goto failure;		}				if (prifdesc)		{			bind_value_by_fid(pStmt,												prifdesc->identifier,												&id,												prifdesc->type,												prifdesc->msize);		}				*record = record_new(element->db_elm_template);		while ((ret = sqlite3_step(pStmt)) == SQLITE_ROW)		{			guint32 ftid = FIELD_TEMPLATE_ID_INVALID;			guint32 fid = FIELD_ID_INVALID;								ftid = sqlite3_column_int(pStmt, 1);			fid = sqlite3_column_int(pStmt, 2);		/*				g_print("%s(): fid = %d, ftid = %d\n",								__FUNCTION__,								fid,								ftid);*/					record_add_know_field(*record, fid);					record_set_default_fid(*record,														 ftid,														 fid);		}						if (ret == SQLITE_BUSY)		{			sqlite3_finalize(pStmt);							goto failure;		}					ret = sqlite3_finalize(pStmt);	} while (ret == SQLITE_SCHEMA);	/*	 * read data from main table	 */	g_string_assign(sql_str, "");	g_string_append_printf(sql_str,												 "SELECT * FROM %s_main where ( ftid%d = :%03d );",												 element->db_elm_name,												 element->db_elm_id_ftid,												 element->db_elm_id_ftid);	do	{		const FieldDescriptor * prifdesc = NULL;		gboolean existed = FALSE;				prifdesc = field_template_get(element->db_elm_template,																	element->db_elm_id_ftid);/*		if (element->db_elm_id_type == DB_FIELDS_BLOB)		{			g_print("%s(): BINARY id, use fixed size %d\n",							__FUNCTION__,							prifdesc->msize);		}*/				ret = sqlite3_prepare(element->db->database,													sql_str->str,													-1,													&pStmt,													0);		if (ret != SQLITE_OK)		{			g_print("%s(): sqlite3_step() WARNING: %d[%s].\n",							__FUNCTION__,							sqlite3_errcode(element->db->database),							sqlite3_errmsg(element->db->database));					goto failure;		}		if (prifdesc)		{			bind_value_by_fid(pStmt,												prifdesc->identifier,												&id,												prifdesc->type,												prifdesc->msize);		}				if (*record == NULL)		{			*record = record_new(element->db_elm_template);		}				while ((ret = sqlite3_step(pStmt)) == SQLITE_ROW)		{			gint count;			gint i;			count = sqlite3_column_count(pStmt);/*			g_print("%s(): count = %d\n",							__FUNCTION__,							count);*/						for (i = 0; i < count; i++)			{				int ftid = FIELD_TEMPLATE_ID_INVALID;				const gchar * colname = NULL;				colname = sqlite3_column_name(pStmt, i);				if (g_strcasecmp(colname, "extflag") == 0)				{					(*record)->extended = sqlite3_column_int(pStmt, i);					continue;				}				/*				g_print("%s(): colname = %s\n",								__FUNCTION__,								colname);				*/						sscanf(colname,							 "ftid%d",							 &ftid);							 /*				g_print("%s(): colname = %s, ftid = %d\n",								__FUNCTION__,								colname,								ftid);*/								if (sqlite3_column_type(pStmt, i) == SQLITE_BLOB)				{					record_set_field_default(*record,																	 ftid,																	 sqlite3_column_blob(pStmt, i),																	 sqlite3_column_bytes(pStmt, i));				}				else				{					gpointer value = NULL;								value = dump_field_value(pStmt, i);					record_set_field_default(*record,																	 ftid,																	 value,																	 FIELD_SIZE_AUTO);					g_free(value);				}			}						existed = TRUE;		}				if (ret == SQLITE_BUSY)		{			sqlite3_finalize(pStmt);						goto failure;		}				if (existed == FALSE)		{			sqlite3_finalize(pStmt);				/*			 * if existed is FALSE.			 * it means querying a invalid record, then 			 * go to failure. 			 *//*			g_print("%s(): *record = 0x%08x\n",							__FUNCTION__,							(guint32)*record);		 */			record_free(*record);			*record = NULL;						goto failure;		}				ret = sqlite3_finalize(pStmt);	} while (ret == SQLITE_SCHEMA);	/*	 * read from extend table	 */		if (record_is_extended(*record))	{		gint i;				for (i = 0; i < DB_FIELDS_NTYPES; i++)		{			gchar * ftstr = NULL;					if (!(element->db_elm_field_types & (1 << i)))			{				continue; 			}					g_string_assign(sql_str, "");			g_string_append_printf(sql_str,														 "SELECT * FROM %s_ext_%s where ( ftid%d = :%03d );",														 element->db_elm_name,														 ftstrs[i],														 element->db_elm_id_ftid,														 element->db_elm_id_ftid);					do			{				const FieldDescriptor * prifdesc = NULL;								prifdesc = field_template_get(element->db_elm_template,																			element->db_elm_id_ftid);		/*				if (element->db_elm_id_type == DB_FIELDS_BLOB)				{					g_print("%s(): BINARY id, use fixed size %d\n",									__FUNCTION__,									prifdesc->msize);				}*/								ret = sqlite3_prepare(element->db->database,															sql_str->str,															-1,															&pStmt,															0);				if (ret != SQLITE_OK)				{						g_print("%s(): sqlite3_step() WARNING: %d[%s].\n",										__FUNCTION__,										sqlite3_errcode(element->db->database),										sqlite3_errmsg(element->db->database));										goto failure;				}						if (prifdesc)				{					bind_value_by_fid(pStmt,														prifdesc->identifier,														&id,														prifdesc->type,														prifdesc->msize);				}						while ((ret = sqlite3_step(pStmt)) == SQLITE_ROW)				{					guint32 fid = FIELD_ID_INVALID;					const gchar * label = NULL;					const gchar * desc = NULL;										fid = sqlite3_column_int(pStmt, 1);					label = sqlite3_column_text(pStmt, 2);					desc = sqlite3_column_text(pStmt, 3);					/*					g_print("%s(): fid = %d, label = %s, desc = %s\n",									__FUNCTION__,									fid,									(label ? label : "(null)"),									(desc ? desc : "(null)"));*/										record_add_know_field(*record, fid);										if (label)					{						record_set_label(*record,														 fid,														 label);					}						if (desc)					{						record_set_desc(*record,														fid,														label);					}						if (sqlite3_column_type(pStmt, 4) == SQLITE_BLOB)					{						record_set_field(*record,														 fid,														 sqlite3_column_blob(pStmt, 4),														 sqlite3_column_bytes(pStmt, 4));					}					else					{						gpointer value = NULL;										value = dump_field_value(pStmt, 4);						record_set_field(*record,														 fid,														 value,														 FIELD_SIZE_AUTO);						g_free(value);					}				}								if (ret == SQLITE_BUSY)				{					sqlite3_finalize(pStmt);										goto failure;				}								ret = sqlite3_finalize(pStmt);			} while (ret == SQLITE_SCHEMA);		}	}		record_db_inner_trans_commit(element->db);		g_string_free(sql_str, TRUE);	return DB_ERROR_NONE;	failure:	g_string_free(sql_str, TRUE);	if (ret == SQLITE_BUSY)	{		return DB_ERROR_BUSY;	}		if (*record == NULL)	{		return DB_ERROR_RESULT_NULL;	}	return DB_ERROR_FAILED;}static voiditer_parse_record_from_hash_table (gpointer	 key,																	 gpointer	 value,																	 gpointer	 user_data)

⌨️ 快捷键说明

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