From 0d91b0fb7a106b5a53e78b899a725a3f9fc0dc2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rombauts?= Date: Wed, 29 Jan 2020 23:19:04 +0100 Subject: [PATCH] Add an example2 based on SQLiteCpp_Example repository Demonstrates how to use SQLiteCpp as a subdirectory (out of tree) of a CMake project. --- .github/workflows/{actions.yml => build.yml} | 2 +- .github/workflows/subdir_example.yml | 48 +++++++++++ CMakeLists.txt | 5 +- examples/example2/CMakeLists.txt | 21 +++++ examples/example2/README.md | 3 + examples/example2/build.bat | 21 +++++ examples/example2/build.sh | 18 +++++ examples/example2/src/main.cpp | 84 ++++++++++++++++++++ 8 files changed, 199 insertions(+), 3 deletions(-) rename .github/workflows/{actions.yml => build.yml} (98%) create mode 100644 .github/workflows/subdir_example.yml create mode 100644 examples/example2/CMakeLists.txt create mode 100644 examples/example2/README.md create mode 100644 examples/example2/build.bat create mode 100644 examples/example2/build.sh create mode 100644 examples/example2/src/main.cpp diff --git a/.github/workflows/actions.yml b/.github/workflows/build.yml similarity index 98% rename from .github/workflows/actions.yml rename to .github/workflows/build.yml index 49df2a0f..7f1515ba 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/build.yml @@ -38,7 +38,7 @@ jobs: } steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: submodule run: git submodule update --init --recursive - name: configure diff --git a/.github/workflows/subdir_example.yml b/.github/workflows/subdir_example.yml new file mode 100644 index 00000000..c71ac762 --- /dev/null +++ b/.github/workflows/subdir_example.yml @@ -0,0 +1,48 @@ +name: subdir_example + +on: [push] + +jobs: + build: + name: ${{ matrix.config.name }} + runs-on: ${{ matrix.config.os }} + strategy: + fail-fast: false + matrix: + config: + - { + name: "Windows Latest MSVC", + os: windows-latest, + build_type: "Debug", cc: "cl", cxx: "cl", + } + - { + name: "Ubuntu Latest GCC", + os: ubuntu-latest, + build_type: "Debug", cc: "gcc", cxx: "g++" + } + - { + name: "Ubuntu 16.04 GCC", + os: ubuntu-16.04, + build_type: "Debug", cc: "gcc", cxx: "g++" + } + - { + name: "macOS Latest Clang", + os: macos-latest, + build_type: "Debug", cc: "clang", cxx: "clang++" + } + + steps: + - uses: actions/checkout@v2 + - name: configure + shell: cmake -P {0} + run: | + set(ENV{CC} ${{matrix.config.cc}}) + set(ENV{CXX} ${{matrix.config.cxx}}) + - name: generate + run: | + cd examples/example2 + mkdir build + cd build + cmake -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=${{matrix.config.build_type}} .. + - name: build + run: cmake --build examples/example2/build --config ${{matrix.config.build_type}} diff --git a/CMakeLists.txt b/CMakeLists.txt index a8dd1121..8eb29b03 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ # or copy at http://opensource.org/licenses/MIT) cmake_minimum_required(VERSION 3.1) # for "CMAKE_CXX_STANDARD" version list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") # custom CMake modules like FindSQLiteCpp -project(SQLiteCpp VERSION "2.99") +project(SQLiteCpp VERSION 2.99) # SQLiteC++ 3.x now requires C++11 compiler set(CMAKE_CXX_STANDARD 11) @@ -145,7 +145,8 @@ source_group(doc FILES ${SQLITECPP_DOC}) set(SQLITECPP_SCRIPT .editorconfig .gitbugtraq - .github/workflows/actions.yml + .github/workflows/build.yml + .github/workflows/subdir_example.yml .gitignore .gitmodules .travis.yml diff --git a/examples/example2/CMakeLists.txt b/examples/example2/CMakeLists.txt new file mode 100644 index 00000000..349328af --- /dev/null +++ b/examples/example2/CMakeLists.txt @@ -0,0 +1,21 @@ +# Example CMake file for compiling & linking a project with the the SQLiteCpp wrapper +# +# Copyright (c) 2012-2020 Sebastien Rombauts (sebastien.rombauts@gmail.com) +# +# Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt +# or copy at http://opensource.org/licenses/MIT) +cmake_minimum_required(VERSION 3.1) # for "CMAKE_CXX_STANDARD" version +project(SQLiteCpp_Example VERSION 2.0) + +# Add SQLite3 C++ wrapper around sqlite3 library (and sqlite3 itself provided for ease of use) +# Here you can set CMake variables to avoid building Example, as well as cpplint, cppcheck... +# or set them in the cmake command line (see for instance provided build.bat/build.sh scripts) +set(SQLITECPP_RUN_CPPCHECK OFF CACHE BOOL "" FORCE) +set(SQLITECPP_RUN_CPPLINT OFF CACHE BOOL "" FORCE) +add_subdirectory(../.. SQLiteCpp) # out-of-source build requires explicit subdir name for compilation artifacts + +# Add main.cpp example source code to the executable +add_executable(SQLiteCpp_Example src/main.cpp) + +# Link SQLiteCpp_example1 with SQLiteCpp +target_link_libraries(SQLiteCpp_Example SQLiteCpp) diff --git a/examples/example2/README.md b/examples/example2/README.md new file mode 100644 index 00000000..f680104f --- /dev/null +++ b/examples/example2/README.md @@ -0,0 +1,3 @@ +SQLiteCpp_Example demonstrates how to use SQLiteCpp as a subdirectory of a CMake project. + +See https://github.com/SRombauts/SQLiteCpp_Example \ No newline at end of file diff --git a/examples/example2/build.bat b/examples/example2/build.bat new file mode 100644 index 00000000..db842c13 --- /dev/null +++ b/examples/example2/build.bat @@ -0,0 +1,21 @@ +@REM Copyright (c) 2012-2020 Sebastien Rombauts (sebastien.rombauts@gmail.com) +@REM +@REM Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt +@REM or copy at http://opensource.org/licenses/MIT) +mkdir build +cd build + +@REM Generate a Visual Studio solution for latest version found +cmake .. +@if ERRORLEVEL 1 goto onError + +@REM Build default configuration (ie 'Debug') +cmake --build . +@if ERRORLEVEL 1 goto onError + +goto onSuccess + +:onError +@echo An error occured! +:onSuccess +cd .. diff --git a/examples/example2/build.sh b/examples/example2/build.sh new file mode 100644 index 00000000..9fb1bede --- /dev/null +++ b/examples/example2/build.sh @@ -0,0 +1,18 @@ +#!/bin/sh +# Copyright (c) 2012-2020 Sébastien Rombauts (sebastien.rombauts@gmail.com) +# +# Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt +# or copy at http://opensource.org/licenses/MIT) + +# exit on first error +set -e + +mkdir -p build +cd build + +# Generate a Makefile for GCC (or Clang, depanding on CC/CXX envvar) +cmake -DCMAKE_BUILD_TYPE=Debug .. + +# Build (ie 'make') +cmake --build . + diff --git a/examples/example2/src/main.cpp b/examples/example2/src/main.cpp new file mode 100644 index 00000000..ec8c8753 --- /dev/null +++ b/examples/example2/src/main.cpp @@ -0,0 +1,84 @@ +/** + * @file main.cpp + * @brief A few short examples in a row. + * + * Demonstrates how-to use the SQLite++ wrapper + * + * Copyright (c) 2012-2020 Sebastien Rombauts (sebastien.rombauts@gmail.com) + * + * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt + * or copy at http://opensource.org/licenses/MIT) + */ + +#include +#include +#include + +#include + + +#ifdef SQLITECPP_ENABLE_ASSERT_HANDLER +namespace SQLite +{ +/// definition of the assertion handler enabled when SQLITECPP_ENABLE_ASSERT_HANDLER is defined in the project (CMakeList.txt) +void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg) +{ + // Print a message to the standard error output stream, and abort the program. + std::cerr << apFile << ":" << apLine << ":" << " error: assertion failed (" << apExpr << ") in " << apFunc << "() with message \"" << apMsg << "\"\n"; + std::abort(); +} +} +#endif + +int main () +{ + // Using SQLITE_VERSION would require #include which we want to avoid: use SQLite::VERSION if possible. +// std::cout << "SQlite3 version " << SQLITE_VERSION << std::endl; + std::cout << "SQlite3 version " << SQLite::VERSION << " (" << SQLite::getLibVersion() << ")" << std::endl; + std::cout << "SQliteC++ version " << SQLITECPP_VERSION << std::endl; + + //////////////////////////////////////////////////////////////////////////// + // Simple batch queries example : + try + { + // Open a database file in create/write mode + SQLite::Database db("test.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE); + std::cout << "SQLite database file '" << db.getFilename().c_str() << "' opened successfully\n"; + + // Create a new table with an explicit "id" column aliasing the underlying rowid + db.exec("DROP TABLE IF EXISTS test"); + db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); + + // first row + int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")"); + std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl; + + // second row + nb = db.exec("INSERT INTO test VALUES (NULL, \"second\")"); + std::cout << "INSERT INTO test VALUES (NULL, \"second\")\", returned " << nb << std::endl; + + // update the second row + nb = db.exec("UPDATE test SET value=\"second-updated\" WHERE id='2'"); + std::cout << "UPDATE test SET value=\"second-updated\" WHERE id='2', returned " << nb << std::endl; + + // Check the results : expect two row of result + SQLite::Statement query(db, "SELECT * FROM test"); + std::cout << "SELECT * FROM test :\n"; + while (query.executeStep()) + { + std::cout << "row (" << query.getColumn(0) << ", \"" << query.getColumn(1) << "\")\n"; + } + + db.exec("DROP TABLE test"); + } + catch (std::exception& e) + { + std::cout << "SQLite exception: " << e.what() << std::endl; + return EXIT_FAILURE; // unexpected error : exit the example program + } + remove("test.db3"); + + std::cout << "everything ok, quitting\n"; + + return EXIT_SUCCESS; +}