cpp-SQLiteORM用于现代C的SQLiteORM库只有header

  • q2_548262
    了解作者
  • 337.6KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-14 04:59
    上传日期
SQLite ORM:用于现代C 的SQLite ORM库,只有header
cpp-SQLiteORM用于现代C的SQLiteORM库只有header.zip
内容介绍
<p align="center"> <img src="https://github.com/fnc12/sqlite_orm/blob/master/logo.png" alt="Sublime's custom image" width="557"/> </p> [![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause) [![Donate using PayPal](https://img.shields.io/badge/donate-PayPal-brightgreen.svg)](https://paypal.me/fnc12) [![Twitter URL](https://img.shields.io/twitter/url/https/twitter.com/fold_left.svg?style=social&label=Follow%20%40sqlite_orm)](https://twitter.com/sqlite_orm) # SQLite ORM SQLite ORM light header only library for modern C++ # Status | Branch | Travis | Appveyor | Coverity Scan | codecov.io | Website | | :----- | :----- | :------- | :------------ | :--------- | :------ | | [`master`](https://github.com/fcn12/sqlite_orm/tree/master) | [![Build Status](https://travis-ci.org/fnc12/sqlite_orm.svg?branch=master)](https://travis-ci.org/fcn12/sqlite_orm) | [![Build status](https://ci.appveyor.com/api/projects/status/github/fnc12/sqlite_orm?branch=master&svg=true)](https://ci.appveyor.com/project/fnc12/sqlite-orm/history) | | | [![Website](https://img.shields.io/badge/official-website-brightgreen.svg)](https://github.com/fcn12/sqlite_orm/) | | [`dev`](https://github.com/fcn12/sqlite_orm/tree/dev) | [![Build Status](https://travis-ci.org/fnc12/sqlite_orm.svg?branch=dev)](https://travis-ci.org/fcn12/sqlite_orm) | [![Build status](https://ci.appveyor.com/api/projects/status/github/fnc12/sqlite_orm?branch=dev&svg=true)](https://ci.appveyor.com/project/fnc12/sqlite-orm/history) | | | [![Website](https://img.shields.io/badge/official-website-brightgreen.svg)](https://github.com/fcn12/sqlite_orm/tree/dev) | # Advantages * **No raw string queries** * **Intuitive syntax** * **Comfortable interface - one code line per single query** * **Built with modern C++14 features (no macros and external scripts)** * **CRUD support** * **Pure select query support** * **UNION, EXCEPT and INTERSECT support** * **STL compatible** * **Custom types binding support** * **BLOB support** - maps to `std::vector<char>` or one can bind your custom type * **FOREIGN KEY support** * **Composite key support** * **JOIN support** * **Transactions support** * **Migrations functionality** * **Powerful conditions** * **ORDER BY and LIMIT, OFFSET support** * **GROUP BY / DISTINCT support** * **INDEX support** * **Follows single responsibility principle** - no need write code inside your data model classes * **Easy integration** - single header only lib. * **The only dependency** - libsqlite3 * **C++ standard code style** * **No undefined behaviour** - if something goes wrong lib throws an exception * **In memory database support** - provide `:memory:` or empty filename * **COLLATE support** * **Limits setting/getting support** `sqlite_orm` library allows to create easy data model mappings to your database schema. It is built to manage (CRUD) objects with a single column with primary key and without it. It also allows you to specify table names and column names explicitly no matter how your classes actually named. Take a look at example: ```c++ struct User{ int id; std::string firstName; std::string lastName; int birthDate; std::unique_ptr<std::string> imageUrl; int typeId; }; struct UserType { int id; std::string name; }; ``` So we have database with predefined schema like `CREATE TABLE users (id integer primary key autoincrement, first_name text not null, last_name text not null, birth_date integer not null, image_url text, type_id integer not null)` `CREATE TABLE user_types (id integer primary key autoincrement, name text not null DEFAULT 'name_placeholder')` Now we tell `sqlite_orm` library about schema and provide database filename. We create `storage` service object that has CRUD interface. Also we create every table and every column. All code is intuitive and minimalistic. ```c++ using namespace sqlite_orm; auto storage = make_storage("db.sqlite", make_table("users", make_column("id", &User::id, autoincrement(), primary_key()), make_column("first_name", &User::firstName), make_column("last_name", &User::lastName), make_column("birth_date", &User::birthDate), make_column("image_url", &User::imageUrl), make_column("type_id", &User::typeId)), make_table("user_types", make_column("id", &UserType::id, autoincrement(), primary_key()), make_column("name", &UserType::name, default_value("name_placeholder")))); ``` Too easy isn't it? You do not have to specify mapped type explicitly - it is deduced from your member pointers you pass during making a column (for example: `&User::id`). To create a column you have to pass two arguments at least: its name in the table and your mapped class member pointer. You can also add extra arguments to tell your storage about column's constraints like ~~`not_null`~~ (deduced from type), `primary_key`, `autoincrement`, `default_value` or `unique`(order isn't important). If your datamodel classes have private or protected members to map to sqlite then you can make a storage with setter and getter functions. More info in the [example](https://github.com/fnc12/sqlite_orm/blob/master/examples/private_class_members.cpp). More details about making storage can be found in [tutorial](https://github.com/fnc12/sqlite_orm/wiki/Making-storage). # CRUD Let's create and insert new `User` into database. First we need to create a `User` object with any id and call `insert` function. It will return id of just created user or throw exception if something goes wrong. ```c++ User user{-1, "Jonh", "Doe", 664416000, std::make_unique<std::string>("url_to_heaven"), 3 }; auto insertedId = storage.insert(user); cout << "insertedId = " << insertedId << endl; // insertedId = 8 user.id = insertedId; User secondUser{-1, "Alice", "Inwonder", 831168000, {} , 2}; insertedId = storage.insert(secondUser); secondUser.id = insertedId; ``` Next let's get our user by id. ```c++ try{ auto user = storage.get<User>(insertedId); cout << "user = " << user.firstName << " " << user.lastName << endl; }catch(std::system_error e) { cout << e.what() << endl; }catch(...){ cout << "unknown exeption" << endl; } ``` Probably you may not like throwing exceptions. Me too. Exception `std::system_error` is thrown because return type in `get` function is not nullable. You can use alternative version `get_pointer` which returns `std::unique_ptr` and doesn't throw `not_found_exception` if nothing found - just returns `nullptr`. ```c++ if(auto user = storage.get_pointer<User>(insertedId)){ cout << "user = " << user->firstName << " " << user->lastName << endl; }else{ cout << "no user with id " << insertedId << endl; } ``` `std::unique_ptr` is used as optional in `sqlite_orm`. Of course there is class optional in C++14 located at `std::experimental::optional`. But we don't want to use it until it is `experimental`. We can also update our user. It updates row by id provided in `user` object and sets all other non `primary_key` fields to values stored in the passed `user` object. So you can just assign members to `user` object you want and call `update` ```c++ user.firstName = "Nicholas"; user.imageUrl = "https://cdn1.iconfinder.com/data/icons/man-icon-set/100/man_icon-21-512.png" storage.update(user); ``` Also there is a non-CRUD update version `update_all`: ```c++ storage.update_all(set(c(&User::lastName) = "Hardey", c(&User::typeId) = 2), where(c(&User::firstName) == "Tom")); ``` And delete. To delete you have to pass id only, not whole object. Also we need to explici
评论
    相关推荐
    • SQLite SQL语法中文手册
      SQLite SQL语法中文手册 SQLite是个轻便的数据库,不太适合网络环境,但是使用还是很方便的。
    • SQL As Understood By SQLite
      SQL As Understood By SQLite SQL As Understood By SQLite SQL As Understood By SQLite SQL As Understood By SQLite SQL As Understood By SQLite
    • SQLiteSQL语法
      SQLiteSQL语法 SQLite库可以解析大部分标准SQL语言。但它也省去了一些特性 并且加入了一些自己的新特性。这篇文档就是试图描述那些SQLite支持/不支持的SQL语法的。
    • Sqlite sql 语法
      Sqlite sql 语法 Sqlite sql 语法
    • sqlite sql 语法
      sqlite sql 语法的一个简单的介绍。入门学习使用。
    • SQLite Sql语法参考手册
      提供一个详细的SQLite Sql语法手册,供各位使用.对于有兴趣使用嵌入式数据库的同仁来说谅是一个不错的参考书.
    • sqlite sql 语法(chm版)
      sqlite sql 语法(chm版) SQLite库可以解析大部分标准SQL语言。但它也省去了一些特性 并且加入了一些自己的新特性。
    • Sqlite SQL基本语法汇总
      Sqlite SQL基本语法汇总,在其官方网站及网络整理的。
    • sql server 转换成 sqlite
      sql server 转换成 sqlite
    • SQLite支持的SQL语言
      本资源是SQLite支持的SQL的语法的总结。 已经算是很全。