[odb-users] Feature request: relationships not in pointers

Feiyun Wang feiyunw at yahoo.com
Tue Jul 24 22:51:45 EDT 2018


Hi Boris,
Thanks for your info. That's really awesome!I tried the "points_to" feature and there are some problems:
Produce.h:````#pragma once
namespace orm{#pragma db object struct produce {#pragma db id auto int id;#pragma db points_to(factory) int id_factory;#pragma db points_to(product) int id_product; };
// #pragma db view object(produce) object(factory) object(product)#pragma db view object(produce) object(factory: produce::id_factory) object(product: produce::id_product) struct factory_produce_product {#pragma db column(produce::id) int id; int id_factory;#pragma db column(factory::name) std::string factory_name; int id_product;#pragma db column(product::name) std::string product_name; };}````
Building.h:````#pragma once#include <string>
#include "orm-pre.h"
namespace orm{#pragma db object abstract struct building {#pragma db id auto int id;#pragma db unique std::string name; int purchase_price; int base_costs_per_month; int demolition_costs;#pragma db default(1900) int start_year; };
#pragma db object struct factory : public building { int worker_costs_per_month; };// ... unrelated stuff omitted}````
Product.h:````#pragma once#include <string>
#include "orm-pre.h"
namespace orm{#pragma db object struct product {#pragma db id auto int id; std::string name; int costs_per_unit; int sell_price; int production_time;#pragma db default(1900) int start_year; };}````
orm-pre.h:````#pragma once#pragma db value(std::string) type("VARCHAR(45)")
````
runodb.bat:````set PATH=C:\build2\bin;%PATH%cd /d %~dp0where odb.exeECHO ===== Running odb.exe for MySQL MyISAModb.exe -d mysql --mysql-engine MyISAM --generate-schema --at-once --input-name AllTable --generate-query --generate-session --std c++14 Building.h Product.h Produce.h````
Output:````1>------ Build started: Project: TestApp, Configuration: Debug x64 ------1>C:\build2\bin\odb.exe1>===== Running odb.exe for MySQL MyISAM1>AllTable-odb.cxx1>g:\prj\industrygiant\src\orm\alltable-odb.cxx(4583): error C2679: binary '+=': no operator found which takes a right-hand operand of type 'const odb::query_columns<orm::produce,odb::id_mysql,A>::id_factory_type_' (or there is no acceptable conversion)1>        with1>        [1>            A=odb::access::object_traits_impl<orm::produce,odb::id_mysql>1>        ]1>g:\prj\industrygiant\src\include\odb\mysql\query.hxx(339): note: could be 'odb::mysql::query_base &odb::mysql::query_base::operator +=(const std::string &)'1>g:\prj\industrygiant\src\include\odb\mysql\query.hxx(336): note: or       'odb::mysql::query_base &odb::mysql::query_base::operator +=(const odb::mysql::query_base &)'1>g:\prj\industrygiant\src\orm\alltable-odb.cxx(4583): note: while trying to match the argument list '(odb::access::view_traits_impl<orm::factory_produce_product,odb::id_mysql>::query_base_type, const odb::query_columns<orm::produce,odb::id_mysql,A>::id_factory_type_)'1>        with1>        [1>            A=odb::access::object_traits_impl<orm::produce,odb::id_mysql>1>        ]1>g:\prj\industrygiant\src\orm\alltable-odb.cxx(4587): error C2679: binary '+=': no operator found which takes a right-hand operand of type 'const odb::query_columns<orm::produce,odb::id_mysql,A>::id_product_type_' (or there is no acceptable conversion)1>        with1>        [1>            A=odb::access::object_traits_impl<orm::produce,odb::id_mysql>1>        ]1>g:\prj\industrygiant\src\include\odb\mysql\query.hxx(339): note: could be 'odb::mysql::query_base &odb::mysql::query_base::operator +=(const std::string &)'1>g:\prj\industrygiant\src\include\odb\mysql\query.hxx(336): note: or       'odb::mysql::query_base &odb::mysql::query_base::operator +=(const odb::mysql::query_base &)'1>g:\prj\industrygiant\src\orm\alltable-odb.cxx(4587): note: while trying to match the argument list '(odb::access::view_traits_impl<orm::factory_produce_product,odb::id_mysql>::query_base_type, const odb::query_columns<orm::produce,odb::id_mysql,A>::id_product_type_)'1>        with1>        [1>            A=odb::access::object_traits_impl<orm::produce,odb::id_mysql>1>        ]1>Done building project "TestApp.vcxproj" -- FAILED.========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========````
AllTable-odb.cxx snippet:````  access::view_traits_impl< ::orm::factory_produce_product, id_mysql >::query_base_type  access::view_traits_impl< ::orm::factory_produce_product, id_mysql >::  query_statement (const query_base_type& q)  {    query_base_type r (      "SELECT "      "`produce`.`id`, "      "`produce`.`id_factory`, "      "`factory`.`name`, "      "`produce`.`id_product`, "      "`product`.`name` ");
    r += "FROM `produce`";
    r += " LEFT JOIN `factory` ON";    // From Produce.h:17:33    r += query_columns::produce::id_factory;
    r += " LEFT JOIN `product` ON";    // From Produce.h:17:70    r += query_columns::produce::id_product;
    if (!q.empty ())    {      r += " ";      r += q.clause_prefix ();      r += q;    }
    return r;  }````
Is there a special way to use "points_to" with "db view" (except using native view), or did I do something wrong?BTW, "#pragma db view object(produce) object(factory) object(product)" should have provided enough clues for the ODB compiler to work.
Sincerely,Feiyun Wang
P.S.As for lazy_ptr, "unload all the pointer before passing" will ease the burden somehow, but it is still risky as somebody could miss it.
   On Tuesday, July 24, 2018, 6:02:36 PM GMT+8, Boris Kolpackov <boris at codesynthesis.com> wrote:  
 
 Feiyun Wang <feiyunw at yahoo.com> writes:

> In the ODB manual, Chapter 6 Relationships:> Relationships between
> persistent objects are expressed with pointers or containers of pointers. I
> suggest to add the support for relationships expressed in the ODB pragma
> language.

This has already been implemented:

https://git.codesynthesis.com/cgit/odb/odb/commit/?id=e78db08d98d5adb4dee3006eea8c3569e383c562

It is not yet documented in the manual, but the basic usage is:

#pragma db object
class obj1
{
  #pragma db id
  int id;

  ...
};

#pragma db object
class obj2
{
  ...

  #pragma db points_to(obj1)
  int o1;
};


Note also that in your case you may also consider lazy object pointers. You
could just unload all the pointer before passing things to another thread.
  


More information about the odb-users mailing list