[odb-users] querying object and loading into a stack variable, then setting its id to 0 and persisting

MM finjulhich at gmail.com
Wed Jul 31 04:20:44 EDT 2024


>> The full picture is
>>
>> struct Base {
>>   std::uint64_t id;   #pragma  member(Base::id) id auto
>> virtual(std::uint64_t)
>> };

>I am not sure about being full, but I am pretty sure it's not an
>accurate picture. If I try to compile the above code fragment as-is
>(after removing the line break between `auto` and `virtual` which
I> assume was added by your email client), so this:

>struct Base {
> std::uint64_t id;   #pragma  member(Base::id) id auto
virtual(std::uint64_t)
>};

>I get:

>test.hxx:2:23: error: stray ‘#’ in program

>Please send a complete, compilable reproducer if you want me to look
>into this further.

Hello
This standalone file compiles for me with gcc

#include <odb/core.hxx>
#include <cstdint>

typedef std::uint64_t Baseid_t;

struct Base {
  void              set_id(Baseid_t);
  Baseid_t      get_id() const;
  virtual Base* clone() const =0;
  virtual           ~Base() =default;
protected:
  Base();
  Baseid_t id_; /// obtained from database
};

#pragma db object(Base) abstract definition transient
#pragma db member(Base::id) id auto virtual(Baseid_t)


template <typename tbase, typename derived>
class cloneable : public tbase
{
protected:
  using tbase::tbase;

  virtual tbase* clone() const override
  {
    return new derived{static_cast<derived const &>(*this)};
  }
};

struct Derived;
using derived_Derived_ctrct = cloneable<Base,Derived>;
#pragma db object(derived_Derived_ctrct) abstract definition



struct Derived : public cloneable<Base, Derived> {
  double data1;

};

#pragma db object(Derived) table("Deriveds") definition


int main() {

  return 0;
}

Let me know if I should add to this code for opening a database and for
calling the persist() method, where the tracer shows that the INSERT INTO
query still uses the 'id' column?

>From a first look as to how I wrote the Base::id  member pragma, do you
think the generated SQL stmt should not use column id because the database
will take care of that?


More information about the odb-users mailing list