#include <rude/database.h>
#include <rude/config.h>
#include <iostream>

using namespace std;
using namespace rude;


void doStuff();


int main(void)
{
	rude::Config config;
	
	if(!config.load("dbcontexts.ini"))
	{
		cerr << "Could not load dbparams.ini" << config.getError() << "\n";
		return -1;
	}

 	config.setSection("DB Context Index");

	int numcontexts=config.getNumDataMembers();

	for(int x=0; x< numcontexts; x++)
	{
		config.setSection("DB Context Index");	
		string contextname=config.getDataNameAt(x);
		  
		if( config.setSection(contextname.c_str(), false) ) // the false means "don't create the section if it is missing"
		{
		  Database::addContext(	contextname.c_str(),
							config.getStringValue("database"),
							config.getStringValue("username"), 
							config.getStringValue("password"),
							config.getStringValue("server"),
							config.getIntValue("port")
						);
		}
		else
		{
       	cerr << "Could not find definition for Database Context " << contextname << "\n";
			return -1; 
		}
	}


	// Your application here ...
	//
	doStuff();
	

   Database::finish(); 
   return 0; 

}


void doStuff()
{
	// let's use a read-only context
	//
	Database *database = Database::instance("readonly");

	string sql="SELECT user FROM user";

	if(database->useResultQuery(sql.c_str()))
	{
		while(database->nextRow())
		{
			cout << database->column(0) << "\n";
		}
	}
	else
	{
		cerr << database->getError() <<"\n";
	}
	
}

