1#include <iostream>
2#include <mysqlxx/mysqlxx.h>
3
4
5int main(int, char **)
6{
7 try
8 {
9 mysqlxx::Connection connection("test", "127.0.0.1", "root", "qwerty", 3306);
10 std::cerr << "Connected." << std::endl;
11
12 {
13 mysqlxx::Query query = connection.query();
14 query << "SELECT 1 x, '2010-01-01 01:01:01' d";
15 mysqlxx::UseQueryResult result = query.use();
16 std::cerr << "use() called." << std::endl;
17
18 while (mysqlxx::Row row = result.fetch())
19 {
20 std::cerr << "Fetched row." << std::endl;
21 std::cerr << row[0] << ", " << row["x"] << std::endl;
22 std::cerr << row[1] << ", " << row["d"]
23 << ", " << row[1].getDate()
24 << ", " << row[1].getDateTime()
25 << ", " << row[1].getDate()
26 << ", " << row[1].getDateTime()
27 << std::endl
28 << row[1].getDate() << ", " << row[1].getDateTime() << std::endl
29 << row[1].getDate() << ", " << row[1].getDateTime() << std::endl
30 << row[1].getDate() << ", " << row[1].getDateTime() << std::endl
31 << row[1].getDate() << ", " << row[1].getDateTime() << std::endl
32 ;
33
34 time_t t1 = row[0];
35 time_t t2 = row[1];
36 std::cerr << t1 << ", " << LocalDateTime(t1) << std::endl;
37 std::cerr << t2 << ", " << LocalDateTime(t2) << std::endl;
38 }
39 }
40
41 {
42 mysqlxx::Query query = connection.query();
43 query << "SELECT 1234567890 abc, 12345.67890 def UNION ALL SELECT 9876543210, 98765.43210";
44 mysqlxx::StoreQueryResult result = query.store();
45
46 std::cerr << result.at(0)["abc"].getUInt() << ", " << result.at(0)["def"].getDouble() << std::endl
47 << result.at(1)["abc"].getUInt() << ", " << result.at(1)["def"].getDouble() << std::endl;
48 }
49
50 {
51 mysqlxx::UseQueryResult result = connection.query("SELECT 'abc\\\\def' x").use();
52 mysqlxx::Row row = result.fetch();
53 std::cerr << row << std::endl;
54 std::cerr << row << std::endl;
55 }
56
57 {
58 mysqlxx::Query query = connection.query("SEL");
59 query << "ECT 1";
60
61 std::cerr << query.store().at(0).at(0) << std::endl;
62 }
63
64 {
65 /// Копирование Query
66 mysqlxx::Query query = connection.query("SELECT 'Ok' x");
67 using Queries = std::vector<mysqlxx::Query>;
68 Queries queries;
69 queries.push_back(query);
70
71 for (Queries::iterator it = queries.begin(); it != queries.end(); ++it)
72 {
73 std::cerr << it->str() << std::endl;
74 std::cerr << it->store().at(0) << std::endl;
75 }
76 }
77
78 {
79 /// Копирование Query
80 mysqlxx::Query query1 = connection.query("SELECT");
81 mysqlxx::Query query2 = query1;
82 query2 << " 1";
83
84 std::cerr << query1.str() << ", " << query2.str() << std::endl;
85 }
86
87 {
88 /// Копирование Query
89 using Queries = std::list<mysqlxx::Query>;
90 Queries queries;
91 queries.push_back(connection.query("SELECT"));
92 mysqlxx::Query & qref = queries.back();
93 qref << " 1";
94
95 for (Queries::iterator it = queries.begin(); it != queries.end(); ++it)
96 {
97 std::cerr << it->str() << std::endl;
98 std::cerr << it->store().at(0) << std::endl;
99 }
100 }
101
102 {
103 /// Транзакции
104 connection.query("DROP TABLE IF EXISTS tmp").execute();
105 connection.query("CREATE TABLE tmp (x INT, PRIMARY KEY (x)) ENGINE = InnoDB").execute();
106
107 mysqlxx::Transaction trans(connection);
108 connection.query("INSERT INTO tmp VALUES (1)").execute();
109
110 std::cerr << connection.query("SELECT * FROM tmp").store().size() << std::endl;
111
112 trans.rollback();
113
114 std::cerr << connection.query("SELECT * FROM tmp").store().size() << std::endl;
115 }
116
117 {
118 /// Транзакции
119 connection.query("DROP TABLE IF EXISTS tmp").execute();
120 connection.query("CREATE TABLE tmp (x INT, PRIMARY KEY (x)) ENGINE = InnoDB").execute();
121
122 {
123 mysqlxx::Transaction trans(connection);
124 connection.query("INSERT INTO tmp VALUES (1)").execute();
125 std::cerr << connection.query("SELECT * FROM tmp").store().size() << std::endl;
126 }
127
128 std::cerr << connection.query("SELECT * FROM tmp").store().size() << std::endl;
129 }
130
131 {
132 /// Транзакции
133 mysqlxx::Connection connection2("test", "127.0.0.1", "root", "qwerty", 3306);
134 connection2.query("DROP TABLE IF EXISTS tmp").execute();
135 connection2.query("CREATE TABLE tmp (x INT, PRIMARY KEY (x)) ENGINE = InnoDB").execute();
136
137 mysqlxx::Transaction trans(connection2);
138 connection2.query("INSERT INTO tmp VALUES (1)").execute();
139 std::cerr << connection2.query("SELECT * FROM tmp").store().size() << std::endl;
140 }
141 std::cerr << connection.query("SELECT * FROM tmp").store().size() << std::endl;
142
143 {
144 /// NULL
145 mysqlxx::Null<int> x = mysqlxx::null;
146 std::cerr << (x == mysqlxx::null ? "Ok" : "Fail") << std::endl;
147 std::cerr << (x == 0 ? "Fail" : "Ok") << std::endl;
148 std::cerr << (x.isNull() ? "Ok" : "Fail") << std::endl;
149 x = 1;
150 std::cerr << (x == mysqlxx::null ? "Fail" : "Ok") << std::endl;
151 std::cerr << (x == 0 ? "Fail" : "Ok") << std::endl;
152 std::cerr << (x == 1 ? "Ok" : "Fail") << std::endl;
153 std::cerr << (x.isNull() ? "Fail" : "Ok") << std::endl;
154 }
155
156 {
157 /// Исключения при попытке достать значение не того типа
158 try
159 {
160 connection.query("SELECT -1").store().at(0).at(0).getUInt();
161 std::cerr << "Fail" << std::endl;
162 }
163 catch (const mysqlxx::Exception & e)
164 {
165 std::cerr << "Ok, " << e.message() << std::endl;
166 }
167
168 try
169 {
170 connection.query("SELECT 'xxx'").store().at(0).at(0).getInt();
171 std::cerr << "Fail" << std::endl;
172 }
173 catch (const mysqlxx::Exception & e)
174 {
175 std::cerr << "Ok, " << e.message() << std::endl;
176 }
177
178 try
179 {
180 connection.query("SELECT NULL").store().at(0).at(0).getString();
181 std::cerr << "Fail" << std::endl;
182 }
183 catch (const mysqlxx::Exception & e)
184 {
185 std::cerr << "Ok, " << e.message() << std::endl;
186 }
187
188 try
189 {
190 connection.query("SELECT 123").store().at(0).at(0).getDate();
191 std::cerr << "Fail" << std::endl;
192 }
193 catch (const mysqlxx::Exception & e)
194 {
195 std::cerr << "Ok, " << e.message() << std::endl;
196 }
197
198 try
199 {
200 connection.query("SELECT '2011-01-01'").store().at(0).at(0).getDateTime();
201 std::cerr << "Fail" << std::endl;
202 }
203 catch (const mysqlxx::Exception & e)
204 {
205 std::cerr << "Ok, " << e.message() << std::endl;
206 }
207 }
208 }
209 catch (const mysqlxx::Exception & e)
210 {
211 std::cerr << e.code() << ", " << e.message() << std::endl;
212 throw;
213 }
214
215 return 0;
216}
217