/*----------------------------------------------------------------- * * testlo.c-- * LIBPQ로 큰 물체를 사용하여 테스트하십시오 * * 저작권 (C) 1994, 캘리포니아 대학교 리전트 * * * 식별 * /usr/local/devel/pglite/cvs/src/doc/manual.me,v 1.16 1995/09/01 23:55:00 Jolly Exp * *------------------------------------------------------------------------ */ #include <stdio.h #include "libpq-fe.h" #include "libpq/libpq-fs.h" #define bufsize 1024 /* * importFile * "in_filename"import 파일 "in_filename"은 큰 개체로 데이터베이스로 "LOB로" 조이드 " * */ OID importfile (pg토토 핫 *토토 핫, char *filename) Oid lobjid; int lobj_fd; char buf [bufsize]; int nbytes, TMP; int fd; /* * 읽을 파일을 엽니 다 */ fd = Open (filename, o_rdonly, 0666); if (fd <0) / * 오류 * / fprintf (stderr, "Unix 파일을 열 수 없습니다. %s \ n", filename); /* * 큰 물체를 만듭니다 */ lobjid = lo_creat (토토 핫, inv_read | inv_write); if (lobjid == 0) fprintf (stderr, "큰 객체를 만들 수 없습니다 \ n"); lobj_fd = lo_open (토토 핫, lobjid, inv_write); /* * Unix 파일에서 읽고 반전 파일에 쓰기 */ while ((nbytes = read (fd, buf, bufsize)) 0) tmp = lo_write (토토 핫, lobj_fd, buf, nbytes); if (tmp <nbytes) fprintf (stderr, "큰 객체를 읽는 동안 오류 \ n"); (void) 닫기 (fd); (void) lo_close (토토 핫, lobj_fd); 리턴 로지드; 무효의 픽 아웃 (PG토토 핫 *토토 핫, OID LOBJID, Int Start, Int Len) int lobj_fd; char *buf; int nbytes; int nread; lobj_fd = lo_open (토토 핫, lobjid, inv_read); if (lobj_fd <0) fprintf (stderr, "큰 물체 %d \ n을 열 수 없습니다", lobjid); lo_lseek (토토 핫, lobj_fd, start, seek_set); buf = malloc (len + 1); nread = 0; while (len -nread 0) nbytes = lo_read (토토 핫, lobj_fd, buf, len -nread); buf [nbytes] = ''; fprintf (stderr, " %s", buf); nread += nbytes; 무료 (buf); fprintf (stderr, "\ n"); lo_close (토토 핫, lobj_fd); 무효의 덮어 쓰기 (pg토토 핫 *토토 핫, oid lobjid, int start, int len) int lobj_fd; char *buf; int nbytes; int nwritten; int i; lobj_fd = lo_open (토토 핫, lobjid, inv_read); if (lobj_fd <0) fprintf (stderr, "큰 물체 %d \ n을 열 수 없습니다", lobjid); lo_lseek (토토 핫, lobj_fd, start, seek_set); buf = malloc (len + 1); for (i = 0; i <len; i ++) buf [i] = 'x'; buf [i] = ''; nwrretten = 0; while (len -nwritten 0) nbytes = lo_write (토토 핫, lobj_fd, buf + nwritten, len -nwritten); nwrretten += nbytes; 무료 (buf); fprintf (stderr, "\ n"); lo_close (토토 핫, lobj_fd); /* * ExportFile * "out_filename"을 제출하려면 큰 개체 "lobjoid"를 내보내기 * */ 무효의 ExportFile (PG토토 핫 *토토 핫, OID LOBJID, Char *Filename) int lobj_fd; char buf [bufsize]; int nbytes, TMP; int fd; /* * 반전 "객체"를 만듭니다. */ lobj_fd = lo_open (토토 핫, lobjid, inv_read); if (lobj_fd <0) fprintf (stderr, "큰 물체 %d \ n을 열 수 없습니다", lobjid); /* * 작성할 파일을 엽니 다 */ fd = Open (filename, o_creat | o_wronly, 0666); if (fd <0) / * 오류 * / fprintf (stderr, "unix 파일을 열 수 없음", filename); /* * Unix 파일에서 읽고 반전 파일에 쓰기 */ while ((nbytes = lo_read (토토 핫, lobj_fd, buf, bufsize)) 0) tmp = 쓰기 (fd, buf, nbytes); if (tmp <nbytes) fprintf (stderr, " %s \ n을 쓰는 동안 오류", filename); (void) lo_close (토토 핫, lobj_fd); (void) 닫기 (fd); 반품; 무효의 exit_nicely (pg토토 핫 *토토 핫) pqfinish (토토 핫); 출구 (1); int 메인 (Int Argc, char ** argv) char *in_filename, *out_filename; char *데이터베이스; Oid lobjoid; pg토토 핫 *토토 핫; pgresult *res; if (argc! = 4) fprintf (stderr, "usage : %s database_name in_filename out_filename \ n", argv [0]); 출구 (1); 데이터베이스 = argv [1]; in_filename = argv [2]; out_filename = argv [3]; /* * 연결을 설정하십시오 */ 토토 핫 = pqsetdb (null, null, null, null, database); / * 백엔드 연결이 성공적으로 만들어 졌는지 확인하십시오 */ if (pqstatus (토토 핫) == 토토 핫ection_bad) fprintf (stderr, "데이터베이스 '%s'에 대한 연결 실패. \ n", 데이터베이스); fprintf (stderr, "%s", pqerrormessage (토토 핫)); exit_nicely (토토 핫); RES = PQEXEC (토토 핫, "시작"); PQCLEAR (RES); printf ( "파일 가져 오기 %s \ n", in_filename); /* lobjoid = importFile (토토 핫, in_filename); */ lobjoid = lo_import (토토 핫, in_filename); /* printf ( "큰 객체 d. \ n", lobjoid); printf ( "큰 객체의 1000-2000 바이트를 따기 \ n"); 픽 아웃 (토토 핫, Lobjoid, 1000, 1000); printf ( "x 's \ n"을 가진 큰 물체의 1000-2000의 초과 쓰기 바이트); 덮어 쓰기 (토토 핫, Lobjoid, 1000, 1000); */ printf ( "큰 객체를 파일 s \ n으로 내보내는 것", out_filename); /* ExportFile (토토 핫, lobjoid, out_filename); */ lo_export (토토 핫, lobjoid, out_filename); res = pqexec (토토 핫, "end"); PQCLEAR (RES); pqfinish (토토 핫); 출구 (0);