[odb-users] Help, how to generate the hxx files automately by odb

Boris Kolpackov boris at codesynthesis.com
Wed Feb 13 09:42:36 EST 2013


Hi,

fryli <49834715 at qq.com> writes:

> I have a database based on mysql, how can i generate all hxx files
> by odb not by myself manually?

No, this is not yet supported. We do have plans for an SQL-to-C++
compiler, thought.

Note also that ODB supports manually mapping C++ classes to an 
existing database schema. That is, you can tell ODB the names of
the tables/columns/etc., that the class should use, their types,
null/not_null, etc. See the 'schema/custom' example in the 
odb-examples package for some examples.


> Another problem is i test the example "hello", the persistence operation
> only inserts two records into the mysql, but the first records John.Doe
> vanished, there should be three records. Who can tell me how to solve
> the problem?
> I created a person table: 
> id,int, auto increment
> first, varchar(45)
> last, varchar(45)
> age,int

Not sure what the problem is, but the first thing to try is to
tell the ODB compiler the types of each member. For example:

#pragma db object
class person
{
  ...

  #pragma db id auto type("INT")
  unsigned long id_;

  #pragma db type("VARCHAR(45)") null
  std::string first_;

  #pragma db type("VARCHAR(45)") null
  std::string last_;

  #pragma db type("INT") null
  unsigned short age_;
};

Generally, when mapping a class to an existing schema there is
a very useful verification test to do: generate the schema with
the --generate-schema option and make sure that the result SQL
is semantically equivalent to your actual schema.

For example, given the above mapping, I get the following schema:

CREATE TABLE `person` (
  `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `first` VARCHAR(45),
  `last` VARCHAR(45),
  `age` INT);

Which is pretty much what you have (I assume id is a primary key).

Boris



More information about the odb-users mailing list