well, we've had to revise our design AGAIN. now, what i need to do is strip out every alphanumeric string (0-9,a-z,_) delimited by question marks, then grab an environment variable of the same name. at the same time, we want to silently pass over anything that sint a solid alphanumeric string.
ie, in the following query, i need to replace
?sql_fild1? with the environment variable
${sql_field1}, and so on for sql_field2 and sort_field. We don't want to even touch the question mark for QnAField, however.
"SELECT ?sql_field1?, ?sql_field2? FROM Table WHERE QnAField = "What is the question?" ORDER BY ?sort_field? ASC"
// should become:
"SELECT PassedField, AnotherPassedField FROM Table WHERE QnAField = "What is the question?" ORDER BY NameField ASC"
ok, ive managed to put together somthing that *finds* the placeholders (and prints them to stdout for debug purposes), im just not sure how to replace them inline with a
getenv() call:
#include <stdio.h>
#include <string.h>
int do_sql_placeholders(char* source)
{
int in_placeholder = 0; // indicate if we are inside a possible placeholder
int pos = 0;
char placeholder[256]; // to store placeholder name
int placepos = 0;
for (pos=0; pos < (signed int)strlen(source); pos++)
{
if (in_placeholder)
{
// if continue placeholder
if (
(source[pos] >= '0' && source[pos] <= '9') ||
(source[pos] >= 'A' && source[pos] <= 'Z') ||
(source[pos] >= 'a' && source[pos] <= 'z') ||
source[pos] == '_'
)
{
placeholder[placepos] = source[pos];
placepos++;
}
// else, end place holder
else
{
// valid terminated placeholder, interpolate
if (source[pos] == '?')
{
placeholder[placepos] = '\0';
/*
instead of printing this out as a match, i need to call getenv(placeholder)
and replace the placeholder in source with the output of getenv
*/
printf("matched placeholder '%s' at position %d\n", placeholder, pos);
}
// cleanup
in_placeholder = 0;
placeholder[0] = '\0';
placepos=0;
}
}
else
{
if (source[pos] == '?') // entering a potential placeholder
{
in_placeholder = 1;
placepos = 0;
}
}
}
return 0;
}
int main()
{
char query[100] = "SELECT ?sql_field1?, ?sql field2? FROM Table WHERE QnAField = 'What is the question?' ORDER BY ?sort_field? ASC";
do_sql_placeholders(query);
printf("query: %s\n", query);
return 0;
}