Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2885006

Browse files
committed
docs: Update libpq and testlo examples
Josh Kupershmidt
1 parent0f59f4a commit2885006

File tree

2 files changed

+85
-60
lines changed

2 files changed

+85
-60
lines changed

‎doc/src/sgml/libpq.sgml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7906,12 +7906,17 @@ main(int argc, char **argv)
79067906
*
79077907
* INSERT INTO TBL1 VALUES (10);
79087908
*/
7909+
7910+
#ifdef WIN32
7911+
#include <windows.h>
7912+
#endif
79097913
#include <stdio.h>
79107914
#include <stdlib.h>
79117915
#include <string.h>
79127916
#include <errno.h>
79137917
#include <sys/time.h>
7914-
#include <libpq-fe.h>
7918+
#include <sys/types.h>
7919+
#include "libpq-fe.h"
79157920

79167921
static void
79177922
exit_nicely(PGconn *conn)
@@ -8045,11 +8050,17 @@ main(int argc, char **argv)
80458050
* t = (8 bytes) 'ho there'
80468051
* b = (5 bytes) \004\003\002\001\000
80478052
*/
8053+
8054+
#ifdef WIN32
8055+
#include <windows.h>
8056+
#endif
8057+
80488058
#include <stdio.h>
80498059
#include <stdlib.h>
8060+
#include <stdint.h>
80508061
#include <string.h>
80518062
#include <sys/types.h>
8052-
#include<libpq-fe.h>
8063+
#include"libpq-fe.h"
80538064

80548065
/* for ntohl/htonl */
80558066
#include <netinet/in.h>
@@ -8160,10 +8171,10 @@ main(int argc, char **argv)
81608171
* out-of-line parameters, as well as binary transmission of data.
81618172
*
81628173
* This first example transmits the parameters as text, but receives the
8163-
* results in binary format. By using out-of-line parameters we can
8164-
*avoida lot of tedious mucking about with quoting and escaping, even
8165-
*thoughthe data is text. Notice how we don't have to do anything
8166-
*special withthe quote mark in the parameter value.
8174+
* results in binary format. By using out-of-line parameters we can avoid
8175+
* a lot of tedious mucking about with quoting and escaping, even though
8176+
* the data is text. Notice how we don't have to do anything special with
8177+
* the quote mark in the parameter value.
81678178
*/
81688179

81698180
/* Here is our out-of-line parameter value */
@@ -8190,8 +8201,8 @@ main(int argc, char **argv)
81908201
PQclear(res);
81918202

81928203
/*
8193-
* In this second example we transmit an integer parameter in binary
8194-
*form,and again retrieve the results in binary form.
8204+
* In this second example we transmit an integer parameter in binary form,
8205+
* and again retrieve the results in binary form.
81958206
*
81968207
* Although we tell PQexecParams we are letting the backend deduce
81978208
* parameter type, we really force the decision by casting the parameter

‎doc/src/sgml/lobj.sgml

Lines changed: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -597,27 +597,39 @@ SELECT lo_export(image.raster, '/tmp/motd') FROM image
597597
<example id="lo-example">
598598
<title>Large Objects with <application>libpq</application> Example Program</title>
599599
<programlisting><![CDATA[
600-
/*--------------------------------------------------------------
600+
/*-------------------------------------------------------------------------
601601
*
602-
* testlo.c--
602+
* testlo.c
603603
* test using large objects with libpq
604604
*
605-
* Copyright (c) 1994, Regents of the University of California
605+
* Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
606+
* Portions Copyright (c) 1994, Regents of the University of California
606607
*
607-
*--------------------------------------------------------------
608+
*
609+
* IDENTIFICATION
610+
* src/test/examples/testlo.c
611+
*
612+
*-------------------------------------------------------------------------
608613
*/
609614
#include <stdio.h>
615+
#include <stdlib.h>
616+
617+
#include <sys/types.h>
618+
#include <sys/stat.h>
619+
#include <fcntl.h>
620+
#include <unistd.h>
621+
610622
#include "libpq-fe.h"
611623
#include "libpq/libpq-fs.h"
612624

613-
#define BUFSIZE1024
625+
#define BUFSIZE 1024
614626

