Skip to content

Commit 0aa586c

Browse files
committed
Refactoring to address SonarCloud code smells
1 parent e86d186 commit 0aa586c

File tree

6 files changed

+23
-34
lines changed

6 files changed

+23
-34
lines changed

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ sonar.python.version=3.8
44
sonar.language=c
55
sonar.c.file.suffixes=.c,.h
66
sonar.cpp.file.suffixes=.cc,.cpp,.cxx,.c++,.hh,.hpp,.hxx,.h++,.ipp
7-
sonar.coverage.exclusions=tests/**
7+
sonar.coverage.exclusions=tests/**, source/utilities/base64.cpp
88
sonar.sources=source/

source/databaseConnection.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@
1010
DatabaseConnection::DatabaseConnection(const std::string& endpoint, int port,
1111
const std::string& dbname, const std::string& user,
1212
const std::string& password) {
13-
connection_string =
14-
"host=" + endpoint + " "
15-
"port=" + std::to_string(port) + " "
16-
"dbname=" + dbname + " "
17-
"user=" + user + " "
18-
"password=" + password + " "
19-
"connect_timeout=3";
13+
connection_string = std::format("host={} port={} dbname={} user={} password={} connect_timeout=3",
14+
endpoint, port, dbname, user, password);
2015
}
2116

2217
void DatabaseConnection::executeQuery(const std::string& query) const {
@@ -49,13 +44,11 @@ void DatabaseConnection::executeQuery(const std::string& query) const {
4944
txn.commit();
5045

5146
} catch (const pqxx::broken_connection& e) {
52-
std::cerr << "Connection error: " << e.what() << std::endl;
53-
} catch (const pqxx::sql_error& e) {
54-
std::cerr << "SQL error: " << e.what() << std::endl;
55-
std::cerr << "Query was: " << e.query() << std::endl;
56-
} catch (const pqxx::usage_error& e) {
57-
std::cerr << "Usage error: " << e.what() << std::endl;
58-
} catch (const std::exception& e) {
59-
std::cerr << "General error: " << e.what() << std::endl;
60-
}
47+
std::cerr << "Connection error: " << e.what() << std::endl;
48+
} catch (const pqxx::sql_error& e) {
49+
std::cerr << "SQL error: " << e.what() << std::endl;
50+
std::cerr << "Query was: " << e.query() << std::endl;
51+
} catch (const pqxx::usage_error& e) {
52+
std::cerr << "Usage error: " << e.what() << std::endl;
53+
}
6154
}

source/include/configManager.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ class ConfigManager {
1414
ConfigManager& operator=(const ConfigManager&) = delete;
1515

1616
public:
17-
static std::shared_ptr<ConfigManager> getInstance() {
18-
static std::shared_ptr<ConfigManager> instance = std::shared_ptr<ConfigManager>(new ConfigManager());
19-
return instance;
20-
}
17+
18+
static std::shared_ptr<ConfigManager> getInstance() {
19+
static auto instance = std::shared_ptr<ConfigManager>(new ConfigManager());
20+
return instance;
21+
}
2122

22-
std::string getConfig() const {
23-
return "config data"; // Replace with actual implementation
24-
}
23+
std::string getConfig() const {
24+
return "config data"; // Replace with actual implementation
25+
}
26+
2527
};

source/include/databaseConnection.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DatabaseConnection {
2020

2121
void executeQuery(const std::string& query) const;
2222

23-
const std::string& getConnectionString(){
23+
const std::string& getConnectionString() const {
2424
return connection_string;
2525
}
2626

source/include/serviceA.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ class ServiceA {
1313
std::shared_ptr<ConfigManager> configManager;
1414

1515
public:
16-
ServiceA(std::shared_ptr<ConfigManager> cm = ConfigManager::getInstance())
16+
explicit ServiceA(std::shared_ptr<ConfigManager> cm = ConfigManager::getInstance())
1717
: configManager(cm) {}
1818

19-
void doSomething() {
19+
void doSomething() const {
2020
std::cout << "ServiceA using config: " << configManager->getConfig() << std::endl;
2121
}
2222
};
23+

source/main.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,6 @@ int parseJson(const std::string& input) {
4949
int main(int argc, const char * argv[]) {
5050

5151
// Connect to QuestDb argv[1]
52-
53-
// DatabaseConnection Constructor
54-
// const std::string& endpoint
55-
// int port
56-
// const std::string& dbname
57-
// const std::string& user
58-
// const std::string& password
5952
DatabaseConnection db(argv[1], 8812, "qdb", "admin", "quest");
6053

6154
// Load strategy from Base64 argv[2]

0 commit comments

Comments
 (0)