VastbaseG100

基于openGauss内核开发的企业级关系型数据库。

Menu

分段操作

OCI在对非常大的数据类型操作时,可以执行数据的分段插入、更新和获取。通过应用程序在运行时分割均匀的或不均匀的片段大小。将OCIBindByPos()或OCIBindByPos2()或OCIBindByName()或OCIBindByName2()调用的mode参数设置为OCI_DATA_AT_EXEC时,表示OCI应用程序在运行时动态为插入或更新操作提供数据,OCIDefineByPos()或OCIDefineByPos2()调用的mode参数设置为OCI_DYNAMIC_FETCH时,它表示应用程序在提取时动态地为接收数据提供分配空间。当OCIStmtFetch2或OCIStmtExecute函数返回码为OCI_NEED_DAT。

OCI提供OCIStmtGetPieceInfo(获取分段操作的分段信息)和OCIStmtSetPieceInfo(设置分段操作的分段信息)。

执行插入或者更新流程

if(OCIStmtPrepare(stmthp0, errhp, sqlstmt0, strlen((char *)sqlstmt0), OCI_NTV_SYNTAX, 0))
    {
        report_error(errhp);
        printf("FAILED: log_on()\n");
        return OCI_ERROR;
    }

    if(OCIStmtExecute(svchp, stmthp0, errhp, 1, 0, 0, 0, OCI_DEFAULT))
    {
        report_error(errhp);
        printf("FAILED: OCIStmtExecute()\n");
        return OCI_ERROR;
    }

    //测试准备一条SQL
    if(OCIStmtPrepare(stmthp, errhp, sqlstmt, strlen((char *)sqlstmt), OCI_NTV_SYNTAX, 0))
    {
        report_error(errhp);
        printf("FAILED: log_on()\n");
        return OCI_ERROR;
    }

    if(OCIBindByName(stmthp, &bnd2p, errhp, (text *) ":c2",
                          (sb4) -1, (dvoid *) c2,
                          (sb4) DATA_SIZE, SQLT_CHR,
                          (dvoid *) 0, (ub2 *)0, (ub2 *)0, (ub4) 0, (ub4 *) 0,
                          (ub4) OCI_DATA_AT_EXEC))
    {
        printf("FAILED: OCIBindByName()\n");
        return OCI_ERROR;
    }

    if(OCIBindByName(stmthp, &bnd3p, errhp, (text *) ":c3",
                          (sb4) -1, (dvoid *) c3,
                          (sb4) DATA_SIZE, SQLT_CHR,
                          (dvoid *) 0, (ub2 *)0, (ub2 *)0, (ub4) 0, (ub4 *) 0,
                          (ub4) OCI_DATA_AT_EXEC))
    {
        printf("FAILED: OCIBindByName()\n");
        return OCI_ERROR;
    }

    ub1    piece;
    ub4    alenp2 = PIECE_SIZE, alenp3 = PIECE_SIZE;

    dvoid *hdlptr = (dvoid *) 0;
    ub4 hdltype;
    ub1 in_outp;
    ub4 iterp;
    ub4 idxp;
    ub1 piecep;
    char *c ;
    //测试SQL执行
    while(1)
    {
        int status = OCIStmtExecute(svchp, stmthp, errhp, 1, 0, 0, 0, OCI_DEFAULT);
        switch(status)
        {
            case OCI_NEED_DATA:
                if(OCIStmtGetPieceInfo(stmthp, errhp, &hdlptr, &hdltype, &in_outp, &iterp, &idxp, &piecep))
                    printf("ERROR: OCIStmtGetPieceInfo returned");
                printf("bnd%dp, piecep=%d\n", hdlptr==bnd2p?2:3, piecep);
                set_piece(&piece);

                if(hdlptr == bnd2p)
                    c=c2;
                else
                    c=c3;

                if (OCIStmtSetPieceInfo(hdlptr, OCI_HTYPE_BIND, errhp, (dvoid *)c, &alenp2, piece, (dvoid *) 0, NULL))
                {
                    printf("ERROR: OCIStmtSetPieceInfo returned %d \n", status);
                    break;
                }
                status = OCI_NEED_DATA;
                break;

            case OCI_SUCCESS:
                break;

             default:
                printf( "oci exec returned %d \n", status);
                report_error(errhp);
                status = 0;
         }
         if (!status)
             break;
    }

