Skip to content

Commit 4ab9233

Browse files
config class, basic doc
1 parent cc36d23 commit 4ab9233

File tree

4 files changed

+124
-3
lines changed

4 files changed

+124
-3
lines changed

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,40 @@ No support is offered for this software at present. Your mileage may vary. I hav
1313

1414
libmysqlclient-dev
1515
D++
16+
fmtlib
1617
A C++ compiler capable of building D++ bots with coroutine support, if you want to use the asynchronous interface
1718

1819
## Documentation
1920

2021
All functions in the `db` namespace have Doxygen comment blocks.
22+
23+
## Using the wrapper
24+
25+
This is an example of using the asynchronous interface:
26+
27+
```cpp
28+
#include <dpp/dpp.h>
29+
#include "database.h"
30+
#include "config.h"
31+
32+
int main(int argc, char const *argv[]) {
33+
std::setlocale(LC_ALL, "en_GB.UTF-8");
34+
35+
config::init("../config.json");
36+
37+
dpp::cluster bot(config::get("token"));
38+
39+
bot.on_ready([&bot](const dpp::ready_t& event) -> dpp::task<void> {
40+
auto rs = co_await db::co_query("SELECT * FROM bigtable WHERE bar = ?", { "baz" });
41+
std::cout << "Number of rows returned: " << rs.size() << "\n";
42+
if (!rs.empty()) {
43+
std::cout << "First row 'bar' value: " << rs[0].at("bar") << "\n";
44+
}
45+
co_return;
46+
});
47+
48+
db::init(bot);
49+
50+
bot.start(dpp::st_wait);
51+
}
52+
```

config.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/************************************************************************************
2+
*
3+
* dpp-mysql - An asynchronous MySQL database wrapper for D++ bots
4+
*
5+
* Copyright 2020-2024 Craig Edwards <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
************************************************************************************/
20+
#include <dpp/dpp.h>
21+
#include <dpp/json.h>
22+
#include <fstream>
23+
24+
namespace config {
25+
26+
static json configdocument;
27+
28+
void init(const std::string& config_file) {
29+
std::ifstream configfile(config_file);
30+
configfile >> configdocument;
31+
}
32+
33+
bool exists(const std::string& key) {
34+
return configdocument.contains(key);
35+
}
36+
37+
json& get(const std::string& key) {
38+
if (key.empty()) {
39+
return configdocument;
40+
}
41+
return configdocument.at(key);
42+
}
43+
};

config.h

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/************************************************************************************
2+
*
3+
* dpp-mysql - An asynchronous MySQL database wrapper for D++ bots
4+
*
5+
* Copyright 2020-2024 Craig Edwards <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
************************************************************************************/
20+
#pragma once
21+
#include <dpp/json_fwd.h>
22+
23+
namespace config {
24+
25+
/**
26+
* @brief Initialise config file
27+
*
28+
* @param config_file Config file to read
29+
*/
30+
void init(const std::string& config_file);
31+
32+
/**
33+
* @brief Get all config values from a specific key
34+
*
35+
* @param key The key, if empty/omitted the root node is returned
36+
* @return json& configuration or empty container if not found
37+
*/
38+
json& get(const std::string& key = "");
39+
40+
/**
41+
* Returns true if the specified key exists
42+
* @param key
43+
* @return true if key exists
44+
*/
45+
bool exists(const std::string& key);
46+
47+
};

database.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
*
1919
************************************************************************************/
2020

21-
#include <ssod/ssod.h>
22-
#include <ssod/database.h>
23-
#include <ssod/config.h>
21+
#include "database.h"
22+
#include "config.h"
2423
#include <mysql/mysql.h>
2524
#include <fmt/format.h>
2625
#include <unordered_map>

0 commit comments

Comments
 (0)