1- /* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.60 2006/08/22 12:46:17 meskes Exp $ */
1+ /* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.61 2006/08/23 12:01:52 meskes Exp $ */
22
33/*
44 * The aim is to get a simpler inteface to the database routines.
3636 *escaped.
3737 */
3838static char *
39- quote_postgres (char * arg ,int lineno )
39+ quote_postgres (char * arg ,bool quote , int lineno )
4040{
41- char * res = (char * )ECPGalloc (2 * strlen (arg )+ 3 ,lineno );
42- int i ,quoted = false,
43- ri = 0 ;
44- if (!res )
45- return (res );
41+ char * res ;
42+ int i ,ri = 0 ;
4643
47- /*
48- *We don't know if the target database is using
49- *standard_conforming_strings, so we always use E'' strings.
50- */
51- if (strchr (arg ,'\\' )!= NULL )
52- res [ri ++ ]= ESCAPE_STRING_SYNTAX ;
53-
54- i = 0 ;
55- res [ri ++ ]= '\'' ;
56- /* do not quote the string if it is already quoted */
57- if (* arg == '\'' && arg [strlen (arg )- 1 ]== '\'' )
44+ /* if quote is false we just need to store things in a descriptor
45+ * they will be quoted once they are inserted in a statement */
46+ if (!quote )
47+ return res = ECPGstrdup (arg ,lineno );
48+ else
5849{
59- quoted = true ;
60- i = 1 ;
61- }
50+ res = ( char * ) ECPGalloc ( 2 * strlen ( arg ) + 3 , lineno ) ;
51+ if (! res )
52+ return ( res );
6253
63- for (; arg [ i ]; i ++ , ri ++ )
64- {
65- if ( SQL_STR_DOUBLE ( arg [ i ], true))
66- res [ ri ++ ] = arg [ i ];
67- res [ ri ] = arg [ i ];
68- }
54+ /*
55+ *We don't know if the target database is using
56+ *standard_conforming_strings, so we always use E'' strings.
57+ */
58+ if ( strchr ( arg , '\\' ) != NULL )
59+ res [ ri ++ ] = ESCAPE_STRING_SYNTAX ;
6960
70- /* do not quote the string if it is already quoted */
71- if (quoted )
72- ri -- ;
73- else
7461res [ri ++ ]= '\'' ;
7562
76- res [ri ]= '\0' ;
63+ for (i = 0 ;arg [i ];i ++ ,ri ++ )
64+ {
65+ if (SQL_STR_DOUBLE (arg [i ], true))
66+ res [ri ++ ]= arg [i ];
67+ res [ri ]= arg [i ];
68+ }
69+
70+ res [ri ++ ]= '\'' ;
71+ res [ri ]= '\0' ;
7772
78- ECPGfree (arg );
79- return res ;
73+ ECPGfree (arg );
74+ return res ;
75+ }
8076}
8177
8278#if defined(__GNUC__ )&& (defined (__powerpc__ )|| defined(__amd64__ )|| defined(__x86_64__ ))
@@ -522,7 +518,7 @@ ECPGstore_result(const PGresult *results, int act_field,
522518
523519bool
524520ECPGstore_input (const int lineno ,const bool force_indicator ,const struct variable * var ,
525- const char * * tobeinserted_p ,bool * malloced_p )
521+ const char * * tobeinserted_p ,bool * malloced_p , bool quote )
526522{
527523char * mallocedval = NULL ;
528524char * newcopy = NULL ;
@@ -839,7 +835,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
839835strncpy (newcopy , (char * )var -> value ,slen );
840836newcopy [slen ]= '\0' ;
841837
842- mallocedval = quote_postgres (newcopy ,lineno );
838+ mallocedval = quote_postgres (newcopy ,quote , lineno );
843839if (!mallocedval )
844840return false;
845841
@@ -873,7 +869,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
873869strncpy (newcopy ,variable -> arr ,variable -> len );
874870newcopy [variable -> len ]= '\0' ;
875871
876- mallocedval = quote_postgres (newcopy ,lineno );
872+ mallocedval = quote_postgres (newcopy ,quote , lineno );
877873if (!mallocedval )
878874return false;
879875
@@ -961,7 +957,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
961957{
962958for (element = 0 ;element < var -> arrsize ;element ++ )
963959{
964- str = quote_postgres (PGTYPESinterval_to_asc ((interval * ) ((var + var -> offset * element )-> value )),lineno );
960+ str = quote_postgres (PGTYPESinterval_to_asc ((interval * ) ((var + var -> offset * element )-> value )),quote , lineno );
965961if (!str )
966962return false;
967963slen = strlen (str );
@@ -984,7 +980,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
984980}
985981else
986982{
987- str = quote_postgres (PGTYPESinterval_to_asc ((interval * ) (var -> value )),lineno );
983+ str = quote_postgres (PGTYPESinterval_to_asc ((interval * ) (var -> value )),quote , lineno );
988984if (!str )
989985return false;
990986slen = strlen (str );
@@ -1015,7 +1011,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
10151011{
10161012for (element = 0 ;element < var -> arrsize ;element ++ )
10171013{
1018- str = quote_postgres (PGTYPESdate_to_asc (* (date * ) ((var + var -> offset * element )-> value )),lineno );
1014+ str = quote_postgres (PGTYPESdate_to_asc (* (date * ) ((var + var -> offset * element )-> value )),quote , lineno );
10191015if (!str )
10201016return false;
10211017slen = strlen (str );
@@ -1038,7 +1034,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
10381034}
10391035else
10401036{
1041- str = quote_postgres (PGTYPESdate_to_asc (* (date * ) (var -> value )),lineno );
1037+ str = quote_postgres (PGTYPESdate_to_asc (* (date * ) (var -> value )),quote , lineno );
10421038if (!str )
10431039return false;
10441040slen = strlen (str );
@@ -1069,7 +1065,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
10691065{
10701066for (element = 0 ;element < var -> arrsize ;element ++ )
10711067{
1072- str = quote_postgres (PGTYPEStimestamp_to_asc (* (timestamp * ) ((var + var -> offset * element )-> value )),lineno );
1068+ str = quote_postgres (PGTYPEStimestamp_to_asc (* (timestamp * ) ((var + var -> offset * element )-> value )),quote , lineno );
10731069if (!str )
10741070return false;
10751071
@@ -1093,7 +1089,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
10931089}
10941090else
10951091{
1096- str = quote_postgres (PGTYPEStimestamp_to_asc (* (timestamp * ) (var -> value )),lineno );
1092+ str = quote_postgres (PGTYPEStimestamp_to_asc (* (timestamp * ) (var -> value )),quote , lineno );
10971093if (!str )
10981094return false;
10991095slen = strlen (str );
@@ -1212,7 +1208,7 @@ ECPGexecute(struct statement * stmt)
12121208desc_inlist .ind_varcharsize = desc_inlist .ind_arrsize = 1 ;
12131209desc_inlist .ind_offset = 0 ;
12141210}
1215- if (!ECPGstore_input (stmt -> lineno ,stmt -> force_indicator ,& desc_inlist ,& tobeinserted ,& malloced ))
1211+ if (!ECPGstore_input (stmt -> lineno ,stmt -> force_indicator ,& desc_inlist ,& tobeinserted ,& malloced , true ))
12161212{
12171213ECPGfree (copiedquery );
12181214return false;
@@ -1230,7 +1226,7 @@ ECPGexecute(struct statement * stmt)
12301226}
12311227else
12321228{
1233- if (!ECPGstore_input (stmt -> lineno ,stmt -> force_indicator ,var ,& tobeinserted ,& malloced ))
1229+ if (!ECPGstore_input (stmt -> lineno ,stmt -> force_indicator ,var ,& tobeinserted ,& malloced , true ))
12341230return false;
12351231}
12361232