?? cdemodp.c
字號:
#ifdef RCSIDstatic char *RCSid = "$Header: /home/cvs/cvsroot/TIBS_HOME/src/liboci/cdemodp.c,v 1.1.1.1 2006/05/21 19:27:38 cvs Exp $ ";#endif /* RCSID *//*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*//* Copyright (c) 1998, 2001, Oracle Corporation. All rights reserved. *//* *//*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*//*** NAME:** cdemodp.c - C Demo program for Direct Path api****** DESCRIPTION:** - Direct Path Api driver program to demonstrate loading.**** NOTES:** Demonstrates usage of the direct path API.**** -- cdemodp.c --** This is one of two C files needed to create a demo that loads** data through direct path api.**** To build and run the demo, please read directions located in** the header section of the cdemdp*.c modules.******** MODIFIED (MM/DD/YY)** msakayed 11/02/01 - Bug #2094292: add/set OCI_ATTR_DIRPATH_INPUT** eegolf 03/04/01 - Updated for 9i** cmlim 09/16/98 - Creation (abrumm 04/07/98)*/#include <sys/types.h>#include <sys/stat.h>#include <ctype.h>#include <fcntl.h>#include <assert.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <oratypes.h>#include <oci.h>#include <cdemodp0.h>#include <cdemodp.h>#ifndef bit# define bit(x, y) ((x) & (y))#endif#ifndef OER# define OER(x) (x)#endifstruct loadctl{ ub4 nrow_ctl; /* number of rows in column array */ ub2 ncol_ctl; /* number of columns in column array */ OCIEnv *envhp_ctl; /* environment handle */ OCIServer *srvhp_ctl; /* server handle */ OCIError *errhp_ctl; /* error handle */ OCIError *errhp2_ctl; /* yet another error handle */ OCISvcCtx *svchp_ctl; /* service context */ OCISession *authp_ctl; /* authentication context */ OCIParam *colLstDesc_ctl; /* column list parameter handle */ OCIDirPathCtx *dpctx_ctl; /* direct path context */ OCIDirPathColArray *dpca_ctl; /* direct path column array handle */ OCIDirPathColArray *dpobjca_ctl; /* dp column array handle for obj*/ OCIDirPathColArray *dpnestedobjca_ctl; /* dp col array hndl for nested obj*/ OCIDirPathStream *dpstr_ctl; /* direct path stream handle */ ub1 *buf_ctl; /* pre-alloc'd buffer for out-of-line data */ ub4 bufsz_ctl; /* size of buf_ctl in bytes */ ub4 bufoff_ctl; /* offset into buf_ctl */ ub4 *otor_ctl; /* Offset to Recnum mapping */ ub1 *inbuf_ctl; /* buffer for input records */ struct pctx pctx_ctl; /* partial field context */ boolean loadobjcol_ctl; /* load to obj col(s)? T/F */};/* Forward references: */STATICF void field_flush(/*_ struct loadctl *ctlp, ub4 rowoff _*/);STATICF sword field_set(/*_ struct loadctl *ctlp, struct tbl *tblp, struct obj *objp, text *recp, ub4 rowoff, ub1 bufflg _*/);STATICF void init_obj_load(/*_ struct loadctl *ctlp, struct tbl *tblp, struct obj *objp _*/);STATICF void alloc_obj_ca(/*_ struct loadctl *ctlp, struct tbl *tblp, struct obj *objp _*/);STATICF void init_load(/*_ struct loadctl *ctl, struct tbl *table, struct sess *session _*/);STATICF void simple_load(/*_ struct loadctl *ctlp, struct tbl *tblp, struct sess *session, FILE *inputfp _*/);STATICF void finish_load(/*_ struct loadctl *ctl _*/);STATICF void errprint(/*_ dvoid *errhp, ub4 htype, sb4 *errcodep _*/);STATICF void checkerr(/*_ dvoid *errhp, ub4 htype, sword status, text *note, sb4 state, text *file, sb4 line _*/);STATICF void cleanup(/*_ struct loadctl *ctlp, sb4 ex_status _*/);STATICF sword do_convert(/*_ struct loadctl *ctlp, ub4 startoff, ub4 rowcnt, ub4 *cvtCntp, ub2 *badcoffp _*/);STATICF sword do_load(/*_ struct loadctl *ctlp, ub4 *loadCntp _*/);STATICF int main(/*_ int argc, char *argv[] _*/);STATICF void free_obj_hndls(struct loadctl *ctlp, struct obj *objp);/* OCI_CHECK(errhp, ub4 errhptype, sb4 status, struct loadctl *ctlp, * OCIfunction()); * errhp is typically a (OCIError *), and errhptype is OCI_HTYPE_ERROR. * errhp in some cases may be an (OCIEnv *), and errhptype is OCI_HTYPE_ENV. */#define OCI_CHECK(errhp, htype, status, ctlp, OCIfunc) \if (OCI_SUCCESS != ((status) = (OCIfunc))) \{ \ checkerr((dvoid *)(errhp), (ub4)(htype), (sword)(status), (text *)0, \ (sb4)0, (text *)__FILE__, (sb4)__LINE__); \ if ((status) != OCI_SUCCESS_WITH_INFO) \ cleanup((struct loadctl *)ctlp, (sb4)1); \} else#define CHECKERR(errhp, htype, status) \ checkerr((dvoid *)errhp, (ub4)(htype), (sword)(status), (text *)0, \ (sb4)0, (text *)__FILE__, (sb4)__LINE__);#define FATAL(note, state) \do \{ \ checkerr((dvoid *)0, (ub4)OCI_HTYPE_ERROR, (sword)OCI_SUCCESS, \ (text *)(note), (sb4)(state), (text *)__FILE__, (sb4)__LINE__); \ cleanup((ctlp), (sb4)2); \} while (0)/* External references: */externref struct tbl table;externref struct sess session;/* External definitions: */externdef FILE *output_fp; /* for error msgs *//*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*//* main *//* *//*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/int main(argc, argv)int argc;char *argv[];{ sword ociret; struct loadctl ctl; struct loadctl *ctlp = &ctl; output_fp = (session.outfn_sess) ? fopen((char *)session.outfn_sess, "w") : stderr; memset((dvoid *)ctlp, 0, sizeof(struct loadctl)); /* set up OCI environment and connect to the ORACLE server */ OCI_CHECK((dvoid *)0, (ub4)0, ociret, ctlp, OCIInitialize((ub4) OCI_DEFAULT, (dvoid *)0, (dvoid * (*)(dvoid *, size_t)) 0, (dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *)) 0 )); OCI_CHECK(ctlp->envhp_ctl, OCI_HTYPE_ENV, ociret, ctlp, OCIEnvInit((OCIEnv **)&ctlp->envhp_ctl, OCI_DEFAULT, (size_t)0, (dvoid **)0)); /* allocate error handles */ OCI_CHECK(ctlp->envhp_ctl, OCI_HTYPE_ENV, ociret, ctlp, OCIHandleAlloc((dvoid *)ctlp->envhp_ctl, (dvoid **)&ctlp->errhp_ctl, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0)); OCI_CHECK(ctlp->envhp_ctl, OCI_HTYPE_ENV, ociret, ctlp, OCIHandleAlloc((dvoid *)ctlp->envhp_ctl, (dvoid **)&ctlp->errhp2_ctl, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0)); /* server contexts */ OCI_CHECK(ctlp->envhp_ctl, OCI_HTYPE_ENV, ociret, ctlp, OCIHandleAlloc((dvoid *)ctlp->envhp_ctl, (dvoid **)&ctlp->srvhp_ctl, OCI_HTYPE_SERVER, (size_t)0, (dvoid **)0)); OCI_CHECK(ctlp->envhp_ctl, OCI_HTYPE_ENV, ociret, ctlp, OCIHandleAlloc((dvoid *)ctlp->envhp_ctl, (dvoid **)&ctlp->svchp_ctl, OCI_HTYPE_SVCCTX, (size_t)0, (dvoid **)0)); OCI_CHECK(ctlp->errhp_ctl, OCI_HTYPE_ERROR, ociret, ctlp, OCIServerAttach(ctlp->srvhp_ctl, ctlp->errhp_ctl, session.inst_sess, (sb4)strlen((const char *)session.inst_sess), OCI_DEFAULT)); /* set attribute server context in the service context */ OCI_CHECK(ctlp->errhp_ctl, OCI_HTYPE_ERROR, ociret, ctlp, OCIAttrSet((dvoid *)ctlp->svchp_ctl, OCI_HTYPE_SVCCTX, (dvoid *)ctlp->srvhp_ctl, (ub4)0, OCI_ATTR_SERVER, ctlp->errhp_ctl)); OCI_CHECK(ctlp->envhp_ctl, OCI_HTYPE_ENV, ociret, ctlp, OCIHandleAlloc((dvoid *)ctlp->envhp_ctl, (dvoid **)&ctlp->authp_ctl, (ub4)OCI_HTYPE_SESSION, (size_t)0, (dvoid **)0)); OCI_CHECK(ctlp->errhp_ctl, OCI_HTYPE_ERROR, ociret, ctlp, OCIAttrSet((dvoid *)ctlp->authp_ctl, (ub4)OCI_HTYPE_SESSION, (dvoid *)session.username_sess, (ub4)strlen((char *)session.username_sess), (ub4)OCI_ATTR_USERNAME, ctlp->errhp_ctl)); OCI_CHECK(ctlp->errhp_ctl, OCI_HTYPE_ERROR, ociret, ctlp, OCIAttrSet((dvoid *)ctlp->authp_ctl, (ub4)OCI_HTYPE_SESSION, (dvoid *)session.password_sess, (ub4)strlen((char *)session.password_sess), (ub4)OCI_ATTR_PASSWORD, ctlp->errhp_ctl)); /* begin a session */ OCI_CHECK(ctlp->errhp_ctl, OCI_HTYPE_ERROR, ociret, ctlp, OCISessionBegin(ctlp->svchp_ctl, ctlp->errhp_ctl, ctlp->authp_ctl, OCI_CRED_RDBMS, (ub4)OCI_DEFAULT)); /* set authentication context into service context */ OCI_CHECK(ctlp->errhp_ctl, OCI_HTYPE_ERROR, ociret, ctlp, OCIAttrSet((dvoid *)ctlp->svchp_ctl, (ub4)OCI_HTYPE_SVCCTX, (dvoid *)ctlp->authp_ctl, (ub4)0, (ub4)OCI_ATTR_SESSION, ctlp->errhp_ctl)); init_load(ctlp, &table, &session); /* initialize the load */ simple_load(ctlp, &table, &session, stdin); /* load data */ finish_load(ctlp); /* finish the load */ cleanup(ctlp, (sb4)0); /* NOTREACHED */return 1;}/***++++++++++++++++++++++++++++++ alloc_obj_ca +++++++++++++++++++++++++++++++++**** Description:**** Function allocates the column arrays for any objects or nested object columns.**** Assumptions:**** Parameters:**** ctlp load control structure pointer ** tblp table pointer ** objp object pointer **** Returns:****-------------------------------------------------------------------------*/STATICF void alloc_obj_ca(ctlp, tblp, objp)struct loadctl *ctlp; /* load control structure pointer */struct tbl *tblp; /* table pointer */ struct obj *objp; /* object pointer */{ struct col *colp; sword ociret; /* return code from OCI calls*/ ub2 i; /* * Allocate a separate column array for the column object */ OCI_CHECK(ctlp->envhp_ctl, OCI_HTYPE_ENV, ociret, ctlp, OCIHandleAlloc((dvoid *)(objp->ctx_obj), (dvoid **)&(objp->ca_obj), (ub4)OCI_HTYPE_DIRPATH_FN_COL_ARRAY, (size_t)0, (dvoid **)0)); /* get number of rows in the column array just allocated */ OCI_CHECK(ctlp->errhp_ctl, OCI_HTYPE_ERROR, ociret, ctlp, OCIAttrGet((CONST dvoid *)(objp->ca_obj), OCI_HTYPE_DIRPATH_FN_COL_ARRAY, (dvoid *)(&objp->nrows_obj), (ub4 *)0, OCI_ATTR_NUM_ROWS, ctlp->errhp_ctl)); /* get number of columns in the column array just allocated */ OCI_CHECK(ctlp->errhp_ctl, OCI_HTYPE_ERROR, ociret, ctlp, OCIAttrGet((CONST dvoid *)(objp->ca_obj), OCI_HTYPE_DIRPATH_FN_COL_ARRAY, (dvoid *)(&objp->ncol_obj), (ub4 *)0, OCI_ATTR_NUM_COLS, ctlp->errhp_ctl)); /* * If there are fewer rows in the object column array than in them top-level, * one, only use as many rows in the other column array. This will happen * when the object requires more space than all of the other columns inits * parent table. This simplifies the loop for loading the column arrays * so that we only have to worry about when we've filled the top-level * column array. */ if (objp->nrows_obj < ctlp->nrow_ctl) { ctlp->nrow_ctl = objp->nrows_obj; } /* check each column to see if it is an object, opaque or ref */ /* and if so, recurse */ for (i = 0, colp = objp->col_obj; i < objp->ncol_obj; i++, colp++) { if (colp->exttyp_col == SQLT_NTY || colp->exttyp_col == SQLT_REF) { alloc_obj_ca(ctlp, tblp, colp->obj_col); } }}/***++++++++++++++++++++++++++++++ init_obj_load +++++++++++++++++++++++++++++++++**** Description:**** Function which prepares the load of an object column. This should only** be called from init_load or recursively.**** Assumptions:**** Parameters:**** ctlp load control structure pointer ** tblp table pointer ** objp object pointer **** Returns:****-------------------------------------------------------------------------*/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -