1 #include "../impl/mysql_traits.hpp" // mysql traits
2 #include "demo_cxx_gen.hpp" // generated
8 typedef sqlgg
<mysql_traits
> gen
;
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
));
39 MYSQL
* db
= connect();
42 mysql_query(db
,"DROP TABLE person");
43 mysql_query(db
,"DROP TABLE money");
46 gen::create_person(db
);
47 gen::create_money(db
);
49 // add all person records
50 gen::add_person(db
,"John","Black");
51 int64 john
= mysql_insert_id(db
);
52 gen::add_person(db
,"Ivan","Petrov");
53 int64 ivan
= mysql_insert_id(db
);
54 gen::add_person(db
,"Sancho","Alvares");
55 int64 sancho
= mysql_insert_id(db
);
57 // add money relations
58 gen::add_money(db
,john
,ivan
,200);
59 gen::add_money(db
,john
,sancho
,100);
60 gen::add_money(db
,john
,sancho
,250);
61 gen::add_money(db
,sancho
,ivan
,300);
63 // summarize by person
64 typedef vector
<gen::data_4
> collection
;
66 gen::calc_total(db
,all
);
69 cout
<< "Total transfers:" << endl
;
70 for (collection::const_iterator i
= all
.begin(), end
= all
.end(); i
!= end
; ++i
)
72 cout
<< i
->fullname
<< " = " << i
->total
<< endl
;
76 typedef vector
<gen::data_5
> people
;
78 gen::list_donors(db
,p
,"petrov",100);
80 cout
<< "Donors:" << endl
;
81 for (people::const_iterator i
= p
.begin(), end
= p
.end(); i
!= end
; ++i
)
83 cout
<< i
->surname
<< endl
;
86 // properly close database