query/include/matador/sql/foreign.hpp

55 lines
1.1 KiB
C++

#ifndef QUERY_FOREIGN_HPP
#define QUERY_FOREIGN_HPP
#include "matador/utils/identifiable.hpp"
namespace matador::sql {
template < class Type >
class foreign : public utils::identifiable
{
public:
foreign() = default;
explicit foreign(Type *obj)
: obj_(obj) {}
void reset(const utils::identifier &id) override {
id_ = id;
}
[[nodiscard]] bool has_primary_key() const override {
return true;
}
[[nodiscard]] const utils::identifier &primary_key() const override {
return id_;
}
utils::identifier &primary_key() override {
return id_;
}
[[nodiscard]] utils::identifier create_identifier() const override {
return {id_};
}
Type* operator->() { return obj_.get(); }
const Type* operator->() const { return obj_.get(); }
Type& operator*() { return *obj_; }
const Type& operator*() const { return *obj_; }
private:
utils::identifier id_;
std::unique_ptr<Type> obj_;
};
template<class Type, typename... Args>
[[maybe_unused]] foreign<Type> make_foreign(Args&&... args)
{
return foreign(new Type(std::forward<Args>(args)...));
}
}
#endif //QUERY_FOREIGN_HPP