#include <sqlpp/ref_constraint.hpp>
#include <sqlpp/table.hpp>
#include <sqlpp/exception.hpp>
namespace sqlpp{
ref_constraint::ref_constraint(
table_shared_ptr table_,
string_const_reference name_,
field_container_const_reference fields_,
table_shared_ptr reference_table_,
EventAction on_delete_,
EventAction on_update_,
Match match_
)
:
constraint(table_, name_, fields_),
m_reference_table(reference_table_),
m_on_delete( on_delete_),
m_on_update( on_update_),
m_match( match_)
{};
table_shared_ptr ref_constraint::get_reference_table() const
{
table_shared_ptr t=m_reference_table.lock();
if (!t)
throw exceptions::sqlpp_exception("weak pointer to reference table is deprecated");
return t;
};
string_type ref_constraint::get_sql() const
{
if (empty())
return string_type();
string_type s = constraint::get_sql();
if (s.empty())
return string_type();
table_shared_ptr t = get_reference_table();
table_shared_ptr table=get_checked_table();
std::ostringstream out;
out<<s<<" REFERENCES "<<t->get_name();
if (m_on_delete != EventActionUnknown )
out<<" ON DELETE "<< event_action_to_string(m_on_delete);
if (m_on_update != EventActionUnknown )
out<<" ON UPDATE "<< event_action_to_string(m_on_update);
if (m_match != MatchUnknown)
out<<" MATCH "<< match_to_string(m_match);
return out.str();
};
const char* ref_constraint::event_action_to_string(ref_constraint::EventAction action) const
{
switch(action)
{
case EventActionCascade:
return "CASCADE";
case EventActionSetNull:
return "SET NULL";
case EventActionSetDefault:
return "SET DEFAULT";
case EventActionNoAction:
return "NO ACTION";
case EventActionUnknown:
return "";
default:
return "<unknown event action>";
}
};
const char* ref_constraint::match_to_string(ref_constraint::Match match) const
{
switch(match)
{
case MatchUnknown:
return "";
case MatchFull:
return "FULL";
case MatchPartial:
return "PARTIAL";
default:
return "<unknown match rule>";
};
};
};