|
Boost : |
From: Vertleyb (vertleyb_at_[hidden])
Date: 2002-04-27 14:44:16
Hi everybody,
We used template meta-programming technics to implement a library that
defines a type-safe "relational table" container, as well as the full set of
relational operations on such containers.
Any type that can be used in an STL container, can be used as a column type.
For example, one could define a table like following:
------------------------------------------
using namespace rel;
COLUMN(c1, int);
COLUMN(c2, std::string);
typedef table<tuple<
list<c1,
list<c2> > > > table_t;
table_t tab;
tab.insert(tuple_t(1, "line1"));
tab.insert(tuple_t(2, "line2"));
print(projection<list<c2> >(tab));
--------------------------------------------
The result will be:
line1
line2
The rel::tuple class differs from tuples::tuple, and is based on the idea of
loki::GenScatterHierarchy suggested by Andrei Alexandrescu (In fact, all the
idea of writing the library was inspired by this book). We didn't used loki
though, since we wanted the library compiled by MSVC (because of the same
reason we use our own implementation of typelist, select, etc.). The tuple
class, therefore, is designed as following:
Each column is represented by a class. The above COLUMN macro unwrappes
into something like this:
class c1 : public column<int>
{
...
};
As is clear from the example, columns are placed into a typelist, and passed
to the tuple class template, who inherits from all of them. Then
static_cast is used
to access individual fields.
We believe using classes as column identifiers is quite benefitial, since
classes are much easier to operate at compile time than strings. We found
no limitations so far.
Each operation is itself a "table", so it's possible to combine them,
something like this:
print(projection<list<c1> >(cross_product(tab1, tab2)));
The library now compiles with MSVC and G++ (thanks to the idea of
"identity", "borrowed" from mpl, the number of #ifdefs is minimal), and
implements the following relational operations:
-selection
-projection
-cross product
-intersection
-difference
-union
as well as some additional operations and utilities:
-fast selection and join based on the sort order
-groupping operations
-print, serialization, etc.
We beleve there is a range of tasks, where one could benefit from the
relational algebra without the need to use an external database and ODBC.
For such applications our library provides the benefits of consistency and
type safety, ensured by the compiler.
Please let us know if there is any interest in such a library.
Arkadiy Vertleyb
Dmitriy Arapov
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk