EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BookmarkDB.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Bloody.Rabbit
24  Updated: Allan (Zhy) 18Jan14
25  Again: Allan 2Feb20 (Bookmark system overhaul)
26 */
27 
28 
29 #include "eve-server.h"
30 
31 #include "system/BookmarkDB.h"
32 #include "system/BookmarkService.h"
33 
35 {
36  DBQueryResult res;
37  if (!sDatabase.RunQuery(res,
38  "SELECT"
39  " bookmarkID,"
40  " folderID"
41  " FROM bookmarks"
42  " WHERE folderID = %u",
43  folderID))
44  {
45  sLog.Error( "BookmarkDB::GetBMData()", "Failed to query bookmarks for folderID %u: %s.", folderID, res.error.c_str() );
46  return nullptr;
47  }
48 
49  //return DBResultToCRowset(res);
50  PyList* list = new PyList();
51  DBResultRow row;
52  while (res.GetRow(row)) {
53  PyDict* dict = new PyDict();
54  dict->SetItemString("bookmarkID", new PyInt(row.GetInt(0)));
55  if (row.IsNull(1) or (row.GetInt(1) == 0)) {
56  dict->SetItemString("folderID", PyStatic.NewNone());
57  } else {
58  dict->SetItemString("folderID", new PyInt(row.GetInt(1)));
59  }
60  list->AddItem(new PyObject("util.KeyVal", dict));
61  }
62 
63  return list;
64 }
65 
66 void BookmarkDB::GetBookmarkByFolderID(int32 folderID, std::vector< int32 >& bmIDs)
67 {
68  DBQueryResult res;
69  if (!sDatabase.RunQuery(res, "SELECT bookmarkID FROM bookmarks WHERE folderID = %u", folderID)) {
70  sLog.Error( "BookmarkDB::GetBookmarkByFolderID()", "Failed to query bookmarks for folderID %u: %s.", folderID, res.error.c_str() );
71  return;
72  }
73 
74  DBResultRow row;
75  while (res.GetRow(row))
76  bmIDs.push_back(row.GetInt(0));
77 }
78 
80  DBQueryResult res;
81  if (!sDatabase.RunQuery(res,
82  "SELECT"
83  " bookmarkID,"
84  " ownerID,"
85  " itemID,"
86  " typeID,"
87  " memo,"
88  " created,"
89  " x, y, z,"
90  " locationID,"
91  " note,"
92  " creatorID,"
93  " folderID"
94  " FROM bookmarks"
95  " WHERE ownerID = %u",
96  ownerID))
97  {
98  sLog.Error( "BookmarkDB::GetBookmarks()", "Failed to query bookmarks for owner %u: %s.", ownerID, res.error.c_str() );
99  return nullptr;
100  }
101 
102  //return DBResultToCRowset(res);
103  PyList* list = new PyList();
104  DBResultRow row;
105  while (res.GetRow(row)) {
106  PyDict* dict = new PyDict();
107  dict->SetItemString("bookmarkID", new PyInt(row.GetInt(0)));
108  dict->SetItemString("ownerID", new PyInt(row.GetInt(1)));
109  dict->SetItemString("itemID", new PyInt(row.GetInt(2)));
110  dict->SetItemString("typeID", new PyInt(row.GetInt(3)));
111  dict->SetItemString("memo", new PyString(row.GetText(4)));
112  dict->SetItemString("created", new PyLong(row.GetInt64(5)));
113  dict->SetItemString("x", new PyFloat(row.GetFloat(6)));
114  dict->SetItemString("y", new PyFloat(row.GetFloat(7)));
115  dict->SetItemString("z", new PyFloat(row.GetFloat(8)));
116  dict->SetItemString("locationID", new PyInt(row.GetInt(9)));
117  if (row.IsNull(10)) {
118  dict->SetItemString("note", PyStatic.NewNone());
119  } else {
120  dict->SetItemString("note", new PyInt(row.GetInt(10)));
121  }
122  dict->SetItemString("creatorID", new PyInt(row.GetInt(11)));
123  if (row.IsNull(12) or (row.GetInt(12) == 0)) {
124  dict->SetItemString("folderID", PyStatic.NewNone());
125  } else {
126  dict->SetItemString("folderID", new PyInt(row.GetInt(12)));
127  }
128  list->AddItem(new PyObject("util.KeyVal", dict));
129  }
130 
131  return list;
132 }
133 
135  DBQueryResult res;
136  if (!sDatabase.RunQuery(res,
137  "SELECT"
138  " ownerID,"
139  " folderID,"
140  " folderName,"
141  " creatorID"
142  " FROM bookmarkFolders"
143  " WHERE ownerID = %u",
144  ownerID))
145  {
146  sLog.Error( "BookmarkDB::GetFolders()", "Failed to query bookmarks for owner %u: %s.", ownerID, res.error.c_str() );
147  return nullptr;
148  }
149 
150  //return DBResultToCRowset(res);
151  PyList* list = new PyList();
152  DBResultRow row;
153  while (res.GetRow(row)) {
154  PyDict* dict = new PyDict();
155  dict->SetItemString("ownerID", new PyInt(row.GetInt(0)));
156  dict->SetItemString("folderID", new PyInt(row.GetInt(1)));
157  dict->SetItemString("folderName", new PyString(row.GetText(2)));
158  dict->SetItemString("creatorID", new PyInt(row.GetInt(3)));
159  list->AddItem(new PyObject("util.KeyVal", dict));
160  }
161 
162  return list;
163 }
164 
165 const char* BookmarkDB::GetBookmarkName(uint32 bookmarkID)
166 {
167  DBQueryResult res;
168  if (!sDatabase.RunQuery(res, "SELECT memo FROM bookmarks WHERE bookmarkID = %u", bookmarkID)) {
169  sLog.Error( "BookmarkDB::GetBookmarkName()", "Failed to query bookmarkID %u: %s.", bookmarkID, res.error.c_str() );
170  return nullptr;
171  }
172 
173  DBResultRow row;
174  if (!res.GetRow(row))
175  return nullptr;
176 
177  return row.GetText(0);
178 }
179 
181 {
182  DBQueryResult res;
183  if (!sDatabase.RunQuery(res, "SELECT memo, note FROM bookmarks WHERE bookmarkID = %u", bookmarkID)) {
184  sLog.Error( "BookmarkDB::GetBookmarkName()", "Failed to query bookmarkID %u: %s.", bookmarkID, res.error.c_str() );
185  return nullptr;
186  }
187 
188  PyTuple* tuple = new PyTuple(2);
189  DBResultRow row;
190  if (res.GetRow(row)) {
191  tuple->SetItem(0, new PyString(row.GetText(0)));
192  if (row.IsNull(1)) {
193  tuple->SetItem(1, PyStatic.NewNone());
194  } else {
195  tuple->SetItem(1, new PyString(row.GetText(1)));
196  }
197  } else {
198  tuple->SetItem(0, PyStatic.NewNone());
199  tuple->SetItem(1, PyStatic.NewNone());
200  }
201 
202  return tuple;
203 }
204 
205 bool BookmarkDB::GetBookmarkInformation(uint32 bookmarkID, uint32& itemID, uint16& typeID, uint32& locationID, double& x, double& y, double& z)
206 {
207  DBQueryResult res;
208  if (!sDatabase.RunQuery(res,
209  "SELECT"
210  " itemID,"
211  " typeID,"
212  " locationID,"
213  " x, y, z"
214  " FROM bookmarks "
215  " WHERE bookmarkID = %u ", bookmarkID))
216  {
217  sLog.Error( "BookmarkDB::GetBookmarkInformation()", "Error in query: %s", res.error.c_str() );
218  return false;
219  }
220 
221  DBResultRow row;
222  if (!res.GetRow(row))
223  return false;
224 
225  itemID = row.GetUInt(0);
226  typeID = row.GetUInt(1);
227  locationID = row.GetUInt(2);
228  x = row.GetDouble(3);
229  y = row.GetDouble(4);
230  z = row.GetDouble(5);
231 
232  return true;
233 }
234 
235 
237 {
238  std::string eMemo, eNote;
239  sDatabase.DoEscapeString(eMemo, data.memo.c_str());
240  sDatabase.DoEscapeString(eNote, data.note.c_str());
241 
242  DBerror err;
243  if (data.folderID > 0) {
244  if (!sDatabase.RunQueryLID(err, data.bookmarkID,
245  "INSERT INTO bookmarks"
246  " (ownerID, itemID, typeID, memo, created, x, y, z, locationID, note, creatorID, folderID)"
247  " VALUES (%u, %u, %u, '%s', %f, %f, %f, %f, %u, '%s', %u, %u) ",
248  data.ownerID, data.itemID, data.typeID, eMemo.c_str(), GetFileTimeNow(), data.point.x, data.point.y,
249  data.point.z, data.locationID, eNote.c_str(), data.creatorID, data.folderID ))
250  {
251  sLog.Error( "BookmarkDB::SaveNewBookmarkToDatabase(1)", "Error in query, Bookmark content couldn't be saved: %s", err.c_str() );
252  }
253  } else {
254  if (!sDatabase.RunQueryLID(err, data.bookmarkID,
255  "INSERT INTO bookmarks"
256  " (ownerID, itemID, typeID, memo, created, x, y, z, locationID, note, creatorID)"
257  " VALUES (%u, %u, %u, '%s', %f, %f, %f, %f, %u, '%s', %u) ",
258  data.ownerID, data.itemID, data.typeID, eMemo.c_str(), GetFileTimeNow(), data.point.x, data.point.y,
259  data.point.z, data.locationID, eNote.c_str(), data.creatorID ))
260  {
261  sLog.Error( "BookmarkDB::SaveNewBookmarkToDatabase(2)", "Error in query, Bookmark content couldn't be saved: %s", err.c_str() );
262  }
263  }
264 }
265 
266 bool BookmarkDB::DeleteBookmark(uint32 ownerID, uint32 bookmarkID)
267 {
268  DBerror err;
269  if (!sDatabase.RunQuery(err,
270  "DELETE FROM bookmarks"
271  " WHERE ownerID = %u AND bookmarkID = %u", ownerID, bookmarkID))
272  {
273  sLog.Error( "BookmarkDB::DeleteBookmarkFromDatabase()", "Error in query: %s", err.c_str() );
274  return false;
275  }
276 
277  return true;
278 }
279 
280 bool BookmarkDB::DeleteBookmarks(std::vector<int32>* bookmarkList)
281 {
282  std::stringstream st;
283  std::string listString;
284 
285  std::size_t size = bookmarkList->size();
286  for (int8 i=0; i<size; ++i) {
287  st << bookmarkList->at(i);
288  if (i < (size-1))
289  st << ", ";
290  }
291 
292  DBerror err;
293  if (!sDatabase.RunQuery(err, "DELETE FROM bookmarks WHERE bookmarkID IN (%s)", st.str().c_str())) {
294  sLog.Error( "BookmarkDB::DeleteBookmarksFromDatabase()", "Error in query: %s", err.c_str() );
295  return false;
296  }
297 
298  return true;
299 }
300 
301 void BookmarkDB::ChangeOwner(uint32 bookmarkID, uint32 ownerID/*1*/) {
302  DBerror err;
303  sDatabase.RunQuery(err, "UPDATE bookmarks SET ownerID = %u WHERE bookmarkID = %u", ownerID, bookmarkID);
304 }
305 
306 
307 bool BookmarkDB::UpdateBookmark(uint32 bookmarkID, uint32 ownerID, std::string memo, std::string note, uint32 folderID/*0*/)
308 {
309  std::string memo_fixed = "";
310  sDatabase.DoEscapeString(memo_fixed, memo.c_str());
311 
312  DBerror err;
313  if (!sDatabase.RunQuery(err, "UPDATE bookmarks SET memo = '%s', note = '%s', folderID = %u WHERE bookmarkID = %u AND ownerID = %u",
314  memo_fixed.c_str(), note.c_str(), folderID, bookmarkID, ownerID))
315  {
316  sLog.Error( "BookmarkDB::UpdateBookmarkInDatabase()", "Error in query: %s", err.c_str() );
317  return false;
318  }
319 
320  return true;
321 }
322 
323 uint32 BookmarkDB::SaveNewFolder(std::string folderName, uint32 ownerID)
324 {
325  uint32 folderID(0);
326  std::string eName = "";
327  sDatabase.DoEscapeString( eName, folderName.c_str());
328 
329  DBerror err;
330  if (!sDatabase.RunQueryLID(err, folderID, "INSERT INTO bookmarkFolders (folderName, ownerID, creatorID) VALUES ('%s', %u, %u) ",
331  eName.c_str(), ownerID, ownerID ))
332  {
333  sLog.Error( "BookmarkDB::SaveNewFolderToDatabase()", "Error in query, Folder couldn't be saved: %s", err.c_str() );
334  return 0;
335  }
336 
337  return folderID;
338 }
339 
340 bool BookmarkDB::UpdateFolder(int32 folderID, std::string folderName)
341 {
342  std::string eName = "";
343  sDatabase.DoEscapeString( eName, folderName.c_str());
344 
345  DBerror err;
346  if (!sDatabase.RunQuery(err, "UPDATE bookmarkFolders SET folderName = '%s' WHERE folderID = %u",
347  eName.c_str(), folderID))
348  {
349  sLog.Error( "BookmarkDB::UpdateFolder()", "Error in query, Folder couldn't be saved: %s", err.c_str() );
350  return false;
351  }
352 
353  return true;
354 }
355 
357 {
358  DBerror err;
359  if (!sDatabase.RunQuery(err, "DELETE FROM bookmarkFolders WHERE folderID = %u", folderID))
360  sLog.Error( "BookmarkDB::DeleteFolder(1)", "Error in query: %s", err.c_str() );
361 
362  // these bms may have copies....cannot delete yet
363  if (!sDatabase.RunQuery(err, "UPDATE bookmarks SET ownerID = 1 WHERE folderID = %u", folderID))
364  sLog.Error( "BookmarkDB::DeleteFolder(2)", "Error in query: %s", err.c_str() );
365 
366  return true;
367 }
368 
369 void BookmarkDB::MoveBookmarkToFolder(int32 folderID, std::vector<int32>* bookmarkList)
370 {
371  std::stringstream st;
372  std::size_t size = bookmarkList->size();
373  for (int8 i=0; i<size; ++i) {
374  st << bookmarkList->at(i);
375  if (i < (size-1))
376  st << ", ";
377  }
378 
379  DBerror err;
380  sDatabase.RunQuery(err, "UPDATE bookmarks SET folderID = %i WHERE bookmarkID IN (%s)", folderID, st.str().c_str());
381 }
382 
384 
385  DBQueryResult res;
386  if (!sDatabase.RunQuery(res,
387  "SELECT ownerID, itemID, typeID, memo, created, x, y, z, "
388  "locationID, note, creatorID, folderID "
389  "FROM bookmarks WHERE bookmarkID = %u", data.bookmarkID))
390  {
391  sLog.Error( "BookmarkDB::GetVoucherData()", "Failed to query data for Bookmark %u: %s.", data.bookmarkID, res.error.c_str() );
392  return;
393  }
394 
395 
396  DBResultRow row;
397  if (res.GetRow(row)) {
398  data.ownerID = row.GetUInt(0);
399  data.itemID = row.GetUInt(1);
400  data.typeID = row.GetUInt(2);
401  data.memo = row.GetText(3);
402  data.created = row.GetInt64(4);
403  data.point.x = row.GetFloat(5);
404  data.point.y = row.GetFloat(6);
405  data.point.z = row.GetFloat(7);
406  data.locationID = row.GetUInt(8);
407  if (row.IsNull(9)) {
408  data.note = "";
409  } else {
410  data.note = row.GetText(9);
411  }
412  data.creatorID = row.GetUInt(10);
413  if (row.IsNull(11)){
414  data.folderID = 0;
415  } else {
416  data.folderID = row.GetUInt(11);
417  }
418  }
419 }
Base Python wire object.
Definition: PyRep.h:66
#define sDatabase
Definition: dbcore.h:199
int64 created
Definition: BookmarkDB.h:51
void GetVoucherData(BmData &data)
Definition: BookmarkDB.cpp:383
const char * GetText(uint32 index) const
Definition: dbcore.h:104
itemID[count] Create count or of the specified() x() y(z)-Jump to the specified position in space.Stopped." ) COMMAND( translocate
std::string memo
Definition: BookmarkDB.h:53
float GetFloat(uint32 index) const
Definition: dbcore.cpp:682
Python string.
Definition: PyRep.h:430
int32 GetInt(uint32 index) const
Definition: dbcore.cpp:635
Python's dictionary.
Definition: PyRep.h:719
void GetBookmarkByFolderID(int32 folderID, std::vector< int32 > &bmIDs)
Definition: BookmarkDB.cpp:66
PyRep * GetBookmarksInFolder(uint32 folderID)
Definition: BookmarkDB.cpp:34
uint32 SaveNewFolder(std::string folderName, uint32 ownerID)
Definition: BookmarkDB.cpp:323
uint32 GetUInt(uint32 index) const
Definition: dbcore.cpp:658
double GetDouble(uint32 index) const
Definition: dbcore.cpp:693
Python floating point number.
Definition: PyRep.h:292
bool UpdateBookmark(uint32 bookmarkID, uint32 ownerID, std::string memo, std::string note, uint32 folderID=0)
Definition: BookmarkDB.cpp:307
uint32 folderID
Definition: BookmarkDB.h:49
Python tuple.
Definition: PyRep.h:567
GaFloat x
Definition: GaTypes.h:207
signed __int8 int8
Definition: eve-compat.h:45
bool GetBookmarkInformation(uint32 bookmarkID, uint32 &itemID, uint16 &typeID, uint32 &locationID, double &x, double &y, double &z)
Definition: BookmarkDB.cpp:205
void AddItem(PyRep *i)
Definition: PyRep.h:701
signed __int32 int32
Definition: eve-compat.h:49
bool DeleteBookmarks(std::vector< int32 > *bookmarkList)
Definition: BookmarkDB.cpp:280
bool GetRow(DBResultRow &into)
Definition: dbcore.cpp:552
uint32 creatorID
Definition: BookmarkDB.h:50
uint32 locationID
Definition: BookmarkDB.h:48
uint32 ownerID
Definition: BookmarkDB.h:46
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
GPoint point
Definition: BookmarkDB.h:52
void ChangeOwner(uint32 bookmarkID, uint32 ownerID=1)
Definition: BookmarkDB.cpp:301
bool DeleteBookmark(uint32 ownerID, uint32 bookmarkID)
Definition: BookmarkDB.cpp:266
const char * c_str() const
Definition: dbcore.h:48
Python object.
Definition: PyRep.h:826
bool UpdateFolder(int32 folderID, std::string folderName)
Definition: BookmarkDB.cpp:340
void SaveNewBookmark(BmData &data)
Definition: BookmarkDB.cpp:236
static const char * GetBookmarkName(uint32 bookmarkID)
Definition: BookmarkDB.cpp:165
void SetItem(size_t index, PyRep *object)
Stores Python object.
Definition: PyRep.h:610
Python integer.
Definition: PyRep.h:231
#define PyStatic
Definition: PyRep.h:1209
bool IsNull(uint32 index) const
Definition: dbcore.h:102
bool DeleteFolder(int32 folderID)
Definition: BookmarkDB.cpp:356
unsigned __int32 uint32
Definition: eve-compat.h:50
PyRep * GetBookmarks(uint32 ownerID)
Definition: BookmarkDB.cpp:79
GaFloat y
Definition: GaTypes.h:207
double GetFileTimeNow()
Definition: utils_time.cpp:84
PyRep * GetFolders(uint32 ownerID)
Definition: BookmarkDB.cpp:134
DBerror error
Definition: dbcore.h:69
uint32 bookmarkID
Definition: BookmarkDB.h:45
typeID Spawn an NPC with the specified type text Search for items matching the specified query() type()() itemID() copy() materialLevel()() itemID(attributeID)-Retrieves attribute value." ) COMMAND( setattr
uint16 typeID
Definition: BookmarkDB.h:44
void MoveBookmarkToFolder(int32 folderID, std::vector< int32 > *bookmarkList)
Definition: BookmarkDB.cpp:369
int64 GetInt64(uint32 index) const
Definition: dbcore.cpp:670
unsigned __int16 uint16
Definition: eve-compat.h:48
uint32 itemID
Definition: BookmarkDB.h:47
Definition: dbcore.h:39
Python list.
Definition: PyRep.h:639
GaFloat z
Definition: GaTypes.h:207
void SetItemString(const char *key, PyRep *value)
SetItemString adds or sets a database entry.
Definition: PyRep.h:812
static PyTuple * GetBookmarkDescription(uint32 bookmarkID)
Definition: BookmarkDB.cpp:180
Python long integer.
Definition: PyRep.h:261
std::string note
Definition: BookmarkDB.h:54