YAMAGUCHI::weblog

噛み付き地蔵に憧れて、この神の世界にやってきました。マドンナみたいな男の子、コッペです。

libpqxx & gccで遊んでみた その2

こんな感じのコードを書いてみた.ちょっとずつ使えそうな感じがしてきた.サンプルコードをひたすら読むに限る.

/** @file  test_libpqxx.cpp
 * @brief  this file is test program using libpqxx2.6.9
 * @author YAMAGUCHI
 * @date   2007.12.23
 * @attention  needs libpqxx
 * http://pqxx.org/development/libpqxx/
 */


#include <iostream>
#include <sstream>
#include <pqxx/pqxx>

using namespace pqxx;
using namespace std;

string make_connecter(const string& dbname, const string& dbuser = "", const string& dbhost = "");
int insert_data(connection& conn);
int select_data(connection& conn);
int delete_data(connection& conn);
int update_data(connection& conn);
int close_connection(connection& conn);

int main()
{
	cerr << "************** program start **************" << endl << endl;
	string connecter = make_connecter("testdb", "postgres");
	connection conn(connecter);
	cerr << "**** connected" << endl;
	int ret = delete_data(conn);
	if (!ret)  ret = insert_data(conn);
	if (!ret)  ret = select_data(conn);
	if (!ret)  ret = update_data(conn);
	if (!ret)  ret = select_data(conn);
	if (!ret)  ret = close_connection(conn);
	return ret;
}

string make_connecter(const string& dbname, const string& dbuser, const string& dbhost)
{
	string connecter("dbname=" + dbname);
	if (dbuser != "") {
		connecter += " user=" + dbuser;
	}
	if (dbhost != "") {
		connecter += " host=" + dbhost;
	}
	return connecter;
}

int insert_data(connection& conn) {
	try
	{
		cerr << "connecting" << endl;
		work T(conn);
		for (int i = 0; i < 100; i++) {
			ostringstream qry;
			cerr << "**** inserting " << i << "th data" << endl;
			qry << "insert into test (id, name) values (" << i << ", 'number_" << i << "')";
			cerr << "**** inserted " << i << "th data" << endl;
			T.exec(qry.str());
		}
		T.commit();

	}
	catch (const exception &e)
	{
		cerr << e.what() << endl;
		return -1;
	}
	catch (const sql_error& e)
	{
		cerr << e.what() << " : SQL->" << e.query() << endl;
		return -2;
	}
	catch (...)
	{
		cerr << "unhandled exception" << endl;
		return -100;
	}

	cerr << "**** inserted all" << endl;
	return 0;
}

int select_data(connection& conn) 
{
	try
	{
		work T(conn);
		result R(T.exec("select * from test order by id"));
		for( result::const_iterator c = R.begin(); c != R.end(); ++c ) {
			cerr << "id = " << c[0].as(string()) << " : name=" << c[1].as(string()) << endl;
		}
	}
	catch (const exception& e)
	{
		cerr << e.what() << endl;
		return -1;
	}
	catch (const sql_error& e)
	{
		cerr << e.what() << " : SQL->" << e.query() << endl;
		return -2;
	}
	catch (...)
	{
		cerr << "unhandled exception" << endl;
		return -100;
	}
	return 0;
}

int delete_data(connection& conn)
{
	try
	{
		work T(conn);
		T.exec("delete from test");
		T.commit();
		cerr << "**** deleted data" << endl;
	}
	catch (const exception& e)
	{
		cerr << e.what() << endl;
		return -1;
	}
	catch (const sql_error& e)
	{
		cerr << e.what() << " : SQL->" << e.query() << endl;
		return -2;
	}
	catch (...)
	{
		cerr << "unhandled exception" << endl;
		return -100;
	}
	return 0;
}

int update_data(connection& conn)
{
	try
	{
		work T(conn);
		T.exec("update test set name = name || '_2' where id%2 = 0");
		T.commit();
		cerr << "**** updated data" << endl;
	}
	catch (const exception& e)
	{
		cerr << e.what() << endl;
		return -1;
	}
	catch (const sql_error& e)
	{
		cerr << e.what() << " : SQL->" << e.query() << endl;
		return -2;
	}
	catch (...)
	{
		cerr << "unhandled exception" << endl;
		return -100;
	}
	return 0;
}

int close_connection(connection& conn)
{
	conn.disconnect();
	return 0;
}