Skip to content

Commit 7f06050

Browse files
committed
fix: fix #37
1 parent 1355179 commit 7f06050

File tree

3 files changed

+163
-168
lines changed

3 files changed

+163
-168
lines changed

src/legacy/api/ContainerAPI.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,7 @@ Local<Value> ContainerClass::setItem(const Arguments& args) {
160160
return Local<Value>();
161161
}
162162

163-
ItemStack* itemOld = (ItemStack*)&container->getItem(args[0].toInt());
164-
if (!itemOld) return Boolean::newBoolean(false);
165-
166-
auto tag = itemOld->save();
167-
item->fromTag(*tag);
163+
container->setItem(args[0].asNumber().toInt32(), *item);
168164
return Boolean::newBoolean(true);
169165
}
170166
CATCH("Fail in getItem!");

src/legacy/legacyapi/db/RowSet.h

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,78 @@
1-
c #pragma once
1+
#pragma once
22
#include "legacyapi/db/Row.h"
33

44
#include <vector>
55

66
#undef max
77

8-
namespace DB {
8+
namespace DB {
99

10-
class RowSet : public std::vector<Row> {
10+
class RowSet : public std::vector<Row> {
1111

12-
using Base = std::vector<Row>;
12+
using Base = std::vector<Row>;
1313

14-
public:
15-
std::shared_ptr<RowHeader> header; //!< The header of the rows
14+
public:
15+
std::shared_ptr<RowHeader> header; //!< The header of the rows
1616

17-
/**
18-
* @brief Construct a new Row Set object
19-
*
20-
* @param header The header(column names) of rows(shared_ptr)
21-
*/
22-
RowSet(const std::shared_ptr<RowHeader>& header = nullptr);
23-
/**
24-
* @brief Construct a new Row Set object
25-
*
26-
* @param header The header(column names) of rows
27-
*/
28-
RowSet(const RowHeader& header);
29-
/// Move constructor
30-
RowSet(RowSet&& set) noexcept;
31-
/// Copy constructor
32-
RowSet(const RowSet& set);
33-
/// Move assignment operator
34-
RowSet& operator=(RowSet&& set) noexcept;
35-
/// Copy assignment operator
36-
RowSet& operator=(const RowSet& set);
17+
/**
18+
* @brief Construct a new Row Set object
19+
*
20+
* @param header The header(column names) of rows(shared_ptr)
21+
*/
22+
RowSet(const std::shared_ptr<RowHeader>& header = nullptr);
23+
/**
24+
* @brief Construct a new Row Set object
25+
*
26+
* @param header The header(column names) of rows
27+
*/
28+
RowSet(const RowHeader& header);
29+
/// Move constructor
30+
RowSet(RowSet&& set) noexcept;
31+
/// Copy constructor
32+
RowSet(const RowSet& set);
33+
/// Move assignment operator
34+
RowSet& operator=(RowSet&& set) noexcept;
35+
/// Copy assignment operator
36+
RowSet& operator=(const RowSet& set);
3737

38-
/**
39-
* @brief Add a row to the set.
40-
*
41-
* @param row The row to add
42-
*/
43-
void add(const Row& row);
44-
/**
45-
* @brief Get if the set is valid
46-
*
47-
* @return bool True if valid
48-
*/
49-
bool valid();
50-
/**
51-
* @brief Add a row to the set.
52-
*
53-
* @param row The row to add
54-
* @see add(const Row&)
55-
*/
56-
void push_back(const Row& row);
57-
/**
58-
* @brief Convert to the table string.
59-
*
60-
* @param nullPattern When the value is null, what to replace with(default
61-
* '\<NULL\>')
62-
* @return std::string The result string
63-
* @par sample
64-
* @code
65-
* | a | b |
66-
* |=====|========|
67-
* | awa | 114514 |
68-
* | qwq | 233 |
69-
* | ll | <NULL> |
70-
* |=====|========|
71-
* @endcode
72-
*/
73-
std::string toTableString(const std::string& nullPattern = "<NULL>") const;
74-
};
38+
/**
39+
* @brief Add a row to the set.
40+
*
41+
* @param row The row to add
42+
*/
43+
void add(const Row& row);
44+
/**
45+
* @brief Get if the set is valid
46+
*
47+
* @return bool True if valid
48+
*/
49+
bool valid();
50+
/**
51+
* @brief Add a row to the set.
52+
*
53+
* @param row The row to add
54+
* @see add(const Row&)
55+
*/
56+
void push_back(const Row& row);
57+
/**
58+
* @brief Convert to the table string.
59+
*
60+
* @param nullPattern When the value is null, what to replace with(default
61+
* '\<NULL\>')
62+
* @return std::string The result string
63+
* @par sample
64+
* @code
65+
* | a | b |
66+
* |=====|========|
67+
* | awa | 114514 |
68+
* | qwq | 233 |
69+
* | ll | <NULL> |
70+
* |=====|========|
71+
* @endcode
72+
*/
73+
std::string toTableString(const std::string& nullPattern = "<NULL>") const;
74+
};
7575

76-
using ResultSet = RowSet;
76+
using ResultSet = RowSet;
7777

7878
} // namespace DB

src/legacy/legacyapi/db/Session.cpp

Lines changed: 98 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,123 @@
1-
l #include "legacyapi/db/Session.h"
1+
#include "legacyapi/db/Session.h"
22

33
#include "legacyapi/db/impl/mysql/Session.h"
4-
54
#include "legacyapi/db/impl/sqlite/Session.h"
65
#include "ll/api/utils/StringUtils.h"
76

8-
namespace DB {
7+
namespace DB {
98

10-
ll::Logger dbLogger("LLSEDB");
9+
ll::Logger dbLogger("LLSEDB");
1110

12-
void Session::setDebugOutput(bool enable) { debugOutput = enable; }
11+
void Session::setDebugOutput(bool enable) { debugOutput = enable; }
1312

14-
bool Session::relogin(const std::string& user, const std::string& password, const std::string& db) {
15-
throw std::runtime_error("Session::relogin: Not implemented");
16-
}
13+
bool Session::relogin(const std::string& user, const std::string& password, const std::string& db) {
14+
throw std::runtime_error("Session::relogin: Not implemented");
15+
}
1716

18-
ResultSet Session::query(const std::string& query) {
19-
bool headerSet = false;
20-
ResultSet result;
21-
this->query(query, [&](const Row& row) {
22-
if (!headerSet) {
23-
result.header = row.header;
24-
headerSet = true;
25-
}
26-
result.push_back(row);
27-
return true;
28-
});
29-
IF_ENDBG {
30-
if (result.valid()) {
31-
dbLogger.debug("Session::query: Results >");
32-
for (auto& str : ll::string_utils::splitByPattern(result.toTableString(), "\n")) {
33-
dbLogger.debug(str);
34-
}
35-
} else {
36-
dbLogger.debug("Session::query: Query returned no result");
17+
ResultSet Session::query(const std::string& query) {
18+
bool headerSet = false;
19+
ResultSet result;
20+
this->query(query, [&](const Row& row) {
21+
if (!headerSet) {
22+
result.header = row.header;
23+
headerSet = true;
24+
}
25+
result.push_back(row);
26+
return true;
27+
});
28+
IF_ENDBG {
29+
if (result.valid()) {
30+
dbLogger.debug("Session::query: Results >");
31+
for (auto& str : ll::string_utils::splitByPattern(result.toTableString(), "\n")) {
32+
dbLogger.debug(str);
3733
}
34+
} else {
35+
dbLogger.debug("Session::query: Query returned no result");
3836
}
39-
return result;
4037
}
38+
return result;
39+
}
4140

42-
std::string Session::getLastError() const { throw std::runtime_error("Session::getLastError: Not implemented"); }
41+
std::string Session::getLastError() const { throw std::runtime_error("Session::getLastError: Not implemented"); }
4342

44-
std::weak_ptr<Session> Session::getOrSetSelf() {
45-
if (self.expired()) {
46-
IF_ENDBG dbLogger.debug("Session::getOrSetSelf: `self` expired, trying fetching");
47-
return self = getSession(this);
48-
}
49-
return self;
43+
std::weak_ptr<Session> Session::getOrSetSelf() {
44+
if (self.expired()) {
45+
IF_ENDBG dbLogger.debug("Session::getOrSetSelf: `self` expired, trying fetching");
46+
return self = getSession(this);
5047
}
48+
return self;
49+
}
5150

52-
SharedPointer<Stmt> Session::operator<<(const std::string& query) { return prepare(query); }
51+
SharedPointer<Stmt> Session::operator<<(const std::string& query) { return prepare(query); }
5352

54-
SharedPointer<Session> Session::create(DBType type) { return _Create(type); }
55-
SharedPointer<Session> Session::create(const ConnParams& params) {
56-
static std::unordered_map<std::string, DBType> names{
57-
{"sqlite", DBType::SQLite},
58-
{"sqlite3", DBType::SQLite},
59-
{"file", DBType::SQLite},
60-
{"mysql", DBType::MySQL },
61-
{"mysqlc", DBType::MySQL }
62-
};
63-
ConnParams copy = params;
64-
auto scheme = copy.getScheme();
65-
std::transform(scheme.begin(), scheme.end(), scheme.begin(), ::tolower);
66-
if (names.count(scheme)) {
67-
return _Create(names[scheme], params);
68-
} else {
69-
throw std::runtime_error("Session::create: Unknown/Unsupported database type");
70-
}
53+
SharedPointer<Session> Session::create(DBType type) { return _Create(type); }
54+
SharedPointer<Session> Session::create(const ConnParams& params) {
55+
static std::unordered_map<std::string, DBType> names{
56+
{"sqlite", DBType::SQLite},
57+
{"sqlite3", DBType::SQLite},
58+
{"file", DBType::SQLite},
59+
{"mysql", DBType::MySQL },
60+
{"mysqlc", DBType::MySQL }
61+
};
62+
ConnParams copy = params;
63+
auto scheme = copy.getScheme();
64+
std::transform(scheme.begin(), scheme.end(), scheme.begin(), ::tolower);
65+
if (names.count(scheme)) {
66+
return _Create(names[scheme], params);
67+
} else {
68+
throw std::runtime_error("Session::create: Unknown/Unsupported database type");
7169
}
72-
SharedPointer<Session> Session::create(DBType type, const ConnParams& params) { return _Create(type, params); }
73-
SharedPointer<Session> Session::create(
74-
DBType type,
75-
const std::string& host,
76-
uint16_t port,
77-
const std::string& user,
78-
const std::string& password,
79-
const std::string& database
80-
) {
81-
return _Create(
82-
type,
83-
{
84-
{"host", host },
85-
{"port", port },
86-
{"user", user },
87-
{"password", password},
88-
{"database", database}
89-
}
90-
);
70+
}
71+
SharedPointer<Session> Session::create(DBType type, const ConnParams& params) { return _Create(type, params); }
72+
SharedPointer<Session> Session::create(
73+
DBType type,
74+
const std::string& host,
75+
uint16_t port,
76+
const std::string& user,
77+
const std::string& password,
78+
const std::string& database
79+
) {
80+
return _Create(
81+
type,
82+
{
83+
{"host", host },
84+
{"port", port },
85+
{"user", user },
86+
{"password", password},
87+
{"database", database}
9188
}
92-
SharedPointer<Session> Session::create(DBType type, const std::string& path) {
93-
return _Create(
94-
type,
95-
{
96-
{"path", path}
97-
}
98-
);
89+
);
90+
}
91+
SharedPointer<Session> Session::create(DBType type, const std::string& path) {
92+
return _Create(
93+
type,
94+
{
95+
{"path", path}
9996
}
97+
);
98+
}
10099

101-
SharedPointer<Session> Session::_Create(DBType type, const ConnParams& params) {
102-
Session* session = nullptr;
103-
switch (type) {
104-
case DBType::SQLite:
105-
session = params.empty() ? new SQLiteSession() : new SQLiteSession(params);
106-
break;
107-
// case DBType::MySQL:
108-
// session = params.empty() ? new MySQLSession() : new MySQLSession(params);
109-
// break;
110-
default:
111-
throw std::runtime_error("Session::_Create: Unknown/Unsupported database type");
112-
}
113-
if (session) {
114-
auto result = SharedPointer<Session>(session);
115-
result->self = result;
116-
sessionPool.push_back(result);
117-
return result;
118-
}
119-
return SharedPointer<Session>();
100+
SharedPointer<Session> Session::_Create(DBType type, const ConnParams& params) {
101+
Session* session = nullptr;
102+
switch (type) {
103+
case DBType::SQLite:
104+
session = params.empty() ? new SQLiteSession() : new SQLiteSession(params);
105+
break;
106+
// case DBType::MySQL:
107+
// session = params.empty() ? new MySQLSession() : new MySQLSession(params);
108+
// break;
109+
default:
110+
throw std::runtime_error("Session::_Create: Unknown/Unsupported database type");
111+
}
112+
if (session) {
113+
auto result = SharedPointer<Session>(session);
114+
result->self = result;
115+
sessionPool.push_back(result);
116+
return result;
120117
}
118+
return SharedPointer<Session>();
119+
}
121120

122-
std::vector<std::weak_ptr<Session>> Session::sessionPool = std::vector<std::weak_ptr<Session>>();
121+
std::vector<std::weak_ptr<Session>> Session::sessionPool = std::vector<std::weak_ptr<Session>>();
123122

124123
} // namespace DB

0 commit comments

Comments
 (0)