📄 cdemodr2.c
字号:
return OCI_ERROR;
/* delete rows
between 201 and 202; -- expecting 2 rows returned.
between 204 and 206; -- expecting 3 rows returned.
between 208 and 209; -- expecting 2 rows returned.
*/
lowc1[0] = 201;
highc1[0] = 202;
lowc1[1] = 204;
highc1[1] = 206;
lowc1[2] = 208;
highc1[2] = 209;
for (i=0; i<MAXITER; i++)
rowsret[i] = 0;
if ((retval = OCIStmtExecute(svchp, stmthp, errhp, (ub4) range_size, (ub4) 0,
(CONST OCISnapshot*) 0, (OCISnapshot*) 0,
(ub4) OCI_DEFAULT)) != OCI_SUCCESS &&
retval != OCI_SUCCESS_WITH_INFO)
{
(void) printf("FAILED: OCIStmtExecute() delete, retval = %d\n", retval);
report_error(errhp);
return OCI_ERROR;
}
(void) check_lob(range_size, svchp);
return OCI_SUCCESS;
}
/* ----------------------------------------------------------------- */
/* Intbind callback that does not do any data input. */
/* ----------------------------------------------------------------- */
sb4 cbf_no_data(ctxp, bindp, iter, index, bufpp, alenpp, piecep, indpp)
dvoid *ctxp;
OCIBind *bindp;
ub4 iter, index;
dvoid **bufpp;
ub4 *alenpp;
ub1 *piecep;
dvoid **indpp;
{
*bufpp = (dvoid *) 0;
*alenpp = 0;
null_ind = -1;
*indpp = (dvoid *) &null_ind;
*piecep = OCI_ONE_PIECE;
return OCI_CONTINUE;
}
/* ----------------------------------------------------------------- */
/* Outbind callback for returning data. */
/* ----------------------------------------------------------------- */
sb4 cbf_get_data(ctxp, bindp, iter, index, bufpp, alenp, piecep, indpp,
rcodepp)
dvoid *ctxp;
OCIBind *bindp;
ub4 iter, index;
dvoid **bufpp;
ub4 **alenp;
ub1 *piecep;
dvoid **indpp;
ub2 **rcodepp;
{
static ub4 bufl = 0;
static ub4 rows = 0;
ub4 pos = *((ub4 *)ctxp);
if (index == 0)
{
(void) OCIAttrGet((CONST dvoid *) bindp, OCI_HTYPE_BIND, (dvoid *)&rows,
(ub4 *)sizeof(ub2), OCI_ATTR_ROWS_RETURNED, errhp);
rowsret[iter] = (ub2)rows;
if (alloc_buffer(pos, iter, rows))
return OCI_ERROR;
}
switch(pos)
{
case 0:
rl[pos][iter][index] = sizeof(int);
*bufpp = (dvoid *) (p1[iter]+index);
break;
case 1:
rl[pos][iter][index] = (ub4) 86;
*bufpp = (dvoid *) p2[iter][index];
break;
case 2:
rl[pos][iter][index] = (ub4) 86;
*bufpp = (dvoid *) p3[iter][index];
break;
default:
bufl = 0;
*bufpp = (dvoid *) 0;
*alenp = (ub4 *) 0;
(void) printf("ERROR: invalid position number: %d\n", *((ub2 *)ctxp));
}
*piecep = OCI_ONE_PIECE;
ind[pos][iter][index] = 0;
*indpp = (dvoid *) &ind[pos][iter][index];
rc[pos][iter][index] = 0;
*rcodepp = &rc[pos][iter][index];
*alenp = &rl[pos][iter][index];
return OCI_CONTINUE;
}
/* ----------------------------------------------------------------- */
/* allocate buffers for callback. */
/* ----------------------------------------------------------------- */
sword alloc_buffer(pos, iter, rows)
ub4 pos, iter, rows;
{
ub4 i;
switch(pos)
{
case 0:
p1[iter] = (int *) malloc(sizeof(int) * rows);
break;
case 1:
p2[iter] = (OCILobLocator **) malloc(rows * sizeof(OCILobLocator *));
if (locator_alloc(p2[iter], rows))
return OCI_ERROR;
break;
case 2:
p3[iter] = (OCILobLocator **) malloc(rows * sizeof(OCILobLocator *));
if (locator_alloc(p3[iter], rows))
return OCI_ERROR;
break;
default:
(void) printf("ERROR: invalid position number: %d\n", pos);
return OCI_ERROR;
}
ind[pos][iter] = (short *) malloc(rows * sizeof(short));
rc[pos][iter] = (ub2 *) malloc(rows * sizeof(ub2));
rl[pos][iter] = (ub4 *) malloc(rows * sizeof(ub4));
return OCI_SUCCESS;
}
/* ----------------------------------------------------------------- */
/* allocate locators. */
/* ----------------------------------------------------------------- */
sword locator_alloc(lob, rows)
OCILobLocator *lob[];
ub4 rows;
{
ub4 i;
for (i = 0; i < rows; i++)
{
if (OCIDescriptorAlloc((dvoid *) envhp, (dvoid **) &lob[i],
(ub4) OCI_DTYPE_LOB, (size_t) 0, (dvoid **) 0))
{
(void) printf("FAILED: OCIDescriptorAlloc()\n");
return OCI_ERROR;
}
}
return OCI_SUCCESS;
}
/* ----------------------------------------------------------------- */
/* free locators. */
/* ----------------------------------------------------------------- */
sword locator_free(lob, rows)
OCILobLocator *lob[];
ub4 rows;
{
ub4 i;
for (i = 0; i < rows; i++)
{
if (OCIDescriptorFree((dvoid *) lob[i], (ub4) OCI_DTYPE_LOB))
{
(void) printf("FAILED: OCIDescriptorFree()\n");
return OCI_ERROR;
}
}
return OCI_SUCCESS;
}
/* ----------------------------------------------------------------- */
/* select BLOB, CLOB between rows 201 and 210 and use them to */
/* update the rows between 101 and 110. */
/* ----------------------------------------------------------------- */
sword select_locator(svchp, stmthp, bndhp, errhp)
OCISvcCtx *svchp;
OCIStmt *stmthp;
OCIBind *bndhp[];
OCIError *errhp;
{
OCIDefine *dfnhp[3];
text *sqlstmt = (text *)
"SELECT C2, C3 FROM TAB2 WHERE C1 BETWEEN 201 AND 210";
if (locator_alloc(in2, MAXITER)
|| locator_alloc(in3, MAXITER))
{
(void) printf("FAILED: locator_alloc()\n");
return OCI_ERROR;
}
if (OCIStmtPrepare(stmthp, errhp, sqlstmt, (ub4)strlen((char *)sqlstmt),
(ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT))
{
(void) printf("FAILED: OCIStmtPrepare() select\n");
report_error(errhp);
return OCI_ERROR;
}
dfnhp[0] = (OCIDefine *) 0;
dfnhp[1] = (OCIDefine *) 0;
dfnhp[2] = (OCIDefine *) 0;
if (OCIDefineByPos(stmthp, &dfnhp[0], errhp, (ub4) 1,
(dvoid *) &in2[0], (sb4) -1, (ub2) SQLT_BLOB,
(dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) OCI_DEFAULT)
|| OCIDefineByPos(stmthp, &dfnhp[1], errhp, (ub4) 2,
(dvoid *) &in3[0], (sb4) -1, (ub2) SQLT_CLOB,
(dvoid *) 0, (ub2 *) 0, (ub2 *) 0, (ub4) OCI_DEFAULT))
{
(void) printf("FAILED: OCIDefineByPos()\n");
report_error(errhp);
return OCI_ERROR;
}
if (OCIDefineArrayOfStruct(dfnhp[0], errhp, sizeof(in2[0]),
indsk[0], rlsk[0], rcsk[0])
|| OCIDefineArrayOfStruct(dfnhp[1], errhp, sizeof(in3[0]),
indsk[1], rlsk[1], rcsk[1]))
{
(void) printf("FAILED: OCIDefineArrayOfStruct()\n");
report_error(errhp);
return OCI_ERROR;
}
if (OCIStmtExecute(svchp, stmthp, errhp, (ub4) MAXITER, (ub4) 0,
(CONST OCISnapshot*) 0, (OCISnapshot*) 0,
(ub4) OCI_DEFAULT))
{
(void) printf("FAILED: OCIStmtExecute() select\n");
report_error(errhp);
return OCI_ERROR;
}
return OCI_SUCCESS;
}
/* ----------------------------------------------------------------- */
/* check the lob returned with read/write. */
/* ----------------------------------------------------------------- */
sword check_lob(iters, svchp)
int iters;
OCISvcCtx *svchp;
{
int i, j;
for (i = 0; i < iters; i++)
{
(void) printf("\n*** ITERATION *** : %d\n", i);
(void) printf("(...returning %d rows)\n", rowsret[i]);
for (j = 0; j < rowsret[i] ; j++)
{
/* Column 1 */
(void) printf("COL1 [%d]: ind = %d, rc = %d, retl = %d\n",
j, ind[0][i][j], rc[0][i][j], rl[0][i][j]);
if (ind[0][i][j] == -1)
(void) printf("COL1 [%d]: NULL\n", j);
else
(void) printf("COL1 [%d]: %d\n", j, *(p1[i]+j) );
/* Column 2, BLOB */
(void) printf("COL2 [%d]: ind = %d, rc = %d, retl = %d\n",
j, ind[1][i][j], rc[1][i][j], rl[1][i][j]);
if (ind[1][i][j] == -1)
(void) printf("COL2 [%d]: NULL\n", j);
else
{
(void) printf("COL2 [%d]: \n", j);
(void) read_piece(svchp, p2[i][j], SQLT_BLOB);
(void) write_piece(svchp, p2[i][j], SQLT_BLOB);
}
/* Column 3, CLOB */
(void) printf("COL3 [%d]: ind = %d, rc = %d, retl = %d\n",
j, ind[2][i][j], rc[2][i][j], rl[2][i][j]);
if (ind[2][i][j] == -1)
(void) printf("COL3 [%d]: NULL\n", j);
else
{
(void) printf("COL3 [%d]: \n", j);
(void) read_piece(svchp, p3[i][j], SQLT_CLOB);
(void) write_piece(svchp, p3[i][j], SQLT_CLOB);
}
}
(void) locator_free(p2[i], rowsret[i]);
(void) locator_free(p3[i], rowsret[i]);
}
return OCI_SUCCESS;
}
/* ----------------------------------------------------------------- */
/* read a piece of the lob/file. */
/* ----------------------------------------------------------------- */
sword read_piece(svchp, lob, lobtype)
OCISvcCtx *svchp;
OCILobLocator *lob;
ub2 lobtype;
{
ub1 buf[MAXBUFLEN];
ub4 amt = MAXBUFLEN;
ub4 offset = 1;
memset((void *)buf, (int) '\0', (size_t) MAXBUFLEN);
if (OCILobRead(svchp, errhp, lob, &amt, offset, (dvoid *) buf,
(ub4) MAXBUFLEN, (dvoid *)0,
(sb4 (*) (dvoid *, CONST dvoid *, ub4, ub1)) 0,
(ub2) 0, (ub1) SQLCS_IMPLICIT))
{
(void) printf("FAILED: OCILobRead()\n");
report_error(errhp);
return OCI_ERROR;
}
if (lobtype == SQLT_CLOB)
(void) printf("READ buf:%.*s\n", amt, buf);
else
{
(void) printf("READ buf: ");
print_raw(buf, amt);
}
return OCI_SUCCESS;
}
/* ----------------------------------------------------------------- */
/* write a piece of the lob/file. */
/* ----------------------------------------------------------------- */
sword write_piece(svchp, lob, lobtype)
OCISvcCtx *svchp;
OCILobLocator *lob;
ub2 lobtype;
{
ub1 buf[MAXBUFLEN];
ub4 amt = MAXBUFLEN;
ub4 offset = 1;
ub4 i;
if (SQLT_CLOB)
memset((void *)buf, (int) 'A', (size_t) MAXBUFLEN);
else
for (i = 0; i < MAXBUFLEN; i++)
buf[i] = (ub1) (i%0x10);
if (OCILobWrite(svchp, errhp, lob, &amt, offset, (dvoid *) buf,
(ub4) MAXBUFLEN, OCI_ONE_PIECE, (dvoid *)0,
(sb4 (*)(dvoid *, dvoid *, ub4 *, ub1 *)) 0,
(ub2) 0, (ub1) SQLCS_IMPLICIT))
{
report_error(errhp);
(void) printf("FAILED: OCILobWrite()\n");
return OCI_ERROR;
}
return OCI_SUCCESS;
}
/* end of file cdemodr2.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -