原文地址:http://www.codesynthesis.com/products/odb/doc/manual.xhtml#2.7
表单列函数主要作用:是用C++类的成员对象来盛放表单的列函数结果。
使用方式:
1 在模板头文件中定义盛放结果的结构体,这个结构体的定义完全隶属于person类,所以放到person类的头文件中定义最好。
#pragma db view object(person)
struct person_stat
{
#pragma db column("count(" + person::id_ + ")")
std::size_t count;
#pragma db column("min(" + person::age_ + ")")
unsigned short min_age;
#pragma db column("max(" + person::age_ + ")")
unsigned short max_age;
};
2 用odb命令行重新编译person.hxx头文件,生成新的-odb.hxx和-odb.cxx文件,放到项目中使用。
3 调用源代码:
#include <memory> // std::auto_ptr
#include <iostream>
#include <odb/database.hxx>
#include <odb/transaction.hxx>
#include <odb/mysql/database.hxx>
#include "person.hxx"
#include "person-odb.hxx"
using namespace std;
using namespace odb::core;
int
main (int argc, char* argv[])
{
try
{
auto_ptr<odb::database> db (
new odb::mysql::database (
"root" // database login name
,"123456" // database password
,"collect" // database name
,"localhost"
,13306
));
{
transaction t (db->begin ());
person_stat ps (db->query_value<person_stat> ());
cout << "count : " << ps.count << endl
<< "min age: " << ps.min_age << endl
<< "max age: " << ps.max_age << endl;
t.commit ();
}
}
catch (const odb::exception& e)
{
cerr << e.what () << endl;
return 1;
}
cin.get();
}