Click here to Skip to main content
15,881,089 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 410.2K   5.4K   133  
A helper framework for generation of SQL queries in C++ and Lua
#include <sqlpp/expressions/mathematical_function_expression.hpp>

namespace sqlpp{
namespace expressions{

	unary_mathematical_function_expression::unary_mathematical_function_expression(
		numeric_expression_shared_ptr expression_,
		UnaryMathematicalFunction fcn_
		)
		: m_expression(expression_), m_fcn(fcn_){};
	
	std::ostream& unary_mathematical_function_expression::get_expression(std::ostream& out_
		, adaptors::database_adaptor_shared_ptr adaptor_) const
	{
		out_<<m_fcn<<"( ";
		if (m_expression)
			m_expression->get_expression(out_, adaptor_);
		return out_<<")";
	};

unary_mathematical_function_expression_shared_ptr abs(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionAbs
			)
			);
};
unary_mathematical_function_expression_shared_ptr degrees(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionDegrees
			)
			);
}; 
unary_mathematical_function_expression_shared_ptr rand(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionRand
			)
			);
};
unary_mathematical_function_expression_shared_ptr acos(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionAcos
			)
			);
};
unary_mathematical_function_expression_shared_ptr exp(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionExp
			)
			);
};
unary_mathematical_function_expression_shared_ptr round(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionRound
			)
			);
}; 
unary_mathematical_function_expression_shared_ptr asin(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionAsin
			)
			);
};
unary_mathematical_function_expression_shared_ptr floor(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionFloor
			)
			);
};
unary_mathematical_function_expression_shared_ptr sign(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionSign
			)
			);
}; 
unary_mathematical_function_expression_shared_ptr atan(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionAtan
			)
			);
}; 
unary_mathematical_function_expression_shared_ptr log(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionLog
			)
			);
};

unary_mathematical_function_expression_shared_ptr sin(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionSin
			)
			);
};
unary_mathematical_function_expression_shared_ptr log10(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionLog10
			)
			);
};
unary_mathematical_function_expression_shared_ptr square(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionSquare
			)
			);
};

unary_mathematical_function_expression_shared_ptr ceiling(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionCeiling
			)
			);
};
unary_mathematical_function_expression_shared_ptr pi()
{
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			numeric_expression_shared_ptr(),
			UnaryMathematicalFunctionPi
			)
			);
};

unary_mathematical_function_expression_shared_ptr sqrt(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionSqrt
			)
			);
};
unary_mathematical_function_expression_shared_ptr cos(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionCos
			)
			);
};
unary_mathematical_function_expression_shared_ptr tan(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionTan
			)
			);
};
unary_mathematical_function_expression_shared_ptr cot(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionCot
			)
			);
};

unary_mathematical_function_expression_shared_ptr radians(
	numeric_expression_shared_ptr expression_
	)
{
	if (!expression_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	return unary_mathematical_function_expression_shared_ptr(
		new unary_mathematical_function_expression(
			expression_,
			UnaryMathematicalFunctionRadians
			)
			);
};

	binary_mathematical_function_expression::binary_mathematical_function_expression(
		numeric_expression_shared_ptr first_,
		numeric_expression_shared_ptr second_,
		BinaryMathematicalFunction fcn_
		)
		:m_first(first_), m_second(second_),m_fcn(fcn_){};
	
	std::ostream& binary_mathematical_function_expression::get_expression(std::ostream& out_
		, adaptors::database_adaptor_shared_ptr adaptor_) const
	{
		if (!m_first)
			throw exceptions::sqlpp_exception("math function: expression is null");
		if (!m_second)
			throw exceptions::sqlpp_exception("math function: expression is null");

		out_<<m_fcn<<"( ";
		m_first->get_expression(out_, adaptor_);
		out_<<" , ";
		m_second->get_expression(out_, adaptor_);
		out_<<" )";
		return out_;
	}

binary_mathematical_function_expression_shared_ptr power(
	numeric_expression_shared_ptr first_,
	numeric_expression_shared_ptr second_
	)
{
	if (!first_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	if (!second_)
		throw exceptions::sqlpp_exception("math function: expression is null");

	return binary_mathematical_function_expression_shared_ptr(
		new binary_mathematical_function_expression(
			first_,
			second_,
			BinaryMathematicalFunctionPower
			)
			);
};

binary_mathematical_function_expression_shared_ptr atn2(
	numeric_expression_shared_ptr first_,
	numeric_expression_shared_ptr second_
	)
{
	if (!first_)
		throw exceptions::sqlpp_exception("math function: expression is null");
	if (!second_)
		throw exceptions::sqlpp_exception("math function: expression is null");

	return binary_mathematical_function_expression_shared_ptr(
		new binary_mathematical_function_expression(
			first_,
			second_,
			BinaryMathematicalFunctionAtn2
			)
			);
};


}; // expressions
};//sqlpp

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