PostgreSQL Source Code  git master
large_obj.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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)
 

Function Documentation

◆ do_lo_export()

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

Definition at line 142 of file large_obj.c.

143 {
144  int status;
145  bool own_transaction;
146 
147  if (!start_lo_xact("\\lo_export", &own_transaction))
148  return false;
149 
150  SetCancelConn(NULL);
151  status = lo_export(pset.db, atooid(loid_arg), filename_arg);
152  ResetCancelConn();
153 
154  /* of course this status is documented nowhere :( */
155  if (status != 1)
156  {
158  return fail_lo_xact("\\lo_export", own_transaction);
159  }
160 
161  if (!finish_lo_xact("\\lo_export", own_transaction))
162  return false;
163 
164  print_lo_result("lo_export");
165 
166  return true;
167 }
void ResetCancelConn(void)
Definition: cancel.c:107
void SetCancelConn(PGconn *conn)
Definition: cancel.c:77
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6841
int lo_export(PGconn *conn, Oid lobjId, const char *filename)
Definition: fe-lobj.c:748
static bool start_lo_xact(const char *operation, bool *own_transaction)
Definition: large_obj.c:56
static bool fail_lo_xact(const char *operation, bool own_transaction)
Definition: large_obj.c:121
static void print_lo_result(const char *fmt,...) pg_attribute_printf(1
Definition: large_obj.c:19
static bool finish_lo_xact(const char *operation, bool own_transaction)
Definition: large_obj.c:98
#define pg_log_info(...)
Definition: logging.h:124
#define atooid(x)
Definition: postgres_ext.h:42
PsqlSettings pset
Definition: startup.c:32
PGconn * db
Definition: settings.h:82

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

Referenced by exec_command_lo().

◆ do_lo_import()

bool do_lo_import ( const char *  filename_arg,
const char *  comment_arg 
)

Definition at line 176 of file large_obj.c.

177 {
178  PGresult *res;
179  Oid loid;
180  char oidbuf[32];
181  bool own_transaction;
182 
183  if (!start_lo_xact("\\lo_import", &own_transaction))
184  return false;
185 
186  SetCancelConn(NULL);
187  loid = lo_import(pset.db, filename_arg);
188  ResetCancelConn();
189 
190  if (loid == InvalidOid)
191  {
193  return fail_lo_xact("\\lo_import", own_transaction);
194  }
195 
196  /* insert description if given */
197  if (comment_arg)
198  {
199  char *cmdbuf;
200  char *bufptr;
201  size_t slen = strlen(comment_arg);
202 
203  cmdbuf = pg_malloc_extended(slen * 2 + 256, MCXT_ALLOC_NO_OOM);
204  if (!cmdbuf)
205  return fail_lo_xact("\\lo_import", own_transaction);
206  sprintf(cmdbuf, "COMMENT ON LARGE OBJECT %u IS '", loid);
207  bufptr = cmdbuf + strlen(cmdbuf);
208  bufptr += PQescapeStringConn(pset.db, bufptr, comment_arg, slen, NULL);
209  strcpy(bufptr, "'");
210 
211  if (!(res = PSQLexec(cmdbuf)))
212  {
213  free(cmdbuf);
214  return fail_lo_xact("\\lo_import", own_transaction);
215  }
216 
217  PQclear(res);
218  free(cmdbuf);
219  }
220 
221  if (!finish_lo_xact("\\lo_import", own_transaction))
222  return false;
223 
224  print_lo_result("lo_import %u", loid);
225 
226  sprintf(oidbuf, "%u", loid);
227  SetVariable(pset.vars, "LASTOID", oidbuf);
228 
229  return true;
230 }
PGresult * PSQLexec(const char *query)
Definition: common.c:581
size_t PQescapeStringConn(PGconn *conn, char *to, const char *from, size_t length, int *error)
Definition: fe-exec.c:4105
Oid lo_import(PGconn *conn, const char *filename)
Definition: fe-lobj.c:626
void * pg_malloc_extended(size_t size, int flags)
Definition: fe_memutils.c:59
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:17
#define free(a)
Definition: header.h:65
#define sprintf
Definition: port.h:240
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
VariableSpace vars
Definition: settings.h:122
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211

References _psqlSettings::db, fail_lo_xact(), finish_lo_xact(), free, InvalidOid, lo_import(), MCXT_ALLOC_NO_OOM, pg_log_info, pg_malloc_extended(), PQclear(), PQerrorMessage(), PQescapeStringConn(), print_lo_result(), pset, PSQLexec(), res, ResetCancelConn(), SetCancelConn(), SetVariable(), sprintf, start_lo_xact(), and _psqlSettings::vars.

Referenced by exec_command_lo().

◆ do_lo_unlink()

bool do_lo_unlink ( const char *  loid_arg)

Definition at line 239 of file large_obj.c.

240 {
241  int status;
242  Oid loid = atooid(loid_arg);
243  bool own_transaction;
244 
245  if (!start_lo_xact("\\lo_unlink", &own_transaction))
246  return false;
247 
248  SetCancelConn(NULL);
249  status = lo_unlink(pset.db, loid);
250  ResetCancelConn();
251 
252  if (status == -1)
253  {
255  return fail_lo_xact("\\lo_unlink", own_transaction);
256  }
257 
258  if (!finish_lo_xact("\\lo_unlink", own_transaction))
259  return false;
260 
261  print_lo_result("lo_unlink %u", loid);
262 
263  return true;
264 }
int lo_unlink(PGconn *conn, Oid lobjId)
Definition: fe-lobj.c:589

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

Referenced by exec_command_lo().