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

📄 ds_table.cpp

📁 内存数据库
💻 CPP
字号:
  
#include "ds_head.h"
#include "ds_array.h"
#include "ds_table.h"
#include "ds_view.h"


static void DST_UpdateAllViews( HTABLE hTable, OPER_EVENT* oper_event );
BOOL _DST_DeleteRecord( HTABLE hTable, LPOSITION pos );


HTABLE DST_CreateTable( const char* table_name, UINT field_num )
{
	DS_TABLE*	pTable;

	if( table_name == NULL || strlen(table_name) > TABLE_NAME_MAXLEN )
	{
		return NULL;
	}

	if( field_num <= 0 )
	{
		return NULL;
	}
	
	pTable = (DS_TABLE*)malloc( sizeof(DS_TABLE) );
	if( pTable == NULL )
	{
		return NULL;
	}

	//create field
	if( !DSA_CreateArray( &pTable->field_info, field_num, sizeof(TB_FIELD) ) )
	{
		free( pTable );
		return NULL;		
	}

	//initial record list
	DSL_InitList( &pTable->record_list, 0 );


	pTable->field_num = field_num;
	pTable->record_num = 0;
	strcpy( pTable->table_name, table_name );

	//initial views list
	DSL_InitList( &pTable->self_views, sizeof(TB_VIEW) );


	return pTable;
}

BOOL DST_AttachView( HTABLE hTable, HVIEW hView )
{
	TB_VIEW			*tb_view, new_view;
	LPOSITION		pos;

	pos = hTable->self_views.head;
	while( pos )
	{
		tb_view = (TB_VIEW*)pos->data;
		if( tb_view->view_handle == hView )
		{
			return FALSE;
		}
		pos = DSL_NextPos( pos );
	}

	new_view.view_handle = hView;

	DSL_AddHead( &hTable->self_views, &new_view );
	
	return TRUE;
}

BOOL DST_DestroyTable( HTABLE hTable )
{
	LPOSITION	curpos, nextpos;
	
	if( hTable == NULL )
		return FALSE;

	if( hTable->field_info.head )
	{
		free( hTable->field_info.head );
		hTable->field_info.head = NULL;
	}
	hTable->field_info.count = 0;
	hTable->field_info.size = 0;

	curpos = hTable->record_list.head;
	while( curpos )
	{
		nextpos = DSL_NextPos( curpos );
		_DST_DeleteRecord( hTable, curpos );
		curpos = nextpos;
		hTable->record_list.count--;
	}

	free( hTable );
	
	return TRUE;
}


BOOL DST_SetFieldInfo( HTABLE hTable, UINT field_index, TB_FIELD fieldinfo )
{
	TB_FIELD* pFieldInfo;

	if( !DST_IsTable( hTable ) )
		return FALSE;

	if( field_index >= hTable->field_num )
		return FALSE;

	pFieldInfo = (TB_FIELD*)hTable->field_info.head;
	pFieldInfo[field_index] = fieldinfo;

	return TRUE;
}


BOOL DST_GetFieldInfo( HTABLE hTable, UINT field_index, TB_FIELD* fieldinfo )
{
	TB_FIELD* pFieldInfo;

	if( !DST_IsTable( hTable ) )
		return FALSE;

	if( field_index >= hTable->field_num )
		return FALSE;

	if( fieldinfo == NULL )
		return FALSE;

	pFieldInfo = (TB_FIELD*)hTable->field_info.head;
	*fieldinfo = pFieldInfo[field_index];

	return TRUE;
}

BOOL _DST_AddRecord( HTABLE hTable )
{
	DS_ARRAY		record;

	if( !DST_IsTable( hTable ) )
		return FALSE;

	if( !DSA_CreateArray( &record, hTable->field_num, sizeof(TB_VARIANT) ) )
	{
		return FALSE;
	}

	hTable->current_position = DSL_AddTail( &hTable->record_list, record.head );
	if( hTable->current_position == NULL )
	{
		return FALSE;
	}

	memset( record.head, 0, record.size*record.count );
	
	hTable->record_num = hTable->record_list.count;
	
	return TRUE;
}

BOOL DST_AddRecord( HTABLE hTable )
{
	OPER_EVENT		oper_event;

	if( !_DST_AddRecord( hTable ) )
		return FALSE;
	
	oper_event.oper_type = OPER_ADD;
	oper_event.oper_record = hTable->current_position;
	oper_event.oper_field = -1;
	DST_UpdateAllViews( hTable, &oper_event );

	return TRUE;
}

BOOL DST_DeleteCurRecord( HTABLE hTable )
{
	LPOSITION	new_curpos;
	
	if( hTable == NULL )
		return FALSE;

	if( hTable->current_position == NULL )
		return FALSE;

	new_curpos = hTable->current_position->next;

	if( new_curpos == NULL )
	{
		new_curpos = hTable->record_list.head;
	}
	
	if( !DST_DeleteRecord( hTable, hTable->current_position ) )
		return FALSE;

	hTable->current_position = new_curpos;

	return TRUE;
}

