]> jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservgldl/src/DataLayer/MySQL/MySQLMasterDAO.cpp
f9c394173036343720e6aed24555ae1596ae4481
[irc/gameservirc.git] / gameserv-2.0 / libgameservgldl / src / DataLayer / MySQL / MySQLMasterDAO.cpp
1 #include <GameServ/DataLayer/MySQL/MySQLMasterDAO.h>
2 using GameServ::DataLayer::MySQL::MySQLMasterDAO;
3
4 #include <GameServ/GameLayer/GameObjects/MasterGO.h>
5 using GameServ::GameLayer::GameObjects::MasterGO;
6
7 #include <GameServ/DataLayer/DataLayerExceptions.h>
8 using GameServ::DataLayer::Exceptions::ResourceException;
9 using GameServ::DataLayer::Exceptions::DataLayerException;
10
11 #include <GameServ/DataLayer/MySQL/MySQLId.h>
12 using GameServ::DataLayer::MySQL::MySQLId;
13
14 #include <boost/shared_ptr.hpp>
15 #include <boost/format.hpp>
16 #include <boost/algorithm/string.hpp>
17 #include <boost/lexical_cast.hpp>
18 using boost::shared_ptr;
19 using boost::format;
20 using boost::str;
21 using boost::algorithm::to_upper;
22 using boost::lexical_cast;
23
24 #include <string>
25 using std::string;
26
27 #include <vector>
28 using std::vector;
29
30 #include <mysql++.h>
31 #include <ssqls.h>
32
33 sql_create_10(master, 1, 0,
34 mysqlpp::sql_int_unsigned, id,
35 mysqlpp::sql_varchar, name,
36 mysqlpp::sql_varchar, weaponname,
37 mysqlpp::sql_int, strength,
38 mysqlpp::sql_int, defense,
39 mysqlpp::sql_int, gold,
40 mysqlpp::sql_int, experience,
41 mysqlpp::sql_int_unsigned, levelId,
42 mysqlpp::sql_int_unsigned, health,
43 mysqlpp::sql_varchar, partingwords);
44
45 shared_ptr<MasterGO> CreateMasterFromMasterSSQLS(const master &m);
46 master CreateMasterSSQLSFromMaster(shared_ptr<MasterGO> spMaster);
47
48 MySQLMasterDAO::MySQLMasterDAO()
49 {
50 Initialize("master");
51 }
52
53 MySQLMasterDAO::~MySQLMasterDAO()
54 {
55 }
56
57 void MySQLMasterDAO::Initialize(const string &tablename)
58 {
59 mTableName = tablename;
60 }
61
62 shared_ptr<MasterGO> MySQLMasterDAO::GetById(const string &Id) const
63 {
64 // TODO: Enable and catch exceptions
65 mysqlpp::Connection conn;
66
67 // TODO: Load from config
68 try
69 {
70 conn.connect("gameserv", "localhost", "gameserv", "gameserv", 3306);
71 }
72 catch (mysqlpp::ConnectionFailed &ex)
73 {
74 throw DataLayerException(str(format("Failed to connect to the database. Error: %1%") %
75 ex.what()), __FILE__, __LINE__);
76 }
77 catch (mysqlpp::Exception &ex)
78 {
79 throw DataLayerException(str(format("Unknown error connecting to the database. Error: %1%") %
80 ex.what()), __FILE__, __LINE__);
81 }
82
83 mysqlpp::Query query = conn.query();
84
85 try
86 {
87 query << "select * from " << mTableName << " where id = " << MySQLId::GetMasterNumberFromId(Id);
88 }
89 catch (GameServException &e)
90 {
91 throw DataLayerException(str(format("Error generating sql query: %1%") % e.VerboseError()),
92 __FILE__, __LINE__);
93 }
94
95 vector<master> masters;
96
97 try
98 {
99 query.storein(masters);
100 }
101 catch (mysqlpp::UseQueryError &ex)
102 {
103 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
104 query % ex.what()), __FILE__, __LINE__);
105 }
106 catch (mysqlpp::Exception &ex)
107 {
108 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
109 query % ex.what()), __FILE__, __LINE__);
110 }
111
112 if (masters.size() == 1)
113 {
114 return CreateMasterFromMasterSSQLS(masters[0]);
115 }
116 else if (masters.size() == 0)
117 {
118 return shared_ptr<MasterGO>();
119 }
120 else
121 {
122 throw DataLayerException("Problem with database key... more than one row returned on getbyid",
123 __FILE__, __LINE__);
124 }
125 }
126
127
128 void MySQLMasterDAO::Insert(shared_ptr<MasterGO> spMaster)
129 {
130 // TODO: Enable and catch exceptions
131 mysqlpp::Connection conn;
132
133 // TODO: Load from config
134 try
135 {
136 conn.connect("gameserv", "localhost", "gameserv", "gameserv", 3306);
137 }
138 catch (mysqlpp::ConnectionFailed &ex)
139 {
140 throw DataLayerException(str(format("Failed to connect to the database. Error: %1%") %
141 ex.what()), __FILE__, __LINE__);
142 }
143 catch (mysqlpp::Exception &ex)
144 {
145 throw DataLayerException(str(format("Unknown error connecting to the database. Error: %1%") %
146 ex.what()), __FILE__, __LINE__);
147 }
148
149 mysqlpp::Query query = conn.query();
150 master m = CreateMasterSSQLSFromMaster(spMaster);
151
152 m.id = 0;
153
154 try
155 {
156 query.insert(m);
157 query.execute();
158 }
159 catch (mysqlpp::UseQueryError &ex)
160 {
161 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
162 query % ex.what()), __FILE__, __LINE__);
163 }
164 catch (mysqlpp::Exception &ex)
165 {
166 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
167 query % ex.what()), __FILE__, __LINE__);
168 }
169
170 spMaster->Id(MySQLId::CreateMasterId(query.insert_id()));
171 }
172
173 void MySQLMasterDAO::Update(shared_ptr<MasterGO> spMaster)
174 {
175 // TODO: Enable and catch exceptions
176 mysqlpp::Connection conn;
177 // TODO: Load from config
178 try
179 {
180 conn.connect("gameserv", "localhost", "gameserv", "gameserv", 3306);
181 }
182 catch (mysqlpp::ConnectionFailed &ex)
183 {
184 throw DataLayerException(str(format("Failed to connect to the database. Error: %1%") %
185 ex.what()), __FILE__, __LINE__);
186 }
187 catch (mysqlpp::Exception &ex)
188 {
189 throw DataLayerException(str(format("Unknown error connecting to the database. Error: %1%") %
190 ex.what()), __FILE__, __LINE__);
191 }
192
193
194 mysqlpp::Query query = conn.query();
195 master oldrow, newrow = CreateMasterSSQLSFromMaster(spMaster);
196
197 try
198 {
199 query << "select * from " << mTableName << " where id = " << MySQLId::GetMasterNumberFromId(spMaster->Id());
200 }
201 catch (GameServException &e)
202 {
203 throw DataLayerException(str(format("Error generating sql query: %1%") % e.VerboseError()),
204 __FILE__, __LINE__);
205 }
206
207 try
208 {
209 mysqlpp::StoreQueryResult res = query.store();
210 if (res.empty())
211 {
212 throw DataLayerException(str(format("Attempt to update a non-existant master. Id: %1%") % spMaster->Id()),
213 __FILE__, __LINE__);
214 }
215 oldrow = res[0];
216 }
217 catch (mysqlpp::UseQueryError &ex)
218 {
219 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
220 query % ex.what()), __FILE__, __LINE__);
221 }
222 catch (mysqlpp::Exception &ex)
223 {
224 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
225 query % ex.what()), __FILE__, __LINE__);
226 }
227
228 try
229 {
230 query.update(oldrow, newrow);
231 query.execute();
232 }
233 catch (mysqlpp::UseQueryError &ex)
234 {
235 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
236 query % ex.what()), __FILE__, __LINE__);
237 }
238 catch (mysqlpp::Exception &ex)
239 {
240 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
241 query % ex.what()), __FILE__, __LINE__);
242 }
243 }
244
245 shared_ptr<MasterGO> CreateMasterFromMasterSSQLS(const master &m)
246 {
247 shared_ptr<MasterGO> spMaster = shared_ptr<MasterGO>(new MasterGO());
248
249 spMaster->Id(MySQLId::CreateMasterId(m.id));
250 spMaster->Name(m.name);
251 spMaster->WeaponName(m.weaponname);
252
253 spMaster->Strength(m.strength);
254 spMaster->Defense(m.defense);
255 spMaster->Gold(m.gold);
256 spMaster->Experience(m.experience);
257 spMaster->Health(m.health);
258 spMaster->PartingWords(m.partingwords);
259 spMaster->LevelId(MySQLId::CreateLevelId(m.levelId));
260
261 return spMaster;
262 }
263 master CreateMasterSSQLSFromMaster(shared_ptr<MasterGO> spMaster)
264 {
265 master m;
266
267 m.id = static_cast<mysqlpp::sql_int_unsigned>(MySQLId::GetMasterNumberFromId(spMaster->Id()));
268 m.name = spMaster->Name();
269 m.weaponname = spMaster->WeaponName();
270 m.strength = spMaster->Strength();
271 m.defense = spMaster->Defense();
272 m.gold = spMaster->Gold();
273 m.experience = spMaster->Experience();
274 m.health = spMaster->Health();
275 m.partingwords = spMaster->PartingWords();
276 m.levelId = static_cast<mysqlpp::sql_int_unsigned>(MySQLId::GetLevelNumberFromId(spMaster->LevelId()));
277
278 return m;
279 }
280
281 bool MySQLMasterDAO::IdExists(const std::string &Id) const
282 {
283 mysqlpp::Connection conn;
284 // TODO: Load from config
285 try
286 {
287 conn.connect("gameserv", "localhost", "gameserv", "gameserv", 3306);
288 }
289 catch (mysqlpp::ConnectionFailed &ex)
290 {
291 throw DataLayerException(str(format("Failed to connect to the database. Error: %1%") %
292 ex.what()), __FILE__, __LINE__);
293 }
294 catch (mysqlpp::Exception &ex)
295 {
296 throw DataLayerException(str(format("Unknown error connecting to the database. Error: %1%") %
297 ex.what()), __FILE__, __LINE__);
298 }
299 mysqlpp::Query query = conn.query();
300
301 try
302 {
303 query << "SELECT id from " << mTableName << " where Id=" << MySQLId::GetMasterNumberFromId(Id);
304 }
305 catch (GameServException &e)
306 {
307 throw DataLayerException(str(format("Error generating sql query: %1%") % e.VerboseError()),
308 __FILE__, __LINE__);
309 }
310
311 try
312 {
313 mysqlpp::StoreQueryResult res = query.store();
314 if (res.empty())
315 {
316 return false;
317 }
318 else
319 {
320 return true;
321 }
322 }
323 catch (mysqlpp::UseQueryError &ex)
324 {
325 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
326 query % ex.what()), __FILE__, __LINE__);
327 }
328 catch (mysqlpp::Exception &ex)
329 {
330 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
331 query % ex.what()), __FILE__, __LINE__);
332 }
333
334 }
335
336 string MySQLMasterDAO::GetMasterTableName(void) const
337 {
338 return mTableName;
339 }
340
341