适用于C++的SQLite数据库的orm,https://github.com/fnc12/sqlite_orm
表结构
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;
};
SQL语句
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')
sqlite_orm语句
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"))));
插入 insert
User user{-1, "Jonh", "Doe", 664416000, std::make_unique<std::string>("url_to_heaven"), 3 };
auto insertedId = storage.insert(user);
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;
}```
或者
```cpp
if(auto user = storage.get_pointer<User>(insertedId)){
cout << "user = " << user->firstName << " " << user->lastName << endl;
}else{
cout << "no user with id " << insertedId << endl;
}
修改 updata
user.firstName = "Nicholas";
user.imageUrl = "https://cdn1.iconfinder.com/data/icons/man-icon-set/100/man_icon-21-512.png"
storage.update(user);
storage.update_all(set(c(&User::lastName) = "Hardey",
c(&User::typeId) = 2),
where(c(&User::firstName) == "Tom"));
删除 remove
storage.remove<User>(insertedId)
遍历所有对象 get.all()
auto allUsers = storage.get_all<User>();
cout << "allUsers (" << allUsers.size() << "):" << endl;
for(auto &user : allUsers) {
cout << storage.dump(user) << endl;
}
for(auto &user : storage.iterate<User>()) {
cout << storage.dump(user) << endl;
}
复杂查询
// SELECT doctor_id
// FROM visits
// WHERE LENGTH(patient_name) > 8
auto selectStatement = storage.prepare(select(&Visit::doctor_id, where(length(&Visit::patient_name) > 8)));
cout << "selectStatement = " << selectStatement.sql() << endl; // prints "SELECT doctor_id FROM ..."
auto rows = storage.execute(selectStatement); // rows is std::vector
// SELECT doctor_id
// FROM visits
// WHERE LENGTH(patient_name) > 11
get<0>(selectStatement) = 11;
auto rows2 = storage.execute(selectStatement);
avg() 平均值 .avg()
count() 统计值 .count()
MAX() 最大值 .max() return std::unique_ptr
MIN() 最小值 min() return std::unique_ptr
SUM() 求和 return std::unique_ptr
TOTAL()
auto id5and7 = storage.get_all<User>(where(c(&User::id) <= 7 and c(&User::id) >= 5 and not (c(&User::id) == 6)));
cout << "id5and7 count = " << id5and7.size() << endl;
for(auto &user : id5and7) {
cout << storage.dump(user) << endl;
}
// SELECT id FROM users WHERE last_name = 'Doe'
auto doeIds = storage.select(&User::id, where(c(&User::lastName) == "Doe"));
cout << "doeIds count = " << doeIds.size() << endl; // doeIds is std::vector
for(auto &doeId : doeIds) {
cout << doeId << " ";
}
cout << endl;
查询若干列
// `SELECT first_name, last_name FROM users WHERE id > 250 ORDER BY id`
auto partialSelect = storage.select(columns(&User::firstName, &User::lastName),
where(c(&User::id) > 250),
order_by(&User::id));
cout << "partialSelect count = " << partialSelect.size() << endl;
for(auto &t : partialSelect) {
auto &firstName = std::get<0>(t);
auto &lastName = std::get<1>(t);
cout << firstName << " " << lastName << endl;
}
// `SELECT * FROM users WHERE id < 250 ORDER BY first_name`
auto orderedUsers2 = storage.get_all<User>(where(c(&User::id) < 250), order_by(&User::firstName));
cout << "orderedUsers2 count = " << orderedUsers2.size() << endl;
for(auto &user : orderedUsers2) {
cout << storage.dump(user) << endl;
}