https://github.com/LASTRADA-Software/Lightweight
thin and lightweight, fast, ODBC API wrapper for modern C++ uses
https://github.com/LASTRADA-Software/Lightweight
activerecord cplusplus cplusplus-23 datamapper library odbc
Last synced: 4 months ago
JSON representation
thin and lightweight, fast, ODBC API wrapper for modern C++ uses
- Host: GitHub
- URL: https://github.com/LASTRADA-Software/Lightweight
- Owner: christianparpart
- Created: 2024-07-31T19:22:19.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2024-10-18T20:07:08.000Z (8 months ago)
- Last Synced: 2024-10-19T10:57:56.887Z (8 months ago)
- Topics: activerecord, cplusplus, cplusplus-23, datamapper, library, odbc
- Language: C++
- Homepage:
- Size: 142 KB
- Stars: 4
- Watchers: 4
- Forks: 0
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Lightweight, an ODBC SQL API for C++23
**Lightweight** is a thin and modern C++ ODBC wrapper for **easy** and **fast** raw database access.
Documentation is available at [https://lastrada-software.github.io/Lightweight/](https://lastrada-software.github.io/Lightweight/).
## Goals
- **Easy to use** - simple, expressive and intuitive API
- **Production ready** - targeting production grade systems
- **Performance** - do as little as possible, and as much as necessary - **Zero overhead abstraction** is a key design requirement.
- **Extensible** - support for custom data types for writing to and reading from columns
- **Resource aware** - efficient resource management and exception safety## Example: CRUD-style High level Data Mapping
```cpp
// Define a person structure, mapping to a table
// The field members are mapped to the columns in the table,
// and the Field<> template parameter specifies the type of the column.
// Field<> is also used to track what fields are modified and need to be updated.
struct Person
{
Field id;
Field> name;
Field is_active { true };
Field> age;
};void CRUD(DataMapper& dm)
{
// Creates the table if it does not exist
dm.CreateTable();// Create a new person
auto person = Person {};
person.name = "John Doe";
person.is_active = true;
dm.Create(person);// Update the person
name.age = 25;
dm.Update(person);// Query the person
if (auto const po = dm.QuerySingle(person.id); po)
std::println("Person: {} ({})", po->name, DataMapper::Inspect(*po));// Query all persons
auto const persons = dm.Query();// Delete the person
dm.Delete(person);
}
```## Example: Simple row retrieval via structs
When only read access is needed, you can use a simple struct to represent the row,
and also do not need to wrap the fields into `Field<>` template.
The struct must have fields that match the columns in the query. The fields can be of any type that can be converted from the column type. The struct can have more fields than the columns in the query, but the fields that match the columns must be in the same order as the columns in the query.```cpp
struct SimpleStruct
{
uint64_t pkFromA;
uint64_t pkFromB;
SqlAnsiString<30> c1FromA;
SqlAnsiString<30> c2FromA;
SqlAnsiString<30> c1FromB;
SqlAnsiString<30> c2FromB;
};void SimpleStructExample(DataMapper& dm)
{
if (auto maybeObject = dm.Query(
"SELECT A.pk, B.pk, A.c1, A.c2, B.c1, B.c2 FROM A LEFT JOIN B ON A.pk = B.pk"); maybeObject)
))
{
for (auto const& obj : *maybeObject)
std::println("{}", DataMapper::Inspect(obj));
}
}
```## Supported platforms
Only ODBC is supported, so it should work on any platform that has an ODBC driver and
a modern enough C++ compiler.- Windows (Visual Studio 2022, toolkit v143)
- Linux (GCC 14, Clang 19)## Supported databases
- Microsoft SQL
- PostgreSQL
- SQLite3
- Oracle database (work in progress)## Using SQLite for testing on Windows operating system
You need to have the SQLite3 ODBC driver for SQLite installed.
- ODBC driver download URL: http://www.ch-werner.de/sqliteodbc/
- Example connection string: `DRIVER={SQLite3 ODBC Driver};Database=file::memory:`### SQLite ODBC driver installation on other operating systems
```sh
# Fedora Linux
sudo dnf install sqliteodbc# Ubuntu Linux
sudo apt install sqliteodbc# macOS
arch -arm64 brew install sqliteodbc
```- sqliteODBC Documentation: http://www.ch-werner.de/sqliteodbc/html/index.html
- Example connection string: `DRIVER=SQLite3;Database=file::memory:`