6
6
*
7
7
*
8
8
* IDENTIFICATION
9
- * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.2 1996/07/23 02:23:15 scrappy Exp $
9
+ * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.2.2.1 1996/08/24 20:53:39 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 )
@@ -194,6 +201,9 @@ CopyTo(Relation rel, bool binary, FILE *fp, char *delim)
194
201
195
202
length = tuple -> t_len - tuple -> t_hoff ;
196
203
fwrite (& length , sizeof (int32 ), 1 , fp );
204
+ if (oids )
205
+ fwrite ((char * ) & tuple -> t_oid , sizeof (int32 ), 1 , fp );
206
+
197
207
fwrite (& null_ct , sizeof (int32 ), 1 , fp );
198
208
if (null_ct > 0 ) {
199
209
for (i = 0 ; i < attr_count ; i ++ ) {
@@ -219,7 +229,7 @@ CopyTo(Relation rel, bool binary, FILE *fp, char *delim)
219
229
}
220
230
221
231
static void
222
- CopyFrom (Relation rel , bool binary , FILE * fp , char * delim )
232
+ CopyFrom (Relation rel , bool binary , bool oids , FILE * fp , char * delim )
223
233
{
224
234
HeapTuple tuple ;
225
235
IndexTuple ituple ;
@@ -257,7 +267,8 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
257
267
int n_indices ;
258
268
InsertIndexResult indexRes ;
259
269
TupleDesc tupDesc ;
260
-
270
+ Oid loaded_oid ;
271
+
261
272
tupDesc = RelationGetTupleDescriptor (rel );
262
273
attr = tupDesc -> attrs ;
263
274
attr_count = tupDesc -> natts ;
@@ -371,8 +382,18 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
371
382
372
383
while (!done ) {
373
384
if (!binary ) {
385
+ if (oids ) {
386
+ string = CopyReadAttribute (fp , & isnull , delim );
387
+ if (string == NULL )
388
+ done = 1 ;
389
+ else {
390
+ loaded_oid = oidin (string );
391
+ if (loaded_oid < BootstrapObjectIdData )
392
+ elog (WARN , "COPY TEXT: Invalid Oid" );
393
+ }
394
+ }
374
395
for (i = 0 ; i < attr_count && !done ; i ++ ) {
375
- string = CopyReadAttribute (i , fp , & isnull , delim );
396
+ string = CopyReadAttribute (fp , & isnull , delim );
376
397
if (isnull ) {
377
398
values [i ] = PointerGetDatum (NULL );
378
399
nulls [i ] = 'n' ;
@@ -398,6 +419,11 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
398
419
if (feof (fp )) {
399
420
done = 1 ;
400
421
}else {
422
+ if (oids ) {
423
+ fread (& loaded_oid , sizeof (int32 ), 1 , fp );
424
+ if (loaded_oid < BootstrapObjectIdData )
425
+ elog (WARN , "COPY BINARY: Invalid Oid" );
426
+ }
401
427
fread (& null_ct , sizeof (int32 ), 1 , fp );
402
428
if (null_ct > 0 ) {
403
429
for (i = 0 ; i < null_ct ; i ++ ) {
@@ -473,6 +499,8 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
473
499
474
500
tupDesc = CreateTupleDesc (attr_count , attr );
475
501
tuple = heap_formtuple (tupDesc , values , nulls );
502
+ if (oids )
503
+ tuple -> t_oid = loaded_oid ;
476
504
heap_insert (rel , tuple );
477
505
478
506
if (has_index ) {
@@ -696,7 +724,7 @@ inString(char c, char* s)
696
724
*/
697
725
698
726
static char *
699
- CopyReadAttribute (int attno , FILE * fp , bool * isnull , char * delim )
727
+ CopyReadAttribute (FILE * fp , bool * isnull , char * delim )
700
728
{
701
729
static char attribute [EXT_ATTLEN ];
702
730
char c ;
0 commit comments