Click here to Skip to main content
15,895,142 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.2K   5.4K   133  
A helper framework for generation of SQL queries in C++ and Lua
#include <sqlpp/queries/stored_procedure.hpp>
#include <sqlpp/database.hpp>
#include <sqlpp/data_types.hpp>

namespace sqlpp{
namespace queries{

	stored_procedure::stored_procedure(
		select_query_base_shared_ptr body_,
		string_const_reference name_
		)
		: properties::name_property(name_), m_body(body_)
	{};

	std::ostream& stored_procedure::get_sql( std::ostream& out_ )
	{
		using std::endl;

		if (!m_body)
			throw exceptions::sqlpp_exception("stored procedure pointer is null");

		variable_container_type vars = get_variables();
		database_shared_ptr db = m_body->get_checked_database();
		adaptors::database_adaptor_shared_ptr adaptor = db->get_adaptor();

		if (!adaptor->support_stored_procedure())
			throw exceptions::sqlpp_exception("this database does not support stored procedures");

		out_<<"CREATE PROCEDURE "<<get_name()<<endl;

		variable_container_type::iterator it, it_end = vars.end();
		for (
			it = vars.begin();
			it != it_end;
			++it
		)
		{
			(*it)->get_declaration( out_, adaptor );
			out_<<","<<endl;
		}

		out_<<"AS"<<endl
			<<m_body->get_sql()
			<<adaptor->end_statement();

		return out_;
	};

	stored_procedure::variable_container_type stored_procedure::get_variables()
	{
		return variable_container_type();
	};

	stored_procedure_shared_ptr store(
		select_query_base_shared_ptr body_,
		string_const_reference name_
		)
	{
		return stored_procedure_shared_ptr(
			new stored_procedure(
				body_,
				name_
				)
			);
	};

};
};

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