ORM 加入 table 行数

ORM get joined table row count

我目前在一个项目中将 Witty 和 DBO 与 SQLite3 一起使用,现在我需要加入一个相关的 table 以仅获取行数。

表格

CREATE TABLE "rfnode" (
  "mac" varchar(16) not null,
  "zone" integer not null,
  "subZone" integer not null,
  "unit" text not null,
  "pwm" integer not null,
  "led" integer not null,
  "network" integer not null,
  "lastContact" text,
  "ioConfiguration" integer not null,
  primary key ("mac")
)

CREATE TABLE "nodemeasure" (
  "id" integer primary key autoincrement,
  "mac" text not null,
  "type" integer not null,
  "date" text,
  "batchDate" text,
  "value" real not null
)

通常 SQL 查询是:

SELECT
    rn.*,
    COUNT(nm.id) AS measuresCount
FROM rfnode rn
LEFT JOIN nodemeasure nm
    ON nm.mac = rn.mac
GROUP BY rn.mac

现在的问题是我不知道如何使用 Wt/DBO 库查询和获取结果。

第一次尝试

class RFNode
{
    public:
        std::string mac, unit;
        int zone = 0, subZone = 0, pwm = 0, led = 0, network = 0, ioConfiguration = 0, measuresCount = 0;
        Wt::WDateTime lastContact;

        RFNode()
        {
            mac             = "";
            zone            = 0;
            subZone         = 0;
            unit            = "";
            pwm             = 8;
            led             = 8;
            network         = 0;
            ioConfiguration = 0;
            measuresCount   = 0;
        }

        RFNode(std::string p_sMAC, int p_nZone, int p_nSubZone, std::string p_sUnit, int p_nPWM, int p_nLED, int p_nNetwork, Wt::WDateTime p_oLastContact, int p_nIOConfiguration, int p_nMeasuresCount = 0)
        {
            mac             = p_sMAC;
            zone            = p_nZone;
            subZone         = p_nSubZone;
            unit            = p_sUnit;
            pwm             = p_nPWM;
            led             = p_nLED;
            network         = p_nNetwork;
            lastContact     = p_oLastContact;
            ioConfiguration = p_nIOConfiguration;
            measuresCount   = p_nMeasuresCount;
        }

        template<class Action>
        void persist(Action& a)
        {
            dbo::id(a,      mac,            "mac",16);
            dbo::field(a,   zone,           "zone");
            dbo::field(a,   subZone,        "subZone");
            dbo::field(a,   unit,           "unit");
            dbo::field(a,   pwm,            "pwm");
            dbo::field(a,   led,            "led");
            dbo::field(a,   network,        "network");
            dbo::field(a,   lastContact,    "lastContact");
            dbo::field(a,   ioConfiguration,"ioConfiguration");
            dbo::field(a,   measuresCount,  "measuresCount");
        };
};

使用查询:

Wt::Dbo::collection<Wt::Dbo::ptr<RFNode>> lTemp = m_oSession.query<Wt::Dbo::ptr<RFNode>>("SELECT rn.*, COUNT(nm.id) AS measuresCount FROM rfnode rn LEFT JOIN nodemeasure nm ON nm.mac = rn.mac").resultList();

导致 Session::query(): too many aliases for result.

第二次尝试

使用相同的 class RFNode,除了删除字段 measuresCount

typedef Wt::Dbo::ptr_tuple<RFNode, int>::type RFNodeJoin;
        typedef Wt::Dbo::collection<RFNodeJoin> RFNodeJoinC;

        RFNodeJoinC lTemp = m_oSession.query<RFNodeJoin>("SELECT rn.*, COUNT(nm.id) AS measuresCount FROM rfnode rn LEFT JOIN nodemeasure nm ON nm.mac = rn.mac GROUP BY rn.mac").resultList();

        std::vector<RFNodeJoin> lTransition(lTemp.begin(),lTemp.end());

但它没有编译,导致错误:error: request for member ‘persist’ in ‘obj’, which is of non-class type ‘int’。我想我需要为每个 table 映射一个 class,但由于我想要一个计数并且没有现有字段,我认为 int class 无法完成这项工作。

最终通过反复试验我找到了解决方案...

首先,RFNode class 不再有字段 measuresCount

请求和迭代如下:

Wt::Dbo::collection<boost::tuple<Wt::Dbo::ptr<RFNode>, int>> lTemp = m_oSession.query<boost::tuple<Wt::Dbo::ptr<RFNode>, int>>("SELECT rn, COUNT(nm.id) FROM rfnode rn LEFT JOIN nodemeasure nm ON nm.mac = rn.mac GROUP BY rn.mac").resultList();

for(Wt::Dbo::collection<boost::tuple<Wt::Dbo::ptr<RFNode>, int>>::const_iterator i = lTemp.begin();i != lTemp.end();++i)
{
    Wt::Dbo::ptr<RFNode> pRFNode;
    int nNodeMeasureCount;

    boost::tie(pRFNode,nNodeMeasureCount) = *i;

    lNodes.push_back(
            RFNode(
                    pRFNode->mac,
                    pRFNode->zone,
                    pRFNode->subZone,
                    pRFNode->unit,
                    pRFNode->pwm,
                    pRFNode->led,
                    pRFNode->network,
                    pRFNode->lastContact,
                    pRFNode->ioConfiguration,
                    nNodeMeasureCount
            )
    );
}

希望有一天它能对某人有所帮助!