1 /*-------------------------------------------------------------------------
5 * Implementation of an archive that is never saved; it is used by
6 * pg_dump to output a plain text SQL script instead of saving
9 * See the headers to pg_restore for more details.
11 * Copyright (c) 2000, Philip Warner
12 * Rights are granted to use this software in any way so long
13 * as this notice is not removed.
15 * The author is not responsible for loss or damages that may
16 * result from it's use.
20 * src/bin/pg_dump/pg_backup_null.c
22 *-------------------------------------------------------------------------
25 #include "pg_backup_archiver.h"
26 #include "dumputils.h"
28 #include <unistd.h> /* for dup */
30 #include "libpq/libpq-fs.h"
33 static size_t _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
34 static size_t _WriteBlobData(ArchiveHandle *AH, const void *data, size_t dLen);
35 static void _EndData(ArchiveHandle *AH, TocEntry *te);
36 static int _WriteByte(ArchiveHandle *AH, const int i);
37 static size_t _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
38 static void _CloseArchive(ArchiveHandle *AH);
39 static void _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt);
40 static void _StartBlobs(ArchiveHandle *AH, TocEntry *te);
41 static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
42 static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
43 static void _EndBlobs(ArchiveHandle *AH, TocEntry *te);
50 InitArchiveFmt_Null(ArchiveHandle *AH)
52 /* Assuming static functions, this can be copied for each format. */
53 AH->WriteDataPtr = _WriteData;
54 AH->EndDataPtr = _EndData;
55 AH->WriteBytePtr = _WriteByte;
56 AH->WriteBufPtr = _WriteBuf;
57 AH->ClosePtr = _CloseArchive;
59 AH->PrintTocDataPtr = _PrintTocData;
61 AH->StartBlobsPtr = _StartBlobs;
62 AH->StartBlobPtr = _StartBlob;
63 AH->EndBlobPtr = _EndBlob;
64 AH->EndBlobsPtr = _EndBlobs;
66 AH->DeClonePtr = NULL;
68 /* Initialize LO buffering */
69 AH->lo_buf_size = LOBBUFSIZE;
70 AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
73 * Now prevent reading...
75 if (AH->mode == archModeRead)
76 exit_horribly(NULL, "this format cannot be read\n");
80 * - Start a new TOC entry
84 * Called by dumper via archiver from within a data dump routine
87 _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
89 /* Just send it to output */
90 ahwrite(data, 1, dLen, AH);
95 * Called by dumper via archiver from within a data dump routine
96 * We substitute this for _WriteData while emitting a BLOB
99 _WriteBlobData(ArchiveHandle *AH, const void *data, size_t dLen)
103 PQExpBuffer buf = createPQExpBuffer();
105 appendByteaLiteralAHX(buf,
106 (const unsigned char *) data,
110 ahprintf(AH, "SELECT pg_catalog.lowrite(0, %s);\n", buf->data);
112 destroyPQExpBuffer(buf);
118 _EndData(ArchiveHandle *AH, TocEntry *te)
120 ahprintf(AH, "\n\n");
124 * Called by the archiver when starting to save all BLOB DATA (not schema).
125 * This routine should save whatever format-specific information is needed
126 * to read the BLOBs back into memory.
128 * It is called just prior to the dumper's DataDumper routine.
130 * Optional, but strongly recommended.
133 _StartBlobs(ArchiveHandle *AH, TocEntry *te)
135 ahprintf(AH, "BEGIN;\n\n");
139 * Called by the archiver when the dumper calls StartBlob.
143 * Must save the passed OID for retrieval at restore-time.
146 _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
148 bool old_blob_style = (AH->version < K_VERS_1_12);
151 exit_horribly(NULL, "invalid OID for large object\n");
153 /* With an old archive we must do drop and create logic here */
154 if (old_blob_style && AH->ropt->dropSchema)
155 DropBlobIfExists(AH, oid);
158 ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n",
161 ahprintf(AH, "SELECT pg_catalog.lo_open('%u', %d);\n",
164 AH->WriteDataPtr = _WriteBlobData;
168 * Called by the archiver when the dumper calls EndBlob.
173 _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
175 AH->WriteDataPtr = _WriteData;
177 ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n");
181 * Called by the archiver when finishing saving all BLOB DATA.
186 _EndBlobs(ArchiveHandle *AH, TocEntry *te)
188 ahprintf(AH, "COMMIT;\n\n");
192 * Called as part of a RestoreArchive call; for the NULL archive, this
193 * just sends the data for a given TOC entry to the output.
197 _PrintTocData(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt)
203 if (strcmp(te->desc, "BLOBS") == 0)
206 (*te->dataDumper) ((Archive *) AH, te->dataDumperArg);
208 if (strcmp(te->desc, "BLOBS") == 0)
216 _WriteByte(ArchiveHandle *AH, const int i)
218 /* Don't do anything */
223 _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
225 /* Don't do anything */
230 _CloseArchive(ArchiveHandle *AH)