PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
large_obj.c File Reference
#include "postgres_fe.h"
#include "large_obj.h"
#include "settings.h"
#include "common.h"
Include dependency graph for large_obj.c:

Go to the source code of this file.

Functions

static void print_lo_result (const char *fmt,...) pg_attribute_printf(1
 
static bool start_lo_xact (const char *operation, bool *own_transaction)
 
static bool finish_lo_xact (const char *operation, bool own_transaction)
 
static bool fail_lo_xact (const char *operation, bool own_transaction)
 
bool do_lo_export (const char *loid_arg, const char *filename_arg)
 
bool do_lo_import (const char *filename_arg, const char *comment_arg)
 
bool do_lo_unlink (const char *loid_arg)
 
bool do_lo_list (void)
 

Function Documentation

bool do_lo_export ( const char *  loid_arg,
const char *  filename_arg 
)

Definition at line 141 of file large_obj.c.

References atooid, _psqlSettings::db, fail_lo_xact(), finish_lo_xact(), lo_export(), PQerrorMessage(), print_lo_result(), pset, psql_error(), ResetCancelConn(), SetCancelConn(), start_lo_xact(), and status().

Referenced by exec_command_lo().

142 {
143  int status;
144  bool own_transaction;
145 
146  if (!start_lo_xact("\\lo_export", &own_transaction))
147  return false;
148 
149  SetCancelConn();
150  status = lo_export(pset.db, atooid(loid_arg), filename_arg);
151  ResetCancelConn();
152 
153  /* of course this status is documented nowhere :( */
154  if (status != 1)
155  {
157  return fail_lo_xact("\\lo_export", own_transaction);
158  }
159 
160  if (!finish_lo_xact("\\lo_export", own_transaction))
161  return false;
162 
163  print_lo_result("lo_export");
164 
165  return true;
166 }
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6014
PsqlSettings pset
Definition: startup.c:33
static bool finish_lo_xact(const char *operation, bool own_transaction)
Definition: large_obj.c:97
int lo_export(PGconn *conn, Oid lobjId, const char *filename)
Definition: fe-lobj.c:785
static bool fail_lo_xact(const char *operation, bool own_transaction)
Definition: large_obj.c:120
void SetCancelConn(void)
Definition: common.c:445
#define atooid(x)
Definition: postgres_ext.h:42
static void print_lo_result(const char *fmt,...) pg_attribute_printf(1
Definition: large_obj.c:18
void psql_error(const char *fmt,...)
Definition: common.c:220
static bool start_lo_xact(const char *operation, bool *own_transaction)
Definition: large_obj.c:55
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
void ResetCancelConn(void)
Definition: common.c:475
bool do_lo_import ( const char *  filename_arg,
const char *  comment_arg 
)

Definition at line 175 of file large_obj.c.

References _psqlSettings::db, fail_lo_xact(), finish_lo_xact(), free, InvalidOid, lo_import(), malloc, NULL, PQclear(), PQerrorMessage(), PQescapeStringConn(), print_lo_result(), pset, psql_error(), PSQLexec(), ResetCancelConn(), SetCancelConn(), SetVariable(), start_lo_xact(), and _psqlSettings::vars.

Referenced by exec_command_lo().

176 {
177  PGresult *res;
178  Oid loid;
179  char oidbuf[32];
180  bool own_transaction;
181 
182  if (!start_lo_xact("\\lo_import", &own_transaction))
183  return false;
184 
185  SetCancelConn();
186  loid = lo_import(pset.db, filename_arg);
187  ResetCancelConn();
188 
189  if (loid == InvalidOid)
190  {
192  return fail_lo_xact("\\lo_import", own_transaction);
193  }
194 
195  /* insert description if given */
196  if (comment_arg)
197  {
198  char *cmdbuf;
199  char *bufptr;
200  size_t slen = strlen(comment_arg);
201 
202  cmdbuf = malloc(slen * 2 + 256);
203  if (!cmdbuf)
204  return fail_lo_xact("\\lo_import", own_transaction);
205  sprintf(cmdbuf, "COMMENT ON LARGE OBJECT %u IS '", loid);
206  bufptr = cmdbuf + strlen(cmdbuf);
207  bufptr += PQescapeStringConn(pset.db, bufptr, comment_arg, slen, NULL);
208  strcpy(bufptr, "'");
209 
210  if (!(res = PSQLexec(cmdbuf)))
211  {
212  free(cmdbuf);
213  return fail_lo_xact("\\lo_import", own_transaction);
214  }
215 
216  PQclear(res);
217  free(cmdbuf);
218  }
219 
220  if (!finish_lo_xact("\\lo_import", own_transaction))
221  return false;
222 
223  print_lo_result("lo_import %u", loid);
224 
225  sprintf(oidbuf, "%u", loid);
226  SetVariable(pset.vars, "LASTOID", oidbuf);
227 
228  return true;
229 }
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6014
PsqlSettings pset
Definition: startup.c:33
static bool finish_lo_xact(const char *operation, bool own_transaction)
Definition: large_obj.c:97
unsigned int Oid
Definition: postgres_ext.h:31
#define malloc(a)
Definition: header.h:50
static bool fail_lo_xact(const char *operation, bool own_transaction)
Definition: large_obj.c:120
size_t PQescapeStringConn(PGconn *conn, char *to, const char *from, size_t length, int *error)
Definition: fe-exec.c:3314
void SetCancelConn(void)
Definition: common.c:445
static void print_lo_result(const char *fmt,...) pg_attribute_printf(1
Definition: large_obj.c:18
void psql_error(const char *fmt,...)
Definition: common.c:220
#define InvalidOid
Definition: postgres_ext.h:36
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
static bool start_lo_xact(const char *operation, bool *own_transaction)
Definition: large_obj.c:55
void ResetCancelConn(void)
Definition: common.c:475
Oid lo_import(PGconn *conn, const char *filename)
Definition: fe-lobj.c:669
PGresult * PSQLexec(const char *query)
Definition: common.c:642
VariableSpace vars
Definition: settings.h:116
bool do_lo_list ( void  )

Definition at line 273 of file large_obj.c.

References _, buf, gettext_noop, _psqlSettings::logfile, NULL, printQueryOpt::nullPrint, _psqlSettings::popt, PQclear(), printQuery(), pset, PSQLexec(), _psqlSettings::queryFout, snprintf(), _psqlSettings::sversion, printQueryOpt::title, printQueryOpt::topt, printQueryOpt::translate_header, and printTableOpt::tuples_only.

Referenced by exec_command_d(), and exec_command_lo().

274 {
275  PGresult *res;
276  char buf[1024];
277  printQueryOpt myopt = pset.popt;
278 
279  if (pset.sversion >= 90000)
280  {
281  snprintf(buf, sizeof(buf),
282  "SELECT oid as \"%s\",\n"
283  " pg_catalog.pg_get_userbyid(lomowner) as \"%s\",\n"
284  " pg_catalog.obj_description(oid, 'pg_largeobject') as \"%s\"\n"
285  " FROM pg_catalog.pg_largeobject_metadata "
286  " ORDER BY oid",
287  gettext_noop("ID"),
288  gettext_noop("Owner"),
289  gettext_noop("Description"));
290  }
291  else
292  {
293  snprintf(buf, sizeof(buf),
294  "SELECT loid as \"%s\",\n"
295  " pg_catalog.obj_description(loid, 'pg_largeobject') as \"%s\"\n"
296  "FROM (SELECT DISTINCT loid FROM pg_catalog.pg_largeobject) x\n"
297  "ORDER BY 1",
298  gettext_noop("ID"),
299  gettext_noop("Description"));
300  }
301 
302  res = PSQLexec(buf);
303  if (!res)
304  return false;
305 
306  myopt.topt.tuples_only = false;
307  myopt.nullPrint = NULL;
308  myopt.title = _("Large objects");
309  myopt.translate_header = true;
310 
311  printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
312 
313  PQclear(res);
314  return true;
315 }
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
printTableOpt topt
Definition: print.h:165
#define gettext_noop(x)
Definition: c.h:139
FILE * queryFout
Definition: settings.h:84
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
bool tuples_only
Definition: print.h:107
static char * buf
Definition: pg_test_fsync.c:66
bool translate_header
Definition: print.h:169
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3285
#define _(x)
Definition: elog.c:84
PGresult * PSQLexec(const char *query)
Definition: common.c:642
bool do_lo_unlink ( const char *  loid_arg)

Definition at line 238 of file large_obj.c.

References atooid, _psqlSettings::db, fail_lo_xact(), finish_lo_xact(), lo_unlink(), PQerrorMessage(), print_lo_result(), pset, psql_error(), ResetCancelConn(), SetCancelConn(), start_lo_xact(), and status().

Referenced by exec_command_lo().

239 {
240  int status;
241  Oid loid = atooid(loid_arg);
242  bool own_transaction;
243 
244  if (!start_lo_xact("\\lo_unlink", &own_transaction))
245  return false;
246 
247  SetCancelConn();
248  status = lo_unlink(pset.db, loid);
249  ResetCancelConn();
250 
251  if (status == -1)
252  {
254  return fail_lo_xact("\\lo_unlink", own_transaction);
255  }
256 
257  if (!finish_lo_xact("\\lo_unlink", own_transaction))
258  return false;
259 
260  print_lo_result("lo_unlink %u", loid);
261 
262  return true;
263 }
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6014
PsqlSettings pset
Definition: startup.c:33
int lo_unlink(PGconn *conn, Oid lobjId)
Definition: fe-lobj.c:629
static bool finish_lo_xact(const char *operation, bool own_transaction)
Definition: large_obj.c:97
unsigned int Oid
Definition: postgres_ext.h:31
static bool fail_lo_xact(const char *operation, bool own_transaction)
Definition: large_obj.c:120
void SetCancelConn(void)
Definition: common.c:445
#define atooid(x)
Definition: postgres_ext.h:42
static void print_lo_result(const char *fmt,...) pg_attribute_printf(1
Definition: large_obj.c:18
void psql_error(const char *fmt,...)
Definition: common.c:220
static bool start_lo_xact(const char *operation, bool *own_transaction)
Definition: large_obj.c:55
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
void ResetCancelConn(void)
Definition: common.c:475
static bool fail_lo_xact ( const char *  operation,
bool  own_transaction 
)
static

Definition at line 120 of file large_obj.c.

References _psqlSettings::autocommit, PQclear(), pset, and PSQLexec().

Referenced by do_lo_export(), do_lo_import(), and do_lo_unlink().

121 {
122  PGresult *res;
123 
124  if (own_transaction && pset.autocommit)
125  {
126  /* close out our own xact */
127  res = PSQLexec("ROLLBACK");
128  PQclear(res);
129  }
130 
131  return false; /* always */
132 }
PsqlSettings pset
Definition: startup.c:33
bool autocommit
Definition: settings.h:123
void PQclear(PGresult *res)
Definition: fe-exec.c:650
PGresult * PSQLexec(const char *query)
Definition: common.c:642
static bool finish_lo_xact ( const char *  operation,
bool  own_transaction 
)
static

Definition at line 97 of file large_obj.c.

References _psqlSettings::autocommit, PQclear(), pset, and PSQLexec().

Referenced by do_lo_export(), do_lo_import(), and do_lo_unlink().

98 {
99  PGresult *res;
100 
101  if (own_transaction && pset.autocommit)
102  {
103  /* close out our own xact */
104  if (!(res = PSQLexec("COMMIT")))
105  {
106  res = PSQLexec("ROLLBACK");
107  PQclear(res);
108  return false;
109  }
110  PQclear(res);
111  }
112 
113  return true;
114 }
PsqlSettings pset
Definition: startup.c:33
bool autocommit
Definition: settings.h:123
void PQclear(PGresult *res)
Definition: fe-exec.c:650
PGresult * PSQLexec(const char *query)
Definition: common.c:642
static void static void print_lo_result ( const char *  fmt,
  ... 
)
static

Definition at line 18 of file large_obj.c.

References printTableOpt::format, _psqlSettings::logfile, _psqlSettings::popt, PRINT_HTML, pset, _psqlSettings::queryFout, _psqlSettings::quiet, and printQueryOpt::topt.

Referenced by do_lo_export(), do_lo_import(), and do_lo_unlink().

19 {
20  va_list ap;
21 
22  if (!pset.quiet)
23  {
25  fputs("<p>", pset.queryFout);
26 
27  va_start(ap, fmt);
28  vfprintf(pset.queryFout, fmt, ap);
29  va_end(ap);
30 
32  fputs("</p>\n", pset.queryFout);
33  else
34  fputs("\n", pset.queryFout);
35  }
36 
37  if (pset.logfile)
38  {
39  va_start(ap, fmt);
40  vfprintf(pset.logfile, fmt, ap);
41  va_end(ap);
42  fputs("\n", pset.logfile);
43  }
44 }
PsqlSettings pset
Definition: startup.c:33
enum printFormat format
Definition: print.h:98
printTableOpt topt
Definition: print.h:165
FILE * queryFout
Definition: settings.h:84
FILE * logfile
Definition: settings.h:114
printQueryOpt popt
Definition: settings.h:91
static bool start_lo_xact ( const char *  operation,
bool own_transaction 
)
static

Definition at line 55 of file large_obj.c.

References _psqlSettings::db, PQclear(), PQTRANS_IDLE, PQTRANS_INERROR, PQTRANS_INTRANS, PQtransactionStatus(), pset, psql_error(), and PSQLexec().

Referenced by do_lo_export(), do_lo_import(), and do_lo_unlink().

56 {
58  PGresult *res;
59 
60  *own_transaction = false;
61 
62  if (!pset.db)
63  {
64  psql_error("%s: not connected to a database\n", operation);
65  return false;
66  }
67 
68  tstatus = PQtransactionStatus(pset.db);
69 
70  switch (tstatus)
71  {
72  case PQTRANS_IDLE:
73  /* need to start our own xact */
74  if (!(res = PSQLexec("BEGIN")))
75  return false;
76  PQclear(res);
77  *own_transaction = true;
78  break;
79  case PQTRANS_INTRANS:
80  /* use the existing xact */
81  break;
82  case PQTRANS_INERROR:
83  psql_error("%s: current transaction is aborted\n", operation);
84  return false;
85  default:
86  psql_error("%s: unknown transaction status\n", operation);
87  return false;
88  }
89 
90  return true;
91 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:33
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:5969
void psql_error(const char *fmt,...)
Definition: common.c:220
void PQclear(PGresult *res)
Definition: fe-exec.c:650
PGTransactionStatusType
Definition: libpq-fe.h:101
PGresult * PSQLexec(const char *query)
Definition: common.c:642