-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_context.h++
81 lines (64 loc) · 2.11 KB
/
data_context.h++
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#pragma once
#include <tagsql/query/select_query.h++>
#include <tagsql/query/insert_query.h++>
#include <tagsql/query/update_query.h++>
#include <tagsql/core/formatter.h++>
#include <tagsql/core/update_expression.h++>
#include <tagsql/clauses/all.h++>
#include <tagsql/anatomy/table.h++>
namespace tagsql
{
template<typename ...Columns>
struct make_select_clause
{
using type = select_clause<select_query<::std::tuple<typename get_tag<Columns>::type...>>>;
};
class data_context
{
public:
data_context(std::string dbname, std::string host, int port, std::string username, std::string password)
{
std::string connection_string = ::foam::strlib::format("dbname={0} host={1} port={2} user={3} password={4}", dbname, host, port, username, password);
_connection.reset(new pqxx::connection(connection_string));
}
//select (DB style)
template<typename ... Columns>
auto select(Columns ... ) -> typename make_select_clause<Columns...>::type
{
return {_connection };
}
//insert (DB sytle)
template<typename Table>
auto insert_into(Table const &) -> insert_query<data_context, table_tag_t<Table>>
{
return {*this};
}
//insert (C++ container style)
template<typename Table>
auto insert(Table const & item) -> pqxx::result
{
formatting::sql_insert_formatter<Table> fmt { item };
auto insert_command = ::foam::strlib::format("INSERT INTO {0} ({1}) VALUES ({2})",
metaspace::meta_table<Table>::name(),
tagsql::join(",", std::get<0>(fmt.output())),
tagsql::join(",", std::get<1>(fmt.output())));
return execute(insert_command);
}
//update (DB style)
template<typename Table>
auto update(Table const &) -> update_query<data_context, table_tag_t<Table>>
{
return { *this };
}
//execute raw query
auto execute(std::string query) -> pqxx::result
{
pqxx::work transaction { *_connection } ;
auto ret = transaction.exec(query);
transaction.commit();
return ret;
}
private:
std::shared_ptr<pqxx::connection> _connection;
};
} //tagsql