Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rafaeljusto/dbplus

C++ database interface
https://github.com/rafaeljusto/dbplus

Last synced: about 9 hours ago
JSON representation

C++ database interface

Awesome Lists containing this project

README

        

DBplus
=======

**DBplus** is a C++ database interface to use the most common database
clients, such as MySQL and PostgreSQL
.

Prerequisits
------------

* g++ 4.6 -
* python 2.7 -
* scons 2.0 -
* libboost-system-dev 1.4 -
* libboost-test-dev 1.4 -
* libboost-date-time-dev 1.4 -
* libmysqlclient-dev 5.1 -
* libpq-dev 5 -

The project was compiled using the above compilers and libraries,
higher versions should work well.

To run the unit tests:

* install mysql-server package (MySQL)
* install postgresql package (PostgreSQL)
* create a root user with the password "abc123" on each database server
* create a database with the name "dbplus" on each database server

Installation
------------

Define the compiler using the CXX environment variable, by default
is used "g++".

# scons
# scons install

For API documentation:

# scons doc

Usage
-----

Source code (using MySQL):

#include
#include
#include
#include
#include

#include
#include

#include
#include

class Object
{
public:
Object() : _id(0), _value("") {}

void print()
{
std::cout << "-----------------" << std::endl;
std::cout << "Id: " << _id << std::endl;
std::cout << "Value: " << _value << std::endl;
std::cout << "Date: " << _date << std::endl;
}

void setId(const long id)
{
_id = id;
}

void setValue(const std::string &value)
{
_value = value;
}

void setDate(const boost::posix_time::ptime &date)
{
_date = date;
}

private:
long _id;
std::string _value;
boost::posix_time::ptime _date;
};

int main()
{
dbplus::MySql mysql;
mysql.connect("dbplus", "root", "abc123", "127.0.0.1");

std::string sql = "SELECT id, value, date FROM test";
std::shared_ptr result =
std::dynamic_pointer_cast(mysql.execute(sql));

std::list objects =
result->getAll([](std::map row) {
Object object;
object.setId(boost::any_cast(row["id"]));
object.setValue(boost::any_cast(row["value"]));
object.setDate(boost::any_cast(row["date"]));
return object;
});

for (Object object: objects) {
object.print();
}
}

Compiling:

g++ -std=c++0x test.cpp -o test -ldbplus -lboost_date_time -lmysqlclient

Output:

-----------------
Id: 1
Value: This is a test
Date: 2011-Nov-11 11:11:11

Contact
-------

Rafael Dantas Justo

License
-------

CGIplus Copyright (C) 2012 Rafael Dantas Justo

This file is part of CGIplus.

CGIplus is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGIplus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with CGIplus. If not, see .