/*****************************************************************************
** ai_database.c: AL Layer Internal Database Source Code
**
** Description: the internal interface of database module.
**
** Copyright(c) 2011 Sunmedia Technologies - All Rights Reserved
**
** Author : kun.dai
**
** $Id$
*****************************************************************************/

/******************************************************************************
 *	Include Files
 ******************************************************************************/
#include "cdbi.h"
#include "ai_database.h"

/******************************************************************************
 *	Debug and Error Print
 ******************************************************************************/
#define AIDB_DEBUG_FLAG (0)
#define AIDB_ERROR_FLAG (1)

#if AIDB_DEBUG_FLAG
	#define AIDB_DEBUG_MSG(fmt, arg...) \
	do { \
		UMFDBG(0,"[Debug]%s: %d Line ", __FUNCTION__, __LINE__); \
		UMFDBG(0,fmt, ##arg); \
	} while(0)
#else
	#define AIDB_DEBUG_MSG(fmt, arg...);
#endif

#if AIDB_ERROR_FLAG
	#define AIDB_ERROR_MSG(fmt, arg...) \
	do { \
		UMFDBG(0,"[Error]%s: %d Line ", __FUNCTION__, __LINE__); \
		UMFDBG(0,fmt, ##arg); \
	} while(0)
#else
	#define AIDB_ERROR_MSG(fmt, arg...);
#endif
/******************************************************************************
 *	Macros
 ******************************************************************************/
#define AIDB_RESULT(ret) ((DB_SUCCESS == (ret)) ? AL_SUCCESS : AL_FAILURE)

#define AIDB_CHECKTYPE(type, typeEnum) ((typeEnum) == ((type) & (typeEnum)))

/******************************************************************************
 *	Global Variables
 ******************************************************************************/


/******************************************************************************
 *	Internal Function definition
 ******************************************************************************/
AL_Return_t AI_DB_Store_ATV(AL_DB_EDBType_t eDBType)
{
	EDBError_t eRet = DB_FAILURE;

	if (AIDB_CHECKTYPE(eDBType, CDBI_DBTYPE_DVB_ATV))
	{
		eRet = CDBISyncDBModule(CDBI_DBTYPE_DVB_ATV);
	}

	return AIDB_RESULT(eRet);
}
AL_Return_t AI_DB_Store_DTV(AL_DB_EDBType_t eDBType)
{
	EDBError_t eRet = DB_FAILURE;

	if (AIDB_CHECKTYPE(eDBType, CDBI_DBTYPE_DVB_T))
	{
		eRet = CDBISyncDBModule(CDBI_DBTYPE_DVB_T);
	}
    #ifdef CONFIG_ISDB_SYSTEM
	if (AIDB_CHECKTYPE(eDBType, CDBI_DBTYPE_DVB_SBTVD))
	{
		eRet = CDBISyncDBModule(CDBI_DBTYPE_DVB_SBTVD);
	}
    #endif
	if (AIDB_CHECKTYPE(eDBType, CDBI_DBTYPE_DVB_C))
	{
		eRet = CDBISyncDBModule(CDBI_DBTYPE_DVB_C);
	}

	if (AIDB_CHECKTYPE(eDBType, CDBI_DBTYPE_DVB_S))
	{
		eRet = CDBISyncDBModule(CDBI_DBTYPE_DVB_S);
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_SetDefault_ATV(AL_DB_EDBType_t eDBType)
{
	EDBError_t eRet = DB_SUCCESS;
	return AIDB_RESULT(eRet);
}
AL_Return_t AI_DB_SetDefault_DTV(AL_DB_EDBType_t eDBType)
{
	EDBError_t eRet = DB_SUCCESS;
	return AIDB_RESULT(eRet);
}
AL_Return_t AI_DB_Empty_ATV(AL_DB_EDBType_t eDBType)
{
	EDBError_t eRet = DB_FAILURE;

	if (AIDB_CHECKTYPE(eDBType, CDBI_DBTYPE_DVB_ATV))
	{
		eRet = CDBIResetDBModule(CDBI_DBTYPE_DVB_ATV);
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_Empty_DTV(AL_DB_EDBType_t eDBType)
{
	EDBError_t eRet = DB_FAILURE;

	if (AIDB_CHECKTYPE(eDBType, CDBI_DBTYPE_DVB_T))
	{
		eRet = CDBIResetDBModule(CDBI_DBTYPE_DVB_T);
	}

	#ifdef CONFIG_ISDB_SYSTEM
	if (AIDB_CHECKTYPE(eDBType, CDBI_DBTYPE_DVB_SBTVD))
	{
		eRet = CDBIResetDBModule(CDBI_DBTYPE_DVB_SBTVD);
	}
	#endif

	if (AIDB_CHECKTYPE(eDBType, CDBI_DBTYPE_DVB_C))
	{
		eRet = CDBIResetDBModule(CDBI_DBTYPE_DVB_C);
	}

	if (AIDB_CHECKTYPE(eDBType, CDBI_DBTYPE_DVB_S))
	{
		eRet = CDBIResetDBModule(CDBI_DBTYPE_DVB_S);
	}

	return AIDB_RESULT(eRet);

}
AL_Return_t AI_DB_GetCapability(AL_DB_EDBType_t eDBType,AL_DBCapability_t *pstCapability)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIGetCapability(eDBType, pstCapability);

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_AddRecord(AL_DB_EDBType_t eDBType,
	AL_DB_ERecordType_t eRecType, const al_void * pstRecDetail,
	AL_RecHandle_t hParentHdl, AL_RecHandle_t *phRecHdl)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIAddRec(eDBType, eRecType, pstRecDetail, hParentHdl, (CDBIHandle_t *)phRecHdl);

	if (DB_SUCCESS != eRet)
	{
		*phRecHdl = CDBI_INVALID_HDL;
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_GetFistRecord(AL_DB_EDBType_t eDBType,
	AL_DB_ERecordType_t eRecType, AL_RecHandle_t *phRecHdl)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIGetFirstRecord(eDBType, eRecType, (CDBIHandle_t *)phRecHdl);

	if (DB_SUCCESS != eRet)
	{
		*phRecHdl = CDBI_INVALID_HDL;
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_GetLastRecord(AL_DB_EDBType_t eDBType,
	AL_DB_ERecordType_t eRecType, AL_RecHandle_t *phRecHdl)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIGetLastRecord(eDBType, eRecType, (CDBIHandle_t *)phRecHdl);

	if (DB_SUCCESS != eRet)
	{
		*phRecHdl = CDBI_INVALID_HDL;
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_GetNextRecord(AL_RecHandle_t hRefHdl, AL_DB_ERecordType_t eRecType, AL_RecHandle_t *phRecHdl)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIGetNextRecord(hRefHdl, eRecType, (CDBIHandle_t *)phRecHdl);

	if (DB_SUCCESS != eRet)
	{
		*phRecHdl = CDBI_INVALID_HDL;
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_GetPrevRecord(AL_RecHandle_t hRefHdl, AL_DB_ERecordType_t eRecType, AL_RecHandle_t *phRecHdl)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIGetPrevRecord(hRefHdl, eRecType, (CDBIHandle_t *)phRecHdl);

	if (DB_SUCCESS != eRet)
	{
		*phRecHdl = CDBI_INVALID_HDL;
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_GetParentRecord(AL_RecHandle_t hRefHdl, AL_RecHandle_t *phRecHdl)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIGetParentRec((CDBIHandle_t)hRefHdl, (CDBIHandle_t *)phRecHdl);

	if (DB_SUCCESS != eRet)
	{
		*phRecHdl = CDBI_INVALID_HDL;
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_GetChildRecordNum(AL_RecHandle_t hParentHdl, al_uint32 *puiRecCnt)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIGetNoOfChildrenRec((CDBIHandle_t)hParentHdl, puiRecCnt);

	if (DB_SUCCESS != eRet)
	{
		*puiRecCnt = 0;
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_GetChildRecord(AL_RecHandle_t hParentHdl, AL_RecHandle_t *phRecHdl)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIChildRecTraversal((CDBIHandle_t)hParentHdl, (CDBIHandle_t *)phRecHdl);

	if (DB_SUCCESS != eRet)
	{
		*phRecHdl = CDBI_INVALID_HDL;
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_MatchRecord(AL_DB_RecordMatchFunc fRecMatch,
	al_void *pMatchParam, AL_DB_EDBType_t eDBType, AL_DB_ERecordType_t eRecType,
	al_bool bNext, AL_RecHandle_t *phRecHdl)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIFindRecord(eDBType, eRecType, fRecMatch, pMatchParam, bNext, (CDBIHandle_t *)phRecHdl);


	if (DB_SUCCESS != eRet)
	{
		*phRecHdl = CDBI_INVALID_HDL;
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_MatchRecordExt(AL_DB_RecordMatchFunc fRecMatch,
	al_void *pMatchParam, AL_DB_EDBType_t eDBType, AL_DB_ERecordType_t eRecType,
	al_bool bAscending, al_uint32 uiSkipNum, AL_RecHandle_t *phRecHdl, al_uint32 *puiRecNum)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIFindRecordExt(eDBType, eRecType, fRecMatch, pMatchParam, bAscending, 
		uiSkipNum, (CDBIHandle_t *)phRecHdl, puiRecNum);

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_GetRecordType(AL_RecHandle_t hRecHdl,
	AL_DB_EDBType_t *peDBType, AL_DB_ERecordType_t *peRecType)
{
	EDBError_t eRet = DB_FAILURE;
	ECDBIDBType_t eDBType;
	ECDBIRecordType_t eRecType;

	eRet = CDBIGetRecTypeAndDBType((CDBIHandle_t)hRecHdl, (ECDBIDBType_t *)&eDBType, (ECDBIRecordType_t *)&eRecType);

	if (DB_SUCCESS == eRet)
	{
		if (al_null != peDBType)
		{
			*peDBType = eDBType;
		}
		if (al_null != peRecType)
		{
			*peRecType = eRecType;
		}
	}
	else
	{
		if (al_null != peDBType)
		{
			*peDBType = AL_DBTYPE_MAX;
		}
		if (al_null != peRecType)
		{
			*peRecType = AL_RECTYPE_MAX;
		}
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_MoveRecordToFirst(AL_RecHandle_t hRecHdl)
{
	EDBError_t eRet = DB_FAILURE;
	CDBIHandle_t tempHdl = CDBI_INVALID_HDL;
	ECDBIDBType_t eDBType;
	ECDBIRecordType_t eRecType;

	if (DB_SUCCESS == CDBIGetRecTypeAndDBType((CDBIHandle_t)hRecHdl, &eDBType, &eRecType))
	{
		if (DB_SUCCESS == CDBIGetFirstRecord(eDBType, eRecType, &tempHdl))
		{
			eRet = CDBIMoveRec((CDBIHandle_t)hRecHdl, tempHdl, al_true);
		}
	}

	return AIDB_RESULT(eRet);
}
AL_Return_t AI_DB_MoveRecordToLast(AL_RecHandle_t hRecHdl)
{
	EDBError_t eRet = DB_FAILURE;
	CDBIHandle_t tempHdl = CDBI_INVALID_HDL;
	ECDBIDBType_t eDBType;
	ECDBIRecordType_t eRecType;

	if (DB_SUCCESS == CDBIGetRecTypeAndDBType((CDBIHandle_t)hRecHdl, &eDBType, &eRecType))
	{
		if (DB_SUCCESS == CDBIGetLastRecord(eDBType, eRecType, &tempHdl))
		{
			eRet = CDBIMoveRec((CDBIHandle_t)hRecHdl, tempHdl, al_false);
		}
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_MoveRecordToFront(AL_RecHandle_t hCurrRecHdl, AL_RecHandle_t hRefRecHdl)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIMoveRec((CDBIHandle_t)hCurrRecHdl, (CDBIHandle_t)hRefRecHdl, al_true);

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_MoveRecordToBehind(AL_RecHandle_t hCurrRecHdl, AL_RecHandle_t hRefRecHdl)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIMoveRec((CDBIHandle_t)hCurrRecHdl, (CDBIHandle_t)hRefRecHdl, al_false);

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_SwapRecord(AL_RecHandle_t hCurrRecHdl, AL_RecHandle_t hRefRecHdl)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBISwapRec((CDBIHandle_t)hCurrRecHdl, (CDBIHandle_t)hRefRecHdl);

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_RemoveRecord(AL_RecHandle_t hRecHdl)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIRemoveRec((CDBIHandle_t)hRecHdl);

	return AIDB_RESULT(eRet);
}
AL_Return_t AI_DB_GetTotalNum(AL_DB_EDBType_t eDBType,
	AL_DB_ERecordType_t eRecType, al_uint32 *pu32Num)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIGetNoOfRecInDB((ECDBIDBType_t)eDBType, (ECDBIDBType_t)eRecType, (UINT32 *)pu32Num);

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_SearchChildRecords(AL_DB_RecordMatchFunc fRecMatch,al_void *pMatchParam,
	AL_DB_EDBType_t eDBType, AL_DB_ERecordType_t eRecType, CDBIHandle_t hHdl,
	al_uint32 u32NeedNum, al_uint32 u32SearchOffset, al_uint32 *pu32TotalNum,
	al_uint32 *pu32GotNum, AL_RecHandle_t *pstRecList)
{
	EDBError_t eRet = DB_SUCCESS;
	CDBIHandle_t hTempHdl = CDBI_INVALID_HDL;
	al_uint32 totalNum = 0;
	al_uint32 gotNum = 0;
	AL_RecordDetail_t stRecInfo;

	while (AL_SUCCESS == AI_DB_GetChildRecord(hHdl, &hTempHdl))
	{
		AI_DB_QueryDetail(hTempHdl, &stRecInfo);
		if (((fRecMatch) && (pMatchParam) && (fRecMatch(pMatchParam, &stRecInfo)))
			|| (fRecMatch == NULL)
			|| (pMatchParam == NULL))
		{
			totalNum++;
			if (al_null != pstRecList)
			{
				if (totalNum > u32SearchOffset)
				{
					if (gotNum < u32NeedNum)
					{
						if (pstRecList)
						{
							pstRecList[gotNum] = (AL_RecHandle_t)hTempHdl;
						}
						gotNum++;
					}
				}
			}
		}
	}

	if (al_null != pu32TotalNum)
	{
		*pu32TotalNum = totalNum;
	}

	if (al_null != pu32GotNum)
	{
		*pu32GotNum = gotNum;
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_SearchRecords(AL_DB_RecordMatchFunc fRecMatch,al_void *pMatchParam,
	AL_DB_EDBType_t eDBType, AL_DB_ERecordType_t eRecType, al_uint32 u32NeedNum,
	al_uint32 u32SearchOffset, al_uint32 *pu32TotalNum, al_uint32 *pu32GotNum, AL_RecHandle_t *pstRecList)
{
	EDBError_t eRet = DB_SUCCESS;
	CDBIHandle_t tempHdl = CDBI_INVALID_HDL;
	al_uint32 totalNum = 0;
	al_uint32 gotNum = 0;

	while (DB_SUCCESS == CDBIFindRecord(eDBType, eRecType, fRecMatch, pMatchParam, al_true, &tempHdl))
	{
		totalNum++;
		if (al_null != pstRecList)
		{
			if (totalNum > u32SearchOffset)
			{
				if (gotNum < u32NeedNum)
				{
					pstRecList[gotNum] = (AL_RecHandle_t)tempHdl;
					gotNum++;
				}
			}
		}
	}

	if (al_null != pu32TotalNum)
	{
		*pu32TotalNum = totalNum;
	}

	if (al_null != pu32GotNum)
	{
		*pu32GotNum = gotNum;
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_QueryDetail(AL_RecHandle_t hRecHdl, al_void *pDetailData)
{
	EDBError_t eRet = DB_FAILURE;
	ECDBIDBType_t eDBType = 0;
	ECDBIRecordType_t eRecType = 0;

	CDBIGetRecTypeAndDBType((CDBIHandle_t)hRecHdl, &eDBType, &eRecType);

	switch (eDBType)
	{
		case CDBI_DBTYPE_DVB_ATV:
			if (AIDB_CHECKTYPE(eRecType, CDBI_RECTYPE_ATVSERVICE))
			{
				eRet = CDBIGetRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SADBServInfo_t));
			}
			break;
		case CDBI_DBTYPE_DVB_T:
			{
				switch (eRecType)
				{
					case CDBI_RECTYPE_DVBTV:
					case CDBI_RECTYPE_DVBRADIO:
                    case CDBI_RECTYPE_DVBDATA:
						eRet = CDBIGetRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(STDBServInfo_t));
						break;
					case CDBI_RECTYPE_DVBMULTIPLEX:
						eRet = CDBIGetRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(STDBMultiplexInfo_t));
						break;
					case CDBI_RECTYPE_DVBNETWORK:
						eRet = CDBIGetRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(STDBNetworkInfo_t));
						break;
					default:
						eRet = DB_FAILURE;
						break;
				}
			}
			break;
        #ifdef CONFIG_ISDB_SYSTEM
		case CDBI_DBTYPE_DVB_SBTVD:
			{
				switch (eRecType)
				{
					case CDBI_RECTYPE_DVBTV:
					case CDBI_RECTYPE_DVBRADIO:
                    case CDBI_RECTYPE_DVBDATA:
						eRet = CDBIGetRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SSBTVDDBServInfo_t));
						break;
					case CDBI_RECTYPE_DVBMULTIPLEX:
						eRet = CDBIGetRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SSBTVDDBMultiplexInfo_t));
						break;
					case CDBI_RECTYPE_DVBNETWORK:
						eRet = CDBIGetRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SSBTVDDBNetworkInfo_t));
						break;
					default:
						eRet = DB_FAILURE;
						break;
				}
			}
			break;
        #endif
		case CDBI_DBTYPE_DVB_C:
			{
				switch (eRecType)
				{
					case CDBI_RECTYPE_DVBTV:
					case CDBI_RECTYPE_DVBRADIO:
                    case CDBI_RECTYPE_DVBDATA:
						eRet = CDBIGetRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SCDBServInfo_t));
						break;
					case CDBI_RECTYPE_DVBMULTIPLEX:
						eRet = CDBIGetRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SCDBMultiplexInfo_t));
						break;
					default:
						eRet = DB_FAILURE;
						break;
				}
			}
			break;
		case CDBI_DBTYPE_DVB_S:
			{
				switch (eRecType)
				{
					case CDBI_RECTYPE_DVBTV:
					case CDBI_RECTYPE_DVBRADIO:
                    case CDBI_RECTYPE_DVBDATA:
						eRet = CDBIGetRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SDBServInfo_t));
						break;
					case CDBI_RECTYPE_DVBMULTIPLEX:
						eRet = CDBIGetRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SDBMultiplexInfo_t));
						break;
					case CDBI_RECTYPE_DVBNETWORK:
						eRet = CDBIGetRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SDBNetworkInfo_t));
						break;
					default:
						eRet = DB_FAILURE;
						break;
				}
			}
			break;
		default:
			eRet = DB_FAILURE;
			break;
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_QueryDetailField(AL_RecHandle_t hRecHdl,
	al_uint16 u16ItemOffset, al_uint16 u16ItemBytes, al_void *pItemData)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIGetRec((CDBIHandle_t)hRecHdl, pItemData, (UINT16)u16ItemOffset, (UINT16)u16ItemBytes);

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_QueryDetailFieldByName(AL_RecHandle_t hRecHdl,
	al_uint8 *pItemName, al_void *pItemData)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIGetRecBySymbol((CDBIHandle_t)hRecHdl, pItemData, pItemName);

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_UpdateDetail(AL_RecHandle_t hRecHdl, const al_void *pDetailData)
{
	EDBError_t eRet = DB_FAILURE;
	ECDBIDBType_t eDBType = 0;
	ECDBIRecordType_t eRecType = 0;

	CDBIGetRecTypeAndDBType((CDBIHandle_t)hRecHdl, &eDBType, &eRecType);

	switch (eDBType)
	{
		case CDBI_DBTYPE_DVB_ATV:
			if (AIDB_CHECKTYPE(eRecType, CDBI_RECTYPE_ATVSERVICE))
			{
				eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SADBServInfo_t));
			}
			break;
		case CDBI_DBTYPE_DVB_T:
			{
				switch (eRecType)
				{
					case CDBI_RECTYPE_DVBTV:
					case CDBI_RECTYPE_DVBRADIO:
                    case CDBI_RECTYPE_DVBDATA:
						eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(STDBServInfo_t));
						break;
					case CDBI_RECTYPE_DVBMULTIPLEX:
						eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(STDBMultiplexInfo_t));
						break;
					case CDBI_RECTYPE_DVBNETWORK:
						eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(STDBNetworkInfo_t));
						break;
					default:
						eRet = DB_FAILURE;
						break;
				}
			}
			break;
        #ifdef CONFIG_ISDB_SYSTEM
		case CDBI_DBTYPE_DVB_SBTVD:
			{
				switch (eRecType)
				{
					case CDBI_RECTYPE_DVBTV:
					case CDBI_RECTYPE_DVBRADIO:
                    case CDBI_RECTYPE_DVBDATA:
						eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SSBTVDDBServInfo_t));
						break;
					case CDBI_RECTYPE_DVBMULTIPLEX:
						eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SSBTVDDBMultiplexInfo_t));
						break;
					case CDBI_RECTYPE_DVBNETWORK:
						eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SSBTVDDBNetworkInfo_t));
						break;
					default:
						eRet = DB_FAILURE;
						break;
				}
			}
			break;
        #endif
		case CDBI_DBTYPE_DVB_C:
			{
				switch (eRecType)
				{
					case CDBI_RECTYPE_DVBTV:
					case CDBI_RECTYPE_DVBRADIO:
                    case CDBI_RECTYPE_DVBDATA:
						eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SCDBServInfo_t));
						break;
					case CDBI_RECTYPE_DVBMULTIPLEX:
						eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SCDBMultiplexInfo_t));
						break;
					case CDBI_RECTYPE_DVBNETWORK:
						eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SCDBNetworkInfo_t));
						break;
					default:
						eRet = DB_FAILURE;
						break;
				}
			}
			break;

		case CDBI_DBTYPE_DVB_S:
			{
				switch (eRecType)
				{
					case CDBI_RECTYPE_DVBTV:
					case CDBI_RECTYPE_DVBRADIO:
                    case CDBI_RECTYPE_DVBDATA:
						eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SDBServInfo_t));
						break;
					case CDBI_RECTYPE_DVBMULTIPLEX:
						eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SDBMultiplexInfo_t));
						break;
					case CDBI_RECTYPE_DVBNETWORK:
						eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pDetailData, 0, sizeof(SDBNetworkInfo_t));
						break;
					default:
						eRet = DB_FAILURE;
						break;
				}
			}
			break;
		default:
			eRet = DB_FAILURE;
			break;
	}

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_UpdateDetailField(AL_RecHandle_t hRecHdl,
	al_uint16 u16ItemOffset, al_uint16 u16ItemBytes, al_void *pItemData)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIUpdateRec((CDBIHandle_t)hRecHdl, pItemData, (UINT16)u16ItemOffset, (UINT16)u16ItemBytes);

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_UpdateDetailFieldByName(AL_RecHandle_t hRecHdl,
	al_uint8 *pItemName, al_void *pItemData)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBIUpdateRecBySymbol((CDBIHandle_t)hRecHdl, pItemData, pItemName);

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_SortRecords(AL_DB_EDBType_t eDBType,
	AL_DB_ERecordType_t eRecType, AL_DB_ESortType_t eSortCmd, al_bool bAscending)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBISortRec(eDBType, eRecType, eSortCmd, bAscending);

	return AIDB_RESULT(eRet);
}

AL_Return_t AI_DB_SortRecordsByUser(AL_DB_EDBType_t eDBType,
	AL_DB_ERecordType_t eRecType, AL_DB_RecordUserCmp fRecUserCmpCB)
{
	EDBError_t eRet = DB_FAILURE;

	eRet = CDBISortRecByUser(eDBType, eRecType, fRecUserCmpCB);

	return AIDB_RESULT(eRet);
}


