summaryrefslogtreecommitdiff
path: root/odb/tracer/source.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-07-22 14:33:21 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-07-22 14:33:21 +0200
commitcea6fb57ac8c9a893c0f404fef6c1469f0b6222b (patch)
treefed8b6ffa8ea2cb6347ece69c0cb81003d0ccbf6 /odb/tracer/source.cxx
parent5f71c55a1c24c23af1eeb0d664922497a0e5c071 (diff)
Next chunk of functionality
Add SQL language lexer. Implement MySQL type declaration parser. Create sub-directories for databases, currently mysql and tracer. Create MySQL-specific context.
Diffstat (limited to 'odb/tracer/source.cxx')
-rw-r--r--odb/tracer/source.cxx147
1 files changed, 147 insertions, 0 deletions
diff --git a/odb/tracer/source.cxx b/odb/tracer/source.cxx
new file mode 100644
index 0000000..e70adf1
--- /dev/null
+++ b/odb/tracer/source.cxx
@@ -0,0 +1,147 @@
+// file : odb/tracer/source.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#include <odb/common.hxx>
+#include <odb/tracer/source.hxx>
+
+namespace
+{
+ struct class_: traversal::class_, context
+ {
+ class_ (context& c)
+ : context (c)
+ {
+ }
+
+ virtual void
+ traverse (type& c)
+ {
+ if (c.file () != unit.file ())
+ return;
+
+ if (!c.count ("object"))
+ return;
+
+ string const& type (c.fq_name ());
+ string traits ("access::object_traits< " + type + " >");
+
+ id_member t (*this);
+ t.traverse (c);
+ semantics::data_member& id (*t.member ());
+
+ os << "// " << c.name () << endl
+ << "//" << endl
+ << endl;
+
+ // type_name ()
+ //
+ os << "const char* " << traits << "::" << endl
+ << "type_name ()"
+ << "{"
+ << "return \"" << type << "\";"
+ << "}";
+
+ // persist ()
+ //
+ os << "void " << traits << "::" << endl
+ << "persist (database&, object_type& obj)"
+ << "{"
+ << "std::cout << \"insert \" << type_name () << \" id \" << " <<
+ "id (obj) << std::endl;"
+ << endl
+ << "if (id (obj) == id_type ())" << endl
+ << "throw object_already_persistent ();"
+ << "}";
+
+ // store ()
+ //
+ os << "void " << traits << "::" << endl
+ << "store (database&, object_type& obj)"
+ << "{"
+ << "std::cout << \"update \" << type_name () << \" id \" << " <<
+ "id (obj) << std::endl;"
+ << endl
+ << "if (id (obj) == id_type ())" << endl
+ << "throw object_not_persistent ();"
+ << "}";
+
+ // erase ()
+ //
+ os << "void " << traits << "::" << endl
+ << "erase (database&, const id_type& id)"
+ << "{"
+ << "std::cout << \"delete \" << type_name () << \" id \" << " <<
+ "id << std::endl;"
+ << endl
+ << "if (id == id_type ())" << endl
+ << "throw object_not_persistent ();"
+ << "}";
+
+ // find ()
+ //
+ os << traits << "::pointer_type" << endl
+ << traits << "::" << endl
+ << "find (database&, const id_type& id)"
+ << "{"
+ << "std::cout << \"select \" << type_name () << \" id \" << " <<
+ "id << std::endl;"
+ << endl
+ << "if (id == id_type ())" << endl
+ << "return pointer_type (0);"
+ << endl
+ << "pointer_type r (access::object_factory< " << type <<
+ " >::create ());"
+ << "r->" << id.name () << " = id;"
+ << "return r;"
+ << "}";
+
+ os << "bool " << traits << "::" << endl
+ << "find (database&, const id_type& id, object_type& obj)"
+ << "{"
+ << "std::cout << \"select \" << type_name () << \" id \" << " <<
+ "id << std::endl;"
+ << endl
+ << "if (id == id_type ())" << endl
+ << "return false;"
+ << endl
+ << "obj." << id.name () << " = id;"
+ << "return true;"
+ << "}";
+ }
+ };
+}
+
+namespace tracer
+{
+ void
+ generate_source (context& ctx)
+ {
+ traversal::unit unit;
+ traversal::defines unit_defines;
+ traversal::namespace_ ns;
+ class_ c (ctx);
+
+ unit >> unit_defines >> ns;
+ unit_defines >> c;
+
+ traversal::defines ns_defines;
+
+ ns >> ns_defines >> ns;
+ ns_defines >> c;
+
+ ctx.os << "#include <iostream>" << endl
+ << endl;
+
+ ctx.os << "#include <odb/exceptions.hxx>" << endl
+ << endl;
+
+ ctx.os << "namespace odb"
+ << "{";
+
+ unit.dispatch (ctx.unit);
+
+ ctx.os << "}";
+ }
+}