|
1 |
| -l #include "legacyapi/db/Session.h" |
| 1 | +#include "legacyapi/db/Session.h" |
2 | 2 |
|
3 | 3 | #include "legacyapi/db/impl/mysql/Session.h"
|
4 |
| - |
5 | 4 | #include "legacyapi/db/impl/sqlite/Session.h"
|
6 | 5 | #include "ll/api/utils/StringUtils.h"
|
7 | 6 |
|
8 |
| - namespace DB { |
| 7 | +namespace DB { |
9 | 8 |
|
10 |
| - ll::Logger dbLogger("LLSEDB"); |
| 9 | +ll::Logger dbLogger("LLSEDB"); |
11 | 10 |
|
12 |
| - void Session::setDebugOutput(bool enable) { debugOutput = enable; } |
| 11 | +void Session::setDebugOutput(bool enable) { debugOutput = enable; } |
13 | 12 |
|
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 | +} |
17 | 16 |
|
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); |
37 | 33 | }
|
| 34 | + } else { |
| 35 | + dbLogger.debug("Session::query: Query returned no result"); |
38 | 36 | }
|
39 |
| - return result; |
40 | 37 | }
|
| 38 | + return result; |
| 39 | +} |
41 | 40 |
|
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"); } |
43 | 42 |
|
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); |
50 | 47 | }
|
| 48 | + return self; |
| 49 | +} |
51 | 50 |
|
52 |
| - SharedPointer<Stmt> Session::operator<<(const std::string& query) { return prepare(query); } |
| 51 | +SharedPointer<Stmt> Session::operator<<(const std::string& query) { return prepare(query); } |
53 | 52 |
|
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"); |
71 | 69 | }
|
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} |
91 | 88 | }
|
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} |
99 | 96 | }
|
| 97 | + ); |
| 98 | +} |
100 | 99 |
|
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; |
120 | 117 | }
|
| 118 | + return SharedPointer<Session>(); |
| 119 | +} |
121 | 120 |
|
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>>(); |
123 | 122 |
|
124 | 123 | } // namespace DB
|
0 commit comments