Click here to Skip to main content
15,896,278 members
Articles / Database Development / SQL Server

A scripted SQL query generation framework with IDE: SQLpp (v1.4)

Rate me:
Please Sign up or sign in to vote.
4.98/5 (47 votes)
12 Sep 200311 min read 415.8K   5.4K   133  
A helper framework for generation of SQL queries in C++ and Lua
#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>";
	};
};

};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions