1 #include "../impl/mysql_traits.hpp" // mysql traits
2 #include "demo_cxx_gen_mysql.hpp" // generated
8 typedef sqlgg
<mysql_traits
> gen_t
;
9 typedef long long int64
;
13 MYSQL
* conn
= mysql_init (NULL
);
16 fprintf (stderr
, "mysql_init() failed (probably out of memory)\n");
19 if (mysql_real_connect (
20 conn
, /* pointer to connection handler */
21 NULL
, /* host to connect to */
22 "root", /* user name */
24 "test", /* database to use */
25 0, /* port (use default) */
26 NULL
, /* socket (use default) */
30 fprintf (stderr
, "mysql_real_connect() failed:\nError %u (%s)\n",
31 mysql_errno (conn
), mysql_error (conn
));
37 struct output_transfers
39 void operator()(std::string
const& fullname
, int total
)
41 cout
<< fullname
<< " = " << total
<< endl
;
47 void operator()(std::string
const& surname
)
49 cout
<< surname
<< endl
;
55 MYSQL
* db
= connect();
60 mysql_query(db
,"DROP TABLE person");
61 mysql_query(db
,"DROP TABLE money");
67 // add all person records
68 gen
.add_person("John","Black");
69 int64 john
= mysql_insert_id(db
);
70 gen
.add_person("Ivan","Petrov");
71 int64 ivan
= mysql_insert_id(db
);
72 gen
.add_person("Sancho","Alvares");
73 int64 sancho
= mysql_insert_id(db
);
75 // add money relations
76 gen
.add_money(john
,ivan
,200);
77 gen
.add_money(john
,sancho
,100);
78 gen
.add_money(john
,sancho
,250);
79 gen
.add_money(sancho
,ivan
,300);
81 // summarize by person
82 cout
<< "Total transfers:" << endl
;
83 gen
.calc_total(output_transfers());
86 cout
<< "Donors:" << endl
;
87 gen
.list_donors("petrov",100,output_donors());
89 // properly close database