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

namespace sqlpp{
namespace expressions{

std::ostream& operator<<(std::ostream& out_, ArithmeticOperator op_)
{
	switch(op_)
	{
	case ArithmeticOperatorAdd: out_<<'+'; break;
	case ArithmeticOperatorMultiply: out_<<'*'; break;
	case ArithmeticOperatorSubstract: out_<<'-'; break;
	case ArithmeticOperatorDivide: out_<<'/'; break;
	case ArithmeticOperatorModulo: out_<<'%'; break;
	default:
		throw sqlpp::exceptions::sqlpp_exception("ArithmeticOperator: unknown operator");
	};
	return out_;
};

std::ostream& operator<<(std::ostream& out_, UnaryArithmeticOperator op_)
{
	switch(op_)
	{
	case UnaryOperatorPlus: out_<<'+';break;
	case UnaryOperatorMinus: out_<<'-';break;
	case UnaryOperatorTilde: out_<<'~';break;
	default:
		throw sqlpp::exceptions::sqlpp_exception("UnaryArithmeticOperator: unknown operator");
	}
	return out_;
}

std::ostream& operator<<(std::ostream& out_, ComparaisonOperator op_)
{
	switch( op_ )
	{
	case ComparaisonOperatorEquals: out_<<'=';break;
	case ComparaisonOperatorGreaterThan: out_<<'>';break;
	case ComparaisonOperatorGreaterEqualThan: out_<<">=";break;
	case ComparaisonOperatorLessThan: out_<<'<';break;
	case ComparaisonOperatorLessEqualThan: out_<<"<=";break;
	case ComparaisonOperatorNotEqual: out_<<"<>";break;
	case ComparaisonOperatorNotLessThan: out_<<"!<";break;
	case ComparaisonOperatorNotGreaterThan: out_<<"!>";break;
	default:
		throw sqlpp::exceptions::sqlpp_exception("ComparaisonOperator: unknown operator");
	};
	return out_;
}

std::ostream& operator<<(std::ostream& out_, BinaryLogicalOperator op_)
{
	switch( op_)
	{
	case BinaryLogicalOperatorAnd: out_<<"AND";break;
	case BinaryLogicalOperatorOr: out_<<"OR";break;
	default:
		throw sqlpp::exceptions::sqlpp_exception("BinaryLogicalOperator: unknown operator");
	}
	return out_;
};

std::ostream& operator<<(std::ostream& out_, SubqueryComparaisonOperator op_)
{
	switch( op_)
	{
	case SubqueryComparaisonOperatorAny: out_<<"ANY";break;
	case SubqueryComparaisonOperatorAll: out_<<"ALL";break;
	case SubqueryComparaisonOperatorSome: out_<<"SOME";break;
	default:
		throw sqlpp::exceptions::sqlpp_exception("SubqueryComparaisonOperator: unknown operator");
	}
	return out_;
};

std::ostream& operator<<(std::ostream& out_, UnaryMathematicalFunction fcn_)
{
	switch(fcn_)
	{
	case UnaryMathematicalFunctionAbs: out_<<"ABS";break;
	case UnaryMathematicalFunctionDegrees: out_<<"DEGREES";break;
	case UnaryMathematicalFunctionRand: out_<<"RAND";break;
	case UnaryMathematicalFunctionAcos: out_<<"ACOS";break;
	case UnaryMathematicalFunctionExp: out_<<"EXP";break;
	case UnaryMathematicalFunctionRound: out_<<"ROUND";break;
	case UnaryMathematicalFunctionAsin: out_<<"ASIN";break;
	case UnaryMathematicalFunctionFloor: out_<<"FLOOR";break;
	case UnaryMathematicalFunctionSign: out_<<"SIGN";break;
	case UnaryMathematicalFunctionAtan: out_<<"ATAN";break;
	case UnaryMathematicalFunctionLog: out_<<"LOG";break;
	case UnaryMathematicalFunctionSin: out_<<"SIN";break;
	case UnaryMathematicalFunctionLog10: out_<<"LOG10";break;
	case UnaryMathematicalFunctionSquare: out_<<"SQUARE";break;
	case UnaryMathematicalFunctionCeiling: out_<<"CEILING";break;
	case UnaryMathematicalFunctionPi: out_<<"PI";break;
	case UnaryMathematicalFunctionSqrt: out_<<"SQRT";break;
	case UnaryMathematicalFunctionCos: out_<<"COS";break;
	case UnaryMathematicalFunctionTan: out_<<"TAN";break;
	case UnaryMathematicalFunctionCot: out_<<"COT";break;
	case UnaryMathematicalFunctionRadians: out_<<"RADIAN";break;
	default:
		throw exceptions::sqlpp_exception("unknown UnaryMathematicalFunction");
	};

	return out_;
};

std::ostream& operator<<(std::ostream& out_, BinaryMathematicalFunction fcn_)
{
	switch(fcn_)
	{
	case BinaryMathematicalFunctionPower: out_<<"POWER";break;
	case BinaryMathematicalFunctionAtn2: out_<<"ATN2";break;
	default:
		throw exceptions::sqlpp_exception("unknown BinaryMathematicalFunction");
	};

	return out_;
};

std::ostream& operator<<(std::ostream& out_, AggregateFunction fcn_)
{
	switch(fcn_)
	{
	case AggregateFunctionAvg: out_<<"AVG";break;
	case AggregateFunctionMax: out_<<"MAX";break;
	case AggregateFunctionBinary_Checksum: out_<<"BINARY_CHECKSUM";break;
	case AggregateFunctionMin: out_<<"MIN";break;
	case AggregateFunctionChecksum: out_<<"CHECKSUM";break;
	case AggregateFunctionSum: out_<<"SUM";break;
	case AggregateFunctionChecksum_Agg: out_<<"CHECKSUM_AGG";break;
	case AggregateFunctionStdev: out_<<"STDEV";break;
	case AggregateFunctionCount: out_<<"COUNT";break;
	case AggregateFunctionStdevp: out_<<"STDEVP";break;
	case AggregateFunctionCount_Big: out_<<"COUNT_BIG";break;
	case AggregateFunctionVar: out_<<"VAR";break;
	case AggregateFunctionGrouping: out_<<"GROUPING";break;
	case AggregateFunctionVarp: out_<<"VARP";break;
	default:
		throw exceptions::sqlpp_exception("unknown AggregateFunction");
	};

	return out_;
};

}; // 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