names are already unique by scope
[sqlgg.git] / demo / demo_cxx_mysql.cpp
blob2095e5325809ef9c58e703b1683866f78009c73f
1 #include "../impl/mysql_traits.hpp" // mysql traits
2 #include "demo_cxx_gen.hpp" // generated
3 #include <iostream>
4 #include <vector>
6 using namespace std;
8 typedef sqlgg<mysql_traits> gen;
9 typedef long long int64;
11 MYSQL* connect()
13 MYSQL* conn = mysql_init (NULL);
14 if (conn == NULL)
16 fprintf (stderr, "mysql_init() failed (probably out of memory)\n");
17 return NULL;
19 if (mysql_real_connect (
20 conn, /* pointer to connection handler */
21 NULL, /* host to connect to */
22 "root", /* user name */
23 NULL, /* password */
24 "test", /* database to use */
25 0, /* port (use default) */
26 NULL, /* socket (use default) */
27 0) /* flags (none) */
28 == NULL)
30 fprintf (stderr, "mysql_real_connect() failed:\nError %u (%s)\n",
31 mysql_errno (conn), mysql_error (conn));
32 return NULL;
34 return conn;
37 int main()
39 MYSQL* db = connect();
40 if (!db) return 1;
42 mysql_query(db,"DROP TABLE person");
43 mysql_query(db,"DROP TABLE money");
45 // create tables
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;
65 collection all;
66 gen::calc_total(db,all);
68 // output
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;
75 // list donors
76 typedef vector<gen::data_5> people;
77 people p;
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
87 mysql_close(db);
88 db = NULL;
90 return 0;