pro*c에서 blob 컬럼에 있는 텍스트 본문 뽑는 예제입니다.
os : linux redhat 6.2
oracle : Oracle 8i 8.1.6
소스 가지고 열나게 고민할때는 안되더니 makefile 공부하고 나서 좀 고치니 간단히 수행되네요. 쩝. 역시 기본기없이 어설프게 접근하면 안되지요. 아뭏든 이것이 되니 기쁩니당~~
make -f Makefile.ora
# cat Makefile.ora
CC=gcc
TARGET = cgi_ora.c
ETC= $(TARGET)
ETCSRC= $(ETC:.o=.c)
OBJECTS= $(NPROGOBJS)
CSOURCES= $(OBJECTS:.o=.c)
PCSOURCES= $(OBJECTS:.o=.pc)
CFLAGS=-I$(ORACLE_HOME)/include
LDFLAGS = -L$(ORACLE_HOME)/lib
CFLAGS= -g
OTHERS=makefile
PRODUCTS=view.cgi
all : .pc.c view.cgi myrm
view.cgi :$(NPROGOBJS) $(ETC)
$(CC) -o $@ $(CFLAGS) $(NPROGOBJS) $(ETC) $(LDFLAGS) $(PROLDLIBS)
touch:
touch $(PCSOURCES) $(ETCSRC)
myrm :
/bin/rm tp* *.lis
copy :
cp $(PRODUCTS) /user/local/apache/cgi-bin
include $(ORACLE_HOME)/precomp/demo/proc/demo_proc.mk
.pc.c:
make -f $(ORACLE_HOME)/precomp/demo/proc/demo_proc.mk PCCSRC=cgi_ora.pc
I_SYM=include= pc1
.c.o:
$(CC) $(CFLAGS) -c $*.c
$ cat cgi_ora.pc <소스보기>
#include <oci.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlca.h>
#include <sqlcpr.h>
/* Jaedy */
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#define NON_EXISTENT -942
#define NOT_FOUND 1403
#define MAX_BUF 1000000
#define MAX_ERR_BUF 4096
void write_file(char *, OCIBlobLocator *);
void page_view(char *, char*, char*, char*, char*);
void MakeFile(char *, char*, char*);
EXEC SQL INCLUDE sqlca;
typedef struct long_varraw {
ub4 len;
text buf[1];
} long_varraw;
/* Type Equivalence long_varraw to LONG VARRAW.
* All variables of type long_varraw from this point
* on in the file will have external type 95 (LONG VARRAW)
* associated with them.
*/
EXEC SQL TYPE long_varraw IS LONG VARRAW REFERENCE;
unsigned char buff[MAX_BUF];
EXEC SQL VAR buff IS RAW(MAX_BUF);
void DB_error(char* routine){
char message_buffer[MAX_ERR_BUF];
size_t buffer_size;
size_t message_length;
EXEC SQL whenever sqlerror continue;
fprintf(stderr, "Oracle error while executing %s!\n", routine);
buffer_size = sizeof(message_buffer);
sqlglm(message_buffer, &buffer_size, &message_length);
printf("%.*s\n", message_length, message_buffer);
EXEC SQL ROLLBACK WORK RELEASE;
exit(1);
}
void DB_connect(szUid)
EXEC SQL BEGIN DECLARE SECTION;
char szUid[100];
EXEC SQL END DECLARE SECTION;
{
EXEC SQL whenever sqlerror do DB_error("connect");
EXEC SQL connect :szUid;
if(sqlca.sqlcode != 0)
DB_error("Connecting...DB");
}
void main()
{
OCIBlobLocator *blob;
unsigned int amt, offset=1;
char szDate[30];
char YYYY[30];
char MM[30];
char szField[300];
char szContent[1000];
char ImgPath[1024];
int queryid = 0;
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR sqlstmt[1024];
int docid;
VARCHAR majordesc[128];
VARCHAR minordesc[128];
VARCHAR paperdate[16];
VARCHAR title[1025];
int minorid;
int isimage;
EXEC SQL END DECLARE SECTION;
DB_connect("test/test@db");
EXEC SQL ALLOCATE :blob;
EXEC SQL SELECT content INTO :blob FROM tdoc WHERE id = 692914692;
if(sqlca.sqlcode == NOT_FOUND)
DB_error("No data Found(Blob error)!!\n");
amt = MAX_BUF;
EXEC SQL LOB READ :amt FROM :blob AT :offset INTO :buff;
printf("%s\n",buff);
/*
page_view(ImgPath, szField , szDate, title.arr, lftobr(buff));
*/
EXEC SQL FREE :blob;
}
|