PRO*C 函数事例 2 -- 数据库操作

      Pro*C Oracle 的嵌入式开发,数据库处理部分最好能提取到一个模块,按照对不同数据库表的操作分成不同的.pc文件(如 DbsInstStat.pc)。
将此模块编译成库(c文件编译时链接此库),.c文件处理库表时调用对应数据库模块(.pc)中的函数即可。
      函数事例:

DbsDef.h

#define DBS_INIT       0
#define DBS_SELECT     1
#define DBS_LOCK       2
#define DBS_UPDATE     3
#define DBS_DELETE     4
#define DBS_INSERT     5

#define DBS_CURSOR     11
#define DBS_OPEN       12
#define DBS_CLOSE      13
#define DBS_FETCH      14

#define DBS_UPDATE2    15

#define  SQL_NOT_FOUND_RET(ERR_VAL) \
    if( sqlca.sqlcode == 1403 ) \
    {\
        HtLog( ERROR , "error code [%d], reason[%s] \n", \
            sqlca.sqlcode, ( char * )sqlca.sqlerrm.sqlerrmc ) ; \
        strcpy( gPubStru.saBankRespCode, ERR_VAL ) ; \
        return -1; \
    }

#define  SQL_ERR_RET( ) \
    if( sqlca.sqlcode != 0 ) \
    {\
        HtLog( ERROR , "SQL ERROR code [%d], reason[%s] \n", \
            sqlca.sqlcode, ( char * )sqlca.sqlerrm.sqlerrmc ); \
        strcpy( gPubStru.saBankRespCode, SQL_ERR ) ; \
        return -1 ; \
    }

#define  SQL_ERR_RET2( ) \
    if( sqlca.sqlcode != 0 && sqlca.sqlcode != 1403) \
    {\
        HtLog( ERROR , "SQL ERROR code [%d], reason[%s] \n", \
            sqlca.sqlcode, ( char * )sqlca.sqlerrm.sqlerrmc ); \
        strcpy( gPubStru.saBankRespCode, SQL_ERR ) ; \
        return -1 ; \
    }

DbsInstStat.pc

#include "public.h"
#include "errlog.h"
#include "DbsDef.h"
#include "my_sys.h"


EXEC SQL INCLUDE sqlda;
EXEC SQL INCLUDE sqlca;

EXEC SQL BEGIN DECLARE SECTION;
    char    saInstCode[11 + 1];
    char    saInstHsmIndex [3 + 1];
    char    saInstPrimKey [32 + 1];
    char    saInstPinKey [32 + 1];
    char    saInstMacKey [32 + 1];
    char    saInstStat [1 + 1];
EXEC SQL END DECLARE SECTION;


int DbsTblInstStat (int nOpr)
{
    /***********
    * 参数初始化
    ************/
    memset (saInstCode, 0x00, sizeof(saInstCode));
    memset (saInstHsmIndex, 0x00, sizeof(saInstHsmIndex));
    memset (saInstPrimKey, 0x00, sizeof(saInstPrimKey));
    memset (saInstPinKey, 0x00, sizeof(saInstPinKey));
    memset (saInstMacKey, 0x00, sizeof(saInstMacKey));
    memset (saInstStat, 0x00, sizeof(saInstStat));

    memcpy (saInstCode, gPubStru.saFwdInstIdCode, 8);
    trimspace (saInstCode);


    /***********
    * 数据处理
    ************/
    switch (nOpr)
    {
    case DBS_SELECT:
        EXEC SQL
            SELECT INST_HSM_INDEX, INST_PRIM_KEY, nvl(INST_PIN_KEY, ' '), nvl(INST_MAC_KEY, ' '), INST_STAT
            INTO :saInstHsmIndex, :saInstPrimKey, :saInstPinKey, :saInstMacKey, :saInstStat
            FROM TBL_INST_STAT
            WHERE INST_CODE = :saInstCode;

            SQL_NOT_FOUND_RET2 ( );
            SQL_ERR_RET2 ( );

        /******************
        * 保存数据到内部IPC
        *******************/
        memcpy (gPubStru.saInstHsmIndex, saInstHsmIndex, 4);
        memcpy (gPubStru.saInstMainKey, saInstPrimKey, 32);
        memcpy (gPubStru.saPinKey, saInstPinKey, 32);
        memcpy (gPubStru.saMacKey, saInstMacKey, 32);
        memcpy (gPubStru.saInstStat, saInstStat, 1);
        
        break;
    case DBS_UPDATE:
        EXEC SQL
            UPDATE TBL_INST_STAT SET INST_STAT = 'Y'
            WHERE INST_CODE = :saInstCode;

            SQL_NOT_FOUND_RET2 ( );
            SQL_ERR_RET2( )

        break;
    case DBS_UPDATE2:
        memcpy (saInstMacKey, gPubStru.saMacKey1, 16);

        EXEC SQL
            UPDATE TBL_INST_STAT SET INST_MAC_KEY = :saInstMacKey
            WHERE INST_CODE = :saInstCode;

            SQL_NOT_FOUND_RET2 ( );
            SQL_ERR_RET2( )

        break;
    default:
        break;
    }


    return 0;
}

 

posted @ 2015-03-06 17:08  音弛  阅读(408)  评论(0编辑  收藏  举报