SourceForge.net Logo

DB Facade

Copyright 2008 Marc Spoor, Axel Waggershauser

http://dbfacade.sourceforge.net/

Abstract

Consider writing a program that needs to connect to a database back end. During (or even worse: after) your development you decide to use a different database instead. Wouldn't it be nice not having to read through all the existing code, looking for database specific details? At that point you probably wish you had an intermediate layer for db communication.

dbfacade_without.png

DB Facade is exactly that. A facade for various database back ends. It is compiled as a shared library, so it requires no compile time attention. Provided your program only uses SQL statements that can be understood by the new database, changing the back end is only a matter of passing a different string to the factory method.

dbfacade_with.png

API documentation is provided and can be compiled with doxygen (make doc).

Requirements

Subversion

Repository: https://dbfacade.svn.sourceforge.net/svnroot/dbfacade

Website: http://dbfacade.svn.sourceforge.net/viewvc/dbfacade/

Installation

make install

As easy as that.

Make

make Defaults to make install.

make lib Builds the dbfacade.so library.

make doc Compiles a html documentation of the API using doxygen.

make install Duilds and installs the library on your system.

make uninstall Removes the library from your system (undo install).

make clean Removes all expendable files from the current folder, including documentation and compilation results.

make test Compiles a small regression test.

Supported Backends

MySQL

"The world's most popular open source database." http://www.mysql.com/

PostgreSQL

"The world's most advanced open source database." http://www.postgresql.org/

SQLite

"Small. Fast. Reliable. Choose any three." http://www.sqlite.org/

That's it for the moment. It's fairly simple to add more, though. With less than 300 lines of code you should be able to add pretty much any backend you could think of.

Usage

Include the header in your own program:

	include <dbfacade.h>

and compile it with:

	g++ -ldbfacade yourcode.cpp -o YourCode

Inside your code, you can get a connection to the database with:

	std::string dbType = "MySQL"; // change on demand
	dbfacade::DatabasePtr db(dbfacade::Database::getDatabase(dbType));
	db->connect("localhost", "username", "password", "databaseName");

And then you're ready to execute statements such as:

	dbfacade::ResultPtr result(db->sqlSelect("SELECT id, name FROM users"));
	dbfacade::RowPtr row(result->getNextRow());
	std::cout << "id=" << row->getIntAt(0) << std::endl;
	std::cout << "name=" << row->getStringAt(1) << std::endl;

For more sophisticated (and complete) examples on how to use the library, have a look at the regression test code in test.cpp, as it should be complete by its very nature.

Todo / Roadmap

Style Guide

There are no definite rules about the style, only a couple of guidelines:

Generated on Mon Mar 16 20:04:30 2009 for libdbfacade.so by  doxygen 1.5.5