example-database.C

The db2cpp tool generates (static storage) Dv::Sql::Table::Ref declarations and definitions for all tables found in a database. The code below was generated using the command

 db2cpp --database=test --password=mysql1 --out=example-database

which generates two files with declarations, resp. definitions. Suppose the database was defined using the following sql statements:

 create table customer (
   id  int auto_increment primary key,
   first_name  varchar(200) not null,
   last_name  varchar(200) not null,
   birth_date  date,
   sex  char(1),
   email  varchar(200),
   mod_date timestamp,
   unique index(first_name, last_name)
   );
 
 create table account (
   code char(14) not null primary key, # e.g. IBAN code
   description text,
   customer_id int not null references customer(id)
   );
 
 create table transaction (
   id  int auto_increment primary key,
   amount int not null,
   account_code char(14) not null references account(code),
   executed  timestamp,
   description text
   );

. The resulting output is shown below.

example-database.h
#include <dvmysql/row.h>
#include <dvmysql/mysqldb.h>

class Database: public Dv::MySql::Db {
  public:
    Database(const std::string& db_name, const std::string& user_name,
        const std::string& passwd, const std::string& hostname="localhost") throw (std::runtime_error);
    Database(const Dv::Util::Props& config, const std::string& prefix="")
        throw (std::runtime_error);
    static Dv::Sql::Table::Ref account;
    static Dv::Sql::Table::Ref customer;
    static Dv::Sql::Table::Ref transaction;
  private:
    void init() throw (std::runtime_error);
};

namespace Account {
  typedef Dv::Sql::Row<Database::account> Row;
  typedef Row::Set Set;
  typedef Dv::Sql::Row<Database::account>::Column Column;
  extern const Column code;
  extern const Column description;
  extern const Column customer_id;
};

namespace Customer {
  typedef Dv::Sql::Row<Database::customer> Row;
  typedef Row::Set Set;
  typedef Dv::Sql::Row<Database::customer>::Column Column;
  extern const Column id;
  extern const Column first_name;
  extern const Column last_name;
  extern const Column birth_date;
  extern const Column sex;
  extern const Column email;
  extern const Column mod_date;
};

namespace Transaction {
  typedef Dv::Sql::Row<Database::transaction> Row;
  typedef Row::Set Set;
  typedef Dv::Sql::Row<Database::transaction>::Column Column;
  extern const Column id;
  extern const Column amount;
  extern const Column account_code;
  extern const Column executed;
  extern const Column description;
};

and
example-database.C
#include "example-database.h"

Dv::Sql::Table::Ref Database::account;
Dv::Sql::Table::Ref Database::customer;
Dv::Sql::Table::Ref Database::transaction;

Database::Database(const std::string& db_name, const std::string& user_name,
    const std::string& passwd, const std::string& host_name) throw (std::runtime_error):
    Db(db_name, user_name, passwd, host_name) {
  init();
}

Database::Database(const Dv::Util::Props& config,const std::string& prefix)
    throw (std::runtime_error): Db(config, prefix) {
  init();
}

void
Database::init() throw (std::runtime_error) {
  try {
    account = db_table("account");
    customer = db_table("customer");
    transaction = db_table("transaction");
  }
  catch(std::exception& e) {
    throw std::runtime_error(e.what());
  }
}
const Account::Column Account::code(0);
const Account::Column Account::description(1);
const Account::Column Account::customer_id(2);

const Customer::Column Customer::id(0);
const Customer::Column Customer::first_name(1);
const Customer::Column Customer::last_name(2);
const Customer::Column Customer::birth_date(3);
const Customer::Column Customer::sex(4);
const Customer::Column Customer::email(5);
const Customer::Column Customer::mod_date(6);

const Transaction::Column Transaction::id(0);
const Transaction::Column Transaction::amount(1);
const Transaction::Column Transaction::account_code(2);
const Transaction::Column Transaction::executed(3);
const Transaction::Column Transaction::description(4);


dvmysql-1.0.3 [17 November, 2010]