66 *
77 *
88 * 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 $
1010 *
1111 *-------------------------------------------------------------------------
1212 */
@@ -109,7 +109,7 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe, char *filena
109109 if (!pipe ) {
110110 fclose (fp );
111111 }else if (!from && !binary ) {
112- fputs (".\n" , fp );
112+ fputs ("\\ .\n" , fp );
113113 if (IsUnderPostmaster ) fflush (Pfout );
114114 }
115115}
@@ -176,6 +176,9 @@ CopyTo(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
176176 CopyAttributeOut (fp , string , delim );
177177 pfree (string );
178178 }
179+ else
180+ fputs ("\\N" , fp ); /* null indicator */
181+
179182 if (i == attr_count - 1 ) {
180183 fputc ('\n' , fp );
181184 }else {
@@ -731,40 +734,21 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim)
731734 int done = 0 ;
732735 int i = 0 ;
733736
734- if (feof (fp )) {
735- * isnull = (bool ) false;
737+ if (feof (fp ))
736738 return (NULL );
737- }
738739
739740 while (!done ) {
740741 c = getc (fp );
741742
742- if (feof (fp )) {
743- * isnull = (bool ) false;
743+ if (feof (fp ))
744744 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- }
759745 }else if (c == '\\') {
760746 c = getc (fp );
761747#ifdef ESCAPE_PATCH
762748#define ISOCTAL (c ) (((c) >= '0') && ((c) <= '7'))
763749#define VALUE (c ) ((c) - '0')
764- if (feof (fp )) {
765- * isnull = (bool ) false;
750+ if (feof (fp ))
766751 return (NULL );
767- }
768752 switch (c ) {
769753 case '0' :
770754 case '1' :
@@ -783,21 +767,17 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim)
783767 if (ISOCTAL (c )) {
784768 val = (val <<3 ) + VALUE (c );
785769 } else {
786- if (feof (fp )) {
787- * isnull = (bool ) false;
770+ if (feof (fp ))
788771 return (NULL );
789- }
790772 ungetc (c , fp );
791773 }
792774 } else {
793- if (feof (fp )) {
794- * isnull = (bool ) false;
775+ if (feof (fp ))
795776 return (NULL );
796- }
797777 ungetc (c , fp );
798778 }
799779 c = val & 0377 ;
800- }
780+ }
801781 break ;
802782 case 'b' :
803783 c = '\b' ;
@@ -817,6 +797,16 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim)
817797 case 'v' :
818798 c = '\v' ;
819799 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 ;
820810 }
821811#endif
822812 }else if (inString (c ,delim ) || c == '\n ') {
@@ -827,13 +817,7 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim)
827817 elog (WARN , "CopyReadAttribute - attribute length too long" );
828818 }
829819 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 ]);
837821}
838822
839823#ifdef ESCAPE_PATCH
@@ -845,26 +829,26 @@ CopyAttributeOut(FILE *fp, char *string, char *delim)
845829 int len = strlen (string );
846830
847831 /* 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 ] == '}' ))
849833 is_array = true;
850- }
851834
852835 for ( ; c = * string ; string ++ ) {
853- if ((c == delim [0 ]) || (c == '\n' )) {
836+ if (c == delim [0 ] || c == '\n' ||
837+ (c == '\\' && !is_array ))
854838 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+ }
868852 fputc (* string , fp );
869853 }
870854}
0 commit comments