6
6
*
7
7
*
8
8
* IDENTIFICATION
9
- * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.3 1996/08/14 05:33:04 scrappy Exp $
9
+ * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.4 1996/08/24 20:48:14 scrappy Exp $
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
22
22
#include "catalog/pg_index.h"
23
23
#include "catalog/index.h"
24
24
25
+ #include "storage/bufmgr.h"
25
26
#include "access/heapam.h"
26
27
#include "access/htup.h"
27
28
#include "access/itup.h"
28
29
#include "access/relscan.h"
29
30
#include "access/funcindex.h"
31
+ #include "access/transam.h"
30
32
#include "access/tupdesc.h"
31
33
#include "nodes/execnodes.h"
32
34
#include "nodes/plannodes.h"
50
52
static bool reading_from_input = false;
51
53
52
54
/* non-export function prototypes */
53
- static void CopyTo (Relation rel , bool binary , FILE * fp , char * delim );
54
- static void CopyFrom (Relation rel , bool binary , FILE * fp , char * delim );
55
+ static void CopyTo (Relation rel , bool binary , bool oids , FILE * fp , char * delim );
56
+ static void CopyFrom (Relation rel , bool binary , bool oids , FILE * fp , char * delim );
55
57
static Oid GetOutputFunction (Oid type );
56
58
static Oid GetTypeElement (Oid type );
57
59
static Oid GetInputFunction (Oid type );
58
60
static Oid IsTypeByVal (Oid type );
59
61
static void GetIndexRelations (Oid main_relation_oid ,
60
62
int * n_indices ,
61
63
Relation * * index_rels );
62
- static char * CopyReadAttribute (int attno , FILE * fp , bool * isnull , char * delim );
64
+ static char * CopyReadAttribute (FILE * fp , bool * isnull , char * delim );
63
65
static void CopyAttributeOut (FILE * fp , char * string , char * delim );
64
66
static int CountTuples (Relation relation );
65
67
66
68
extern FILE * Pfout , * Pfin ;
67
69
68
70
void
69
- DoCopy (char * relname , bool binary , bool from , bool pipe , char * filename ,
71
+ DoCopy (char * relname , bool binary , bool oids , bool from , bool pipe , char * filename ,
70
72
char * delim )
71
73
{
72
74
FILE * fp ;
@@ -86,7 +88,7 @@ DoCopy(char *relname, bool binary, bool from, bool pipe, char *filename,
86
88
if (fp == NULL ) {
87
89
elog (WARN , "COPY: file %s could not be open for reading" , filename );
88
90
}
89
- CopyFrom (rel , binary , fp , delim );
91
+ CopyFrom (rel , binary , oids , fp , delim );
90
92
}else {
91
93
92
94
mode_t oumask = umask ((mode_t ) 0 );
@@ -102,7 +104,7 @@ DoCopy(char *relname, bool binary, bool from, bool pipe, char *filename,
102
104
if (fp == NULL ) {
103
105
elog (WARN , "COPY: file %s could not be open for writing" , filename );
104
106
}
105
- CopyTo (rel , binary , fp , delim );
107
+ CopyTo (rel , binary , oids , fp , delim );
106
108
}
107
109
if (!pipe ) {
108
110
fclose (fp );
@@ -113,7 +115,7 @@ DoCopy(char *relname, bool binary, bool from, bool pipe, char *filename,
113
115
}
114
116
115
117
static void
116
- CopyTo (Relation rel , bool binary , FILE * fp , char * delim )
118
+ CopyTo (Relation rel , bool binary , bool oids , FILE * fp , char * delim )
117
119
{
118
120
HeapTuple tuple ;
119
121
HeapScanDesc scandesc ;
@@ -159,6 +161,11 @@ CopyTo(Relation rel, bool binary, FILE *fp, char *delim)
159
161
for (tuple = heap_getnext (scandesc , 0 , NULL );
160
162
tuple != NULL ;
161
163
tuple = heap_getnext (scandesc , 0 , NULL )) {
164
+
165
+ if (oids && !binary ) {
166
+ fputs (oidout (tuple -> t_oid ),fp );
167
+ fputc (delim [0 ], fp );
168
+ }
162
169
163
170
for (i = 0 ; i < attr_count ; i ++ ) {
164
171
value = (Datum )
@@ -197,6 +204,9 @@ CopyTo(Relation rel, bool binary, FILE *fp, char *delim)
197
204
198
205
length = tuple -> t_len - tuple -> t_hoff ;
199
206
fwrite (& length , sizeof (int32 ), 1 , fp );
207
+ if (oids )
208
+ fwrite ((char * ) & tuple -> t_oid , sizeof (int32 ), 1 , fp );
209
+
200
210
fwrite (& null_ct , sizeof (int32 ), 1 , fp );
201
211
if (null_ct > 0 ) {
202
212
for (i = 0 ; i < attr_count ; i ++ ) {
@@ -222,7 +232,7 @@ CopyTo(Relation rel, bool binary, FILE *fp, char *delim)
222
232
}
223
233
224
234
static void
225
- CopyFrom (Relation rel , bool binary , FILE * fp , char * delim )
235
+ CopyFrom (Relation rel , bool binary , bool oids , FILE * fp , char * delim )
226
236
{
227
237
HeapTuple tuple ;
228
238
IndexTuple ituple ;
@@ -260,7 +270,8 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
260
270
int n_indices ;
261
271
InsertIndexResult indexRes ;
262
272
TupleDesc tupDesc ;
263
-
273
+ Oid loaded_oid ;
274
+
264
275
tupDesc = RelationGetTupleDescriptor (rel );
265
276
attr = tupDesc -> attrs ;
266
277
attr_count = tupDesc -> natts ;
@@ -374,8 +385,18 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
374
385
375
386
while (!done ) {
376
387
if (!binary ) {
388
+ if (oids ) {
389
+ string = CopyReadAttribute (fp , & isnull , delim );
390
+ if (string == NULL )
391
+ done = 1 ;
392
+ else {
393
+ loaded_oid = oidin (string );
394
+ if (loaded_oid < BootstrapObjectIdData )
395
+ elog (WARN , "COPY TEXT: Invalid Oid" );
396
+ }
397
+ }
377
398
for (i = 0 ; i < attr_count && !done ; i ++ ) {
378
- string = CopyReadAttribute (i , fp , & isnull , delim );
399
+ string = CopyReadAttribute (fp , & isnull , delim );
379
400
if (isnull ) {
380
401
values [i ] = PointerGetDatum (NULL );
381
402
nulls [i ] = 'n' ;
@@ -401,6 +422,11 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
401
422
if (feof (fp )) {
402
423
done = 1 ;
403
424
}else {
425
+ if (oids ) {
426
+ fread (& loaded_oid , sizeof (int32 ), 1 , fp );
427
+ if (loaded_oid < BootstrapObjectIdData )
428
+ elog (WARN , "COPY BINARY: Invalid Oid" );
429
+ }
404
430
fread (& null_ct , sizeof (int32 ), 1 , fp );
405
431
if (null_ct > 0 ) {
406
432
for (i = 0 ; i < null_ct ; i ++ ) {
@@ -476,6 +502,8 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
476
502
477
503
tupDesc = CreateTupleDesc (attr_count , attr );
478
504
tuple = heap_formtuple (tupDesc , values , nulls );
505
+ if (oids )
506
+ tuple -> t_oid = loaded_oid ;
479
507
heap_insert (rel , tuple );
480
508
481
509
if (has_index ) {
@@ -699,7 +727,7 @@ inString(char c, char* s)
699
727
*/
700
728
701
729
static char *
702
- CopyReadAttribute (int attno , FILE * fp , bool * isnull , char * delim )
730
+ CopyReadAttribute (FILE * fp , bool * isnull , char * delim )
703
731
{
704
732
static char attribute [EXT_ATTLEN ];
705
733
char c ;
0 commit comments