[odb-users] Automatic generation of C++ classes from database schema

Boris Kolpackov boris at codesynthesis.com
Sun Feb 2 23:48:05 EST 2014

Hi Per,

Per Edin <info at peredin.com> writes:

> Regarding the "rough draft" approach, this could be achieved by
> including a region in all generated files that will be preserved when
> updating the schema.

Yes, that's one of the ideas I outlined in my original email except
I am not as enthusiastic about it as you are ;-). IMO, any king of
automatic extraction of user changes from generated code will be
very brittle and generally is asking for trouble. The only reason
I am considering this idea is because we can do proper C++ parsing
of the generated header. Which means, for example, that we don't
have to rely on some special comments to indicate the beginning
and the end of the hand-written code (which is how it is normally
done) but instead could use language (or, more precisely, preprocessor)
constructs like pragmas:

class foo
  ... // Generated accessors/modifiers.

#pragma db begin_custom
#pragma db end_custom

  ... // Generated data members, etc.

There are still a lot of issue with this approach, however. For example,
what happens if you rename table foo to bar. Now ODB sees custom code
for class foo but there is no class foo anymore. ODB also cannot be
expected to know that these changes need to be moved to class bar.
More generally, what do we do with user changes that cannot be added
back to the new generated code? We cannot just drop them.

Probably the most pragmatic way to handle this is to save the old
file and issue warnings for each case where old code could not
have been "transplanted".

Thanks for your thoughts!

More information about the odb-users mailing list