]>
jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservgldl/src/DataLayer/MySQL/MySQLMasterDAO.cpp
f9c394173036343720e6aed24555ae1596ae4481
1 #include <GameServ/DataLayer/MySQL/MySQLMasterDAO.h>
2 using GameServ::DataLayer::MySQL::MySQLMasterDAO
;
4 #include <GameServ/GameLayer/GameObjects/MasterGO.h>
5 using GameServ::GameLayer::GameObjects::MasterGO
;
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
;
21 using boost::algorithm::to_upper
;
22 using boost::lexical_cast
;
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
);
45 shared_ptr
<MasterGO
> CreateMasterFromMasterSSQLS(const master
&m
);
46 master
CreateMasterSSQLSFromMaster(shared_ptr
<MasterGO
> spMaster
);
48 MySQLMasterDAO::MySQLMasterDAO()
53 MySQLMasterDAO::~MySQLMasterDAO()
57 void MySQLMasterDAO::Initialize(const string
&tablename
)
59 mTableName
= tablename
;
62 shared_ptr
<MasterGO
> MySQLMasterDAO::GetById(const string
&Id
) const
64 // TODO: Enable and catch exceptions
65 mysqlpp::Connection conn
;
67 // TODO: Load from config
70 conn
.connect("gameserv", "localhost", "gameserv", "gameserv", 3306);
72 catch (mysqlpp::ConnectionFailed
&ex
)
74 throw DataLayerException(str(format("Failed to connect to the database. Error: %1%") %
75 ex
.what()), __FILE__
, __LINE__
);
77 catch (mysqlpp::Exception
&ex
)
79 throw DataLayerException(str(format("Unknown error connecting to the database. Error: %1%") %
80 ex
.what()), __FILE__
, __LINE__
);
83 mysqlpp::Query query
= conn
.query();
87 query
<< "select * from " << mTableName
<< " where id = " << MySQLId::GetMasterNumberFromId(Id
);
89 catch (GameServException
&e
)
91 throw DataLayerException(str(format("Error generating sql query: %1%") % e
.VerboseError()),
95 vector
<master
> masters
;
99 query
.storein(masters
);
101 catch (mysqlpp::UseQueryError
&ex
)
103 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
104 query
% ex
.what()), __FILE__
, __LINE__
);
106 catch (mysqlpp::Exception
&ex
)
108 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
109 query
% ex
.what()), __FILE__
, __LINE__
);
112 if (masters
.size() == 1)
114 return CreateMasterFromMasterSSQLS(masters
[0]);
116 else if (masters
.size() == 0)
118 return shared_ptr
<MasterGO
>();
122 throw DataLayerException("Problem with database key... more than one row returned on getbyid",
128 void MySQLMasterDAO::Insert(shared_ptr
<MasterGO
> spMaster
)
130 // TODO: Enable and catch exceptions
131 mysqlpp::Connection conn
;
133 // TODO: Load from config
136 conn
.connect("gameserv", "localhost", "gameserv", "gameserv", 3306);
138 catch (mysqlpp::ConnectionFailed
&ex
)
140 throw DataLayerException(str(format("Failed to connect to the database. Error: %1%") %
141 ex
.what()), __FILE__
, __LINE__
);
143 catch (mysqlpp::Exception
&ex
)
145 throw DataLayerException(str(format("Unknown error connecting to the database. Error: %1%") %
146 ex
.what()), __FILE__
, __LINE__
);
149 mysqlpp::Query query
= conn
.query();
150 master m
= CreateMasterSSQLSFromMaster(spMaster
);
159 catch (mysqlpp::UseQueryError
&ex
)
161 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
162 query
% ex
.what()), __FILE__
, __LINE__
);
164 catch (mysqlpp::Exception
&ex
)
166 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
167 query
% ex
.what()), __FILE__
, __LINE__
);
170 spMaster
->Id(MySQLId::CreateMasterId(query
.insert_id()));
173 void MySQLMasterDAO::Update(shared_ptr
<MasterGO
> spMaster
)
175 // TODO: Enable and catch exceptions
176 mysqlpp::Connection conn
;
177 // TODO: Load from config
180 conn
.connect("gameserv", "localhost", "gameserv", "gameserv", 3306);
182 catch (mysqlpp::ConnectionFailed
&ex
)
184 throw DataLayerException(str(format("Failed to connect to the database. Error: %1%") %
185 ex
.what()), __FILE__
, __LINE__
);
187 catch (mysqlpp::Exception
&ex
)
189 throw DataLayerException(str(format("Unknown error connecting to the database. Error: %1%") %
190 ex
.what()), __FILE__
, __LINE__
);
194 mysqlpp::Query query
= conn
.query();
195 master oldrow
, newrow
= CreateMasterSSQLSFromMaster(spMaster
);
199 query
<< "select * from " << mTableName
<< " where id = " << MySQLId::GetMasterNumberFromId(spMaster
->Id());
201 catch (GameServException
&e
)
203 throw DataLayerException(str(format("Error generating sql query: %1%") % e
.VerboseError()),
209 mysqlpp::StoreQueryResult res
= query
.store();
212 throw DataLayerException(str(format("Attempt to update a non-existant master. Id: %1%") % spMaster
->Id()),
217 catch (mysqlpp::UseQueryError
&ex
)
219 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
220 query
% ex
.what()), __FILE__
, __LINE__
);
222 catch (mysqlpp::Exception
&ex
)
224 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
225 query
% ex
.what()), __FILE__
, __LINE__
);
230 query
.update(oldrow
, newrow
);
233 catch (mysqlpp::UseQueryError
&ex
)
235 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
236 query
% ex
.what()), __FILE__
, __LINE__
);
238 catch (mysqlpp::Exception
&ex
)
240 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
241 query
% ex
.what()), __FILE__
, __LINE__
);
245 shared_ptr
<MasterGO
> CreateMasterFromMasterSSQLS(const master
&m
)
247 shared_ptr
<MasterGO
> spMaster
= shared_ptr
<MasterGO
>(new MasterGO());
249 spMaster
->Id(MySQLId::CreateMasterId(m
.id
));
250 spMaster
->Name(m
.name
);
251 spMaster
->WeaponName(m
.weaponname
);
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
));
263 master
CreateMasterSSQLSFromMaster(shared_ptr
<MasterGO
> spMaster
)
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()));
281 bool MySQLMasterDAO::IdExists(const std::string
&Id
) const
283 mysqlpp::Connection conn
;
284 // TODO: Load from config
287 conn
.connect("gameserv", "localhost", "gameserv", "gameserv", 3306);
289 catch (mysqlpp::ConnectionFailed
&ex
)
291 throw DataLayerException(str(format("Failed to connect to the database. Error: %1%") %
292 ex
.what()), __FILE__
, __LINE__
);
294 catch (mysqlpp::Exception
&ex
)
296 throw DataLayerException(str(format("Unknown error connecting to the database. Error: %1%") %
297 ex
.what()), __FILE__
, __LINE__
);
299 mysqlpp::Query query
= conn
.query();
303 query
<< "SELECT id from " << mTableName
<< " where Id=" << MySQLId::GetMasterNumberFromId(Id
);
305 catch (GameServException
&e
)
307 throw DataLayerException(str(format("Error generating sql query: %1%") % e
.VerboseError()),
313 mysqlpp::StoreQueryResult res
= query
.store();
323 catch (mysqlpp::UseQueryError
&ex
)
325 throw DataLayerException(str(format("Unable to execute query: \" %1% \". Error: %2%") %
326 query
% ex
.what()), __FILE__
, __LINE__
);
328 catch (mysqlpp::Exception
&ex
)
330 throw DataLayerException(str(format("Unknown error executing query: \" %1% \". Error: %2%") %
331 query
% ex
.what()), __FILE__
, __LINE__
);
336 string
MySQLMasterDAO::GetMasterTableName(void) const