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

537 views Asked by At

I'm writing my first odb code and can't get this basic working, although only the db connection code works:

/*! @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 code:

    // 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;
}

Of course I odb-compiled the overview_record.h with odb.exe --database mssql overview_record.h (otherwise there won't be an .hxx). But I get the following compiler errors by the line db->query<overview_record>();, although instantiating a default constructed result works:

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

1

There are 1 answers

0
Superlokkus On BEST ANSWER

Problem was a missing flag of the odb compiler, in this case the -q e.g. --generate-query flag. Otherwise odb won't add the needed code to the generated files.

So the correct invocation would be odb.exe -q --database mssql overview_record.h