aboutsummaryrefslogtreecommitdiff
path: root/odb
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2012-04-26 11:29:05 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2012-04-26 11:29:05 +0200
commit8e046e3ea7e88f872205c2b65cb7f9b86aaabd93 (patch)
treeaba1ae331454eb1e0cf9525faf8a084d3652bda0 /odb
parent6c88333c2e0232aed9e0b3c9077306f09e36c65c (diff)
Make session optional
Diffstat (limited to 'odb')
-rw-r--r--odb/context.hxx6
-rw-r--r--odb/features.hxx1
-rw-r--r--odb/header.cxx5
-rw-r--r--odb/options.cli11
-rw-r--r--odb/pragma.cxx47
-rw-r--r--odb/relational/header.cxx40
-rw-r--r--odb/relational/processor.cxx66
-rw-r--r--odb/relational/source.hxx2
-rw-r--r--odb/validator.cxx37
9 files changed, 170 insertions, 45 deletions
diff --git a/odb/context.hxx b/odb/context.hxx
index cdb49ac..2bb91cb 100644
--- a/odb/context.hxx
+++ b/odb/context.hxx
@@ -381,6 +381,12 @@ public:
}
static bool
+ session (semantics::class_& c)
+ {
+ return c.get<bool> ("session");
+ }
+
+ static bool
transient (semantics::data_member& m)
{
return m.count ("transient");
diff --git a/odb/features.hxx b/odb/features.hxx
index 0468acb..359d99e 100644
--- a/odb/features.hxx
+++ b/odb/features.hxx
@@ -18,6 +18,7 @@ struct features
bool simple_object;
bool polymorphic_object;
bool no_id_object;
+ bool session_object;
bool view;
};
diff --git a/odb/header.cxx b/odb/header.cxx
index a2b3b96..8e6718d 100644
--- a/odb/header.cxx
+++ b/odb/header.cxx
@@ -46,6 +46,11 @@ namespace header
os << "#include <odb/container-traits.hxx>" << endl;
+ if (ctx.features.session_object)
+ os << "#include <odb/cache-traits.hxx>" << endl;
+ else
+ os << "#include <odb/no-op-cache-traits.hxx>" << endl;
+
if (ctx.features.polymorphic_object)
os << "#include <odb/polymorphic-info.hxx>" << endl;
diff --git a/odb/options.cli b/odb/options.cli
index 95a5d4c..d205039 100644
--- a/odb/options.cli
+++ b/odb/options.cli
@@ -54,8 +54,15 @@ class options
bool --generate-query | -q
{
- "Generate query support code. Without this support you cannot use
- views and can only load objects via their ids."
+ "Generate query support code. Without this support you cannot use views
+ and can only load objects via their ids."
+ };
+
+ bool --generate-session | -e
+ {
+ "Generate session support code. With this option session support will
+ be enabled by default for all the persistent classes except those for
+ which it was explicitly disabled using the \cb{db session} pragma."
};
bool --generate-schema | -s
diff --git a/odb/pragma.cxx b/odb/pragma.cxx
index 698551a..e2dc4cc 100644
--- a/odb/pragma.cxx
+++ b/odb/pragma.cxx
@@ -493,6 +493,17 @@ check_spec_decl_type (tree d,
return false;
}
}
+ else if (p == "session")
+ {
+ // Session can be used only for namespaces and objects.
+ //
+ if (tc != NAMESPACE_DECL && !CLASS_TYPE_P (d))
+ {
+ error (l) << "name '" << name << "' in db pragma " << p << " does "
+ << "not refer to a namespace or class" << endl;
+ return false;
+ }
+ }
else if (p == "schema")
{
// For now schema can be used only for namespaces and objects.
@@ -727,6 +738,42 @@ handle_pragma (cpp_reader* reader,
tt = pragma_lex (&t);
}
+ else if (p == "session")
+ {
+ // session
+ // session (true|false)
+ //
+
+ // Make sure we've got the correct declaration type.
+ //
+ if (decl != 0 && !check_spec_decl_type (decl, decl_name, p, loc))
+ return;
+
+ tt = pragma_lex (&t);
+
+ if (tt == CPP_OPEN_PAREN)
+ {
+ tt = pragma_lex (&t);
+
+ string s;
+ if (tt != CPP_NAME ||
+ ((s = IDENTIFIER_POINTER (t)) != "true" && s != "false"))
+ {
+ error () << "true or false expected in db pragma " << p << endl;
+ return;
+ }
+
+ tt = pragma_lex (&t);
+ if (tt != CPP_CLOSE_PAREN)
+ {
+ error () << "')' expected at the end of db pragma " << p << endl;
+ return;
+ }
+
+ val = (s == "true");
+ tt = pragma_lex (&t);
+ }
+ }
else if (p == "schema")
{
// schema (<name>)
diff --git a/odb/relational/header.cxx b/odb/relational/header.cxx
index 46ba722..48bca91 100644
--- a/odb/relational/header.cxx
+++ b/odb/relational/header.cxx
@@ -384,25 +384,31 @@ traverse_object (type& c)
<< "reference_cache_traits;"
<< endl;
}
- else if (poly_derived)
- {
- os << "typedef" << endl
- << "odb::pointer_cache_traits<root_traits::pointer_type>" << endl
- << "pointer_cache_traits;"
- << "typedef" << endl
- << "odb::reference_cache_traits<root_type>" << endl
- << "reference_cache_traits;"
- << endl;
- }
else
{
- os << "typedef" << endl
- << "odb::pointer_cache_traits<pointer_type>" << endl
- << "pointer_cache_traits;"
- << "typedef" << endl
- << "odb::reference_cache_traits<object_type>" << endl
- << "reference_cache_traits;"
- << endl;
+ char const* p (session (c) ? "" : "no_op_");
+
+ if (poly_derived)
+ {
+ os << "typedef" << endl
+ << "odb::" << p << "pointer_cache_traits<" <<
+ "root_traits::pointer_type>" << endl
+ << "pointer_cache_traits;"
+ << "typedef" << endl
+ << "odb::" << p << "reference_cache_traits<root_type>" << endl
+ << "reference_cache_traits;"
+ << endl;
+ }
+ else
+ {
+ os << "typedef" << endl
+ << "odb::" << p << "pointer_cache_traits<pointer_type>" << endl
+ << "pointer_cache_traits;"
+ << "typedef" << endl
+ << "odb::" << p << "reference_cache_traits<object_type>" << endl
+ << "reference_cache_traits;"
+ << endl;
+ }
}
// Statements typedefs.
diff --git a/odb/relational/processor.cxx b/odb/relational/processor.cxx
index cdacfd4..c45d71f 100644
--- a/odb/relational/processor.cxx
+++ b/odb/relational/processor.cxx
@@ -1820,13 +1820,63 @@ namespace relational
virtual void
traverse_object_pre (type& c)
{
- if (semantics::class_* root = polymorphic (c))
+ semantics::class_* poly_root (polymorphic (c));
+
+ // Determine whether it is a session object.
+ //
+ if (!c.count ("session"))
+ {
+ // If this is a derived class in a polymorphic hierarchy,
+ // then it should have the same session value as the root.
+ //
+ if (poly_root != 0 && poly_root != &c)
+ c.set ("session", session (*poly_root));
+ else
+ {
+ // See if any of the namespaces containing this class specify
+ // the session value.
+ //
+ bool found (false);
+ for (semantics::scope* s (&c.scope ());; s = &s->scope_ ())
+ {
+ using semantics::namespace_;
+
+ namespace_* ns (dynamic_cast<namespace_*> (s));
+
+ if (ns == 0)
+ continue; // Some other scope.
+
+ if (ns->extension ())
+ ns = &ns->original ();
+
+ if (ns->count ("session"))
+ {
+ c.set ("session", ns->get<bool> ("session"));
+ found = true;
+ break;
+ }
+
+ if (ns->global_scope ())
+ break;
+ }
+
+ // If still not found, then use the default value.
+ //
+ if (!found)
+ c.set ("session", options.generate_session ());
+ }
+ }
+
+ if (session (c))
+ features.session_object = true;
+
+ if (poly_root != 0)
{
using namespace semantics;
- semantics::data_member& idm (*id_member (*root));
+ semantics::data_member& idm (*id_member (*poly_root));
- if (root != &c)
+ if (poly_root != &c)
{
// If we are a derived class in the polymorphic persistent
// class hierarchy, then add a synthesized virtual pointer
@@ -1846,23 +1896,23 @@ namespace relational
// Use the raw pointer as this member's type.
//
- if (!root->pointed_p ())
+ if (!poly_root->pointed_p ())
{
// Create the pointer type in the graph. The pointer node
// in GCC seems to always be present, even if not explicitly
// used in the translation unit.
//
- tree t (root->tree_node ());
+ tree t (poly_root->tree_node ());
tree ptr (TYPE_POINTER_TO (t));
assert (ptr != 0);
ptr = TYPE_MAIN_VARIANT (ptr);
pointer& p (unit.new_node<pointer> (f, l, col, ptr));
unit.insert (ptr, p);
- unit.new_edge<points> (p, *root);
- assert (root->pointed_p ());
+ unit.new_edge<points> (p, *poly_root);
+ assert (poly_root->pointed_p ());
}
- unit.new_edge<belongs> (m, root->pointed ().pointer ());
+ unit.new_edge<belongs> (m, poly_root->pointed ().pointer ());
m.set ("not-null", true);
m.set ("deferred", false);
diff --git a/odb/relational/source.hxx b/odb/relational/source.hxx
index 42ff235..bbea0a7 100644
--- a/odb/relational/source.hxx
+++ b/odb/relational/source.hxx
@@ -3368,8 +3368,6 @@ namespace relational
os << endl;
- os << "#include <odb/cache-traits.hxx>" << endl;
-
if (features.polymorphic_object)
os << "#include <odb/polymorphic-map.hxx>" << endl;
diff --git a/odb/validator.cxx b/odb/validator.cxx
index d346b25..eac5086 100644
--- a/odb/validator.cxx
+++ b/odb/validator.cxx
@@ -1047,7 +1047,7 @@ namespace
if (id != 0)
{
- if (semantics::class_* c = composite_wrapper (utype (*id)))
+ if (semantics::class_* cm = composite_wrapper (utype (*id)))
{
// Composite id cannot be auto.
//
@@ -1065,7 +1065,7 @@ namespace
//
if (valid_)
{
- composite_id_members_.traverse (*c);
+ composite_id_members_.traverse (*cm);
if (!valid_)
os << id->file () << ":" << id->line () << ":" << id->column ()
@@ -1074,28 +1074,29 @@ namespace
// Check that the composite value type is default-constructible.
//
- if (!c->default_ctor ())
+ if (!cm->default_ctor ())
{
- os << c->file () << ":" << c->line () << ":" << c->column ()
+ os << cm->file () << ":" << cm->line () << ":" << cm->column ()
<< ": error: composite value type that is used as object id "
<< "is not default-constructible" << endl;
- os << c->file () << ":" << c->line () << ":" << c->column ()
- << ": info: provide default constructor for this value type"
- << endl;
+ os << cm->file () << ":" << cm->line () << ":" << cm->column ()
+ << ": info: provide default constructor for this value type"
+ << endl;
- os << id->file () << ":" << id->line () << ":" << id->column ()
- << ": info: composite id is defined here" << endl;
+ os << id->file () << ":" << id->line () << ":" << id->column ()
+ << ": info: composite id is defined here" << endl;
- valid_ = false;
+ valid_ = false;
}
- // Check that composite values can be compared (used in session).
+ // If this is a session object, make sure that the composite
+ // value can be compared.
//
- if (has_lt_operator_ != 0)
+ if (session (c) && has_lt_operator_ != 0)
{
tree args (make_tree_vec (1));
- TREE_VEC_ELT (args, 0) = c->tree_node ();
+ TREE_VEC_ELT (args, 0) = cm->tree_node ();
tree inst (
instantiate_template (
@@ -1126,17 +1127,21 @@ namespace
if (!v)
{
- os << c->file () << ":" << c->line () << ":" << c->column ()
+ os << cm->file () << ":" << cm->line () << ":" << cm->column ()
<< ": error: composite value type that is used as object id "
- << "does not support the less than (<) comparison"
+ << "in persistent class with session support does not "
+ << "define the less than (<) comparison"
<< endl;
- os << c->file () << ":" << c->line () << ":" << c->column ()
+ os << cm->file () << ":" << cm->line () << ":" << cm->column ()
<< ": info: provide operator< for this value type" << endl;
os << id->file () << ":" << id->line () << ":" << id->column ()
<< ": info: composite id is defined here" << endl;
+ os << c.file () << ":" << c.line () << ":" << c.column ()
+ << ": info: persistent class is defined here" << endl;
+
valid_ = false;
}
}