Click here to Skip to main content
15,896,606 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 416.1K   5.4K   133  
A helper framework for generation of SQL queries in C++ and Lua
#include "test.h"

#include <luabind/out_value_policy.hpp>
#include <luabind/return_reference_to_policy.hpp>
#include <luabind/copy_policy.hpp>

namespace
{

	LUABIND_ANONYMOUS_FIX int feedback = 0;

	struct policies_test_class
	{
		std::string name_;

		policies_test_class() { feedback++; }
		policies_test_class(const char* name): name_(name) { feedback++; }
		~policies_test_class() { feedback--; }

		policies_test_class* make(const char* name) const
		{
			return new policies_test_class(name);
		}

		void f(policies_test_class* p) { delete p; }

		const policies_test_class* internal_ref() { return this; }

		policies_test_class* self_ref()
		{
			return this;
		}

//	private:
		policies_test_class(const policies_test_class&) {}
	};

	policies_test_class global;

	void out_val(float* f) { *f = 3.f; }
	policies_test_class* copy_val() { return &global; }

	struct secret_type {};

	secret_type sec_;
	
	secret_type* secret() { return &sec_; }

} // anonymous namespace

bool test_policies()
{
	try
	{
		using namespace luabind;

		lua_State* L = lua_open();
		lua_closer c(L);
		lua_baselibopen(L);

		int top = lua_gettop(L);

		luabind::open(L);

		module(L)
		[
			class_<policies_test_class>("test")
				.def(constructor<>())
				.def("f", &policies_test_class::f, adopt(_1))
				.def("make", &policies_test_class::make, adopt(return_value))
				.def("internal_ref", &policies_test_class::internal_ref, dependency(return_value, self))
				.def("self_ref", &policies_test_class::self_ref, return_reference_to(self)),

			def("out_val", &out_val, pure_out_value(_1)),
			def("copy_val", &copy_val, copy(result)),
			def("secret", &secret, discard_result)
		];

		feedback = 1;

		if (dostring(L, "a = secret()")) return false;
		
		// copy
		if (dostring(L, "a = copy_val()")) return false;
		if (dostring(L, "a = nil")) return false;
		if (dostring(L, "collectgarbage(0)")) return false;
		if (feedback != 0) return false;

		// out_value
		if (dostring(L, "a = out_val()")) return false;

		// return_reference_to
		if (dostring(L, "a = test()")) return false;
		if (feedback != 1) return false;
		if (dostring(L, "b = a:self_ref()")) return false;
		if (dostring(L, "a = nil")) return false;
		if (dostring(L, "collectgarbage(0)")) return false;
		if (feedback != 1) return false;
		if (dostring(L, "b = nil")) return false;
		if (dostring(L, "collectgarbage(0)")) return false;
		if (feedback != 0) return false;
	
		// dependency
		if (dostring(L, "a = test()")) return false;
		if (feedback != 1) return false;
		if (dostring(L, "b = a:internal_ref()")) return false;
		if (dostring(L, "a = nil")) return false;
		if (dostring(L, "collectgarbage(0)")) return false;
		if (feedback != 1) return false;
		if (dostring(L, "b = nil")) return false;
		if (dostring(L, "collectgarbage(0)")) return false; // two gc-cycles because dependency-table won't be collected in the same cycle
		if (dostring(L, "collectgarbage(0)")) return false; // as the object_rep
		if (feedback != 0) return false;

		// adopt
		if (dostring(L, "a = test()")) return false;
		if (feedback != 1) return false;
		if (dostring(L, "b = a:make('tjosan')")) return false;
		if (feedback != 2) return false;
		if (dostring(L, "a:f(b)")) return false;

		if (top != lua_gettop(L)) return false;

		c.release();
		if (feedback != 0) return false;
	}
	catch(...)
	{
		return false;
	}
	return true;
}

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