66 *
77 *
88 * IDENTIFICATION
9- * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.18 1996/12/14 04:58:20 vadim Exp $
9+ * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.19 1996/12/19 04:58:24 scrappy Exp $
1010 *
1111 *-------------------------------------------------------------------------
1212 */
@@ -49,12 +49,21 @@ static Oid IsTypeByVal(Oid type);
4949static void GetIndexRelations (Oid main_relation_oid ,
5050int * n_indices ,
5151Relation * * index_rels );
52+ #ifdef COPY_PATCH
53+ static void CopyReadNewline (FILE * fp ,int * newline );
54+ static char * CopyReadAttribute (FILE * fp ,bool * isnull ,char * delim ,int * newline );
55+ #else
5256static char * CopyReadAttribute (FILE * fp ,bool * isnull ,char * delim );
57+ #endif
5358static void CopyAttributeOut (FILE * fp ,char * string ,char * delim );
5459static int CountTuples (Relation relation );
5560
5661extern FILE * Pfout ,* Pfin ;
5762
63+ #ifdef COPY_DEBUG
64+ static int lineno ;
65+ #endif
66+
5867/*
5968 * DoCopy executes a the SQL COPY statement.
6069 */
@@ -433,10 +442,24 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
433442byval [i ]= (bool )IsTypeByVal (attr [i ]-> atttypid );
434443 }
435444
445+ #ifdef COPY_DEBUG
446+ lineno = 0 ;
447+ #endif
436448while (!done ) {
437449if (!binary ) {
450+ #ifdef COPY_PATCH
451+ int newline = 0 ;
452+ #endif
453+ #ifdef COPY_DEBUG
454+ lineno ++ ;
455+ elog (DEBUG ,"line %d" ,lineno );
456+ #endif
438457if (oids ) {
458+ #ifdef COPY_PATCH
459+ string = CopyReadAttribute (fp ,& isnull ,delim ,& newline );
460+ #else
439461string = CopyReadAttribute (fp ,& isnull ,delim );
462+ #endif
440463if (string == NULL )
441464done = 1 ;
442465else {
@@ -446,7 +469,11 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
446469 }
447470 }
448471for (i = 0 ;i < attr_count && !done ;i ++ ) {
472+ #ifdef COPY_PATCH
473+ string = CopyReadAttribute (fp ,& isnull ,delim ,& newline );
474+ #else
449475string = CopyReadAttribute (fp ,& isnull ,delim );
476+ #endif
450477if (isnull ) {
451478values [i ]= PointerGetDatum (NULL );
452479nulls [i ]= 'n' ;
@@ -463,10 +490,20 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
463490 */
464491if (!PointerIsValid (values [i ])&&
465492 !(rel -> rd_att -> attrs [i ]-> attbyval )) {
493+ #ifdef COPY_DEBUG
494+ elog (WARN ,
495+ "copy from: line %d - Bad file format" ,lineno );
496+ #else
466497elog (WARN ,"copy from: Bad file format" );
498+ #endif
467499 }
468500 }
469501 }
502+ #ifdef COPY_PATCH
503+ if (!done ) {
504+ CopyReadNewline (fp ,& newline );
505+ }
506+ #endif
470507 }else {/* binary */
471508fread (& len ,sizeof (int32 ),1 ,fp );
472509if (feof (fp )) {
@@ -773,6 +810,27 @@ inString(char c, char* s)
773810return 0 ;
774811}
775812
813+ #ifdef COPY_PATCH
814+ /*
815+ * Reads input from fp until an end of line is seen.
816+ */
817+
818+ void
819+ CopyReadNewline (FILE * fp ,int * newline )
820+ {
821+ if (!* newline ) {
822+ #ifdef COPY_DEBUG
823+ elog (NOTICE ,"CopyReadNewline: line %d - extra fields ignored" ,
824+ lineno );
825+ #else
826+ elog (NOTICE ,"CopyReadNewline: line - extra fields ignored" );
827+ #endif
828+ while (!feof (fp )&& (getc (fp )!= '\n' ));
829+ }
830+ * newline = 0 ;
831+ }
832+ #endif
833+
776834/*
777835 * Reads input from fp until eof is seen. If we are reading from standard
778836 * input, AND we see a dot on a line by itself (a dot followed immediately
@@ -781,13 +839,25 @@ inString(char c, char* s)
781839 */
782840
783841static char *
842+ #ifdef COPY_PATCH
843+ CopyReadAttribute (FILE * fp ,bool * isnull ,char * delim ,int * newline )
844+ #else
784845CopyReadAttribute (FILE * fp ,bool * isnull ,char * delim )
846+ #endif
785847{
786848static char attribute [EXT_ATTLEN ];
787849char c ;
788850int done = 0 ;
789851int i = 0 ;
790852
853+ #ifdef COPY_PATCH
854+ /* if last delimiter was a newline return a NULL attribute */
855+ if (* newline ) {
856+ * isnull = (bool ) true;
857+ return (NULL );
858+ }
859+ #endif
860+
791861* isnull = (bool ) false;/* set default */
792862if (feof (fp ))
793863return (NULL );
@@ -861,6 +931,11 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim)
861931break ;
862932 }
863933 }else if (inString (c ,delim )|| c == '\n' ) {
934+ #ifdef COPY_PATCH
935+ if (c == '\n' ) {
936+ * newline = 1 ;
937+ }
938+ #endif
864939done = 1 ;
865940 }
866941if (!done )attribute [i ++ ]= c ;