summaryrefslogtreecommitdiff
path: root/odb/relational/mysql/schema.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'odb/relational/mysql/schema.cxx')
-rw-r--r--odb/relational/mysql/schema.cxx171
1 files changed, 69 insertions, 102 deletions
diff --git a/odb/relational/mysql/schema.cxx b/odb/relational/mysql/schema.cxx
index 485fa9f..428db1c 100644
--- a/odb/relational/mysql/schema.cxx
+++ b/odb/relational/mysql/schema.cxx
@@ -20,143 +20,110 @@ namespace relational
// Create.
//
- struct create_common: virtual relational::create_common
+ struct create_column: relational::create_column, context
{
- virtual void
- create_table_post ()
- {
- os << ")";
-
- string const& engine (options.mysql_engine ());
-
- if (engine != "default")
- os << endl
- << " ENGINE=" << engine;
-
- os << endl;
- }
- };
-
- struct object_columns: relational::object_columns, context
- {
- object_columns (base const& x): base (x) {}
+ create_column (base const& x): base (x) {}
virtual void
- null (semantics::data_member& m)
+ null (sema_rel::column& c)
{
// MySQL TIMESTAMP is by default NOT NULL. If we want it
// to contain NULL values, we need to explicitly declare
// the column as NULL.
//
- if (context::null (m, prefix_) &&
- column_sql_type (m, prefix_).type == sql_type::TIMESTAMP)
- os << " NULL";
- else
- base::null (m);
+ if (c.null ())
+ {
+ // This should never fail since we have already parsed this.
+ //
+ sql_type const& t (parse_sql_type (c.type ()));
+
+ if (t.type == sql_type::TIMESTAMP)
+ {
+ os << " NULL";
+ return;
+ }
+ }
+
+ base::null (c);
}
virtual void
- default_bool (semantics::data_member&, bool v)
+ auto_ (sema_rel::column&)
{
- // MySQL has TRUE and FALSE as just aliases for 1 and 0. Still
- // use them for self-documentation.
- //
- os << " DEFAULT " << (v ? "TRUE" : "FALSE");
+ os << " AUTO_INCREMENT";
}
+ };
+ entry<create_column> create_column_;
+
+ struct create_foreign_key: relational::create_foreign_key, context
+ {
+ create_foreign_key (base const& x): base (x) {}
virtual void
- default_enum (semantics::data_member& m, tree en, string const& name)
+ traverse (sema_rel::foreign_key& fk)
{
- // Make sure the column is mapped to an ENUM or integer type.
+ // MySQL does not support deferred constraint checking. Output
+ // such foreign keys as comments, for documentation, unless we
+ // are generating embedded schema.
//
- sql_type const& t (column_sql_type (m));
-
- switch (t.type)
- {
- case sql_type::ENUM:
- case sql_type::TINYINT:
- case sql_type::SMALLINT:
- case sql_type::MEDIUMINT:
- case sql_type::INT:
- case sql_type::BIGINT:
- break;
- default:
- {
- cerr << m.file () << ":" << m.line () << ":" << m.column ()
- << ": error: column with default value specified as C++ "
- << "enumerator must map to MySQL ENUM or integer type"
- << endl;
-
- throw operation_failed ();
- }
- }
-
- using semantics::enum_;
- using semantics::enumerator;
-
- enumerator& er (dynamic_cast<enumerator&> (*unit.find (en)));
- enum_& e (er.enum_ ());
-
- if (t.type == sql_type::ENUM)
+ if (fk.deferred ())
{
- // Assuming the enumerators in the C++ enum and MySQL ENUM are
- // in the same order, calculate the poistion of the C++
- // enumerator and use that as an index in the MySQL ENUM.
+ // Don't bloat C++ code with comment strings if we are
+ // generating embedded schema.
//
- size_t pos (0);
-
- for (enum_::enumerates_iterator i (e.enumerates_begin ()),
- end (e.enumerates_end ()); i != end; ++i)
+ if (format_ != schema_format::embedded)
{
- if (&i->enumerator () == &er)
- break;
-
- pos++;
- }
+ os << endl
+ << endl
+ << " /*" << endl;
- if (pos < t.enumerators.size ())
- os << " DEFAULT " << t.enumerators[pos];
- else
- {
- cerr << m.file () << ":" << m.line () << ":" << m.column ()
- << ": error: unable to map C++ enumerator '" << name
- << "' to MySQL ENUM value" << endl;
+ base::create (fk);
- throw operation_failed ();
+ os << endl
+ << " */";
}
}
else
- {
- if (e.unsigned_ ())
- os << " DEFAULT " << er.value ();
- else
- os << " DEFAULT " << static_cast<long long> (er.value ());
- }
+ base::traverse (fk);
}
- virtual void
- constraints (semantics::data_member& m)
+ virtual string
+ name (sema_rel::foreign_key& fk)
{
- base::constraints (m);
-
- if (m.count ("auto"))
- os << " AUTO_INCREMENT";
+ // In MySQL, foreign key names are database-global. Make them
+ // unique by prefixing the key name with table name.
+ //
+ return static_cast<sema_rel::table&> (fk.scope ()).name () +
+ '_' + fk.name ();
}
+ virtual void
+ deferred ()
+ {
+ // MySQL doesn't support deferred.
+ }
};
- entry<object_columns> object_columns_;
+ entry<create_foreign_key> create_foreign_key_;
- struct member_create: relational::member_create, create_common
+ struct create_table: relational::create_table, context
{
- member_create (base const& x): base (x) {}
- };
- entry<member_create> member_create_;
+ create_table (base const& x): base (x) {}
- struct class_create: relational::class_create, create_common
- {
- class_create (base const& x): base (x) {}
+ virtual void
+ create_post ()
+ {
+ os << ")";
+
+ string const& engine (options.mysql_engine ());
+
+ if (engine != "default")
+ os << endl
+ << " ENGINE=" << engine;
+
+ os << endl;
+ }
};
- entry<class_create> class_create_;
+ entry<create_table> create_table_;
}
}
}