OiO.lk Blog C# How to specify in Pro*C datatype equivalences for struct fields?
C#

How to specify in Pro*C datatype equivalences for struct fields?


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 ‘\0’ 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=charz);
}

and this will work fine, but this has the problem that if tomorrow it’s decided that the default mapping must change to char_map=string in the command line options, it will hardcoded the charz mapping from that point and below, and I’m not aware of any way to specify something equivalent to: EXEC ORACLE OPTION (char_map=command_line_value).



You need to sign in to view this answers

Exit mobile version