6
6
*
7
7
*
8
8
* IDENTIFICATION
9
- * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.2.2.2 1996/08/26 06:53:03 scrappy Exp $
9
+ * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.2.2.3 1996/10/28 22:09:30 scrappy Exp $
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
@@ -109,7 +109,7 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe, char *filena
109
109
if (!pipe ) {
110
110
fclose (fp );
111
111
}else if (!from && !binary ) {
112
- fputs (".\n" , fp );
112
+ fputs ("\\ .\n" , fp );
113
113
if (IsUnderPostmaster ) fflush (Pfout );
114
114
}
115
115
}
@@ -176,6 +176,9 @@ CopyTo(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
176
176
CopyAttributeOut (fp , string , delim );
177
177
pfree (string );
178
178
}
179
+ else
180
+ fputs ("\\N" , fp ); /* null indicator */
181
+
179
182
if (i == attr_count - 1 ) {
180
183
fputc ('\n' , fp );
181
184
}else {
@@ -731,40 +734,21 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim)
731
734
int done = 0 ;
732
735
int i = 0 ;
733
736
734
- if (feof (fp )) {
735
- * isnull = (bool ) false;
737
+ if (feof (fp ))
736
738
return (NULL );
737
- }
738
739
739
740
while (!done ) {
740
741
c = getc (fp );
741
742
742
- if (feof (fp )) {
743
- * isnull = (bool ) false;
743
+ if (feof (fp ))
744
744
return (NULL );
745
- }else if (reading_from_input && i == 0 && c == '.' ) {
746
- attribute [0 ] = c ;
747
- c = getc (fp );
748
- if (c == '\n' ) {
749
- * isnull = (bool ) false;
750
- return (NULL );
751
- }else if (inString (c ,delim )) {
752
- attribute [1 ] = 0 ;
753
- * isnull = (bool ) false;
754
- return (& attribute [0 ]);
755
- }else {
756
- attribute [1 ] = c ;
757
- i = 2 ;
758
- }
759
745
}else if (c == '\\') {
760
746
c = getc (fp );
761
747
#ifdef ESCAPE_PATCH
762
748
#define ISOCTAL (c ) (((c) >= '0') && ((c) <= '7'))
763
749
#define VALUE (c ) ((c) - '0')
764
- if (feof (fp )) {
765
- * isnull = (bool ) false;
750
+ if (feof (fp ))
766
751
return (NULL );
767
-
E864
}
768
752
switch (c ) {
769
753
case '0' :
770
754
case '1' :
@@ -783,21 +767,17 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim)
783
767
if (ISOCTAL (c )) {
784
768
val = (val <<3 ) + VALUE (c );
785
769
} else {
786
- if (feof (fp )) {
787
- * isnull = (bool ) false;
770
+ if (feof (fp ))
788
771
return (NULL );
789
- }
790
772
ungetc (c , fp );
791
773
}
792
774
} else {
793
- if (feof (fp )) {
794
- * isnull = (bool ) false;
775
+ if (feof (fp ))
795
776
return (NULL );
796
- }
797
777
ungetc (c , fp );
798
778
}
799
779
c = val & 0377 ;
800
- }
780
+ }
801
781
break ;
802
782
case 'b' :
803
783
c = '\b' ;
@@ -817,6 +797,16 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim)
817
797
case 'v' :
818
798
c = '\v' ;
819
799
break ;
800
+ case 'N' :
801
+ attribute [0 ] = '\0' ; /* just to be safe */
802
+ * isnull = (bool ) true;
803
+ break ;
804
+ case '.' :
805
+ c = getc (fp );
806
+ if (c != '\n' )
807
+ elog (WARN , "CopyReadAttribute - end of record marker corrupted" );
808
+ return (NULL );
809
+ break ;
820
810
}
821
811
#endif
822
812
}else if (inString (c ,delim ) || c == '\n ') {
@@ -827,13 +817,7 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim)
827
817
elog (WARN , "CopyReadAttribute - attribute length too long" );
828
818
}
829
819
attribute [i ] = '\0' ;
830
- if (i == 0 ) {
831
- * isnull = (bool ) true;
832
- return (NULL );
833
- }else {
834
- * isnull = (bool ) false;
835
- return (& attribute [0 ]);
836
- }
820
+ return (& attribute [0 ]);
837
821
}
838
822
839
823
#ifdef ESCAPE_PATCH
@@ -845,26 +829,26 @@ CopyAttributeOut(FILE *fp, char *string, char *delim)
845
829
int len = strlen (string );
846
830
847
831
/* XXX - This is a kludge, we should check the data type */
848
- if (len && (string [0 ] == '{' ) && (string [len - 1 ] == '}' )) {
832
+ if (len && (string [0 ] == '{' ) && (string [len - 1 ] == '}' ))
849
833
is_array = true;
850
- }
851
834
852
835
for ( ; c = * string ; string ++ ) {
853
- if ((c == delim [0 ]) || (c == '\n' )) {
836
+ if (c == delim [0 ] || c == '\n' ||
837
+ (c == '\\' && !is_array ))
854
838
fputc ('\\' , fp );
855
- } else if (( c == '\\' ) && is_array ) {
856
- if (* ( string + 1 ) == '\\' ) {
857
- /* translate \\ to \\\\ */
858
- fputc ( '\\' , fp );
859
- fputc ('\\' , fp );
860
- fputc ('\\' , fp );
861
- string ++ ;
862
- } else if ( * ( string + 1 ) == '"' ) {
863
- /* translate \" to \\\" */
864
- fputc ( '\\' , fp );
865
- fputc ('\\' , fp );
866
- }
867
- }
839
+ else
840
+ if (c == '\\' && is_array )
841
+ if ( * ( string + 1 ) == '\\' ) {
842
+ /* translate \\ to \\\\ */
843
+ fputc ('\\' , fp );
844
+ fputc ('\\' , fp );
845
+ fputc ( '\\' , fp ) ;
846
+ string ++ ;
847
+ } else if ( * ( string + 1 ) == '"' ) {
848
+ /* translate \" to \\\" */
849
+ fputc ('\\' , fp );
850
+ fputc ( '\\' , fp );
851
+ }
868
852
fputc (* string , fp );
869
853
}
870
854
}
0 commit comments