c++ sqlite_orm 多态的写法
在C++中,可以借助SQLite ORM库(sqlite_orm)实现多态的示范。首先安装或将 sqlite_orm 库添加到项目中,可以通过以下命令或从GitHub仓库下载:
git clone https://github.com/fnc12/sqlite_orm.git
SQLite ORM库允许用户以非常简洁的方式处理数据库相关操作。为了实现多态,需要为基类和派生类创建相应的存储。以下是一个实现示例:
实现示例
首先,创建一个基类 Animal 和两个派生类 Dog 和 Cat:
#include <string>
class Animal {
public:
virtual ~Animal() = default;
int id;
std::string type;
};
class Dog : public Animal {
public:
std::string breed;
};
class Cat : public Animal {
public:
int age;
};
数据库映射
#include <sqlite_orm/sqlite_orm.h>
using namespace sqlite_orm;
inline auto initStorage(const std::string &path) {
return make_storage(path,
make_table("animals",
make_column("id", &Animal::id, primary_key()),
make_column("type",&Animal::type)),
make_table("dogs",make_column("animal_id",&Dog::id),
make_foreign_key(&Dog::id,"animals","id",ForeignKeyAction::Cascade,ForeignKeyAction::Cascade),
make_column("breed",&Dog::breed)),
make_table("cats",
make_column("animal_id",&Cat::id),
make_foreign_key(&Cat::id,"animals","id",
ForeignKeyAction::Cascade,
ForeignKeyAction::Cascade),
make_column("age",&Cat::age)));
}
现在,我们可以访问、插入和操作数据库中的Animal
、Dog
和Cat
对象。
简单的用法示例
#include <iostream>
int main() {
auto storage = initStorage("example.sqlite");
storage.sync_schema();
auto newDog = Dog{};
newDog.type = "Dog";
newDog.breed = "Golden Retriever";
auto newCat = Cat{};
newCat.type = "Cat";
newCat.age = 3;
// Insert new dog and cat in the database
storage.insert(newDog);
storage.insert(newCat);
// Fetch all animals
auto animals = storage.get_all<Animal>();
for (auto& animal : animals) {
std::cout << "Animal id: " << animal.id << ", type: " << animal.type << std::endl;
if (animal.type == "Dog")
{
// Fetch dog from dogs table
Dog dog = storage.getog > (animal.id);
std::cout << "\tBreed: " << dog.breed << std::endl;
}
else if (animal.type == "Cat")
{
// Fetch cat from cats table
Cat cat = storage.getat > (animal.id);
std::cout << "\tAge: " << cat.age << std::endl;
}
}
return 0;
}
以上示例会连接和同步“example.sqlite”数据库,然后插入一个新的狗和猫实例。接着,它将获取存储库中所有的动物实例,并通过类型检查执行多态。
c++ sqlite_orm 使用类的写法
我们假设您希望用一个类来处理SQLite ORM相关的操作。以下是一个实现示例,其中演示了如何创建一个名为DatabaseManager
的类来处理上一个示例中的相同操作:
实现示例
首先,创建一个基类Animal
和两个派生类Dog
和Cat
:
#include <string>
class Animal {
public:
virtual ~Animal() = default;
int id;
std::string type;
};
class Dog : public Animal {
public:
std::string breed;
};
class Cat : public Animal {
public:
int age;
};
处理数据库相关操作
创建一个DatabaseManager
类以处理数据库相关操作:
#include <sqlite_orm/sqlite_orm.h>
using namespace sqlite_orm;
class Animal {
public:
virtual ~Animal() = default;
int id;
std::string type;
};
class Dog : public Animal {
public:
std::string breed;
};
class Cat : public Animal {
public:
int age;
};
//数据管理类
class DatabaseManager {
private:
using Storage = decltype(
make_storage("",
make_table("",
make_column("", &Animal::id),
make_column("", &Animal::type)),
make_table("",
make_column("", &Dog::id),
make_column("", &Dog::breed)),
make_table("",
make_column("", &Cat::id),
make_column("", &Cat::age))));
Storage storage;
public:
DatabaseManager(const std::string& path) : storage(initStorage(path)) {
storage.sync_schema();
}
void insertAnimal(const Animal& animal) {
storage.insert(animal);
}
void insertDog(const Dog& dog) {
storage.insert(dog);
}
void insertCat(const Cat& cat) {
storage.insert(cat);
}
std::vector<Animal> getAllAnimals() {
return storage.get_all<Animal>();
}
Dog getDog(int id) {
return storage.getog > (id);
}
Cat getCat(int id) {
return storage.getat > (id);
}
private:
Storage initStorage(const std::string& path) {
return make_storage(path,
make_table("animals",
make_column("id", &Animal::id, primary_key()),
make_column("type", &Animal::type)),
make_table("dogs",
make_column("animal_id", &Dog::id),
make_foreign_key(&Dog::id, "animals", "id"),
make_column("breed", &Dog::breed)),
make_table("cats",
make_column("animal_id", &Cat::id),
make_foreign_key(&Cat::id, "animals", "id"),
make_column("age", &Cat::age)));
}
};
现在,我们可以使用DatabaseManager
类来访问、插入和操作数据库中的Animal
、Dog
和Cat
对象:
简单的用法示例
#include <iostream>
int main() {
DatabaseManager db("example.sqlite");
Dog newDog{};
newDog.type = "Dog";
newDog.breed = "Golden Retriever";
db.insertAnimal(newDog);
db.insertDog(newDog);
Cat newCat{};
newCat.type = "Cat";
newCat.age = 3;
db.insertAnimal(newCat);
db.insertCat(newCat);
auto animals = db.getAllAnimals();
for (const auto& animal : animals) {
std::cout << "Animal id: " << animal.id << ", type: " << animal.type << std::endl;
if (animal.type == "Dog") {
Dog dog = db.getDog(animal.id);
std::cout << "\tBreed: " << dog.breed << std::endl;
}
else if (animal.type == "Cat") {
Cat cat = db.getCat(animal.id);
std::cout << "\tAge: " << cat.age << std::endl;
}
}
return 0;
}
这个示例将连接和同步example.sqlite
数据库,然后插入一个新的Dog和Cat实例。接着,它将获取存储库中所有的动物实例,并通过类型检查执行多态。
评论区