mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-02 07:30:17 -06:00
1.8 KiB
1.8 KiB
sqlgen::write
The sqlgen::write interface provides a type-safe way to write data from C++ containers or ranges to a SQL database. It handles table creation, batch processing, and error handling automatically.
Usage
Basic Write
Write a container of objects to a database:
const auto people = std::vector<Person>({
Person{.id = 0, .first_name = "Homer", .last_name = "Simpson", .age = 45},
Person{.id = 1, .first_name = "Bart", .last_name = "Simpson", .age = 10}
});
// Using with a connection reference
const auto conn = sqlgen::sqlite::connect();
sqlgen::write(conn, people);
With Result<Ref>
Handle connection creation and writing in a single chain:
sqlgen::sqlite::connect("database.db")
.and_then([&](auto&& conn) { return sqlgen::write(conn, people); })
.value();
With Iterators
Write a range of objects using iterators:
std::vector<Person> people = /* ... */;
sqlgen::write(conn, people.begin(), people.end());
How It Works
The write function performs the following operations in sequence:
- Creates a table if it doesn't exist (using the object's structure)
- Prepares an insert statement
- Writes the data in batches of
SQLGEN_BATCH_SIZE, which you can set at compile time - Handles any errors that occur during the process
Notes
- The function automatically creates the table, if it doesn't exist
- Data is written in batches for better performance
- The
Result<Nothing>type provides error handling; use.value()to extract the result (will throw an exception if there's an error) or handle errors as needed - The function has three overloads:
- Takes a connection reference and iterators
- Takes a
Result<Ref<Connection>>and iterators - Takes a connection and a container directly