Created
July 23, 2012 16:26
-
-
Save vegard/3164563 to your computer and use it in GitHub Desktop.
SQL in C++11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <array> | |
#include <iostream> | |
#include <string> | |
#include <sstream> | |
#include <tuple> | |
#include <utility> | |
#include <vector> | |
template<typename T> | |
class table { | |
std::string table_name; | |
//std::array<std::string, std::tuple_size<T>::value> field_names; | |
std::vector<std::string> field_names; | |
/* Create */ | |
void create_field(std::ostringstream &ss, unsigned int i, const int &x) | |
{ | |
ss << field_names[i] << " INTEGER"; | |
} | |
void create_field(std::ostringstream &ss, unsigned int i, const double &x) | |
{ | |
ss << field_names[i] << " DOUBLE"; | |
} | |
template<unsigned int I, typename U> | |
typename std::enable_if<I == std::tuple_size<U>::value, void>::type | |
create_iterate(std::ostringstream &ss) | |
{ | |
} | |
template<unsigned int I, typename U> | |
typename std::enable_if<I < std::tuple_size<U>::value, void>::type | |
create_iterate(std::ostringstream &ss) | |
{ | |
typename std::tuple_element<I, U>::type x; | |
create_field(ss, I, x); | |
if (I < std::tuple_size<U>::value - 1) | |
ss << ", "; | |
create_iterate<I + 1, U>(ss); | |
} | |
/* Insert */ | |
template<unsigned int I, typename U> | |
typename std::enable_if<I == std::tuple_size<U>::value, void>::type | |
insert_iterate_fields(std::ostringstream &ss) | |
{ | |
} | |
template<unsigned int I, typename U> | |
typename std::enable_if<I < std::tuple_size<U>::value, void>::type | |
insert_iterate_fields(std::ostringstream &ss) | |
{ | |
ss << field_names[I]; | |
if (I < std::tuple_size<U>::value - 1) | |
ss << ", "; | |
insert_iterate_fields<I + 1, U>(ss); | |
} | |
template<unsigned int I, typename U> | |
typename std::enable_if<I == std::tuple_size<U>::value, void>::type | |
insert_iterate_values(std::ostringstream &ss, const U &x) | |
{ | |
} | |
template<unsigned int I, typename U> | |
typename std::enable_if<I < std::tuple_size<U>::value, void>::type | |
insert_iterate_values(std::ostringstream &ss, const U &x) | |
{ | |
ss << std::get<I>(x); | |
if (I < std::tuple_size<U>::value - 1) | |
ss << ", "; | |
insert_iterate_values<I + 1, U>(ss, x); | |
} | |
public: | |
table(const std::string &table_name, | |
//std::array<std::string, std::tuple_size<T>::value> field_names) | |
const std::initializer_list<std::string> &field_names): | |
table_name(table_name), | |
field_names(field_names) | |
{ | |
} | |
std::string create() | |
{ | |
std::ostringstream ss; | |
ss << "CREATE TABLE " << table_name << " ("; | |
create_iterate<0, T>(ss); | |
ss << ");"; | |
return ss.str(); | |
} | |
template<typename... Args> | |
std::string insert(Args... args) | |
{ | |
std::ostringstream ss; | |
ss << "INSERT INTO " << table_name << " ("; | |
insert_iterate_fields<0, T>(ss); | |
ss << ") VALUES ("; | |
insert_iterate_values<0, T>(ss, std::make_tuple(args...)); | |
ss << ");"; | |
return ss.str(); | |
} | |
}; | |
int main(int argc, char *argv[]) | |
{ | |
typedef std::tuple<int, int, double> datapoint; | |
table<datapoint> datapoints("Datapoints", { | |
"nr_propagations", | |
"nr_conflicts", | |
"glue" | |
}); | |
/* Create table */ | |
std::cout << datapoints.create() << std::endl; | |
/* = "CREATE TABLE Datapoints (nr_propagations INTEGER, nr_conflicts INTEGER, glue DOUBLE);" */ | |
/* Insert a data point */ | |
std::cout << datapoints.insert(1, 2, 3.4) << std::endl; | |
/* = "INSERT INTO Datapoints (nr_propagations, nr_conflicts, glue) VALUES (1, 2, 3.4);" */ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment