<continued 2>
/*-------------------------------------------------------------------------*/
/* Authenticate users and connect to server
*/
static void authenticate_user(ctx)
ldemodef *ctx;
{
COMMENT ("Authentication for scott is progress...");
/* Set the server handle in service handle */
if (OCIAttrSet (ctx->svchp, OCI_HTYPE_SVCCTX, ctx->srvhp, 0,
OCI_ATTR_SERVER, ctx->errhp) != OCI_SUCCESS)
errrpt(ctx, "conn2serv - OCIAttrSet");
/* set the username/password in user handle */
if (OCIAttrSet(ctx->authp, OCI_HTYPE_SESSION, "scott", 5,
OCI_ATTR_USERNAME, ctx->errhp) != OCI_SUCCESS)
errrpt(ctx, "conn2serv - OCIAttrSet");
/* Set the Authentication handle in the service handle */
if (OCIAttrSet(ctx->svchp, OCI_HTYPE_SVCCTX, ctx->authp, 0,
OCI_ATTR_SESSION, ctx->errhp) != OCI_SUCCESS)
errrpt(ctx, "conn2serv - OCIAttrSet");
}
/*
** Format the output error message and obtain error string from Oracle
** given the error code
*/
void errrpt(ctx, op)
ldemodef *ctx;
CONST text *op;
{
text msgbuf[LONGTEXTLENGTH];
sb4 errcode = 0;
if ((fp = fopen (CDEMOLB_TEXT_FILE, "r"))==NULL)
{
COMMENT ("Cannot open file for reading");
exit (EX_FAILURE);
}
offset = 1; /* Offset for Lobs start at 1 */
while (!feof(fp))
{
/* Read the data from file */
memset (buf, '\0', BUFSIZE);
fread(buf, BUFSIZE, 1, fp);
buf[BUFSIZE]='\0';
/* printf("%s",buf); */
/*Write it into the locator */
amtp = BUFSIZE; /* IN/OUT : IN - amount if data to write */
err = OCILobWrite (ctx->svchp, ctx->errhp, lobp, &amtp, offset,
(dvoid *) buf, (ub4) BUFSIZE, OCI_ONE_PIECE,
(dvoid *)0, (sb4 (*)()) 0,
(ub2) 0, (ub1) SQLCS_IMPLICIT);
if (err == OCI_SUCCESS)
{
printf("Written some data...\n");
offset += amtp;
}
else
{
fclose(fp);
errrpt (ctx, (text *) "Write_to_loc : OCILobWrite");
}
}
COMMENT("Write Successful");
/* Length of the Lob */
err = OCILobGetLength(ctx->svchp, ctx->errhp, lobp, &lenp);
if (err != OCI_SUCCESS)
printf (" get lob length fails. err = %d\n\n", err);
else
printf (" Written %d bytes into Locator Successfully.\n\n", lenp);
fclose (fp);
}
/* --------------------------------------------------------------------- */
/* Read data from the lob locator
*/
void Read_from_loc(ctx, lobp)
ldemodef *ctx;
OCILobLocator *lobp;
{
ub1 buf[BUFSIZE+1];
ub4 offset;
ub4 amtp;
ub4 lenp=0;
sb4 err;
COMMENT("Read from the locator.");
/* Length of the Lob */
if (OCILobGetLength(ctx->svchp, ctx->errhp, lobp, &lenp) != OCI_SUCCESS)
errrpt(ctx, "Read_from_loc: OCILobGetLength");
else
{
printf(" Length of Locator is %d\n", lenp);
}
printf ("Enter the offset to read from (starting at 1): ");
scanf("%d", &offset);
printf ("Enter amount to read: ");
scanf("%d", &amtp);
if ((offset + amtp - 1) > lenp)
{
printf ("Error: Trying to get more data than available!\n");
exit (EX_FAILURE);
}
printf(
"\n------------------------------------------------------------------\n");
/* Copyright (c) 1996, 1999, Oracle Corporation. All rights reserved.
*/
/*
NAME
cdemolb.c - C Demo program to illustrate the OCI Lob interface.
DESCRIPTION
This C file contains code to demonstrate the use of the OCI LOB
(Large OBject) interface. It provides a typical example of how
OCI programs can be used to create/insert lob data, and then
access and manipulate (read, write,copy, append, trim) the data.
PUBLIC FUNCTION(S)
<list of external functions declared/defined - with one-line descriptions>
PRIVATE FUNCTION(S)
<list of static functions defined in .c file - with one-line descriptions>
RETURNS
EX_SUCCESS - if the program terminates succesfully.
EX_FAILURE - if it encounters any error.
NOTES
In the beta-1 release only Lob reads and writes (OCILobRead(),
OCILobWrite()) with no callbacks have been demonstrated.
More examples will be added in future releases.
Before executing this program execute the following sql program:
cdemolb.sql
Executing this sql program will delete any earlier tables created
by this demo and create new tables. Each time the demo program runs
successfully it adds row to tables. So it might be a good idea to run
the sql program after several runs of the demo program.
MODIFIED (MM/DD/YY)
mjaeger 07/14/99 - bug 808870: OCCS: convert tabs, no long lines
svedala 10/01/98 - include stdlib.h - bug 714175
svedala 09/09/98 - lines longer than 79 chars reformatted - bug 722491
svedala 02/17/98 - OCI obsoletion changes
cchau 03/03/97 - change functions to long names
azhao 01/31/97 - removed isnull
azhao 01/30/97 - fix lint error
echen 01/03/97 - OCI beautification
pshah 10/11/96 -
kosinski 09/24/96 - Merge Win32 changes into base development
aroy 07/22/96 - Demonstrate the OCI Lob interface.
aroy 07/22/96 - Creation