mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-01 23:19:58 -06:00
Added sqlgen::Flatten
This commit is contained in:
@@ -4,9 +4,9 @@
|
||||
|
||||
[Defining tables](defining_tables.md) - How to define tables using C++ structs.
|
||||
|
||||
[Reading](reading.md) - How to read data from a database.
|
||||
[sqlgen::read](reading.md) - How to read data from a database.
|
||||
|
||||
[Writing](writing.md) - How to write data to a database.
|
||||
[sqlgen::write](writing.md) - How to write data to a database.
|
||||
|
||||
[sqlgen::Result](result.md) - How sqlgen handles errors.
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define SQLGEN_HPP_
|
||||
|
||||
#include "sqlgen/Connection.hpp"
|
||||
#include "sqlgen/Flatten.hpp"
|
||||
#include "sqlgen/Iterator.hpp"
|
||||
#include "sqlgen/IteratorBase.hpp"
|
||||
#include "sqlgen/Literal.hpp"
|
||||
|
||||
14
include/sqlgen/Flatten.hpp
Normal file
14
include/sqlgen/Flatten.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef SQLGEN_FLATTEN_HPP_
|
||||
#define SQLGEN_FLATTEN_HPP_
|
||||
|
||||
#include <rfl.hpp>
|
||||
|
||||
namespace sqlgen {
|
||||
|
||||
template <class T>
|
||||
using Flatten = rfl::Flatten<T>;
|
||||
|
||||
}; // namespace sqlgen
|
||||
|
||||
#endif
|
||||
|
||||
45
tests/sqlite/test_flatten.cpp
Normal file
45
tests/sqlite/test_flatten.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <sqlgen.hpp>
|
||||
#include <sqlgen/sqlite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace test_write_and_read {
|
||||
|
||||
struct Person {
|
||||
sqlgen::PrimaryKey<uint32_t> id;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
struct Employee {
|
||||
static constexpr const char* tablename = "EMPLOYEES";
|
||||
|
||||
sqlgen::Flatten<Person> person;
|
||||
float salary;
|
||||
};
|
||||
|
||||
TEST(sqlite, test_flatten) {
|
||||
const auto people1 =
|
||||
std::vector<Employee>({Employee{.person = Person{.id = 0,
|
||||
.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.age = 45},
|
||||
.salary = 60000.0}});
|
||||
|
||||
const auto conn = sqlgen::sqlite::connect();
|
||||
|
||||
sqlgen::write(conn, people1);
|
||||
|
||||
const auto people2 = sqlgen::read<std::vector<Employee>>(conn).value();
|
||||
|
||||
const auto json1 = rfl::json::write(people1);
|
||||
const auto json2 = rfl::json::write(people2);
|
||||
|
||||
EXPECT_EQ(json1, json2);
|
||||
}
|
||||
|
||||
} // namespace test_write_and_read
|
||||
Reference in New Issue
Block a user