@@ -525,11 +525,13 @@ ProcessCopyOptions(ParseState *pstate,
525
525
errorConflictingDefElem (defel , pstate );
526
526
format_specified = true;
527
527
if (strcmp (fmt , "text" ) == 0 )
528
- /* default format */ ;
528
+ opts_out -> format = COPY_FORMAT_TEXT ;
529
529
else if (strcmp (fmt , "csv" ) == 0 )
530
- opts_out -> csv_mode = true ;
530
+ opts_out -> format = COPY_FORMAT_CSV ;
531
531
else if (strcmp (fmt , "binary" ) == 0 )
532
- opts_out -> binary = true;
532
+ opts_out -> format = COPY_FORMAT_BINARY ;
533
+ else if (strcmp (fmt , "json" ) == 0 )
534
+ opts_out -> format = COPY_FORMAT_JSON ;
533
535
else
534
536
ereport (ERROR ,
535
537
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
@@ -689,31 +691,47 @@ ProcessCopyOptions(ParseState *pstate,
689
691
* Check for incompatible options (must do these three before inserting
690
692
* defaults)
691
693
*/
692
- if (opts_out -> binary && opts_out -> delim )
694
+ if (opts_out -> format == COPY_FORMAT_BINARY && opts_out -> delim )
693
695
ereport (ERROR ,
694
696
(errcode (ERRCODE_SYNTAX_ERROR ),
695
697
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
696
698
errmsg ("cannot specify %s in BINARY mode" , "DELIMITER" )));
697
699
698
- if (opts_out -> binary && opts_out -> null_print )
700
+ if (opts_out -> format == COPY_FORMAT_JSON && opts_out -> delim )
701
+ ereport (ERROR ,
702
+ errcode (ERRCODE_SYNTAX_ERROR ),
703
+ /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
704
+ errmsg ("cannot specify %s in JSON mode" , "DELIMITER" ));
705
+
706
+ if (opts_out -> format == COPY_FORMAT_BINARY && opts_out -> null_print )
699
707
ereport (ERROR ,
700
708
(errcode (ERRCODE_SYNTAX_ERROR ),
701
709
errmsg ("cannot specify %s in BINARY mode" , "NULL" )));
702
710
703
- if (opts_out -> binary && opts_out -> default_print )
711
+ if (opts_out -> format == COPY_FORMAT_JSON && opts_out -> null_print )
712
+ ereport (ERROR ,
713
+ errcode (ERRCODE_SYNTAX_ERROR ),
714
+ errmsg ("cannot specify %s in JSON mode" , "NULL" ));
715
+
716
+ if (opts_out -> format == COPY_FORMAT_BINARY && opts_out -> default_print )
704
717
ereport (ERROR ,
705
718
(errcode (ERRCODE_SYNTAX_ERROR ),
706
719
errmsg ("cannot specify %s in BINARY mode" , "DEFAULT" )));
707
720
721
+ if (opts_out -> format == COPY_FORMAT_JSON && opts_out -> default_print )
722
+ ereport (ERROR ,
723
+ errcode (ERRCODE_SYNTAX_ERROR ),
724
+ errmsg ("cannot specify %s in JSON mode" , "DEFAULT" ));
725
+
708
726
/* Set defaults for omitted options */
709
727
if (!opts_out -> delim )
710
- opts_out -> delim = opts_out -> csv_mode ? "," : "\t" ;
728
+ opts_out -> delim = opts_out -> format == COPY_FORMAT_CSV ? "," : "\t" ;
711
729
712
730
if (!opts_out -> null_print )
713
- opts_out -> null_print = opts_out -> csv_mode ? "" : "\\N" ;
731
+ opts_out -> null_print = opts_out -> format == COPY_FORMAT_CSV ? "" : "\\N" ;
714
732
opts_out -> null_print_len = strlen (opts_out -> null_print );
715
733
716
- if (opts_out -> csv_mode )
734
+ if (opts_out -> format == COPY_FORMAT_CSV )
717
735
{
718
736
if (!opts_out -> quote )
719
737
opts_out -> quote = "\"" ;
@@ -761,51 +779,56 @@ ProcessCopyOptions(ParseState *pstate,
761
779
* future-proofing. Likewise we disallow all digits though only octal
762
780
* digits are actually dangerous.
763
781
*/
764
- if (! opts_out -> csv_mode &&
782
+ if (opts_out -> format != COPY_FORMAT_CSV &&
765
783
strchr ("\\.abcdefghijklmnopqrstuvwxyz0123456789" ,
766
784
opts_out -> delim [0 ]) != NULL )
767
785
ereport (ERROR ,
768
786
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
769
787
errmsg ("COPY delimiter cannot be \"%s\"" , opts_out -> delim )));
770
788
771
789
/* Check header */
772
- if (opts_out -> binary && opts_out -> header_line )
790
+ if (opts_out -> format == COPY_FORMAT_BINARY && opts_out -> header_line )
773
791
ereport (ERROR ,
774
792
(errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
775
793
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
776
794
errmsg ("cannot specify %s in BINARY mode" , "HEADER" )));
777
795
796
+ if (opts_out -> format == COPY_FORMAT_JSON && opts_out -> header_line )
797
+ ereport (ERROR ,
798
+ errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
799
+ errmsg ("cannot specify %s in JSON mode" , "HEADER" ));
800
+
778
801
/* Check quote */
779
- if (! opts_out -> csv_mode && opts_out -> quote != NULL )
802
+ if (opts_out -> format != COPY_FORMAT_CSV && opts_out -> quote != NULL )
780
803
ereport (ERROR ,
781
804
(errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
782
805
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
783
806
errmsg ("COPY %s requires CSV mode" , "QUOTE" )));
784
807
785
- if (opts_out -> csv_mode && strlen (opts_out -> quote ) != 1 )
808
+ if (opts_out -> format == COPY_FORMAT_CSV && strlen (opts_out -> quote ) != 1 )
786
809
ereport (ERROR ,
787
810
(errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
788
811
errmsg ("COPY quote must be a single one-byte character" )));
789
812
790
- if (opts_out -> csv_mode && opts_out -> delim [0 ] == opts_out -> quote [0 ])
813
+ if (opts_out -> format == COPY_FORMAT_CSV && opts_out -> delim [0 ] == opts_out -> quote [0 ])
791
814
ereport (ERROR ,
792
815
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
793
816
errmsg ("COPY delimiter and quote must be different" )));
794
817
795
818
/* Check escape */
796
- if (! opts_out -> csv_mode && opts_out -> escape != NULL )
819
+ if (opts_out -> format != COPY_FORMAT_CSV && opts_out -> escape != NULL )
797
820
ereport (ERROR ,
798
821
(errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
799
822
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
800
823
errmsg ("COPY %s requires CSV mode" , "ESCAPE" )));
801
824
802
- if (opts_out -> csv_mode && strlen (opts_out -> escape ) != 1 )
825
+ if (opts_out -> format == COPY_FORMAT_CSV && strlen (opts_out -> escape ) != 1 )
803
826
ereport (ERROR ,
804
827
(errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
805
828
errmsg ("COPY escape must be a single one-byte character" )));
806
829
807
830
/* Check force_quote */
808
- if (! opts_out -> csv_mode && (opts_out -> force_quote || opts_out -> force_quote_all ))
831
+ if (opts_out -> format != COPY_FORMAT_CSV && (opts_out -> force_quote || opts_out -> force_quote_all ))
809
832
ereport (ERROR ,
810
833
(errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
811
834
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
@@ -819,8 +842,8 @@ ProcessCopyOptions(ParseState *pstate,
819
842
"COPY FROM" )));
820
843
821
844
/* Check force_notnull */
822
- if (! opts_out -> csv_mode && (opts_out -> force_notnull != NIL ||
823
- opts_out -> force_notnull_all ))
845
+ if (opts_out -> format != COPY_FORMAT_CSV && (opts_out -> force_notnull != NIL ||
846
+ opts_out -> force_notnull_all ))
824
847
ereport (ERROR ,
825
848
(errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
826
849
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
@@ -835,8 +858,8 @@ ProcessCopyOptions(ParseState *pstate,
835
858
"COPY TO" )));
836
859
837
860
/* Check force_null */
838
- if (! opts_out -> csv_mode && (opts_out -> force_null != NIL ||
839
- opts_out -> force_null_all ))
861
+ if (opts_out -> format != COPY_FORMAT_CSV && (opts_out -> force_null != NIL ||
862
+ opts_out -> force_null_all ))
840
863
ereport (ERROR ,
841
864
(errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
842
865
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
@@ -860,7 +883,7 @@ ProcessCopyOptions(ParseState *pstate,
860
883
"NULL" )));
861
884
862
885
/* Don't allow the CSV quote char to appear in the null string. */
863
- if (opts_out -> csv_mode &&
886
+ if (opts_out -> format == COPY_FORMAT_CSV &&
864
887
strchr (opts_out -> null_print , opts_out -> quote [0 ]) != NULL )
865
888
ereport (ERROR ,
866
889
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
@@ -877,6 +900,12 @@ ProcessCopyOptions(ParseState *pstate,
877
900
errmsg ("COPY %s cannot be used with %s" , "FREEZE" ,
878
901
"COPY TO" )));
879
902
903
+ /* Check json format */
904
+ if (opts_out -> format == COPY_FORMAT_JSON && is_from )
905
+ ereport (ERROR ,
906
+ errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
907
+ errmsg ("COPY json mode cannot be used with %s" , "COPY FROM" ));
908
+
880
909
if (opts_out -> default_print )
881
910
{
882
911
if (!is_from )
@@ -896,7 +925,7 @@ ProcessCopyOptions(ParseState *pstate,
896
925
"DEFAULT" )));
897
926
898
927
/* Don't allow the CSV quote char to appear in the default string. */
899
- if (opts_out -> csv_mode &&
928
+ if (opts_out -> format == COPY_FORMAT_CSV &&
900
929
strchr (opts_out -> default_print , opts_out -> quote [0 ]) != NULL )
901
930
ereport (ERROR ,
902
931
(errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
@@ -913,7 +942,7 @@ ProcessCopyOptions(ParseState *pstate,
913
942
errmsg ("NULL specification and DEFAULT specification cannot be the same" )));
914
943
}
915
944
/* Check on_error */
916
- if (opts_out -> binary && opts_out -> on_error != COPY_ON_ERROR_STOP )
945
+ if (opts_out -> format == COPY_FORMAT_BINARY && opts_out -> on_error != COPY_ON_ERROR_STOP )
917
946
ereport (ERROR ,
918
947
(errcode (ERRCODE_SYNTAX_ERROR ),
919
948
errmsg ("only ON_ERROR STOP is allowed in BINARY mode" )));
0 commit comments