mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-06 17:40:15 -06:00
6dee07c4b943d5834a2645edd84af57dca073ef2
sqlgen
sqlgen is an ORM and SQL query generator for C++-20, similar to Python's SQLAlchemy/SQLModel or Rust's Diesel.
Much like SQLModel is closely integrated with pydantic, sqlgen is closely integrated with our sister project reflect-cpp. This allows you to construct highly efficient data pipelines.
Simple example
Here is how you create a simple sqlite database and insert some data:
#include <sqlgen/sqlite.hpp>
struct People {
std::string first_name;
std::string last_name;
uint age;
};
const auto people = std::vector<People>({
person{.first_name = "Homer",
.last_name = "Simpson",
.age = 45}});
const auto conn = sqlgen::sqlite::connect("example.db");
// Will automatically create a table called 'People'
// with the columns 'first_name', 'last_name' and 'age',
// if necessary.
const auto result = sqlgen::write(conn, people);
if (!result) {
std::cout << result.error().what() << std::endl;
}
Here is how you retrieve the same data from the database and print the results as a JSON:
#include <rfl/json.hpp> // reflect-cpp
#include <sqlgen/sqlite.hpp>
const auto conn = sqlgen::sqlite::connect("example.db");
const auto people = sqlgen::read<People>(conn);
if (people) {
std::cout << rfl::json::write(*people) << std::endl;
} else {
std::cout << result.error().what() << std::endl;
}
Languages
C++
98.8%
CMake
0.9%
Python
0.3%