执行查询操作

OCIStmtPrepare(stmthp, errhp, sqlstmt, strlen((char *)sqlstmt),
     OCI_NTV_SYNTAX, 0);

    //测试SQL执行
    if(OCIStmtExecute(svchp, stmthp, errhp, 0, 0, 0, 0, 0))
            printf("FAILED: OCIStmtExecute()\n");

    //绑定缓冲区到相对应的列上
    if(OCIDefineByPos(stmthp, &bndhp[0], errhp, 1,
    (dvoid *)c1, (sb4)sizeof(int), (ub2)SQLT_INT,
    (dvoid *)0, (ub2 *)0, (ub2 *)0, (ub4)OCI_DEFAULT))
    {
        printf("FAILED: OCIDefineByPos0()\n");
        report_error(errhp);
        return OCI_ERROR;
    }

    if (OCIDefineByPos(stmthp, &bndhp[1], errhp, 2,
            (dvoid *)c2, (sb4)CHAR_SIZE, (ub2)SQLT_CHR,
            (dvoid *)0, (ub2 *)0, (ub2 *)0, (ub4)OCI_DYNAMIC_FETCH))
    {
        printf("FAILED: OCIDefineByPos2()\n");
        report_error(errhp);
        return OCI_ERROR;
    }

    if (OCIDefineByPos(stmthp, &bndhp[2], errhp, 3,
        (dvoid *)c3, (sb4)CHAR_SIZE, (ub2)SQLT_CHR,
        (dvoid *)0, (ub2 *)0, (ub2 *)0, (ub4)OCI_DYNAMIC_FETCH))
    {
        printf("FAILED: OCIDefineByPos2()\n");
        report_error(errhp);
        return OCI_ERROR;
    }

    if (OCIDefineArrayOfStruct(bndhp[0], errhp, sizeof(int),
        0, 0, 0))
    {
        printf("FAILED: OCIDefineArrayOfStruct0()\n");
        report_error(errhp);
        return OCI_ERROR;
    }
    if (OCIDefineArrayOfStruct(bndhp[1], errhp, CHAR_SIZE,
        0, 0, 0))
    {
        printf("FAILED: OCIDefineArrayOfStruct2()\n");
        report_error(errhp);
        return OCI_ERROR;
    }
    if (OCIDefineArrayOfStruct(bndhp[2], errhp, CHAR_SIZE,
        0, 0, 0))
    {
        printf("FAILED: OCIDefineArrayOfStruct2()\n");
        report_error(errhp);
        return OCI_ERROR;
    }

    #define PIECE_SIZE 10
    char buf1[100];
    ub4   alen  = PIECE_SIZE;
    ub1   piece = OCI_FIRST_PIECE;
    dvoid *hdlptr = (dvoid *) 0;
    ub4 hdltype = OCI_HTYPE_DEFINE, iter = 0, idx = 0;
    ub1   in_out = 0;
    sb2   indptr = 0;
    ub2   rcode = 0;
    //测试获取 SQL 生成的结果集中的行集
    int status = OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, 0);
    while (status == OCI_NEED_DATA)
    {
        if(OCIStmtGetPieceInfo(stmthp, errhp, &hdlptr, &hdltype, &in_out, &iter, &idx, &piece))
            printf("FAILED: OCIStmtGetPieceInfo()\n");

        //printf("bndhp[1]=%p, bndhp[2]=%p, hdlptr=%p\n", bndhp[1], bndhp[2], hdlptr);
        if(OCIStmtSetPieceInfo((dvoid *)hdlptr, (ub4)hdltype,
                                  errhp, (dvoid *) &buf1, &alen, piece,
                                  (dvoid *)&indptr, &rcode))
            printf("FAILED: OCIStmtGetPieceInfo()\n");

        memset(buf1, 0, alen);
        status = OCIStmtFetch(stmthp,errhp, (ub4) 1,
                          (ub2) OCI_FETCH_NEXT, (ub4) OCI_DEFAULT);

        printf("buf1:%s , %d\n", buf1, alen);
    }
    //总共返的记录的行数
    OCIAttrGet(stmthp, OCI_HTYPE_STMT, &stmrow, 0, OCI_ATTR_ROW_COUNT, errhp);
    printf("stmrow = %d\n", stmrow);