Skip to content

About

An experimental C++ wrapper for SQLite

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

56 Commits

Folders and files

Repository files navigation

SQLiteWrapper

An experimental C++ wrapper for SQLite or a template metaprogramming-based home made ORM with some kind of a Code-First approach that allows to generate database table definitions from C++ structures, insert/update/delete/query the data without dealing with column names or column indices and iterating over the recordsets with standard C++ iterators.

CMake library target

SQLiteWrapper::SQLiteWrapper links AWL and compiles the wrapper implementations and sqlite3.c once per configuration in the build tree. Set SQLITE_SRC_DIR to the SQLite amalgamation directory (sqlite3.c and sqlite3.h). Header paths and AWL's public usage requirements are propagated to consumers.

# Configure/add AWL first when multiple dependencies share it.
set(SQLITE_WRAPPER_BUILD_TESTS OFF)
add_subdirectory(path/to/SQLiteWrapper sqlite-wrapper)
target_link_libraries(MyProgram PRIVATE SQLiteWrapper::SQLiteWrapper)

An existing AWL::Core is reused. Standalone builds configure AWL from AWL_ROOT_DIR (the bundled checkout, or the adjacent AWL directory). The checkout must provide the new AWL library targets. SQLITE_WRAPPER_BUILD_TESTS defaults to ON for standalone builds and OFF as a subproject. SQLiteWrapperTest contains only test sources and links the wrapper library and AWL::TestMain.

Examples

Named ID sequences

SequenceTable stores named counters in one sequences table. Create the schema before using the table, then ensure each named sequence exists:

sqlite::SequenceTable::createScheme().create(*db);
sqlite::SequenceTable sequences(db);
sequences.createSequence("orders");
const int64_t id = sequences.nextId("orders"); // 1 on the first call

createSequence(name, initial_value) leaves existing values unchanged. The initial value must be nonnegative and defaults to zero; the first ID is initial_value + 1. nextId atomically increments and returns a signed 64-bit ID using UPDATE ... RETURNING (SQLite 3.35 or later). Unknown or exhausted sequences throw SQLiteException. Committed counters survive reopening the database. Both SQL statements are prepared in the constructor and reused on subsequent calls, including after SQL errors. Create the schema before constructing SequenceTable.

Operations are synchronous and create no workers. Serialize use of a shared database connection; separate connections can advance the same counter concurrently, subject to SQLite's locking/busy policy. Calls participate in an enclosing transaction: rolling it back also rolls back the increment, so that ID may be issued again. Use autocommit or commit before publishing an ID when reuse must be prevented.

  • BlobTest.cpp is a very simple example of working with a database table as with std::set and working with a column of type BLOB.
  • SetTest.cpp is an example of using composite keys and nested structures.

Cloning the repository

git clone https://github.com/dmitriano/SQLiteWrapper.git --recursive

or

git clone https://github.com/dmitriano/SQLiteWrapper.git
cd SQLiteWrapper
git submodule init
git submodule update

Building on Windows with MSVC2022

Assuming that you cloned the repository into D:/dev/repos/SQLiteWrapper, downloaded SQLite sources into D:\dev\libs\sqlite-amalgamation-3390200 and created D:\dev\build\sw directory to build SQLiteWrapper test project in:

set MY_DRIVE=C:

%MY_DRIVE%
cd \dev\build\sw

set SQLITE_SRC_DIR=%MY_DRIVE%/dev/libs/sqlite-amalgamation-3460000
set MY_CMAKE_EXE=%MY_DRIVE%\dev\tools\cmake-3.24.2-windows-x86_64\bin\cmake.exe
set MY_VS_GENERATOR="Visual Studio 17 2022"

%MY_CMAKE_EXE% ..\..\repos\SQLiteWrapper -G %MY_VS_GENERATOR% -A x64

%MY_CMAKE_EXE% --build . --target SQLiteWrapperTest --config Debug
%MY_CMAKE_EXE% --build . --target SQLiteWrapperTest --config RelWithDebInfo

or

cmake --build . --config Debug
cmake --build . --config RelWithDebInfo

Debug\SQLiteWrapperTest.exe
RelWithDebInfo\SQLiteWrapperTest.exe

Building on Linux with GCC

cd ~
mkdir -p dev/libs
cd dev/libs
wget https://www.sqlite.org/2024/sqlite-amalgamation-3460000.zip
unzip sqlite-amalgamation-3460000.zip
export SQLITE_SRC_DIR=$(realpath sqlite-amalgamation-3460000)
cd ~/dev
mkdir repos
cd repos
git clone https://github.com/dmitriano/SQLiteWrapper.git --recursive
cd ~/dev
mkdir -p build/sw
cd build/sw
cmake ../../repos/SQLiteWrapper -DCMAKE_BUILD_TYPE=Release
cmake --build . --parallel
./SQLiteWrapperTest

Running the tests on Android device

adb push SQLiteWrapperTest /data/local/tmp
adb shell "cd /data/local/tmp && chmod a+x SQLiteWrapperTest && ./SQLiteWrapperTest"

About

An experimental C++ wrapper for SQLite

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages