I am a big fan of the use of structs in combination with arrays and the FOR:
syntax when doing queries in Pro*C (in combination with the Pro*C option parse=partial
):
EXEC SQL BEGIN DECLARE SECTION;
struct row_t {
int id;
char name[11];
};
unsigned nrows = 7;
struct row_t* rows = (struct row_t*)malloc(nrows * sizeof(struct row_t*));
EXEC SQL END DECLARE SECTION;
EXEC SQL FOR :nrows select id, name into :rows from my_table;
However, the default CHAR_MAP
in modern Pro*C versions is CHARZ, which does a blank-padding followed by a ” in name
. I prefer to use CHAR_MAP=STRING
to make the variables follow the C-conventions without the blank-padding. I can specify char_map=string
in the command line or at the beginning of the file with exec oracle option (char_map=string)
.
However, in these rare occasions where I can’t modify the default mapping for the entire file (because there’s other legacy functions in the same file that I can’t touch AT ALL), and I don’t want to create a new file for a single query either, I want to specify name
to be mapped to a STRING
external datatype.
However, the EXEC SQL VAR
construct doesn’t work inside structs apparently, only for direct local variables. This works fine:
EXEC SQL BEGIN DECLARE SECTION;
int id;
char name[11];
EXEC SQL VAR name IS STRING(11);
EXEC SQL END DECLARE SECTION;
But this doesn’t:
EXEC SQL BEGIN DECLARE SECTION;
struct row_t {
int id;
char name[11];
EXEC SQL VAR name IS STRING(11);
};
EXEC SQL END DECLARE SECTION;
QUESTION: How can I change the external data mappings for fields inside a struct?
A trick is to change the mapping temporarily by:
void my_query()
{
EXEC ORACLE OPTION (char_map=string);
...
EXEC ORACLE OPTION (char_map=the_original_value);
}
and it works fine, but this creates a relationship between the second OPTION and what is specified in the command line. If the default mapping specified on the command line changes, you have to remember to change that second ORACLE OPTION
as well. It’s a trick that doesn’t scale well.