]>
jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservgldl/src/DataLayer/MySQL/MySQLLevelDAO.cpp
1 #include <GameServ/DataLayer/MySQL/MySQLLevelDAO.h>
2 using GameServ::DataLayer::MySQL::MySQLLevelDAO
;
4 #include <GameServ/GameLayer/GameObjects/LevelGO.h>
5 using GameServ::GameLayer::GameObjects::LevelGO
;
7 #include <GameServ/DataLayer/DataLayerExceptions.h>
8 using GameServ::DataLayer::Exceptions::ResourceException
;
9 using GameServ::DataLayer::Exceptions::DataLayerException
;
11 #include <GameServ/DataLayer/MySQL/MySQLId.h>
12 using GameServ::DataLayer::MySQL::MySQLId
;
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
;
22 using boost::algorithm::to_upper
;
23 using boost::lexical_cast
;
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
);
47 shared_ptr
<LevelGO
> CreateLevelFromLevelSSQLS(const level
&l
);
48 level
CreateLevelSSQLSFromLevel(shared_ptr
<LevelGO
> spLevel
);
50 MySQLLevelDAO::MySQLLevelDAO()
55 MySQLLevelDAO::~MySQLLevelDAO()
59 void MySQLLevelDAO::Initialize(const string
&tablename
)
61 mTableName
= tablename
;
64 shared_ptr
<LevelGO
> MySQLLevelDAO::GetById(const string
&Id
) const
66 // TODO: Enable and catch exceptions
67 mysqlpp::Connection conn
;
69 // TODO: Load from config
72 conn
.connect("gameserv", "localhost", "gameserv", "gameserv", 3306);
74 catch (mysqlpp::ConnectionFailed
&ex
)
76 throw DataLayerException(str(format("Failed to connect to the database. Error: %1%") %
77 ex
.what()), __FILE__
, __LINE__
);
79 catch (mysqlpp::Exception
&ex
)
81 throw DataLayerException(str(format("Unknown error connecting to the database. Error: %1%") %
82 ex
.what()), __FILE__
, __LINE__
);
85 mysqlpp::Query query
= conn
.query();
89 query
<< "select * from " << mTableName
<< " where id = " << MySQLId::GetLevelNumberFromId(Id
);
91 catch (GameServException
&e
)
93 throw DataLayerException(str(format("Error generating sql query: %1%") % e
.VerboseError()),
101 query
.storein(levels
);
103 catch (mysqlpp::UseQueryError
&ex
)
105 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
106 query
% ex
.what()), __FILE__
, __LINE__
);
108 catch (mysqlpp::Exception
&ex
)
110 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
111 query
% ex
.what()), __FILE__
, __LINE__
);
114 if (levels
.size() == 1)
116 return CreateLevelFromLevelSSQLS(levels
[0]);
118 else if (levels
.size() == 0)
120 return shared_ptr
<LevelGO
>();
124 throw DataLayerException("Problem with database key... more than one row returned on getbyid",
130 void MySQLLevelDAO::Insert(shared_ptr
<LevelGO
> spLevel
)
132 // TODO: Enable and catch exceptions
133 mysqlpp::Connection conn
;
135 // TODO: Load from config
138 conn
.connect("gameserv", "localhost", "gameserv", "gameserv", 3306);
140 catch (mysqlpp::ConnectionFailed
&ex
)
142 throw DataLayerException(str(format("Failed to connect to the database. Error: %1%") %
143 ex
.what()), __FILE__
, __LINE__
);
145 catch (mysqlpp::Exception
&ex
)
147 throw DataLayerException(str(format("Unknown error connecting to the database. Error: %1%") %
148 ex
.what()), __FILE__
, __LINE__
);
151 mysqlpp::Query query
= conn
.query();
152 level l
= CreateLevelSSQLSFromLevel(spLevel
);
161 catch (mysqlpp::UseQueryError
&ex
)
163 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
164 query
% ex
.what()), __FILE__
, __LINE__
);
166 catch (mysqlpp::Exception
&ex
)
168 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
169 query
% ex
.what()), __FILE__
, __LINE__
);
172 spLevel
->Id(MySQLId::CreateLevelId(query
.insert_id()));
175 void MySQLLevelDAO::Update(shared_ptr
<LevelGO
> spLevel
)
177 // TODO: Enable and catch exceptions
178 mysqlpp::Connection conn
;
179 // TODO: Load from config
182 conn
.connect("gameserv", "localhost", "gameserv", "gameserv", 3306);
184 catch (mysqlpp::ConnectionFailed
&ex
)
186 throw DataLayerException(str(format("Failed to connect to the database. Error: %1%") %
187 ex
.what()), __FILE__
, __LINE__
);
189 catch (mysqlpp::Exception
&ex
)
191 throw DataLayerException(str(format("Unknown error connecting to the database. Error: %1%") %
192 ex
.what()), __FILE__
, __LINE__
);
196 mysqlpp::Query query
= conn
.query();
197 level oldrow
, newrow
= CreateLevelSSQLSFromLevel(spLevel
);
201 query
<< "select * from " << mTableName
<< " where id = " << MySQLId::GetLevelNumberFromId(spLevel
->Id());
203 catch (GameServException
&e
)
205 throw DataLayerException(str(format("Error generating sql query: %1%") % e
.VerboseError()),
211 mysqlpp::StoreQueryResult res
= query
.store();
214 throw DataLayerException(str(format("Attempt to update a non-existant level. Id: %1%") % spLevel
->Id()),
219 catch (mysqlpp::UseQueryError
&ex
)
221 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
222 query
% ex
.what()), __FILE__
, __LINE__
);
224 catch (mysqlpp::Exception
&ex
)
226 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
227 query
% ex
.what()), __FILE__
, __LINE__
);
232 query
.update(oldrow
, newrow
);
235 catch (mysqlpp::UseQueryError
&ex
)
237 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
238 query
% ex
.what()), __FILE__
, __LINE__
);
240 catch (mysqlpp::Exception
&ex
)
242 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
243 query
% ex
.what()), __FILE__
, __LINE__
);
247 shared_ptr
<LevelGO
> CreateLevelFromLevelSSQLS(const level
&l
)
249 shared_ptr
<LevelGO
> spLevel
= shared_ptr
<LevelGO
>(new LevelGO());
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
));
260 level
CreateLevelSSQLSFromLevel(shared_ptr
<LevelGO
> spLevel
)
262 assert (spLevel
!= 0);
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();
270 l
.defensehigh
= spLevel
->DefenseRange().High();
271 l
.defenselow
= spLevel
->DefenseRange().Low();
273 l
.goldhigh
= spLevel
->GoldRange().High();
274 l
.goldlow
= spLevel
->GoldRange().Low();
276 l
.experiencehigh
= spLevel
->ExperienceRange().High();
277 l
.experiencelow
= spLevel
->ExperienceRange().Low();
279 l
.healthhigh
= spLevel
->HealthRange().High();
280 l
.healthlow
= spLevel
->HealthRange().Low();
286 bool MySQLLevelDAO::IdExists(const std::string
&Id
) const
288 mysqlpp::Connection conn
;
289 // TODO: Load from config
292 conn
.connect("gameserv", "localhost", "gameserv", "gameserv", 3306);
294 catch (mysqlpp::ConnectionFailed
&ex
)
296 throw DataLayerException(str(format("Failed to connect to the database. Error: %1%") %
297 ex
.what()), __FILE__
, __LINE__
);
299 catch (mysqlpp::Exception
&ex
)
301 throw DataLayerException(str(format("Unknown error connecting to the database. Error: %1%") %
302 ex
.what()), __FILE__
, __LINE__
);
304 mysqlpp::Query query
= conn
.query();
308 query
<< "SELECT id from " << mTableName
<< " where Id=" << MySQLId::GetLevelNumberFromId(Id
);
310 catch (GameServException
&e
)
312 throw DataLayerException(str(format("Error generating sql query: %1%") % e
.VerboseError()),
318 mysqlpp::StoreQueryResult res
= query
.store();
328 catch (mysqlpp::UseQueryError
&ex
)
330 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
331 query
% ex
.what()), __FILE__
, __LINE__
);
333 catch (mysqlpp::Exception
&ex
)
335 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
336 query
% ex
.what()), __FILE__
, __LINE__
);
341 string
MySQLLevelDAO::GetLevelTableName(void) const