BOOL _DST_DeleteRecord( HTABLE hTable, LPOSITION pos )
{
	DS_ARRAY		record;
	TB_VARIANT*		variant;
	TB_FIELD		field;
	UINT			i;


	if( hTable == NULL )
		return FALSE;
	
	if( pos == NULL )
		return FALSE;

	record.head = pos->data;
	record.count = hTable->field_num;
	record.size = sizeof(TB_VARIANT);
	for( i=0; i < hTable->field_num; i++ )		//free string type
	{
		variant = (TB_VARIANT*)DSA_GetAt( &record, i );
		DST_GetFieldInfo( hTable, i, &field );
		if( field.type == FT_STR )
		{
			if( variant->pStrVal )
			{
				free( variant->pStrVal );
				variant->pStrVal = NULL;
			}
		}
	}

	if( ! DSA_DestroyArray( &record ) )
			return FALSE;

	if( !DSL_RemoveAt( &hTable->record_list, pos ) )
		return FALSE;

	return TRUE;

}

BOOL DST_DeleteRecord( HTABLE hTable, LPOSITION pos )
{
	OPER_EVENT		oper_event;

	if( !_DST_DeleteRecord( hTable, pos ) )
		return FALSE;

	oper_event.oper_type = OPER_DELETE;
	oper_event.oper_record = pos;
	oper_event.oper_field = -1;
	DST_UpdateAllViews( hTable, &oper_event );

	return TRUE;

}

static void DST_UpdateAllViews( HTABLE hTable, OPER_EVENT* oper_event )
{
	LPOSITION		view_pos;
	TB_VIEW	*		tb_view;

	view_pos = hTable->self_views.head;
	while( view_pos )
	{
		tb_view = (TB_VIEW*)view_pos->data;
		if( tb_view )
		{
			DSV_Update( (DS_VIEW*)tb_view->view_handle, oper_event );			
		}
			
		view_pos = DSL_NextPos( view_pos );			
	}

}

BOOL _DST_SetFieldValue( HTABLE hTable, LPOSITION record_pos, UINT field_index, TB_VARIANT field_value )
{
	TB_VARIANT*		variant;
	TB_FIELD		field_info;	

	if( hTable == NULL )
		return FALSE;

	if( field_index >= hTable->field_num )
		return FALSE;

	if( hTable->field_info.head == NULL ||
		hTable->field_info.count != hTable->field_num ||
		hTable->field_info.size != sizeof(TB_FIELD)	)
	{
		return FALSE;
	}

	if( record_pos == NULL )
	{
		return FALSE;			
	}

	if( !DST_GetFieldInfo( hTable, field_index, &field_info ) )
		return FALSE;

	variant = (TB_VARIANT*)record_pos->data;

	if( variant == NULL )
	{
		return FALSE;
	}

	if( field_info.type == FT_STR )
	{
		int nLen;

		if( field_value.pStrVal == NULL )
			return FALSE;
	
		nLen = strlen( field_value.pStrVal );
		variant[field_index].pStrVal = (char*)malloc( nLen+1 );
		if( variant[field_index].pStrVal == NULL )
			return FALSE;

		strcpy(variant[field_index].pStrVal, field_value.pStrVal);

	}
	else
	{
		variant[field_index] = field_value;
	}

	return TRUE;
}

BOOL DST_SetFieldValue( HTABLE hTable, UINT field_index, TB_VARIANT field_value )
{
	OPER_EVENT		oper_event;

	if( _DST_SetFieldValue( hTable, hTable->current_position, field_index, field_value ) )
		return FALSE;

	oper_event.oper_type = OPER_MODIFY;
	oper_event.oper_record = hTable->current_position;
	oper_event.oper_field = field_index;
	DST_UpdateAllViews( hTable, &oper_event );


	return TRUE;
}

BOOL _DST_GetFieldValue( HTABLE hTable, LPOSITION record_pos, UINT field_index, TB_VARIANT* field_value )
{
	TB_VARIANT*		record;

	if( hTable == NULL )
		return FALSE;

	if( field_index >= hTable->field_num )
		return FALSE;

	if( hTable->field_info.head == NULL ||
		hTable->field_info.count != hTable->field_num ||
		hTable->field_info.size != sizeof(TB_FIELD)	)
	{
		return FALSE;
	}

	if( field_value == NULL )
	{
		return FALSE;
	}

	if( record_pos == NULL )
	{
		return FALSE;			
	}

	record = (TB_VARIANT*)record_pos->data;

	if( record == NULL )
	{
		return FALSE;
	}

	*field_value = record[field_index];

	return TRUE;
}

BOOL DST_GetFieldValue( HTABLE hTable, UINT field_index, TB_VARIANT* field_value )
{

	return _DST_GetFieldValue( hTable, hTable->current_position, field_index, field_value );
}

void DST_MoveNext( HTABLE hTable )
{
	hTable->current_position = hTable->current_position->next;
}

void DST_MovePrev( HTABLE hTable )
{
	hTable->current_position = hTable->current_position->prev;
}

void DST_MoveFirst( HTABLE hTable )
{
	hTable->current_position = hTable->record_list.head;
}

void DST_MoveLast( HTABLE hTable )
{
	hTable->current_position = hTable->record_list.tail;
}

int DST_GetRecordCount( HTABLE hTable )
{
	if( hTable == NULL )
		return -1;

	return hTable->record_num;
}

BOOL DST_IsTable( HTABLE hTable )
{
	if( hTable == NULL )
		return FALSE;

	if( hTable->field_info.head == NULL ||
		hTable->field_info.count != hTable->field_num ||
		hTable->field_info.size != sizeof(TB_FIELD)	)
	{
		return FALSE;
	}

	return TRUE;
}

void DST_SetAbsolutePostion( HTABLE hTable, LPOSITION pos )
{
	hTable->current_position = pos;
}

⌨️ 快捷键说明

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