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