Header And Logo

PostgreSQL
| The world's most advanced open source database.

testlo.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * testlo.c
00004  *    test using large objects with libpq
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  *
00010  * IDENTIFICATION
00011  *    src/test/examples/testlo.c
00012  *
00013  *-------------------------------------------------------------------------
00014  */
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 
00018 #include <sys/types.h>
00019 #include <sys/stat.h>
00020 #include <fcntl.h>
00021 #include <unistd.h>
00022 
00023 #include "libpq-fe.h"
00024 #include "libpq/libpq-fs.h"
00025 
00026 #define BUFSIZE         1024
00027 
00028 /*
00029  * importFile -
00030  *    import file "in_filename" into database as large object "lobjOid"
00031  *
00032  */
00033 static Oid
00034 importFile(PGconn *conn, char *filename)
00035 {
00036     Oid         lobjId;
00037     int         lobj_fd;
00038     char        buf[BUFSIZE];
00039     int         nbytes,
00040                 tmp;
00041     int         fd;
00042 
00043     /*
00044      * open the file to be read in
00045      */
00046     fd = open(filename, O_RDONLY, 0666);
00047     if (fd < 0)
00048     {                           /* error */
00049         fprintf(stderr, "cannot open unix file\"%s\"\n", filename);
00050     }
00051 
00052     /*
00053      * create the large object
00054      */
00055     lobjId = lo_creat(conn, INV_READ | INV_WRITE);
00056     if (lobjId == 0)
00057         fprintf(stderr, "cannot create large object");
00058 
00059     lobj_fd = lo_open(conn, lobjId, INV_WRITE);
00060 
00061     /*
00062      * read in from the Unix file and write to the inversion file
00063      */
00064     while ((nbytes = read(fd, buf, BUFSIZE)) > 0)
00065     {
00066         tmp = lo_write(conn, lobj_fd, buf, nbytes);
00067         if (tmp < nbytes)
00068             fprintf(stderr, "error while reading \"%s\"", filename);
00069     }
00070 
00071     close(fd);
00072     lo_close(conn, lobj_fd);
00073 
00074     return lobjId;
00075 }
00076 
00077 static void
00078 pickout(PGconn *conn, Oid lobjId, int start, int len)
00079 {
00080     int         lobj_fd;
00081     char       *buf;
00082     int         nbytes;
00083     int         nread;
00084 
00085     lobj_fd = lo_open(conn, lobjId, INV_READ);
00086     if (lobj_fd < 0)
00087         fprintf(stderr, "cannot open large object %u", lobjId);
00088 
00089     lo_lseek(conn, lobj_fd, start, SEEK_SET);
00090     buf = malloc(len + 1);
00091 
00092     nread = 0;
00093     while (len - nread > 0)
00094     {
00095         nbytes = lo_read(conn, lobj_fd, buf, len - nread);
00096         buf[nbytes] = '\0';
00097         fprintf(stderr, ">>> %s", buf);
00098         nread += nbytes;
00099         if (nbytes <= 0)
00100             break;              /* no more data? */
00101     }
00102     free(buf);
00103     fprintf(stderr, "\n");
00104     lo_close(conn, lobj_fd);
00105 }
00106 
00107 static void
00108 overwrite(PGconn *conn, Oid lobjId, int start, int len)
00109 {
00110     int         lobj_fd;
00111     char       *buf;
00112     int         nbytes;
00113     int         nwritten;
00114     int         i;
00115 
00116     lobj_fd = lo_open(conn, lobjId, INV_WRITE);
00117     if (lobj_fd < 0)
00118         fprintf(stderr, "cannot open large object %u", lobjId);
00119 
00120     lo_lseek(conn, lobj_fd, start, SEEK_SET);
00121     buf = malloc(len + 1);
00122 
00123     for (i = 0; i < len; i++)
00124         buf[i] = 'X';
00125     buf[i] = '\0';
00126 
00127     nwritten = 0;
00128     while (len - nwritten > 0)
00129     {
00130         nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
00131         nwritten += nbytes;
00132         if (nbytes <= 0)
00133         {
00134             fprintf(stderr, "\nWRITE FAILED!\n");
00135             break;
00136         }
00137     }
00138     free(buf);
00139     fprintf(stderr, "\n");
00140     lo_close(conn, lobj_fd);
00141 }
00142 
00143 
00144 /*
00145  * exportFile -
00146  *    export large object "lobjOid" to file "out_filename"
00147  *
00148  */
00149 static void
00150 exportFile(PGconn *conn, Oid lobjId, char *filename)
00151 {
00152     int         lobj_fd;
00153     char        buf[BUFSIZE];
00154     int         nbytes,
00155                 tmp;
00156     int         fd;
00157 
00158     /*
00159      * open the large object
00160      */
00161     lobj_fd = lo_open(conn, lobjId, INV_READ);
00162     if (lobj_fd < 0)
00163         fprintf(stderr, "cannot open large object %u", lobjId);
00164 
00165     /*
00166      * open the file to be written to
00167      */
00168     fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
00169     if (fd < 0)
00170     {                           /* error */
00171         fprintf(stderr, "cannot open unix file\"%s\"",
00172                 filename);
00173     }
00174 
00175     /*
00176      * read in from the inversion file and write to the Unix file
00177      */
00178     while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
00179     {
00180         tmp = write(fd, buf, nbytes);
00181         if (tmp < nbytes)
00182         {
00183             fprintf(stderr, "error while writing \"%s\"",
00184                     filename);
00185         }
00186     }
00187 
00188     lo_close(conn, lobj_fd);
00189     close(fd);
00190 
00191     return;
00192 }
00193 
00194 static void
00195 exit_nicely(PGconn *conn)
00196 {
00197     PQfinish(conn);
00198     exit(1);
00199 }
00200 
00201 int
00202 main(int argc, char **argv)
00203 {
00204     char       *in_filename,
00205                *out_filename;
00206     char       *database;
00207     Oid         lobjOid;
00208     PGconn     *conn;
00209     PGresult   *res;
00210 
00211     if (argc != 4)
00212     {
00213         fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
00214                 argv[0]);
00215         exit(1);
00216     }
00217 
00218     database = argv[1];
00219     in_filename = argv[2];
00220     out_filename = argv[3];
00221 
00222     /*
00223      * set up the connection
00224      */
00225     conn = PQsetdb(NULL, NULL, NULL, NULL, database);
00226 
00227     /* check to see that the backend connection was successfully made */
00228     if (PQstatus(conn) != CONNECTION_OK)
00229     {
00230         fprintf(stderr, "Connection to database failed: %s",
00231                 PQerrorMessage(conn));
00232         exit_nicely(conn);
00233     }
00234 
00235     res = PQexec(conn, "begin");
00236     PQclear(res);
00237     printf("importing file \"%s\" ...\n", in_filename);
00238 /*  lobjOid = importFile(conn, in_filename); */
00239     lobjOid = lo_import(conn, in_filename);
00240     if (lobjOid == 0)
00241         fprintf(stderr, "%s\n", PQerrorMessage(conn));
00242     else
00243     {
00244         printf("\tas large object %u.\n", lobjOid);
00245 
00246         printf("picking out bytes 1000-2000 of the large object\n");
00247         pickout(conn, lobjOid, 1000, 1000);
00248 
00249         printf("overwriting bytes 1000-2000 of the large object with X's\n");
00250         overwrite(conn, lobjOid, 1000, 1000);
00251 
00252         printf("exporting large object to file \"%s\" ...\n", out_filename);
00253 /*      exportFile(conn, lobjOid, out_filename); */
00254         if (lo_export(conn, lobjOid, out_filename) < 0)
00255             fprintf(stderr, "%s\n", PQerrorMessage(conn));
00256     }
00257 
00258     res = PQexec(conn, "end");
00259     PQclear(res);
00260     PQfinish(conn);
00261     return 0;
00262 }