0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00
A type safe SQL template library for C++
Go to file
Roland Bock 4a8c941916 Fixed global variable
This would have led to multiple definitions at best.
2021-10-11 10:03:58 +02:00
cmake Copy latest changes from mysql connector library 2021-08-06 10:13:41 +02:00
connector_api Implemented get/set_default_isolation_level() functions to change the 2017-06-04 13:57:41 +02:00
dependencies Add USE_SYSTEM_DATE cmake option (#372) 2021-07-29 07:37:25 +02:00
docs Remove multi_column. 2021-07-30 21:05:36 +02:00
examples Feature/fetch content (#349) 2020-08-08 09:42:02 +02:00
include/sqlpp11 Fixed global variable 2021-10-11 10:03:58 +02:00
scripts Add some MYSQL types to the ddl2cpp script 2020-06-17 07:26:26 +02:00
test_scripts Use Python3 for test_scripts (#298) 2019-10-14 12:09:18 +02:00
test_types Added string_view tests and C++17 tests for travis 2019-02-02 11:00:59 +01:00
tests Fix missing inline attributes for mysql connector 2021-10-11 09:04:57 +02:00
_config.yml Set theme jekyll-theme-minimal 2017-06-04 14:59:27 +02:00
.appveyor.yml Update CI scripts. 2018-01-31 17:55:02 +03:00
.clang-format Stop clang-format from sorting includes 2016-10-07 12:13:31 +02:00
.gitignore add set(tuple<...>) implementation for insert and update statements 2018-02-15 07:55:43 +01:00
.travis.yml Update travis script to include mysql and sqlite3 2021-08-25 07:55:40 +02:00
ChangeLog.md Update ChangeLog.md 2016-08-30 09:48:04 +02:00
CMakeLists.txt Make sqlite3 connector header-only 2021-08-22 12:44:34 +02:00
coveralls add coveralls coverage reporting 2015-05-30 00:56:33 +02:00
CREDITS shamelessly adding myself to CREDITS 2015-05-30 01:12:31 +02:00
LICENSE Update LICENSE 2016-03-05 20:14:35 +01:00
pre-commit Reformatted using clang-format 2015-09-13 21:33:19 +02:00
README.md Fix documentation for enablng connector 2021-08-14 11:06:35 +02:00
wishlist.md Fix some formatting in wish list 2019-08-20 18:58:24 +02:00

sqlpp11

A type safe embedded domain specific language for SQL queries and results in C++

Documentation is found in the wiki

So what is this about?

SQL and C++ are both strongly typed languages. Still, most C/C++ interfaces to SQL are based on constructing queries as strings and on interpreting arrays or maps of strings as results.

sqlpp11 is a templated library representing an embedded domain specific language (EDSL) that allows you to

  • define types representing tables and columns,
  • construct type safe queries checked at compile time for syntax errors, type errors, name errors and even some semantic errors,
  • interpret results by iterating over query-specific structs with appropriately named and typed members.

This results in several benefits, e.g.

  • the library user operates comfortably on structs and functions,
  • the compiler reports many kinds of errors long before the code enters unit testing or production,
  • the library hides the gory details of string construction for queries and interpreting results returned by select calls.

The library supports both static and dynamic queries. The former offers greater benefit in terms of type and consistency checking. The latter makes it easier to construct queries in flight.

sqlpp11's core is vendor-neutral. Specific traits of databases (e.g. unsupported or non-standard features) are handled by connector libraries. Connector libraries can inform the developer of missing features at compile time. They also interpret expressions specifically where needed. For example, the connector could use the operator|| or the concat method for string concatenation without the developer being required to change the statement.

Connectors for MySQL, MariaDB, sqlite3, sqlcipher are included in this repository.

The library is already used in production but it is certainly not complete yet. Feature requests, bug reports, contributions to code or documentation are most welcome.

Examples:

For the examples, lets assume you have a table class representing something like

CREATE TABLE foo (
    id bigint,
    name varchar(50),
    hasFun bool
);

And we assume to have a database connection object:

TabFoo foo;
Db db(/* some arguments*/);

// selecting zero or more results, iterating over the results
for (const auto& row : db(select(foo.name, foo.hasFun).from(foo).where(foo.id > 17 and foo.name.like("%bar%"))))
{
    if (row.name.is_null())
        std::cerr << "name is null, will convert to empty string" << std::endl;
    std::string name = row.name;   // string-like fields are implicitly convertible to string
    bool hasFun = row.hasFun;          // bool fields are implicitly convertible to bool
}

// selecting ALL columns of a table
for (const auto& row : db(select(all_of(foo)).from(foo).where(foo.hasFun or foo.name == "joker")))
{
    int64_t id = row.id; // numeric fields are implicitly convertible to numeric c++ types
}

// selecting zero or one row, showing off with an alias:
SQLPP_ALIAS_PROVIDER(cheese);
if (const auto& row = db(select(foo.name.as(cheese)).from(foo).where(foo.id == 17)))
{
    std::cerr << "found: " << row.cheese << std::endl;
}

// selecting a single row with a single result:
return db(select(count(foo.id)).from(foo).unconditionally()).front().count;

Of course there are joins and subqueries, more functions, order_by, group_by etc.
These will be documented soon.

// A sample insert
db(insert_into(foo).set(foo.id = 17, foo.name = "bar", foo.hasFun = true));

// A sample update
db(update(foo).set(foo.hasFun = not foo.hasFun).where(foo.name != "nobody"));

// A sample delete
db(remove_from(foo).where(not foo.hasFun));

License:

sqlpp11 is distributed under the BSD 2-Clause License.

Status:

Branch / Compiler clang, gcc MSVC Test Coverage
master Build Status Build status Coverage Status
develop Build Status Build status Coverage Status

Additional information available:

Past talks about sqlpp11 and some coding concepts used within the library:

Requirements:

Compiler: sqlpp11 makes heavy use of C++11 and requires a recent compiler and STL. The following compilers are known to compile the test programs:

  • clang-3.4+ on Ubuntu-12.4
  • g++-4.8+ on Ubuntu-12.4
  • g++-4.8+ on cygwin 64bit
  • g++-4.9+ on Debian Unstable
  • Xcode-7 on OS X
  • MSVC 2015 Update 1 on Windows Server 2012

Database Connector: sqlpp11 requires a certain api in order to connect with the database, see database/api.h.

Connectors for MySQL, MariaDB, sqlite3, sqlcipher are included in this repository. You can configure to use them via cmake options, i.e.

-DMYSQL_CONNECTOR=ON
-DMARIADB_CONNECTOR=ON
-DSQLITE3_CONNECTOR=ON
-DSQLCIPHER_CONNECTOR=ON

Other connectors can be found here:

Date Library: sqlpp11 requires Howard Hinnant's date library for date and date_time data types. By default, sqlpp11 uses FetchContent to pull the library automatically in the project. If you want to use an already installed version of the library with find_package, set USE_SYSTEM_DATE option to ON.

Build and Install

Note: Depending on how you use the lib, you might not need to install it (see Basic Usage)

Build from Source:

Download and unpack the latest release from https://github.com/rbock/sqlpp11/releases or clone the repository. Inside the directory run the following commands:

cmake -B build
cmake --build build --target install

The last step will build the library and install it system wide, therefore it might need admins right.

Install via Homebrew (MacOS):

brew install marvin182/zapfhahn/sqlpp11

Some connectors can be installed with the formula. See brew info marvin182/zapfhahn/sqlpp11 for available options.

Build via vcpkg:

You can download and install sqlpp11 using the vcpkg dependency manager:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
vcpkg install sqlpp11

The sqlpp11 port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.

The following connector libraries for sqlpp11 are maintained as a separate package in vcpkg:

Basic usage:

Use with cmake: The library officially supports two ways how it can be used with cmake. You can find examples for both methods in the example folder.

  1. Fetch content (Recommend, no installation required)
  2. Find package (installation required, see above)

Create DDL files:

mysql: 'show create table MyDatabase.MyTable' #or
mysqldump --no-data MyDatabase > MyDatabase.sql

Create headers for them with provided Python script:

%sqlpp11_dir%/scripts/ddl2cpp ~/temp/MyTable.ddl  ~/temp/MyTable %DatabaseNamespaceForExample%

(In case you're getting notes about unsupported column type take a look at the other datatypes in sqlpp11/data_types. They are not hard to implement.)

Include generated header (MyTable.h), that's all.

If you prefer Ruby over Python, you might want to take a look at https://github.com/douyw/sqlpp11gen

Contact:

Breaking changes in 0.36:

See Changes