Ví dụ 35.1là một chươ토토 캔 trình mẫu cho thấy cách giao diện đối tượ토토 캔 lớn tro토토 캔libpqcó thể được sử dụ토토 캔. Các phần của chươ토토 캔 trình được nhận xét như토토 캔 được để lại tro토토 캔 토토 캔uồn cho lợi ích của 토토 캔ười đọc. Chươ토토 캔 trình này cũ토토 캔 có thể được tìm thấy tro토토 캔src/test/exre/testlo.c
Tro토토 캔 phân phối 토토 캔uồn.
Ví dụ 35.1. Các đối tượng lớn vớilibpqChươ토토 캔 trình ví dụ
/*------------------------------------------------------------------------------------------------- * * Testlo.c * Kiểm tra bằng cách sử dụng các đối tượng lớn với libpq * * Phần Bản quyền (c) 1996-2018, Nhóm phát triển toàn cầu PostgreSQL * Phần Bản quyền (c) 1994, Regents of the University of California * * * Nhận dạng * SRC/TEST/Ví dụ/Testlo.C * *--------------------------------------------------------------------------------- */ #include <stdio.h #include <stdlib.h #include <sys/type.h #include <sys/stat.h #include <fcntl.h #include <unistd.h #include "libpq-fe.h" #include "libpq/libpq-fs.h" #Define Bufsize 1024 /* * Nhập khẩu - * Nhập tệp "in_filename" vào cơ sở dữ liệu dưới dạng đối tượng lớn "lobjoid" * */ oid tĩnh Nhập khẩuFile (PGConn *Conn, Char *FileName) Oid lobjid; int lobj_fd; char buf [bufsize]; int nbytes, TMP; int fd; /* * Mở tệp để đọc trong */ fd = open (tên tệp, o_rdonly, 0666); if (fd <0) /* lỗi */ fprintf (stderr, "không thể mở tệp unix \"%s \ "\ n", tên tệp); /* * Tạo đối tượng lớn */ lobjid = lo_creat (Conn, inv_read | inv_write); if (lobjid == 0) fprintf (stderr, "không thể tạo đối tượng lớn"); lobj_fd = lo_open (Conn, lobjid, inv_write); /* * Đọc từ tệp Unix và ghi vào tệp đảo ngược */ while ((nbytes = read (fd, buf, bufsize)) 0) tmp = lo_write (Conn, lobj_fd, buf, nbytes); if (tmp <nbytes) fprintf (stderr, "lỗi trong khi đọc \"%s \ "", tên tệp); đóng (fd); lo_close (Conn, lobj_fd); trả lại lobjid; khoảng trống tĩnh Pickout (PGConn *Conn, Oid lobjid, Int Start, Int Len) int lobj_fd; char *buf; int nbytes; int nread; lobj_fd = lo_open (Conn, lobjid, inv_read); if (lobj_fd <0) fprintf (stderr, "không thể mở đối tượng lớn %u", lobjid); lo_lseek (Conn, lobj_fd, bắt đầu, seek_set); buf = malloc (len + 1); nread = 0; while (len - nread 0) nbytes = lo_read (Conn, lobj_fd, buf, len - nread); buf [nbytes] = '\ 0'; fprintf (stderr, " %s", buf); NREAD += nbytes; if (nbytes <= 0) phá vỡ; /* Không còn dữ liệu nữa? */ miễn phí (BUF); fprintf (stderr, "\ n"); lo_close (Conn, lobj_fd); khoảng trống tĩnh Ghi đè (PGConn *Conn, Oid lobjid, Int Start, Int Len) int lobj_fd; char *buf; int nbytes; int nwritten; int i; lobj_fd = lo_open (Conn, lobjid, inv_write); if (lobj_fd <0) fprintf (stderr, "không thể mở đối tượng lớn %u", lobjid); lo_lseek (Conn, lobj_fd, bắt đầu, seek_set); buf = malloc (len + 1); for (i = 0; i <len; i ++) buf [i] = 'x'; buf [i] = '\ 0'; nwritten = 0; while (Len - nwritten 0) nbytes = lo_write (Conn, lobj_fd, buf + nwritten, len - nwritten); nwritten += nbytes; if (nbytes <= 0) fprintf (stderr, "\ nwrite không thành công! \ n"); phá vỡ; miễn phí (BUF); fprintf (stderr, "\ n"); lo_close (Conn, lobj_fd); /* * xuất khẩu - * Xuất đối tượng lớn "lobjoid" sang tệp "out_filename" * */ khoảng trống tĩnh ExportFile (PGConn *Conn, oid lobjid, char *fileName) int lobj_fd; char buf [bufsize]; int nbytes, TMP; int fd; /* * Mở đối tượng lớn */ lobj_fd = lo_open (Conn, lobjid, inv_read); if (lobj_fd <0) fprintf (stderr, "không thể mở đối tượng lớn %u", lobjid); /* * Mở tệp để được ghi vào */ fd = open (tên tệp, o_creat | o_wronly | o_trunc, 0666); if (fd <0) /* lỗi */ fprintf (stderr, "không thể mở tệp unix \"%s \ "", tên tệp); /* * Đọc từ tệp đảo ngược và ghi vào tệp UNIX */ while ((nbytes = lo_read (Conn, lobj_fd, buf, bufsize)) 0) TMP = Viết (FD, BUF, NBYTES); if (tmp <nbytes) fprintf (stderr, "lỗi trong khi viết \"%s \ "", tên tệp); lo_close (Conn, lobj_fd); đóng (fd); trở lại; khoảng trống tĩnh exit_nicely (pgconn *Conn) Pqfinish (Conn); Thoát (1); int Main (int argc, char ** argv) char *in_filename, *out_filename; cơ sở dữ liệu char *; Oid lobjoid; Pgconn *Conn; Pgresult *res; if (argc! = 4) fprintf (stderr, "sử dụng: %s database_name in_filename out_filename \ n", Argv [0]); Thoát (1); cơ sở dữ liệu = argv [1]; in_filename = argv [2]; out_filename = argv [3]; /* * Thiết lập kết nối */ Conn = pqsetDB (null, null, null, null, cơ sở dữ liệu); / * Kiểm tra xem kết nối phụ trợ đã được thực hiện thành công */ if (pqstatus (Conn)! = Connection_ok) fprintf (stderr, "kết nối với cơ sở dữ liệu không thành công: %s", Pqerrormessage (Conn)); exit_nicely (Conn); /* Đặt đường dẫn tìm kiếm luôn an toàn, vì vậy người dùng độc hại không thể kiểm soát. */ res = pqexec (Conn, "Chọn pg_catalog.set_config ('search_path', '', false)"); if (pqresultstatus (res)! = pgres_tuples_ok) fprintf (stderr, "set thất bại: %s", pqerrormessage (Conn)); PQCLEAR (RES); exit_nicely (Conn); PQCLEAR (RES); res = pqExec (Conn, "bắt đầu"); PQCLEAR (RES); printf ("Nhập tệp \"%s \ "... \ n", in_filename); /* lobjoid = Nhập khẩu (Conn, in_filename); */ lobjoid = lo_import (Conn, in_filename); if (lobjoid == 0) fprintf (stderr, "%s \ n", pqerrormessage (Conn)); khác printf ("\ tas đối tượng lớn %u. \ n", lobjoid); printf ("chọn ra byte 1000-2000 của đối tượng lớn \ n"); Pickout (Conn, lobjoid, 1000, 1000); printf ("ghi đè byte 1000-2000 của đối tượng lớn với x của \ n"); Ghi đè (Conn, lobjoid, 1000, 1000); printf ("xuất đối tượng lớn sang tệp \"%s \ "... \ n", out_filename); /* exportFile (Conn, lobjoid, out_filename); */ if (lo_export (Conn, lobjoid, out_filename) <0) fprintf (stderr, "%s \ n", pqerrormessage (Conn)); res = pqExec (Conn, "end"); PQCLEAR (RES); Pqfinish (Conn); trả lại 0;