We have a legacy C++ DB application, which I'll just oversimplify here: Over a dozen very wide DB tables represent partly similar kinds of data, so there is some overlap in the columns. The schema changes only slightly every few months, but the interface to it is dynamic, where table_name.column_name is looked up and represented by an ID. When we deal with the data in memory, it is all in a list, with each field having its ID associated.
This works well, but addressing data is messy. We have a lookup function for IDs based on a string (get_ID( type_A1, "title" )
), and where we have code dealing with a specific type, colleagues tend to write the IDs literally. I would want to generate symbolic names corresponding to the string, so that much of this can be looked up at compile time. My naive idea went like:
struct ANY {
virtual const int
title, aaa, bbb, ccc, ddd; // ...
}
struct A1 : ANY {
const int
title=17, aaa=29, bbb=5, ddd=27;
}
struct B1 : ANY {
const int
title=71, aaa=92, ccc=45;
}
And usage would be either direct A1::bbb
or B1::aaa
where we know which type we are dealing with or:
const ANY& any = determine_type();
int title_id = any.title;
Alas C++ allows none of this, only methods can be virtual. :-( One solution might be wrapping them in methods:
struct ANY {
virtual int get_title() const = 0;
virtual int get_aaa() const = 0;
}
struct B1 : ANY {
const int
title=71, aaa=92, ccc=45;
int get_title() const { return title; };
int get_aaa() const { return aaa; };
}
For thousands of consts this approach feels so wrong! Another solution might be doing the dynamic part via an indirect name and lookup function:
enum names { title_name, aaa_name, bbb_name, ccc_name };
struct ANY {
virtual int get( names ) const = 0;
}
struct B1 : ANY {
const int
title=71, aaa=92, ccc=45;
static const int[] my_consts = { title, aaa, -1, ccc }; // pseudo code
int get( names n ) const { return my_consts[n]; };
}
This means having all identifiers in two variants – ugly! Does anybody have a clean, intuitive and space efficient solution?