615627
/*
616-
* importFile
628+
* importFile -
617629
* import file "in_filename" into database as large object "lobjOid"
618630
*
619631
*/
620-
Oid
632+
staticOid
621633
importFile(PGconn *conn, char *filename)
622634
{
623635
Oid lobjId;
@@ -633,15 +645,15 @@ importFile(PGconn *conn, char *filename)
633645
fd = open(filename, O_RDONLY, 0666);
634646
if (fd < 0)
635647
{ /* error */
636-
fprintf(stderr, "cannot open unix file %s\n", filename);
648+
fprintf(stderr, "cannot open unix file\"%s\"\n", filename);
637649
}
638650

639651
/*
640652
* create the large object
641653
*/
642654
lobjId = lo_creat(conn, INV_READ | INV_WRITE);
643655
if (lobjId == 0)
644-
fprintf(stderr, "cannot create large object\n");
656+
fprintf(stderr, "cannot create large object");
645657

646658
lobj_fd = lo_open(conn, lobjId, INV_WRITE);
647659

@@ -652,16 +664,16 @@ importFile(PGconn *conn, char *filename)
652664
{
653665
tmp = lo_write(conn, lobj_fd, buf, nbytes);
654666
if (tmp < nbytes)
655-
fprintf(stderr, "error while readinglarge object\n");
667+
fprintf(stderr, "error while reading\"%s\"", filename);
656668
}
657669

658-
(void)close(fd);
659-
(void)lo_close(conn, lobj_fd);
670+
close(fd);
671+
lo_close(conn, lobj_fd);
660672

661673
return lobjId;
662674
}
663675

664-
void
676+
staticvoid
665677
pickout(PGconn *conn, Oid lobjId, int start, int len)
666678
{
667679
int lobj_fd;
@@ -671,10 +683,7 @@ pickout(PGconn *conn, Oid lobjId, int start, int len)
671683

672684
lobj_fd = lo_open(conn, lobjId, INV_READ);
673685
if (lobj_fd < 0)
674-
{
675-
fprintf(stderr, "cannot open large object %d\n",
676-
lobjId);
677-
}
686+
fprintf(stderr, "cannot open large object %u", lobjId);
678687

679688
lo_lseek(conn, lobj_fd, start, SEEK_SET);
680689
buf = malloc(len + 1);
@@ -683,16 +692,18 @@ pickout(PGconn *conn, Oid lobjId, int start, int len)
683692
while (len - nread > 0)
684693
{
685694
nbytes = lo_read(conn, lobj_fd, buf, len - nread);
686-
buf[nbytes] = '';
695+
buf[nbytes] = '\0';
687696
fprintf(stderr, ">>> %s", buf);
688697
nread += nbytes;
698+
if (nbytes <= 0)
699+
break; /* no more data? */
689700
}
690701
free(buf);
691702
fprintf(stderr, "\n");
692703
lo_close(conn, lobj_fd);
693704
}
694705

695-
void
706+
staticvoid
696707
overwrite(PGconn *conn, Oid lobjId, int start, int len)
697708
{
698709
int lobj_fd;
@@ -703,35 +714,38 @@ overwrite(PGconn *conn, Oid lobjId, int start, int len)
703714

704715
lobj_fd = lo_open(conn, lobjId, INV_WRITE);
705716
if (lobj_fd < 0)
706-
{
707-
fprintf(stderr, "cannot open large object %d\n",
708-
lobjId);
709-
}
717+
fprintf(stderr, "cannot open large object %u", lobjId);
710718

711719
lo_lseek(conn, lobj_fd, start, SEEK_SET);
712720
buf = malloc(len + 1);
713721

714722
for (i = 0; i < len; i++)
715723
buf[i] = 'X';
716-
buf[i] = '';
724+
buf[i] = '\0';
717725

718726
nwritten = 0;
719727
while (len - nwritten > 0)
720728
{
721729
nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
722730
nwritten += nbytes;
731+
if (nbytes <= 0)
732+
{
733+
fprintf(stderr, "\nWRITE FAILED!\n");
734+
break;
735+
}
723736
}
724737
free(buf);
725738
fprintf(stderr, "\n");
726739
lo_close(conn, lobj_fd);
727740
}
728741

