[odb-users] Different database schemas - postgresql

Boris Kolpackov boris at codesynthesis.com
Wed Dec 14 09:20:58 EST 2011


Hi Konstantin,

Konstantin Mikhailov <ekimka at gmail.com> writes:

> On Tue, Dec 13, 2011 at 9:18 PM, Boris Kolpackov <boris at codesynthesis.com> wrote:
> 
> > 1. By specifying the schema in the 'db table' pragma, as you
> >   have already tried:
> >
> >   #pragma db object table("myschema.test")
> >
> >    Or, to just specify the schema name and get the auto-derived
> >   table name:
> >
> >   #pragma db object table("myschema.")
> >
> >
> This option looks confusing alittle i think. I'm also think that separate
> pragma option to specify database schema/namespace will be more clear.

I agree, the latter case ("myschema.") is a bit stretched and a
separate specifier is probably better, as in:

#pragma db object namespace("myschema")
class test
{
  ...
};

However, I think it is a good idea to allow "myschema.test" as a shortcut
when specifying both the namespace and the table name:

#pragma db object table("myschema.test")
class test
{
  ...
};

With the more verbose option being:

#pragma db object namespace("myschema") table("test")
class test
{
  ...
};


> > 2. By mapping a C++ namespace to a database schema. This will
> >   result in all the persistent classes in this namespace having
> >   tables in this schema. The syntax is not yet finalized, but
> >   it could look something like this:
> >
> >   #pragma db namespace database("myschema")
> >   namespace N
> >   {
> >     ...
> >   }
> >
> In my opinion this option looks like most elegant one  (i prefer
> something shorter abit #pragma db namespace("myschema") for example).

While I agree '#pragma db namespace("myschema")' looks much better,
the problem with this syntax is that it is inconsistent with the
rest of the pragmas, which have the so-called 'qualifier' follow
the 'db' keyword. The qualifier qualifies what kind of C++ object
this pragma applies to (e.g., 'object', 'value', 'member'). So if
we follow this convention, then the syntax should be something like
this:

#pragma db namespace namespace("myschema")
namespace N
{
  namespace M
  {
  }
}

#pragma db namespace(N::M) namespace("myschema1")
#pragma db namespace() namespace("myschema2") // Global namespace.

The 'namespace namespace' part looks odd but I haven't been able
to think of a better combination (if you have any ideas, let me
know).

Also, thinking about the future, we may want to specify other
database aspects on the C++ namespace basis. For example, we
could specify table options that apply to all the persistent
classes in a C++ namespace:

#pragma db namespace table_options("tablespace mytablespace")
namespace N
{
}


> Unfortunately there will be significant overhead to specify database
> schema/namespace in each header file.

You can always create a common header file that provides the C++ namespace
to database namespace mapping and then either #include it into each
persistent class header or include it into the ODB compilation using
the --odb-epilogue option.

In any case, I will try to implement support for something along these
lines in the next week or two. I will let you know when I have something
for you to try.

Boris



More information about the odb-users mailing list