Click here to Skip to main content
15,893,487 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 414.2K   5.4K   133  
A helper framework for generation of SQL queries in C++ and Lua
#include <iostream>

#include <sqlpp/test/test_db.hpp>
#include <sqlpp/test/test_fields.hpp>
#include <sqlpp/test/test_joins.hpp>
#include <sqlpp/test/test_predicate.hpp>

using namespace sqlpp::test;

void do_tests( sqlpp::database_shared_ptr db )
{
	test_db(std::cout,db);
	test_simple_query(std::cout,db);
	test_some_fields_query(std::cout,db);
	test_aggregate_query(std::cout,db);
	test_field_value_expression(std::cout,db);
	test_field_field_expression(std::cout,db);
	test_datetime(std::cout,db);

	test_expression(std::cout,db);
	test_joins_query(std::cout,db);
	test_self_joins_query(std::cout,db);
	test_multiple_joins_query(std::cout,db);
	test_field_value_comp(std::cout,db);
	test_binary_op(std::cout,db);
	test_field_field_comp(std::cout,db);
	test_is_null(std::cout,db);
	test_field_between(std::cout,db);
	test_field_like(std::cout,db);
	test_in(std::cout,db);
	test_case(std::cout,db);
	test_set(std::cout,db);
}

int main( int charc, char* argv[])
{
	sqlpp::database_shared_ptr db(create_database(create_adaptor()));
	do_tests(db);

	db->set_adaptor( create_mssql_adaptor() );
	do_tests(db);


	system("pause");
	return 0;
};

/*
	table_shared_ptr clients=db->add_table("Clients","Client");
	table_shared_ptr employees=db->add_table("Employees","Employee");
	table_shared_ptr orders=db->add_table("Orders","Order");
	table_shared_ptr products=db->add_table("Products","Product");
	
	clients->add_primary_key("ID");
	clients->add_field("LastName","varchar(50)",true);
	clients->add_field("FirstName","varchar(50)",true);
	clients->add_unique("ClientLastName,ClientFirstName");

	products->add_primary_key();
	products->add_field("Name","varchar(30)",true);
	products->add_field("Price","decimal(3,2)",true);

	employees->add_primary_key();
	employees->add_field("LastName","varchar(50)",true);
	employees->add_field("FirstName","varchar(50)",true);
	employees->add_unique("EmployeeLastName,EmployeeFirstName");
	employees->add_foreign_key(
		employees,
		"ChiefID",
		false, 
		ref_constraint::EventActionSetNull, 
		ref_constraint::EventActionCascade 
		); // circular reference	

	orders->add_primary_key();
	orders->add_foreign_key( clients );
	orders->add_foreign_key( products );
	orders->add_foreign_key( employees );
	orders->add_field( "Quantity", "int", true);
*/
/*
	std::cout<<db->get_create_statement(true)<<std::endl;

	table_shared_ptr clients = db->get_table("Clients");
	clients->set_field_prefix("Client");
	table_shared_ptr orders = db->get_table("Orders");
	orders->set_field_prefix("Order");
	table_shared_ptr products = db->get_table("Products");
	table_shared_ptr employees = db->get_table("Employees");

	
	// creating a query
	select_query_shared_ptr qr(new select_query(db,true));

	// adding tables
	query_table_table_shared_ptr qo = qr->add_table(orders, "O" );
	query_table_table_shared_ptr qp = qr->join( 
		qo, 
		products, 
		"P" , 
		FullOuterJoin
		);
	query_table_table_shared_ptr qc = qr->join( qo, clients, "C" );
	query_table_table_shared_ptr qe = qr->join( qo, employees, "E" );

	// adding result fields to query
	qr->add_field( qc, clients->get_field("ClientFirstName"), "First Name" );
	qr->add_field( qc, clients->get_field("ClientLastName") );
	qr->add_field( qo, orders->get_field("OrderDate"), "Date" );
	qr->add_field( qp, products->get_field("ProductName"), "Product Name");
	qr->add_field( qe, employees->get_field("EmployeeLastName"), "Vendor");

	// add constraints
	std::vector< string_type > names;
	names.push_back("John");
	names.push_back("Bill");

	query_predicate_shared_ptr 
		p1(is_not_null( qc + clients->get_field("LastName"))),
		p2( qc + clients->get_field("FirstName") == "Peli"),
		p3(not_in( qc + clients->get_field("LastName"), names.begin(), names.end() ) ),
		p4(like( qc + clients->get_field("FirstName"), "pi%")),
		p5( between( qo + orders->get_field("Date"),"10-09-1977", "21-09-77") );

	qr->set_where(
		or(
			and(p1,and(p2,p3)),
			and(p4,p5)
		)
		);


	std::cout<<"query: "<<std::endl<<qr->get_sql()<<std::endl<<std::flush;

}
catch( std::exception& e )
{
	std::cerr<<e.what()<<std::endl<<std::flush;
}


	system("pause");
	return 0;
}
*/

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