742+
729743
/*
730-
* exportFile
744+
* exportFile -
731745
* export large object "lobjOid" to file "out_filename"
732746
*
733747
*/
734-
void
748+
staticvoid
735749
exportFile(PGconn *conn, Oid lobjId, char *filename)
736750
{
737751
int lobj_fd;
@@ -745,18 +759,15 @@ exportFile(PGconn *conn, Oid lobjId, char *filename)
745759
*/
746760
lobj_fd = lo_open(conn, lobjId, INV_READ);
747761
if (lobj_fd < 0)
748-
{
749-
fprintf(stderr, "cannot open large object %d\n",
750-
lobjId);
751-
}
762+
fprintf(stderr, "cannot open large object %u", lobjId);
752763

753764
/*
754765
* open the file to be written to
755766
*/
756-
fd = open(filename, O_CREAT | O_WRONLY, 0666);
767+
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
757768
if (fd < 0)
758769
{ /* error */
759-
fprintf(stderr, "cannot open unix file%s\n",
770+
fprintf(stderr, "cannot open unix file\"%s\"",
760771
filename);
761772
}
762773

@@ -768,18 +779,18 @@ exportFile(PGconn *conn, Oid lobjId, char *filename)
768779
tmp = write(fd, buf, nbytes);
769780
if (tmp < nbytes)
770781
{
771-
fprintf(stderr, "error while writing %s\n",
782+
fprintf(stderr, "error while writing\"%s\"",
772783
filename);
773784
}
774785
}
775786

776-
(void)lo_close(conn, lobj_fd);
777-
(void)close(fd);
787+
lo_close(conn, lobj_fd);
788+
close(fd);
778789

779790
return;
780791
}
781792

782-
void
793+
staticvoid
783794
exit_nicely(PGconn *conn)
784795
{
785796
PQfinish(conn);
@@ -813,37 +824,40 @@ main(int argc, char **argv)
813824
conn = PQsetdb(NULL, NULL, NULL, NULL, database);
814825

815826
/* check to see that the backend connection was successfully made */
816-
if (PQstatus(conn)== CONNECTION_BAD)
827+
if (PQstatus(conn)!= CONNECTION_OK)
817828
{
818-
fprintf(stderr, "Connection to database'%s' failed.\n", database);
819-
fprintf(stderr, "%s", PQerrorMessage(conn));
829+
fprintf(stderr, "Connection to databasefailed: %s",
830+
PQerrorMessage(conn));
820831
exit_nicely(conn);
821832
}
822833

823834
res = PQexec(conn, "begin");
824835
PQclear(res);
825-
826-
printf("importing file %s\n", in_filename);
836+
printf("importing file \"%s\" ...\n", in_filename);
827837
/* lobjOid = importFile(conn, in_filename); */
828838
lobjOid = lo_import(conn, in_filename);
829-
/*
830-
printf("as large object %d.\n", lobjOid);
839+
if (lobjOid == 0)
840+
fprintf(stderr, "%s\n", PQerrorMessage(conn));
841+
else
842+
{
843+
printf("\tas large object %u.\n", lobjOid);
831844

832-
printf("picking out bytes 1000-2000 of the large object\n");
833-
pickout(conn, lobjOid, 1000, 1000);
845+
printf("picking out bytes 1000-2000 of the large object\n");
846+
pickout(conn, lobjOid, 1000, 1000);
834847

835-
printf("overwriting bytes 1000-2000 of the large object with X's\n");
836-
overwrite(conn, lobjOid, 1000, 1000);
837-
*/
848+
printf("overwriting bytes 1000-2000 of the large object with X's\n");
849+
overwrite(conn, lobjOid, 1000, 1000);
838850

839-
printf("exporting large object to file %s\n", out_filename);
840-
/* exportFile(conn, lobjOid, out_filename); */
841-
lo_export(conn, lobjOid, out_filename);
851+
printf("exporting large object to file \"%s\" ...\n", out_filename);
852+
/* exportFile(conn, lobjOid, out_filename); */
853+
if (lo_export(conn, lobjOid, out_filename) < 0)
854+
fprintf(stderr, "%s\n", PQerrorMessage(conn));
855+
}
842856

843857
res = PQexec(conn, "end");
844858
PQclear(res);
845859
PQfinish(conn);
846-
exit(0);
860+
return 0;
847861
}
848862
]]>
849863
</programlisting>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp