2013-08-14 03:49:34 +08:00
sqlpp11
=======
2014-01-29 20:10:45 +08:00
A type safe embedded domain specific language for SQL queries and results in C++
2013-08-14 04:43:10 +08:00
2014-07-20 17:43:30 +08:00
Extensive documentation is found in the wiki, https://github.com/rbock/sqlpp11/wiki
2013-08-14 04:43:10 +08:00
2014-08-10 03:07:09 +08:00
Upcoming talks at
* CppCon (Bellevue, Washington, USA): http://cppcon.org
* 2014-09-11: http://sched.co/1r4lue3
* MeetingC++ (Berlin, Germany): http://meetingcpp.com/index.php/mcpp2014.html
* 2014-12-05:http://meetingcpp.com/index.php/tv14/items/4.html
2014-08-18 06:07:21 +08:00
You can contact me
* by posting issues at https://github.com/rbock/sqlpp11/issues
* or via email at rbock at eudoxos dot de
2014-08-10 03:07:09 +08:00
2013-08-14 04:43:10 +08:00
Motivation:
-----------
2013-08-15 15:30:57 +08:00
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.
2013-08-14 04:43:10 +08:00
sqlpp11 is a templated library representing an embedded domain specific language (EDSL) that allows you to
2013-08-17 17:27:41 +08:00
* 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.
2013-08-14 04:43:10 +08:00
2013-08-15 15:30:57 +08:00
This results in several benefits, e.g.
2013-08-17 17:27:41 +08:00
* 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,
2014-08-18 06:07:21 +08:00
* the library hides the gory details of string construction for queries and interpreting results returned by select calls.
2013-08-14 04:43:10 +08:00
2013-09-27 16:57:32 +08:00
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 on the flight.
2014-01-29 20:10:45 +08:00
sqlpp11 is vendor-neutral. Specific traits of databases (e.g. unsupported or non-standard features) are 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.
2013-11-07 18:31:42 +08:00
2013-09-27 16:57:32 +08:00
Your help is needed:
--------------------
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.
2013-08-14 04:43:10 +08:00
Examples:
---------
2013-08-15 15:30:57 +08:00
For the examples, lets assume you have a table class representing something like
2013-08-14 04:43:10 +08:00
```SQL
CREATE TABLE foo (
2013-08-15 15:48:57 +08:00
id bigint,
name varchar(50),
hasFun bool
2013-08-14 04:43:10 +08:00
);
```
And we assume to have a database connection object:
```C++
TabFoo foo;
Db db(/* some arguments*/);
// selecting zero or more results, iterating over the results
2014-07-03 16:15:52 +08:00
for (const auto& row : db(select(foo.name, foo.hasFun).from(foo).where(foo.id > 17 and foo.name.like("%bar%"))))
2013-08-14 04:43:10 +08:00
{
2013-08-15 15:48:57 +08:00
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
2013-10-02 13:34:37 +08:00
bool hasFun = row.hasFun; // bool fields are implicitly convertible to bool
2013-08-14 04:43:10 +08:00
}
// selecting ALL columns of a table
2014-07-03 16:15:52 +08:00
for (const auto& row : db(select(all_of(foo)).from(foo).where(foo.hasFun or foo.name == "joker")))
2013-08-14 04:43:10 +08:00
{
2013-08-15 15:48:57 +08:00
int64_t id = row.id; // numeric fields are implicitly convertible to numeric c++ types
2013-08-14 04:43:10 +08:00
}
// selecting zero or one row, showing off with an alias:
2014-01-29 20:10:45 +08:00
SQLPP_ALIAS_PROVIDER(cheese);
2014-07-03 16:15:52 +08:00
if (const auto& row = db(select(foo.name.as(cheese)).from(foo).where(foo.id == 17)))
2013-08-14 04:43:10 +08:00
{
2013-08-15 15:48:57 +08:00
std::cerr < < "found: " < < row.cheese < < std::endl ;
2013-08-14 04:43:10 +08:00
}
2013-10-01 13:05:12 +08:00
// selecting a single row with a single result:
2014-07-03 16:15:52 +08:00
return db(select(count(foo.id)).from(foo).where(true)).front().count;
2013-08-14 04:43:10 +08:00
Of course there are joins and subqueries, more functions, order_by, group_by etc.
These will be documented soon.
// A sample insert
2014-07-03 16:15:52 +08:00
db(insert_into(foo).set(foo.id = 17, foo.name = "bar", foo.hasFun = true));
2013-08-14 04:43:10 +08:00
// A sample update
2014-07-03 16:15:52 +08:00
db(update(foo).set(foo.hasFun = not foo.hasFun).where(foo.name != "nobody"));
2013-08-14 04:43:10 +08:00
// A sample delete
2014-07-03 16:15:52 +08:00
db(remove_from(foo).where(not foo.hasFun));
2013-08-14 04:43:10 +08:00
```
Requirements:
-------------
2013-08-15 17:54:20 +08:00
__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:
2013-08-14 04:43:10 +08:00
2013-08-19 14:10:11 +08:00
* clang-3.2 on Ubuntu-12.4
2014-07-20 17:43:30 +08:00
* clang-3.4 on Ubuntu-12.4
2013-08-19 14:10:11 +08:00
* g++-4.8 on Ubuntu-12.4
2014-08-21 17:09:50 +08:00
* g++-4.8 on cygwin 64bit
2014-07-20 17:43:30 +08:00
* g++-4.9 on Debian Unstable
2013-08-14 04:43:10 +08:00
2013-08-15 17:54:20 +08:00
__Database Connector:__
sqlpp11 requires a certain api in order to connect with the database, see database/api.h.
2013-08-14 04:43:10 +08:00
2013-09-06 05:57:54 +08:00
* MySQL: https://github.com/rbock/sqlpp11-connector-mysql
2013-09-13 16:37:21 +08:00
* Sqlite3: https://github.com/rbock/sqlpp11-connector-sqlite3
2014-07-03 16:15:52 +08:00
* PostgreSQL: https://github.com/matthijs/sqlpp11-connector-postgresql
2014-07-20 17:43:30 +08:00
To demonstrate that sqlpp11 can work with other backends as well, here is an experimental backend for structs in standard containers:
2013-08-14 04:43:10 +08:00
2014-07-20 17:43:30 +08:00
* STL Container: https://github.com/rbock/sqlpp11-connector-stl