diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-08-19 11:08:12 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-08-19 11:08:12 +0200 |
commit | 6ed5736f519caf6f3637051a2e142e8ce6575707 (patch) | |
tree | d6ba6e102b2388337ae69b6f3055dc1da5b78840 /odb/relational/pgsql/schema.cxx | |
parent | 280c36de4544fc17dc3d4f1ea168e7d2dc859129 (diff) |
Make container object_id reference the object
Make it ON DELETE CASCADE. Also in erase(), delete the container
rows first.
Diffstat (limited to 'odb/relational/pgsql/schema.cxx')
-rw-r--r-- | odb/relational/pgsql/schema.cxx | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/odb/relational/pgsql/schema.cxx b/odb/relational/pgsql/schema.cxx index 6530e82..191d522 100644 --- a/odb/relational/pgsql/schema.cxx +++ b/odb/relational/pgsql/schema.cxx @@ -136,20 +136,36 @@ namespace relational virtual bool column (semantics::data_member& m, string const& name, bool) { - semantics::class_* c (object_pointer (member_type (m, prefix_))); + if (inverse (m)) + return false; - if (c != 0 && !inverse (m)) + if (semantics::class_* c = object_pointer (member_type (m, prefix_))) { pre_statement (); os << "ALTER TABLE " << quote_id (table_) << endl - << " ADD FOREIGN KEY (" << quote_id (name) << ")" << - endl - << " REFERENCES " << quote_id (table_name (*c)) << endl + << " ADD FOREIGN KEY (" << quote_id (name) << ")" << endl + << " REFERENCES " << table_qname (*c) << endl << " INITIALLY DEFERRED" << endl; post_statement (); } + else if (prefix_ == "id") + { + semantics::class_& c (*context::top_object); + + pre_statement (); + + // We don't need INITIALLY DEFERRED here since the object row + // must exist before any container row. + // + os << "ALTER TABLE " << quote_id (table_) << endl + << " ADD FOREIGN KEY (" << quote_id (name) << ")" << endl + << " REFERENCES " << table_qname (c) << endl + << " ON DELETE CASCADE" << endl; + + post_statement (); + } return true; } @@ -188,6 +204,16 @@ namespace relational type& t (m.type ()); type& vt (container_vt (t)); + // object_id + // + { + object_columns_references ocr (e_, os_, name, "id"); + string id_name (column_name (m, "id", "object_id")); + ocr.column (m, id_name, true); + } + + // value + // if (semantics::class_* cvt = comp_value (vt)) { object_columns_references ocr (e_, os_, name); @@ -196,8 +222,8 @@ namespace relational else { object_columns_references ocr (e_, os_, name, "value"); - string const& name (column_name (m, "value", "value")); - ocr.column (m, name, true); + string const& value_name (column_name (m, "value", "value")); + ocr.column (m, value_name, true); } tables_.insert (name); |