gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

284 líneas
9.5KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>34.5. Example Program</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="lo-funcs.html" title="34.4. Server-Side Functions" /><link rel="next" href="ecpg.html" title="Chapter 35. ECPG - Embedded SQL in C" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">34.5. Example Program</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="lo-funcs.html" title="34.4. Server-Side Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="largeobjects.html" title="Chapter 34. Large Objects">Up</a></td><th width="60%" align="center">Chapter 34. Large Objects</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ecpg.html" title="Chapter 35. ECPG - Embedded SQL in C">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LO-EXAMPLESECT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">34.5. Example Program</h2></div></div></div><p>
  3. <a class="xref" href="lo-examplesect.html#LO-EXAMPLE" title="Example 34.1. Large Objects with libpq Example Program">Example 34.1</a> is a sample program which shows how the large object
  4. interface
  5. in <span class="application">libpq</span> can be used. Parts of the program are
  6. commented out but are left in the source for the reader's
  7. benefit. This program can also be found in
  8. <code class="filename">src/test/examples/testlo.c</code> in the source distribution.
  9. </p><div class="example" id="LO-EXAMPLE"><p class="title"><strong>Example 34.1. Large Objects with <span class="application">libpq</span> Example Program</strong></p><div class="example-contents"><pre class="programlisting">
  10. /*-------------------------------------------------------------------------
  11. *
  12. * testlo.c
  13. * test using large objects with libpq
  14. *
  15. * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
  16. * Portions Copyright (c) 1994, Regents of the University of California
  17. *
  18. *
  19. * IDENTIFICATION
  20. * src/test/examples/testlo.c
  21. *
  22. *-------------------------------------------------------------------------
  23. */
  24. #include &lt;stdio.h&gt;
  25. #include &lt;stdlib.h&gt;
  26. #include &lt;sys/types.h&gt;
  27. #include &lt;sys/stat.h&gt;
  28. #include &lt;fcntl.h&gt;
  29. #include &lt;unistd.h&gt;
  30. #include "libpq-fe.h"
  31. #include "libpq/libpq-fs.h"
  32. #define BUFSIZE 1024
  33. /*
  34. * importFile -
  35. * import file "in_filename" into database as large object "lobjOid"
  36. *
  37. */
  38. static Oid
  39. importFile(PGconn *conn, char *filename)
  40. {
  41. Oid lobjId;
  42. int lobj_fd;
  43. char buf[BUFSIZE];
  44. int nbytes,
  45. tmp;
  46. int fd;
  47. /*
  48. * open the file to be read in
  49. */
  50. fd = open(filename, O_RDONLY, 0666);
  51. if (fd &lt; 0)
  52. { /* error */
  53. fprintf(stderr, "cannot open unix file\"%s\"\n", filename);
  54. }
  55. /*
  56. * create the large object
  57. */
  58. lobjId = lo_creat(conn, INV_READ | INV_WRITE);
  59. if (lobjId == 0)
  60. fprintf(stderr, "cannot create large object");
  61. lobj_fd = lo_open(conn, lobjId, INV_WRITE);
  62. /*
  63. * read in from the Unix file and write to the inversion file
  64. */
  65. while ((nbytes = read(fd, buf, BUFSIZE)) &gt; 0)
  66. {
  67. tmp = lo_write(conn, lobj_fd, buf, nbytes);
  68. if (tmp &lt; nbytes)
  69. fprintf(stderr, "error while reading \"%s\"", filename);
  70. }
  71. close(fd);
  72. lo_close(conn, lobj_fd);
  73. return lobjId;
  74. }
  75. static void
  76. pickout(PGconn *conn, Oid lobjId, int start, int len)
  77. {
  78. int lobj_fd;
  79. char *buf;
  80. int nbytes;
  81. int nread;
  82. lobj_fd = lo_open(conn, lobjId, INV_READ);
  83. if (lobj_fd &lt; 0)
  84. fprintf(stderr, "cannot open large object %u", lobjId);
  85. lo_lseek(conn, lobj_fd, start, SEEK_SET);
  86. buf = malloc(len + 1);
  87. nread = 0;
  88. while (len - nread &gt; 0)
  89. {
  90. nbytes = lo_read(conn, lobj_fd, buf, len - nread);
  91. buf[nbytes] = '\0';
  92. fprintf(stderr, "&gt;&gt;&gt; %s", buf);
  93. nread += nbytes;
  94. if (nbytes &lt;= 0)
  95. break; /* no more data? */
  96. }
  97. free(buf);
  98. fprintf(stderr, "\n");
  99. lo_close(conn, lobj_fd);
  100. }
  101. static void
  102. overwrite(PGconn *conn, Oid lobjId, int start, int len)
  103. {
  104. int lobj_fd;
  105. char *buf;
  106. int nbytes;
  107. int nwritten;
  108. int i;
  109. lobj_fd = lo_open(conn, lobjId, INV_WRITE);
  110. if (lobj_fd &lt; 0)
  111. fprintf(stderr, "cannot open large object %u", lobjId);
  112. lo_lseek(conn, lobj_fd, start, SEEK_SET);
  113. buf = malloc(len + 1);
  114. for (i = 0; i &lt; len; i++)
  115. buf[i] = 'X';
  116. buf[i] = '\0';
  117. nwritten = 0;
  118. while (len - nwritten &gt; 0)
  119. {
  120. nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
  121. nwritten += nbytes;
  122. if (nbytes &lt;= 0)
  123. {
  124. fprintf(stderr, "\nWRITE FAILED!\n");
  125. break;
  126. }
  127. }
  128. free(buf);
  129. fprintf(stderr, "\n");
  130. lo_close(conn, lobj_fd);
  131. }
  132. /*
  133. * exportFile -
  134. * export large object "lobjOid" to file "out_filename"
  135. *
  136. */
  137. static void
  138. exportFile(PGconn *conn, Oid lobjId, char *filename)
  139. {
  140. int lobj_fd;
  141. char buf[BUFSIZE];
  142. int nbytes,
  143. tmp;
  144. int fd;
  145. /*
  146. * open the large object
  147. */
  148. lobj_fd = lo_open(conn, lobjId, INV_READ);
  149. if (lobj_fd &lt; 0)
  150. fprintf(stderr, "cannot open large object %u", lobjId);
  151. /*
  152. * open the file to be written to
  153. */
  154. fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
  155. if (fd &lt; 0)
  156. { /* error */
  157. fprintf(stderr, "cannot open unix file\"%s\"",
  158. filename);
  159. }
  160. /*
  161. * read in from the inversion file and write to the Unix file
  162. */
  163. while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) &gt; 0)
  164. {
  165. tmp = write(fd, buf, nbytes);
  166. if (tmp &lt; nbytes)
  167. {
  168. fprintf(stderr, "error while writing \"%s\"",
  169. filename);
  170. }
  171. }
  172. lo_close(conn, lobj_fd);
  173. close(fd);
  174. return;
  175. }
  176. static void
  177. exit_nicely(PGconn *conn)
  178. {
  179. PQfinish(conn);
  180. exit(1);
  181. }
  182. int
  183. main(int argc, char **argv)
  184. {
  185. char *in_filename,
  186. *out_filename;
  187. char *database;
  188. Oid lobjOid;
  189. PGconn *conn;
  190. PGresult *res;
  191. if (argc != 4)
  192. {
  193. fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
  194. argv[0]);
  195. exit(1);
  196. }
  197. database = argv[1];
  198. in_filename = argv[2];
  199. out_filename = argv[3];
  200. /*
  201. * set up the connection
  202. */
  203. conn = PQsetdb(NULL, NULL, NULL, NULL, database);
  204. /* check to see that the backend connection was successfully made */
  205. if (PQstatus(conn) != CONNECTION_OK)
  206. {
  207. fprintf(stderr, "Connection to database failed: %s",
  208. PQerrorMessage(conn));
  209. exit_nicely(conn);
  210. }
  211. /* Set always-secure search path, so malicious users can't take control. */
  212. res = PQexec(conn,
  213. "SELECT pg_catalog.set_config('search_path', '', false)");
  214. if (PQresultStatus(res) != PGRES_TUPLES_OK)
  215. {
  216. fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
  217. PQclear(res);
  218. exit_nicely(conn);
  219. }
  220. PQclear(res);
  221. res = PQexec(conn, "begin");
  222. PQclear(res);
  223. printf("importing file \"%s\" ...\n", in_filename);
  224. /* lobjOid = importFile(conn, in_filename); */
  225. lobjOid = lo_import(conn, in_filename);
  226. if (lobjOid == 0)
  227. fprintf(stderr, "%s\n", PQerrorMessage(conn));
  228. else
  229. {
  230. printf("\tas large object %u.\n", lobjOid);
  231. printf("picking out bytes 1000-2000 of the large object\n");
  232. pickout(conn, lobjOid, 1000, 1000);
  233. printf("overwriting bytes 1000-2000 of the large object with X's\n");
  234. overwrite(conn, lobjOid, 1000, 1000);
  235. printf("exporting large object to file \"%s\" ...\n", out_filename);
  236. /* exportFile(conn, lobjOid, out_filename); */
  237. if (lo_export(conn, lobjOid, out_filename) &lt; 0)
  238. fprintf(stderr, "%s\n", PQerrorMessage(conn));
  239. }
  240. res = PQexec(conn, "end");
  241. PQclear(res);
  242. PQfinish(conn);
  243. return 0;
  244. }
  245. </pre></div></div><br class="example-break" /></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="lo-funcs.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="largeobjects.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ecpg.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">34.4. Server-Side Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 35. <span class="application">ECPG</span> - Embedded <acronym class="acronym">SQL</acronym> in C</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1