odb::result<my_type> 的实例化,例如odb::query<my_type> 失败

Instantation of odb::result<my_type> e.g. odb::query<my_type> fails

我正在编写我的第一个 odb 代码,但无法使这个基本工作正常,尽管只有 db 连接代码有效:

/*! @file overview_record.h
*/
#ifndef OVERVIEW_RECORD_H
#define OVERVIEW_RECORD_H
#include <string>
#include <odb/core.hxx>
#include <odb/nullable.hxx>

#pragma db object table("mddb_overview") no_id 

    class overview_record
    {
    public:

#pragma db column("product_name") type("nvarchar(64)")
        std::wstring product_name;

#pragma db column("order_number") type("int")
        long order_number;
    };

#endif

driver代码:

    // odb_playground.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <memory>
#include <thread>

#include <odb/core.hxx>
#include <odb/database.hxx>
#include <odb/mssql/database.hxx>
#include <odb/mssql/connection-factory.hxx>
#include <odb/mssql/exceptions.hxx>

#include "overview_record-odb.hxx"

int main(int argc, char* argv[])
{
    try{
        std::auto_ptr<odb::mssql::connection_pool_factory> connection_factory(
            new odb::mssql::connection_pool_factory(0, std::thread::hardware_concurrency()));
        std::unique_ptr<odb::database> db(
            new odb::mssql::database("dsn=mddb_local_32", odb::mssql::isolation_read_committed, static_cast<SQLHENV>(0), connection_factory)
            );


        odb::transaction t(db->begin());
        db->query<overview_record>();
        //odb::result<overview_record> result();
        //auto it = result.begin();
        //while(true)
        //{
        //  static int i = 0;
        //  if (i++ > 10)
        //      break;
        //  std::cout << "Order_number " << it->order_number << " product_name " << it->product_name << std::endl;
        //  ++i;

        //}
        t.commit();
    }
    catch (const odb::database_exception &e) {
        std::cout << "ODB database error: " << e.what() << std::endl;
    }
    return 0;
}

当然我odb-compiled overview_record.h 和 odb.exe --database mssql overview_record.h (否则不会有.hxx)。但是我在 db->query<overview_record>(); 行收到以下编译器错误,尽管实例化默认构造的结果有效:

Error 3 error C2504: 'odb::result_base' : base class undefined c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 76 1 odb_playground

Error 4 error C2027: use of undefined type 'odb::result_base' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground

Error 5 error C2146: syntax error : missing ';' before identifier 'value_type' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground

Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground

Error 7 error C2602: 'odb::result::value_type' is not a member of a base class of 'odb::result' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground

Error 8 error C2868: 'odb::result::value_type' : illegal syntax for using-declaration; expected qualified-name c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground

Error 9 error C2027: use of undefined type 'odb::result_base' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground

Error 10 error C2146: syntax error : missing ';' before identifier 'result_impl_type' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground

Error 11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground

Error 12 error C2602: 'odb::result::result_impl_type' is not a member of a base class of 'odb::result' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground

Error 13 error C2868: 'odb::result::result_impl_type' : illegal syntax for using-declaration; expected qualified-name c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground

问题是 odb 编译器缺少标志,在这种情况下 -q 例如--generate-query 标志。否则 odb 不会将需要的代码添加到生成的文件中。

所以正确的调用应该是odb.exe -q --database mssql overview_record.h