ormpp icon indicating copy to clipboard operation
ormpp copied to clipboard

不支持嵌套数据结构吗?

Open mozeat-sun opened this issue 7 years ago • 15 comments

struct Title { int t; std::string code; };

struct person { int id; std::string name; int age; Title title; }; 这时候 REFLECTION是什么样子的?

mozeat-sun avatar Oct 13 '18 10:10 mozeat-sun

支持嵌套,你看下iguana里的例子

qicosmos avatar Oct 13 '18 10:10 qicosmos

....\database\ormpp-master/utility.hpp:88:57: error: no matching function for call to 'type_to_name(ormpp::identity<Title>)' case DBType::sqlite : s = ormpp_sqlite::type_to_name(identity<U>{}); ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~

mozeat-sun avatar Oct 15 '18 03:10 mozeat-sun

REFLECTION(person, id, name, age,title) 直接报错,

mozeat-sun avatar Oct 15 '18 03:10 mozeat-sun

title也要写reflection

qicosmos avatar Oct 15 '18 03:10 qicosmos

struct Title { int t; }; REFLECTION(Title, t)

struct person { int id; std::string name; int age; Title title; }; REFLECTION(person, id, name, age,title)

....\database\ormpp-master/utility.hpp:88:57: error: no matching function for call to 'type_to_name(ormpp::identity<Title>)'

我已经reflection了,

mozeat-sun avatar Oct 15 '18 05:10 mozeat-sun

ormpp不支持结构体嵌套,ormpp里面的结构体和数据库表是对应的,不应该有嵌套情况。iguana序列化本身是支持嵌套的。

qicosmos avatar Oct 15 '18 05:10 qicosmos

那是不是太局限了?

mozeat-sun avatar Oct 15 '18 05:10 mozeat-sun

数据库的表本身不能嵌套其他表啊,你加嵌套上去做什么呢,加上去了没法和数据表做映射了。

qicosmos avatar Oct 15 '18 05:10 qicosmos

恩,这是个好问题。很多人不关心数据库如何存,存什么格式。只关心,存对应的数据结构和能取出对应的结构体,

mozeat-sun avatar Oct 15 '18 06:10 mozeat-sun

在ruby on rails里,数据表A里嵌套另一个数据表B的时候,指的是A的字段里有B的ID,在查询A的时候,顺便也根据A里B_id的值,把B查询出来.

Rails 支持六种关联: belongs_to has_one has_many has_many :through has_one :through has_and_belongs_to_many

struct Title
{
    int t;
};
REFLECTION(Title, t)
struct person
{
    int id;
    std::string name;
    int age;
    Title title;
};
REFLECTION(person, id, name, age,title)

如果这样对应has_one关联的话,person的title字段实际存的是一个int,值为对应Title表里的一个记录的主键id

相关介绍可见 https://rails.guide/book/association_basics.html#active-record-associations

如果能实现这样的操作,数据库相关的操作真的就非常方便了

joshuafc avatar Sep 16 '19 12:09 joshuafc

这个库做了相应尝试,不过看起来使用起来太繁琐. https://github.com/bmuller/StactiveRecord

joshuafc avatar Sep 16 '19 12:09 joshuafc

结构体中包含枚举类型也不行吗?我有一个同样的结构体,里面加了一个enum类型,然后就报这个错了,不加的时候是对的。

newdee avatar Mar 01 '23 07:03 newdee

什么错误?

qicosmos avatar Mar 01 '23 12:03 qicosmos

什么错误? 创建表的时候 大概和这个issue下的类似, error: no matching function for call to 'type_to_name(ormpp::identity<Title>)' 然后发现好像是不支持枚举类型的,估计得手动把枚举转int,另外timestamp转bigint不太好吧,但是问题也不大,string转text,array转varchar,这两个是不是真的不太好~~~ 对了,因为C++的orm很少,你写的这个我觉得很棒,而且是MIT的,但是维护开源项目不易,我只是说一下用户习惯哈,希望别生气

newdee avatar Mar 01 '23 12:03 newdee

哈哈,你的建议很好,其实这些都可以支持,只不过目前主要精力在雅兰亭库和cinatra c++20 协程重构上,等这两个差不多了就可以再来重构优化ormpp了。

qicosmos avatar Mar 01 '23 23:03 qicosmos