summaryrefslogtreecommitdiff
path: root/p2pvr/lib/dbClient.cpp
blob: 5252e5b043bc6826ed3f32e159aea41fa6bb431c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <pch.hpp>
#include "dbClient.h"
#include <rdbmsDataSource.h>
#include <sqlVariableBinder.h>

DatabaseClient::SelectPtr
DatabaseClient::Select(const std::string & sql) const
{
	auto db = dataSource<RdbmsDataSource>("postgres");
	return SelectPtr(db->getReadonly().newSelectCommand(sql));
}
DatabaseClient::SelectPtr
DatabaseClient::Select(const std::string & sql, const std::list<VariableType> & vs) const
{
	SelectPtr sel(Select(sql));
	unsigned int offset = 0;
	BOOST_FOREACH(const auto & v, vs) {
		boost::apply_visitor<const SqlVariableBinder, const VariableType>(SqlVariableBinder(sel.get(), offset++), v);
	}
	return sel;
}

void
DatabaseClient::onAllDatasources(const DataSourceCall & call) const
{
	BOOST_FOREACH(const CommonObjects::DataSources::value_type & ds, CommonObjects::datasources) {
		call(ds.second);
	}
}

DatabaseClient::TxHelper::TxHelper(const DatabaseClient * dbc) :
	so(NULL,
		boost::bind(&DatabaseClient::onAllDatasources, dbc, DataSourceCall(boost::bind(&DataSource::commit, _1))),
		boost::bind(&DatabaseClient::onAllDatasources, dbc, DataSourceCall(boost::bind(&DataSource::rollback, _1))))
{
}