aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/ecpg/ecpglib/execute.c
diff options
context:
space:
mode:
authorMichael Meskes <meskes@postgresql.org>2006-08-23 12:01:53 +0000
committerMichael Meskes <meskes@postgresql.org>2006-08-23 12:01:53 +0000
commit9bade3ff84f4538472fc5e2448969e94a873ee45 (patch)
treef104bb833429e4680969460b1cb493baa1799f7d /src/interfaces/ecpg/ecpglib/execute.c
parentefdec1ac1bcef6f65b453efa70241a36c70863aa (diff)
downloadpostgresql-9bade3ff84f4538472fc5e2448969e94a873ee45.tar.gz
postgresql-9bade3ff84f4538472fc5e2448969e94a873ee45.zip
Replaced double-quote-fix with a hopefully better version.
Use initializer string length as size for character strings. Added ecpg_config.h file that is created via configure.
Diffstat (limited to 'src/interfaces/ecpg/ecpglib/execute.c')
-rw-r--r--src/interfaces/ecpg/ecpglib/execute.c86
1 files changed, 41 insertions, 45 deletions
diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c
index bba3b6ee499..7cd23cfc15e 100644
--- a/src/interfaces/ecpg/ecpglib/execute.c
+++ b/src/interfaces/ecpg/ecpglib/execute.c
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.60 2006/08/22 12:46:17 meskes Exp $ */
+/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.61 2006/08/23 12:01:52 meskes Exp $ */
/*
* The aim is to get a simpler inteface to the database routines.
@@ -36,47 +36,43 @@
* escaped.
*/
static char *
-quote_postgres(char *arg, int lineno)
+quote_postgres(char *arg, bool quote, int lineno)
{
- char *res = (char *) ECPGalloc(2 * strlen(arg) + 3, lineno);
- int i, quoted = false,
- ri = 0;
- if (!res)
- return (res);
+ char *res;
+ int i, ri = 0;
- /*
- * We don't know if the target database is using
- * standard_conforming_strings, so we always use E'' strings.
- */
- if (strchr(arg, '\\') != NULL)
- res[ri++] = ESCAPE_STRING_SYNTAX;
-
- i = 0;
- res[ri++] = '\'';
- /* do not quote the string if it is already quoted */
- if (*arg == '\'' && arg[strlen(arg)-1] == '\'')
+ /* if quote is false we just need to store things in a descriptor
+ * they will be quoted once they are inserted in a statement */
+ if (!quote)
+ return res = ECPGstrdup(arg, lineno);
+ else
{
- quoted = true;
- i = 1;
- }
+ res = (char *) ECPGalloc(2 * strlen(arg) + 3, lineno);
+ if (!res)
+ return (res);
- for (; arg[i]; i++, ri++)
- {
- if (SQL_STR_DOUBLE(arg[i], true))
- res[ri++] = arg[i];
- res[ri] = arg[i];
- }
+ /*
+ * We don't know if the target database is using
+ * standard_conforming_strings, so we always use E'' strings.
+ */
+ if (strchr(arg, '\\') != NULL)
+ res[ri++] = ESCAPE_STRING_SYNTAX;
- /* do not quote the string if it is already quoted */
- if (quoted)
- ri--;
- else
res[ri++] = '\'';
- res[ri] = '\0';
+ for (i = 0; arg[i]; i++, ri++)
+ {
+ if (SQL_STR_DOUBLE(arg[i], true))
+ res[ri++] = arg[i];
+ res[ri] = arg[i];
+ }
+
+ res[ri++] = '\'';
+ res[ri] = '\0';
- ECPGfree(arg);
- return res;
+ ECPGfree(arg);
+ return res;
+ }
}
#if defined(__GNUC__) && (defined (__powerpc__) || defined(__amd64__) || defined(__x86_64__))
@@ -522,7 +518,7 @@ ECPGstore_result(const PGresult *results, int act_field,
bool
ECPGstore_input(const int lineno, const bool force_indicator, const struct variable * var,
- const char **tobeinserted_p, bool *malloced_p)
+ const char **tobeinserted_p, bool *malloced_p, bool quote)
{
char *mallocedval = NULL;
char *newcopy = NULL;
@@ -839,7 +835,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
strncpy(newcopy, (char *) var->value, slen);
newcopy[slen] = '\0';
- mallocedval = quote_postgres(newcopy, lineno);
+ mallocedval = quote_postgres(newcopy, quote, lineno);
if (!mallocedval)
return false;
@@ -873,7 +869,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
strncpy(newcopy, variable->arr, variable->len);
newcopy[variable->len] = '\0';
- mallocedval = quote_postgres(newcopy, lineno);
+ mallocedval = quote_postgres(newcopy, quote, lineno);
if (!mallocedval)
return false;
@@ -961,7 +957,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
{
for (element = 0; element < var->arrsize; element++)
{
- str = quote_postgres(PGTYPESinterval_to_asc((interval *) ((var + var->offset * element)->value)), lineno);
+ str = quote_postgres(PGTYPESinterval_to_asc((interval *) ((var + var->offset * element)->value)), quote, lineno);
if (!str)
return false;
slen = strlen(str);
@@ -984,7 +980,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
}
else
{
- str = quote_postgres(PGTYPESinterval_to_asc((interval *) (var->value)), lineno);
+ str = quote_postgres(PGTYPESinterval_to_asc((interval *) (var->value)), quote, lineno);
if (!str)
return false;
slen = strlen(str);
@@ -1015,7 +1011,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
{
for (element = 0; element < var->arrsize; element++)
{
- str = quote_postgres(PGTYPESdate_to_asc(*(date *) ((var + var->offset * element)->value)), lineno);
+ str = quote_postgres(PGTYPESdate_to_asc(*(date *) ((var + var->offset * element)->value)), quote, lineno);
if (!str)
return false;
slen = strlen(str);
@@ -1038,7 +1034,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
}
else
{
- str = quote_postgres(PGTYPESdate_to_asc(*(date *) (var->value)), lineno);
+ str = quote_postgres(PGTYPESdate_to_asc(*(date *) (var->value)), quote, lineno);
if (!str)
return false;
slen = strlen(str);
@@ -1069,7 +1065,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
{
for (element = 0; element < var->arrsize; element++)
{
- str = quote_postgres(PGTYPEStimestamp_to_asc(*(timestamp *) ((var + var->offset * element)->value)), lineno);
+ str = quote_postgres(PGTYPEStimestamp_to_asc(*(timestamp *) ((var + var->offset * element)->value)), quote, lineno);
if (!str)
return false;
@@ -1093,7 +1089,7 @@ ECPGstore_input(const int lineno, const bool force_indicator, const struct varia
}
else
{
- str = quote_postgres(PGTYPEStimestamp_to_asc(*(timestamp *) (var->value)), lineno);
+ str = quote_postgres(PGTYPEStimestamp_to_asc(*(timestamp *) (var->value)), quote, lineno);
if (!str)
return false;
slen = strlen(str);
@@ -1212,7 +1208,7 @@ ECPGexecute(struct statement * stmt)
desc_inlist.ind_varcharsize = desc_inlist.ind_arrsize = 1;
desc_inlist.ind_offset = 0;
}
- if (!ECPGstore_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, &malloced))
+ if (!ECPGstore_input(stmt->lineno, stmt->force_indicator, &desc_inlist, &tobeinserted, &malloced, true))
{
ECPGfree(copiedquery);
return false;
@@ -1230,7 +1226,7 @@ ECPGexecute(struct statement * stmt)
}
else
{
- if (!ECPGstore_input(stmt->lineno, stmt->force_indicator, var, &tobeinserted, &malloced))
+ if (!ECPGstore_input(stmt->lineno, stmt->force_indicator, var, &tobeinserted, &malloced, true))
return false;
}