From 7ba0c41b8454c417536fd67da21a0b27efa46e60 Mon Sep 17 00:00:00 2001 From: Christian Parpart Date: Sat, 26 Oct 2024 01:12:43 +0200 Subject: [PATCH] Applying some clang-tidyness Signed-off-by: Christian Parpart --- .clangd | 12 ++ .github/workflows/build.yml | 3 +- src/.clang-tidy | 27 ++-- src/Lightweight/CMakeLists.txt | 2 +- .../DataBinder/BasicStringBinder.hpp | 6 +- src/Lightweight/DataBinder/Core.hpp | 6 + src/Lightweight/DataBinder/Primitives.hpp | 8 +- src/Lightweight/DataBinder/SqlFixedString.hpp | 32 ++--- src/Lightweight/DataBinder/SqlNullValue.hpp | 6 +- .../DataBinder/SqlTrimmedString.hpp | 4 +- src/Lightweight/DataBinder/SqlVariant.hpp | 5 +- src/Lightweight/DataBinder/StdString.hpp | 4 +- src/Lightweight/SqlConnection.cpp | 9 +- src/Lightweight/SqlConnection.hpp | 9 +- src/Lightweight/SqlError.hpp | 3 + src/Lightweight/SqlLogger.cpp | 18 +-- src/Lightweight/SqlLogger.hpp | 5 + src/Lightweight/SqlQuery.cpp | 2 +- src/Lightweight/SqlQuery/Core.hpp | 16 +-- src/Lightweight/SqlQuery/Select.hpp | 2 +- src/Lightweight/SqlQueryFormatter.hpp | 5 + src/Lightweight/SqlSchema.cpp | 2 +- src/Lightweight/SqlSchema.hpp | 13 +- src/Lightweight/SqlScopedTraceLogger.hpp | 5 + src/Lightweight/SqlStatement.cpp | 12 +- src/Lightweight/SqlStatement.hpp | 16 ++- src/Lightweight/SqlTraits.hpp | 9 +- src/Lightweight/SqlTransaction.cpp | 30 +++-- src/Lightweight/SqlTransaction.hpp | 14 +- src/Lightweight/SqlUtils.hpp | 124 ------------------ src/tests/CoreTests.cpp | 14 +- src/tests/Utils.hpp | 36 ++--- src/tools/ddl2cpp.cpp | 12 +- 33 files changed, 210 insertions(+), 261 deletions(-) delete mode 100644 src/Lightweight/SqlUtils.hpp diff --git a/.clangd b/.clangd index 7652669f..338bb6fb 100644 --- a/.clangd +++ b/.clangd @@ -2,3 +2,15 @@ CompileFlags: Add: - "-std=c++23" - "-D__cpp_concepts=202002L" +Diagnostics: + ClangTidy: + FastCheckFilter: None + +InlayHints: + Enabled: Yes + BlockEnd: No + Designators: Yes + ParameterNames: Yes + DeducedTypes: Yes + TypeNameLimit: 24 + diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 78d8ee57..96152001 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -165,6 +165,7 @@ jobs: -DCMAKE_EXE_LINKER_FLAGS="${CMAKE_EXE_LINKER_FLAGS}" \ -DCMAKE_INSTALL_PREFIX="/usr/local" \ -DPEDANTIC_COMPILER_WERROR=OFF \ + -DLIGHTWEIGHT_BUILD_SHARED=ON \ --preset linux-gcc-release - name: "build" run: cmake --build --preset linux-gcc-release -- -j3 @@ -226,7 +227,7 @@ jobs: - name: "Dump SQL connection string" run: echo "ODBC_CONNECTION_STRING=${{ steps.setup.outputs.ODBC_CONNECTION_STRING }}" - name: "Run SQL Core tests" - run: LightweightTest --trace-sql --trace-model -s # --odbc-connection-string="${{ steps.setup.outputs.ODBC_CONNECTION_STRING }}" + run: LightweightTest --trace-sql -s # --odbc-connection-string="${{ steps.setup.outputs.ODBC_CONNECTION_STRING }}" env: ODBC_CONNECTION_STRING: "${{ steps.setup.outputs.ODBC_CONNECTION_STRING }}" diff --git a/src/.clang-tidy b/src/.clang-tidy index 956587b5..c32460c2 100644 --- a/src/.clang-tidy +++ b/src/.clang-tidy @@ -3,12 +3,10 @@ Checks: >- -*, bugprone-*, -bugprone-branch-clone, - -bugprone-easily-swappable-parameters, -bugprone-exception-escape, -bugprone-implicit-widening-of-multiplication-result, -bugprone-reserved-identifier, -bugprone-suspicious-include, - -bugprone-unchecked-optional-access, clang-analyzer-core.*, clang-analyzer-cplusplus.*, clang-analyzer-deadcode.*, @@ -28,49 +26,40 @@ Checks: >- -cppcoreguidelines-avoid-magic-numbers, -cppcoreguidelines-avoid-non-const-global-variables, -cppcoreguidelines-macro-usage, - -cppcoreguidelines-no-malloc, - -cppcoreguidelines-non-private-member-variables-in-classes, - -cppcoreguidelines-owning-memory, - -cppcoreguidelines-prefer-member-initializer, -cppcoreguidelines-pro-bounds-array-to-pointer-decay, -cppcoreguidelines-pro-bounds-constant-array-index, -cppcoreguidelines-pro-bounds-pointer-arithmetic, -cppcoreguidelines-pro-type-const-cast, -cppcoreguidelines-pro-type-cstyle-cast, -cppcoreguidelines-pro-type-reinterpret-cast, - -cppcoreguidelines-pro-type-static-cast-downcast, - -cppcoreguidelines-pro-type-union-access, - -cppcoreguidelines-pro-type-vararg, - -cppcoreguidelines-special-member-functions, modernize-*, -modernize-avoid-bind, -modernize-avoid-c-arrays, - -modernize-return-braced-init-list, - -modernize-use-bool-literals, -modernize-use-trailing-return-type, performance-*, - -performance-no-int-to-ptr, readability-*, -readability-avoid-unconditional-preprocessor-if, -readability-braces-around-statements, - -readability-container-contains, -readability-else-after-return, - -readability-function-cognitive-complexity, -readability-identifier-length, -readability-implicit-bool-conversion, -readability-magic-numbers, - -readability-named-parameter, -readability-redundant-access-specifiers, + -readability-redundant-member-init, -readability-simplify-boolean-expr, - -readability-uppercase-literal-suffix, - -readability-use-anyofallof, - misc-const-correctness, WarningsAsErrors: >- modernize-use-nullptr, + performance-move-const-arg, + readability-function-cognitive-complexity, + cppcoreguidelines-rvalue-reference-param-not-moved, UseColor: true HeaderFilterRegex: 'src/' FormatStyle: none CheckOptions: + - key: readability-function-cognitive-complexity.Threshold + value: '16' + - key: readability-function-cognitive-complexity.IgnoreMacros + value: true - key: bugprone-easily-swappable-parameters.MinimumLength value: '3' - key: cert-dcl16-c.NewSuffixes diff --git a/src/Lightweight/CMakeLists.txt b/src/Lightweight/CMakeLists.txt index 67c176a9..18ca04d4 100644 --- a/src/Lightweight/CMakeLists.txt +++ b/src/Lightweight/CMakeLists.txt @@ -117,7 +117,7 @@ else() target_compile_options(Lightweight PUBLIC ${ODBC_CFLAGS}) target_link_libraries(Lightweight PUBLIC ${ODBC_LDFLAGS}) - if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") target_link_libraries(Lightweight PUBLIC stdc++exp) # GCC >= 14 endif() endif() diff --git a/src/Lightweight/DataBinder/BasicStringBinder.hpp b/src/Lightweight/DataBinder/BasicStringBinder.hpp index c59ce1b3..e9aaf642 100644 --- a/src/Lightweight/DataBinder/BasicStringBinder.hpp +++ b/src/Lightweight/DataBinder/BasicStringBinder.hpp @@ -4,8 +4,6 @@ #include "Core.hpp" -#include -#include #include template @@ -126,8 +124,8 @@ struct LIGHTWEIGHT_API SqlDataBinder using ValueType = StringType; using StringTraits = SqlCommonStringBinder; - constexpr static inline auto CType = SQL_C_WCHAR; - constexpr static inline auto SqlType = SQL_WVARCHAR; + static constexpr auto CType = SQL_C_WCHAR; + static constexpr auto SqlType = SQL_WVARCHAR; static SQLRETURN InputParameter(SQLHSTMT stmt, SQLUSMALLINT column, ValueType const& value) noexcept { diff --git a/src/Lightweight/DataBinder/Core.hpp b/src/Lightweight/DataBinder/Core.hpp index d87521e2..9c152ad1 100644 --- a/src/Lightweight/DataBinder/Core.hpp +++ b/src/Lightweight/DataBinder/Core.hpp @@ -24,6 +24,12 @@ class LIGHTWEIGHT_API SqlDataBinderCallback { public: + SqlDataBinderCallback() = default; + SqlDataBinderCallback(SqlDataBinderCallback&&) = default; + SqlDataBinderCallback(SqlDataBinderCallback const&) = default; + SqlDataBinderCallback& operator=(SqlDataBinderCallback&&) = default; + SqlDataBinderCallback& operator=(SqlDataBinderCallback const&) = default; + virtual ~SqlDataBinderCallback() = default; virtual void PlanPostExecuteCallback(std::function&&) = 0; diff --git a/src/Lightweight/DataBinder/Primitives.hpp b/src/Lightweight/DataBinder/Primitives.hpp index 3ab93f04..5a4aaefd 100644 --- a/src/Lightweight/DataBinder/Primitives.hpp +++ b/src/Lightweight/DataBinder/Primitives.hpp @@ -8,14 +8,14 @@ template struct LIGHTWEIGHT_API SqlSimpleDataBinder { - static constexpr inline SqlColumnType ColumnType = TheColumnType; + static constexpr SqlColumnType ColumnType = TheColumnType; static SQLRETURN InputParameter(SQLHSTMT stmt, SQLUSMALLINT column, T const& value) noexcept { return SQLBindParameter(stmt, column, SQL_PARAM_INPUT, TheCType, TheSqlType, 0, 0, (SQLPOINTER) &value, 0, nullptr); } - static SQLRETURN OutputColumn(SQLHSTMT stmt, SQLUSMALLINT column, T* result, SQLLEN* indicator, SqlDataBinderCallback&) noexcept + static SQLRETURN OutputColumn(SQLHSTMT stmt, SQLUSMALLINT column, T* result, SQLLEN* indicator, SqlDataBinderCallback& /*unused*/) noexcept { return SQLBindCol(stmt, column, TheCType, result, 0, indicator); } @@ -49,6 +49,6 @@ template requires(std::is_same_v, SqlDataBinder>) // Ensure that an SqlDataBinder exists struct SqlDataTraits { - static constexpr inline unsigned Size = 0; - static constexpr inline SqlColumnType Type = SqlDataBinder::ColumnType; + static constexpr unsigned Size = 0; + static constexpr SqlColumnType Type = SqlDataBinder::ColumnType; }; diff --git a/src/Lightweight/DataBinder/SqlFixedString.hpp b/src/Lightweight/DataBinder/SqlFixedString.hpp index ac614ddd..33c2c7ad 100644 --- a/src/Lightweight/DataBinder/SqlFixedString.hpp +++ b/src/Lightweight/DataBinder/SqlFixedString.hpp @@ -123,7 +123,7 @@ class LIGHTWEIGHT_API SqlFixedString --_size; } - constexpr std::basic_string_view substr( + [[nodiscard]] constexpr std::basic_string_view substr( std::size_t offset = 0, std::size_t count = (std::numeric_limits::max)()) const noexcept { if (offset >= _size) @@ -136,19 +136,19 @@ class LIGHTWEIGHT_API SqlFixedString } // clang-format off - constexpr pointer_type c_str() noexcept { _data[_size] = '\0'; return _data; } - constexpr pointer_type data() noexcept { return _data; } - constexpr iterator begin() noexcept { return _data; } - constexpr iterator end() noexcept { return _data + size(); } - constexpr T& at(std::size_t i) noexcept { return _data[i]; } - constexpr T& operator[](std::size_t i) noexcept { return _data[i]; } - - constexpr const_pointer_type c_str() const noexcept { const_cast(_data)[_size] = '\0'; return _data; } - constexpr const_pointer_type data() const noexcept { return _data; } - constexpr const_iterator begin() const noexcept { return _data; } - constexpr const_iterator end() const noexcept { return _data + size(); } - constexpr T const& at(std::size_t i) const noexcept { return _data[i]; } - constexpr T const& operator[](std::size_t i) const noexcept { return _data[i]; } + [[nodiscard]] constexpr pointer_type c_str() noexcept { _data[_size] = '\0'; return _data; } + [[nodiscard]] constexpr pointer_type data() noexcept { return _data; } + [[nodiscard]] constexpr iterator begin() noexcept { return _data; } + [[nodiscard]] constexpr iterator end() noexcept { return _data + size(); } + [[nodiscard]] constexpr T& at(std::size_t i) noexcept { return _data[i]; } + [[nodiscard]] constexpr T& operator[](std::size_t i) noexcept { return _data[i]; } + + [[nodiscard]] constexpr const_pointer_type c_str() const noexcept { const_cast(_data)[_size] = '\0'; return _data; } + [[nodiscard]] constexpr const_pointer_type data() const noexcept { return _data; } + [[nodiscard]] constexpr const_iterator begin() const noexcept { return _data; } + [[nodiscard]] constexpr const_iterator end() const noexcept { return _data + size(); } + [[nodiscard]] constexpr T const& at(std::size_t i) const noexcept { return _data[i]; } + [[nodiscard]] constexpr T const& operator[](std::size_t i) const noexcept { return _data[i]; } // clang-format on template @@ -285,8 +285,8 @@ struct LIGHTWEIGHT_API SqlCommonStringBinder> template struct LIGHTWEIGHT_API SqlDataTraits> { - static constexpr inline unsigned Size = N; - static constexpr inline SqlColumnType Type = SqlColumnType::CHAR; + static constexpr unsigned Size = N; + static constexpr SqlColumnType Type = SqlColumnType::CHAR; }; template diff --git a/src/Lightweight/DataBinder/SqlNullValue.hpp b/src/Lightweight/DataBinder/SqlNullValue.hpp index 32e4c04c..a3fc3ed6 100644 --- a/src/Lightweight/DataBinder/SqlNullValue.hpp +++ b/src/Lightweight/DataBinder/SqlNullValue.hpp @@ -11,7 +11,7 @@ struct SqlNullType }; // Used to indicate a NULL value in a SQL query. -constexpr inline auto SqlNullValue = SqlNullType {}; +constexpr auto SqlNullValue = SqlNullType {}; template <> struct SqlDataBinder @@ -34,6 +34,6 @@ struct SqlDataBinder template <> struct SqlDataTraits { - static constexpr inline unsigned Size = 0; - static constexpr inline SqlColumnType Type = SqlColumnType::UNKNOWN; + static constexpr unsigned Size = 0; + static constexpr SqlColumnType Type = SqlColumnType::UNKNOWN; }; diff --git a/src/Lightweight/DataBinder/SqlTrimmedString.hpp b/src/Lightweight/DataBinder/SqlTrimmedString.hpp index 79824811..9aa9b69a 100644 --- a/src/Lightweight/DataBinder/SqlTrimmedString.hpp +++ b/src/Lightweight/DataBinder/SqlTrimmedString.hpp @@ -30,8 +30,8 @@ struct std::formatter: std::formatter template <> struct SqlDataTraits { - static constexpr inline unsigned Size = 0; - static constexpr inline SqlColumnType Type = SqlColumnType::STRING; + static constexpr unsigned Size = 0; + static constexpr SqlColumnType Type = SqlColumnType::STRING; }; template <> diff --git a/src/Lightweight/DataBinder/SqlVariant.hpp b/src/Lightweight/DataBinder/SqlVariant.hpp index 6b15d6af..4951f088 100644 --- a/src/Lightweight/DataBinder/SqlVariant.hpp +++ b/src/Lightweight/DataBinder/SqlVariant.hpp @@ -107,6 +107,7 @@ struct SqlVariant SqlVariant(SqlVariant&&) noexcept = default; SqlVariant& operator=(SqlVariant const&) = default; SqlVariant& operator=(SqlVariant&&) noexcept = default; + ~SqlVariant() = default; SqlVariant(InnerType const& other): value(other) @@ -170,10 +171,10 @@ struct SqlVariant [[nodiscard]] T ValueOr(T&& defaultValue) const noexcept { if constexpr (std::is_integral_v) - return TryGetIntegral().value_or(defaultValue); + return TryGetIntegral().value_or(std::forward(defaultValue)); if (IsNull()) - return defaultValue; + return std::forward(defaultValue); return std::get(value); } diff --git a/src/Lightweight/DataBinder/StdString.hpp b/src/Lightweight/DataBinder/StdString.hpp index ea00aec0..d3f28316 100644 --- a/src/Lightweight/DataBinder/StdString.hpp +++ b/src/Lightweight/DataBinder/StdString.hpp @@ -56,6 +56,6 @@ struct SqlCommonStringBinder> template <> struct SqlDataTraits { - static constexpr inline unsigned Size = 0; - static constexpr inline SqlColumnType Type = SqlColumnType::STRING; + static constexpr unsigned Size = 0; + static constexpr SqlColumnType Type = SqlColumnType::STRING; }; diff --git a/src/Lightweight/SqlConnection.cpp b/src/Lightweight/SqlConnection.cpp index a10e33fe..86e50cc5 100644 --- a/src/Lightweight/SqlConnection.cpp +++ b/src/Lightweight/SqlConnection.cpp @@ -9,7 +9,7 @@ using namespace std::chrono_literals; using namespace std::string_view_literals; -static std::optional gDefaultConnectInfo {}; +static SqlConnectInfo gDefaultConnectInfo {}; static std::atomic gNextConnectionId { 1 }; static std::function gPostConnectedHook {}; @@ -85,7 +85,7 @@ SqlConnection::~SqlConnection() noexcept SqlConnectInfo const& SqlConnection::DefaultConnectInfo() noexcept { - return gDefaultConnectInfo.value(); + return gDefaultConnectInfo; } void SqlConnection::SetDefaultConnectInfo(SqlConnectInfo connectInfo) noexcept @@ -163,6 +163,7 @@ bool SqlConnection::Connect(SqlConnectInfo connectInfo) noexcept if (auto const* info = std::get_if(&m_data->connectInfo)) { + // NOLINTNEXTLINE(performance-no-int-to-ptr) SQLRETURN sqlReturn = SQLSetConnectAttrA(m_hDbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER) info->timeout.count(), 0); if (!SQL_SUCCEEDED(sqlReturn)) { @@ -280,7 +281,7 @@ std::string SqlConnection::ServerVersion() const bool SqlConnection::TransactionActive() const noexcept { SQLUINTEGER state {}; - SQLRETURN sqlResult = SQLGetConnectAttrA(m_hDbc, SQL_ATTR_AUTOCOMMIT, &state, 0, nullptr); + SQLRETURN const sqlResult = SQLGetConnectAttrA(m_hDbc, SQL_ATTR_AUTOCOMMIT, &state, 0, nullptr); return sqlResult == SQL_SUCCESS && state == SQL_AUTOCOMMIT_OFF; } @@ -295,7 +296,7 @@ bool SqlConnection::TransactionsAllowed() const noexcept bool SqlConnection::IsAlive() const noexcept { SQLUINTEGER state {}; - SQLRETURN sqlResult = SQLGetConnectAttrA(m_hDbc, SQL_ATTR_CONNECTION_DEAD, &state, 0, nullptr); + SQLRETURN const sqlResult = SQLGetConnectAttrA(m_hDbc, SQL_ATTR_CONNECTION_DEAD, &state, 0, nullptr); return SQL_SUCCEEDED(sqlResult) && state == SQL_CD_FALSE; } diff --git a/src/Lightweight/SqlConnection.hpp b/src/Lightweight/SqlConnection.hpp index 26a0a4fd..bf0e2a97 100644 --- a/src/Lightweight/SqlConnection.hpp +++ b/src/Lightweight/SqlConnection.hpp @@ -11,7 +11,6 @@ #include "SqlError.hpp" #include "SqlLogger.hpp" #include "SqlTraits.hpp" -#include "SqlUtils.hpp" #include #include @@ -46,10 +45,10 @@ class LIGHTWEIGHT_API SqlConnection final // Constructs a new SQL connection to the given connect informaton. explicit SqlConnection(std::optional connectInfo); - SqlConnection(SqlConnection&&) noexcept; - SqlConnection& operator=(SqlConnection&&) noexcept; - SqlConnection(SqlConnection const&) = delete; - SqlConnection& operator=(SqlConnection const&) = delete; + SqlConnection(SqlConnection&& /*other*/) noexcept; + SqlConnection& operator=(SqlConnection&& /*other*/) noexcept; + SqlConnection(SqlConnection const& /*other*/) = delete; + SqlConnection& operator=(SqlConnection const& /*other*/) = delete; // Destructs this SQL connection object, ~SqlConnection() noexcept; diff --git a/src/Lightweight/SqlError.hpp b/src/Lightweight/SqlError.hpp index 89e624b8..afcce399 100644 --- a/src/Lightweight/SqlError.hpp +++ b/src/Lightweight/SqlError.hpp @@ -87,6 +87,7 @@ enum class SqlError : std::int16_t NO_DATA_FOUND = SQL_NO_DATA_FOUND, UNSUPPORTED_TYPE = 1'000, INVALID_ARGUMENT = 1'001, + TRANSACTION_ERROR = 1'002, }; struct SqlErrorCategory: std::error_category @@ -126,6 +127,8 @@ struct SqlErrorCategory: std::error_category return "SQL_UNSUPPORTED_TYPE"s; case SqlError::INVALID_ARGUMENT: return "SQL_INVALID_ARGUMENT"s; + case SqlError::TRANSACTION_ERROR: + return "SQL_TRANSACTION_ERROR"s; } return std::format("SQL error code {}", code); } diff --git a/src/Lightweight/SqlLogger.cpp b/src/Lightweight/SqlLogger.cpp index 35fa832a..f449e982 100644 --- a/src/Lightweight/SqlLogger.cpp +++ b/src/Lightweight/SqlLogger.cpp @@ -27,8 +27,8 @@ namespace class SqlStandardLogger: public SqlLogger { private: - std::chrono::time_point m_currentTime {}; - std::string m_currentTimeStr {}; + std::chrono::time_point m_currentTime; + std::string m_currentTimeStr; public: void Tick() @@ -66,13 +66,13 @@ class SqlStandardLogger: public SqlLogger WriteMessage(" Message: {}", errorInfo.message); } - void OnConnectionOpened(SqlConnection const&) override {} - void OnConnectionClosed(SqlConnection const&) override {} - void OnConnectionIdle(SqlConnection const&) override {} - void OnConnectionReuse(SqlConnection const&) override {} - void OnExecuteDirect(std::string_view const&) override {} - void OnPrepare(std::string_view const&) override {} - void OnExecute(std::string_view const&) override {} + void OnConnectionOpened(SqlConnection const& /*connection*/) override {} + void OnConnectionClosed(SqlConnection const& /*connection*/) override {} + void OnConnectionIdle(SqlConnection const& /*connection*/) override {} + void OnConnectionReuse(SqlConnection const& /*connection*/) override {} + void OnExecuteDirect(std::string_view const& /*query*/) override {} + void OnPrepare(std::string_view const& /*query*/) override {} + void OnExecute(std::string_view const& /*query*/) override {} void OnExecuteBatch() override {} void OnFetchedRow() override {} }; diff --git a/src/Lightweight/SqlLogger.hpp b/src/Lightweight/SqlLogger.hpp index dae57468..141bb5cd 100644 --- a/src/Lightweight/SqlLogger.hpp +++ b/src/Lightweight/SqlLogger.hpp @@ -14,6 +14,11 @@ class SqlConnection; class LIGHTWEIGHT_API SqlLogger { public: + SqlLogger() = default; + SqlLogger(SqlLogger const& /*other*/) = default; + SqlLogger(SqlLogger&& /*other*/) = default; + SqlLogger& operator=(SqlLogger const& /*other*/) = default; + SqlLogger& operator=(SqlLogger&& /*other*/) = default; virtual ~SqlLogger() = default; // Logs a warning message. diff --git a/src/Lightweight/SqlQuery.cpp b/src/Lightweight/SqlQuery.cpp index fa030ee3..4b826066 100644 --- a/src/Lightweight/SqlQuery.cpp +++ b/src/Lightweight/SqlQuery.cpp @@ -27,7 +27,7 @@ SqlSelectQueryBuilder SqlQueryBuilder::Select() noexcept SqlUpdateQueryBuilder SqlQueryBuilder::Update(std::vector* boundInputs) noexcept { - return SqlUpdateQueryBuilder(m_formatter, std::move(m_table), std::move(m_tableAlias), boundInputs); + return SqlUpdateQueryBuilder { m_formatter, std::move(m_table), std::move(m_tableAlias), boundInputs }; } SqlDeleteQueryBuilder SqlQueryBuilder::Delete() noexcept diff --git a/src/Lightweight/SqlQuery/Core.hpp b/src/Lightweight/SqlQuery/Core.hpp index 11568e04..fdbd3324 100644 --- a/src/Lightweight/SqlQuery/Core.hpp +++ b/src/Lightweight/SqlQuery/Core.hpp @@ -108,18 +108,18 @@ class [[nodiscard]] LIGHTWEIGHT_API SqlWhereClauseBuilder // Constructs or extends a WHERE/AND clause to test for a group of values. template requires std::invocable&> - [[nodiscard]] Derived& Where(Callable&& callable); + [[nodiscard]] Derived& Where(Callable const& callable); // Constructs or extends an WHERE/OR clause to test for a group of values. template requires std::invocable&> - [[nodiscard]] Derived& OrWhere(Callable&& callable); + [[nodiscard]] Derived& OrWhere(Callable const& callable); template [[nodiscard]] Derived& WhereIn(ColumnName const& columnName, InputRange const& values); template - [[nodiscard]] Derived& WhereIn(ColumnName const& columnName, std::initializer_list&& values); + [[nodiscard]] Derived& WhereIn(ColumnName const& columnName, std::initializer_list const& values); template [[nodiscard]] Derived& WhereNull(ColumnName const& columnName); @@ -253,15 +253,15 @@ inline LIGHTWEIGHT_FORCE_INLINE Derived& SqlWhereClauseBuilder::Where(C template template requires std::invocable&> -inline LIGHTWEIGHT_FORCE_INLINE Derived& SqlWhereClauseBuilder::OrWhere(Callable&& callable) +inline LIGHTWEIGHT_FORCE_INLINE Derived& SqlWhereClauseBuilder::OrWhere(Callable const& callable) { - return Or().Where(std::forward(callable)); + return Or().Where(callable); } template template requires std::invocable&> -inline LIGHTWEIGHT_FORCE_INLINE Derived& SqlWhereClauseBuilder::Where(Callable&& callable) +inline LIGHTWEIGHT_FORCE_INLINE Derived& SqlWhereClauseBuilder::Where(Callable const& callable) { auto& condition = SearchCondition().condition; @@ -309,9 +309,9 @@ inline LIGHTWEIGHT_FORCE_INLINE Derived& SqlWhereClauseBuilder::WhereIn template template inline LIGHTWEIGHT_FORCE_INLINE Derived& SqlWhereClauseBuilder::WhereIn(ColumnName const& columnName, - std::initializer_list&& values) + std::initializer_list const& values) { - return Where(columnName, "IN", detail::PopulateSqlSetExpression(std::forward>(values))); + return Where(columnName, "IN", detail::PopulateSqlSetExpression(values)); } template diff --git a/src/Lightweight/SqlQuery/Select.hpp b/src/Lightweight/SqlQuery/Select.hpp index 435c7d18..a8ab2f8a 100644 --- a/src/Lightweight/SqlQuery/Select.hpp +++ b/src/Lightweight/SqlQuery/Select.hpp @@ -14,7 +14,7 @@ class [[nodiscard]] LIGHTWEIGHT_API SqlSelectQueryBuilder final: public detail::SqlWhereClauseBuilder { public: - enum class SelectType + enum class SelectType: std::uint8_t { Undefined, Count, diff --git a/src/Lightweight/SqlQueryFormatter.hpp b/src/Lightweight/SqlQueryFormatter.hpp index a71a133f..464759c4 100644 --- a/src/Lightweight/SqlQueryFormatter.hpp +++ b/src/Lightweight/SqlQueryFormatter.hpp @@ -15,6 +15,11 @@ struct SqlQualifiedTableColumnName; class [[nodiscard]] LIGHTWEIGHT_API SqlQueryFormatter { public: + SqlQueryFormatter() = default; + SqlQueryFormatter(SqlQueryFormatter&&) = default; + SqlQueryFormatter(SqlQueryFormatter const&) = default; + SqlQueryFormatter& operator=(SqlQueryFormatter&&) = default; + SqlQueryFormatter& operator=(SqlQueryFormatter const&) = default; virtual ~SqlQueryFormatter() = default; [[nodiscard]] virtual std::string_view BooleanLiteral(bool value) const noexcept = 0; diff --git a/src/Lightweight/SqlSchema.cpp b/src/Lightweight/SqlSchema.cpp index 83321628..5ba14d71 100644 --- a/src/Lightweight/SqlSchema.cpp +++ b/src/Lightweight/SqlSchema.cpp @@ -203,7 +203,7 @@ void ReadAllTables(std::string_view database, std::string_view schema, EventHand { auto const tableNames = AllTables(database, schema); - for (auto& tableName: tableNames) + for (auto const& tableName: tableNames) { if (tableName == "sqlite_sequence") continue; diff --git a/src/Lightweight/SqlSchema.hpp b/src/Lightweight/SqlSchema.hpp index 7083964d..25d5fd04 100644 --- a/src/Lightweight/SqlSchema.hpp +++ b/src/Lightweight/SqlSchema.hpp @@ -29,9 +29,9 @@ namespace detail struct FullyQualifiedTableName { - std::string catalog {}; - std::string schema {}; - std::string table {}; + std::string catalog; + std::string schema; + std::string table; bool operator==(FullyQualifiedTableName const& other) const noexcept { @@ -90,7 +90,7 @@ struct Column bool isNullable = true; bool isUnique = false; size_t size = 0; - unsigned short decimalDigits; + unsigned short decimalDigits = 0; bool isAutoIncrement = false; bool isPrimaryKey = false; bool isForeignKey = false; @@ -101,6 +101,11 @@ struct Column class EventHandler { public: + EventHandler() = default; + EventHandler(EventHandler&&) = default; + EventHandler(EventHandler const&) = default; + EventHandler& operator=(EventHandler&&) = default; + EventHandler& operator=(EventHandler const&) = default; virtual ~EventHandler() = default; virtual bool OnTable(std::string_view table) = 0; diff --git a/src/Lightweight/SqlScopedTraceLogger.hpp b/src/Lightweight/SqlScopedTraceLogger.hpp index be5543aa..0ecf9454 100644 --- a/src/Lightweight/SqlScopedTraceLogger.hpp +++ b/src/Lightweight/SqlScopedTraceLogger.hpp @@ -32,6 +32,11 @@ class LIGHTWEIGHT_API SqlScopedTraceLogger SQLSetConnectAttrA(m_nativeConnection, SQL_ATTR_TRACE, (SQLPOINTER) SQL_OPT_TRACE_ON, SQL_IS_UINTEGER); } + SqlScopedTraceLogger(SqlScopedTraceLogger&&) = delete; + SqlScopedTraceLogger& operator=(SqlScopedTraceLogger&&) = delete; + SqlScopedTraceLogger(SqlScopedTraceLogger const&) = delete; + SqlScopedTraceLogger& operator=(SqlScopedTraceLogger const&) = delete; + ~SqlScopedTraceLogger() { SQLSetConnectAttrA(m_nativeConnection, SQL_ATTR_TRACE, (SQLPOINTER) SQL_OPT_TRACE_OFF, SQL_IS_UINTEGER); diff --git a/src/Lightweight/SqlStatement.cpp b/src/Lightweight/SqlStatement.cpp index 384b9f20..ff54874d 100644 --- a/src/Lightweight/SqlStatement.cpp +++ b/src/Lightweight/SqlStatement.cpp @@ -41,11 +41,15 @@ void SqlStatement::PlanPostProcessOutputColumn(std::function&& cb) } SqlStatement::SqlStatement(): - m_data { new Data() } + m_data { new Data { + .ownedConnection = SqlConnection(), + .indicators = {}, + .postExecuteCallbacks = {}, + .postProcessOutputColumnCallbacks = {}, + } }, + // NOLINTNEXTLINE(bugprone-unchecked-optional-access) + m_connection { &*m_data->ownedConnection } { - m_data->ownedConnection = SqlConnection(); - m_connection = &*m_data->ownedConnection; - if (m_connection->NativeHandle()) RequireSuccess(SQLAllocHandle(SQL_HANDLE_STMT, m_connection->NativeHandle(), &m_hStmt)); } diff --git a/src/Lightweight/SqlStatement.hpp b/src/Lightweight/SqlStatement.hpp index 4ddb20cb..63277b5d 100644 --- a/src/Lightweight/SqlStatement.hpp +++ b/src/Lightweight/SqlStatement.hpp @@ -9,8 +9,8 @@ #include "Api.hpp" #include "SqlConnection.hpp" -#include "SqlQuery.hpp" #include "SqlDataBinder.hpp" +#include "SqlQuery.hpp" #include #include @@ -49,8 +49,11 @@ class LIGHTWEIGHT_API SqlStatement final: public SqlDataBinderCallback SqlStatement(SqlStatement&&) noexcept = default; SqlStatement& operator=(SqlStatement&&) noexcept = default; + SqlStatement(SqlStatement const&) noexcept = delete; + SqlStatement& operator=(SqlStatement const&) noexcept = delete; + // Construct a new SqlStatement object, using the given connection. - SqlStatement(SqlConnection& relatedConnection); + explicit SqlStatement(SqlConnection& relatedConnection); ~SqlStatement() noexcept final; @@ -169,10 +172,10 @@ class LIGHTWEIGHT_API SqlStatement final: public SqlDataBinderCallback // private data members struct Data; Data* m_data; - SqlConnection* m_connection {}; // Pointer to the connection object - SQLHSTMT m_hStmt {}; // The native oDBC statement handle - std::string m_preparedQuery; // The last prepared query - SQLSMALLINT m_expectedParameterCount {}; // The number of parameters expected by the query + SqlConnection* m_connection {}; // Pointer to the connection object + SQLHSTMT m_hStmt {}; // The native oDBC statement handle + std::string m_preparedQuery; // The last prepared query + SQLSMALLINT m_expectedParameterCount {}; // The number of parameters expected by the query }; // {{{ inline implementation @@ -288,6 +291,7 @@ void SqlStatement::ExecuteBatchNative(FirstColumnBatch const& firstColumnBatch, size_t rowStart = 0; // clang-format off + // NOLINTNEXTLINE(performance-no-int-to-ptr) RequireSuccess(SQLSetStmtAttr(m_hStmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER) rowCount, 0)); RequireSuccess(SQLSetStmtAttr(m_hStmt, SQL_ATTR_PARAM_BIND_OFFSET_PTR, &rowStart, 0)); RequireSuccess(SQLSetStmtAttr(m_hStmt, SQL_ATTR_PARAM_BIND_TYPE, SQL_PARAM_BIND_BY_COLUMN, 0)); diff --git a/src/Lightweight/SqlTraits.hpp b/src/Lightweight/SqlTraits.hpp index e325f528..125a415e 100644 --- a/src/Lightweight/SqlTraits.hpp +++ b/src/Lightweight/SqlTraits.hpp @@ -89,9 +89,9 @@ struct SqlTraits std::string_view LastInsertIdQuery; std::string_view PrimaryKeyAutoIncrement; std::string_view CurrentTimestampExpr; - std::string_view EnforceForeignKeyConstraint {}; + std::string_view EnforceForeignKeyConstraint; size_t MaxStatementLength {}; - std::function ColumnTypeName {}; + std::function ColumnTypeName; }; namespace detail @@ -101,6 +101,7 @@ inline SqlTraits const MicrosoftSqlTraits { .LastInsertIdQuery = "SELECT @@IDENTITY;", .PrimaryKeyAutoIncrement = "INT IDENTITY(1,1) PRIMARY KEY", .CurrentTimestampExpr = "GETDATE()", + .EnforceForeignKeyConstraint = "", .ColumnTypeName = detail::MSSqlColumnTypeName, }; @@ -108,6 +109,7 @@ inline SqlTraits const PostgresSqlTraits { .LastInsertIdQuery = "SELECT LASTVAL()", .PrimaryKeyAutoIncrement = "SERIAL PRIMARY KEY", .CurrentTimestampExpr = "CURRENT_TIMESTAMP", + .EnforceForeignKeyConstraint = "", .ColumnTypeName = [](SqlColumnType value) -> std::string_view { switch (value) { @@ -123,6 +125,7 @@ inline SqlTraits const OracleSqlTraits { .LastInsertIdQuery = "SELECT LAST_INSERT_ID() FROM DUAL", .PrimaryKeyAutoIncrement = "NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY", .CurrentTimestampExpr = "SYSTIMESTAMP", + .EnforceForeignKeyConstraint = "", .ColumnTypeName = detail::DefaultColumnTypeName, }; @@ -138,6 +141,7 @@ inline SqlTraits const MySQLTraits { .LastInsertIdQuery = "SELECT LAST_INSERT_ID()", .PrimaryKeyAutoIncrement = "INT AUTO_INCREMENT PRIMARY KEY", .CurrentTimestampExpr = "NOW()", + .EnforceForeignKeyConstraint = "", .ColumnTypeName = detail::DefaultColumnTypeName, }; @@ -145,6 +149,7 @@ inline SqlTraits const UnknownSqlTraits { .LastInsertIdQuery = "", .PrimaryKeyAutoIncrement = "", .CurrentTimestampExpr = "", + .EnforceForeignKeyConstraint = "", .ColumnTypeName = detail::DefaultColumnTypeName, }; diff --git a/src/Lightweight/SqlTransaction.cpp b/src/Lightweight/SqlTransaction.cpp index f08a96de..8277ca87 100644 --- a/src/Lightweight/SqlTransaction.cpp +++ b/src/Lightweight/SqlTransaction.cpp @@ -10,7 +10,7 @@ SqlTransaction::SqlTransaction(SqlConnection& connection, SqlTransactionMode def SQLSetConnectAttr(m_hDbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER) SQL_AUTOCOMMIT_OFF, SQL_IS_UINTEGER); } -SqlTransaction::~SqlTransaction() +SqlTransaction::~SqlTransaction() noexcept { switch (m_defaultMode) { @@ -25,29 +25,43 @@ SqlTransaction::~SqlTransaction() } } -void SqlTransaction::Rollback() +bool SqlTransaction::Rollback() noexcept { SQLRETURN sqlReturn = SQLEndTran(SQL_HANDLE_DBC, m_hDbc, SQL_ROLLBACK); if (sqlReturn != SQL_SUCCESS && sqlReturn != SQL_SUCCESS_WITH_INFO) - throw SqlException { SqlErrorInfo::fromConnectionHandle(m_hDbc) }; - ; + { + SqlLogger::GetLogger().OnError(SqlErrorInfo::fromConnectionHandle(m_hDbc)); + return false; + } + sqlReturn = SQLSetConnectAttr(m_hDbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER) SQL_AUTOCOMMIT_ON, SQL_IS_UINTEGER); if (sqlReturn != SQL_SUCCESS && sqlReturn != SQL_SUCCESS_WITH_INFO) - throw SqlException { SqlErrorInfo::fromConnectionHandle(m_hDbc) }; + { + SqlLogger::GetLogger().OnError(SqlErrorInfo::fromConnectionHandle(m_hDbc)); + return false; + } m_defaultMode = SqlTransactionMode::NONE; + return true; } // Commit the transaction -void SqlTransaction::Commit() +bool SqlTransaction::Commit() noexcept { SQLRETURN sqlReturn = SQLEndTran(SQL_HANDLE_DBC, m_hDbc, SQL_COMMIT); if (sqlReturn != SQL_SUCCESS && sqlReturn != SQL_SUCCESS_WITH_INFO) - throw SqlException { SqlErrorInfo::fromConnectionHandle(m_hDbc) }; + { + SqlLogger::GetLogger().OnError(SqlErrorInfo::fromConnectionHandle(m_hDbc)); + return false; + } sqlReturn = SQLSetConnectAttr(m_hDbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER) SQL_AUTOCOMMIT_ON, SQL_IS_UINTEGER); if (sqlReturn != SQL_SUCCESS && sqlReturn != SQL_SUCCESS_WITH_INFO) - throw SqlException { SqlErrorInfo::fromConnectionHandle(m_hDbc) }; + { + SqlLogger::GetLogger().OnError(SqlErrorInfo::fromConnectionHandle(m_hDbc)); + return false; + } m_defaultMode = SqlTransactionMode::NONE; + return true; } diff --git a/src/Lightweight/SqlTransaction.hpp b/src/Lightweight/SqlTransaction.hpp index 6d76ef2d..f280b070 100644 --- a/src/Lightweight/SqlTransaction.hpp +++ b/src/Lightweight/SqlTransaction.hpp @@ -16,7 +16,7 @@ class SqlConnection; // Represents the mode of a SQL transaction to be applied, if not done so explicitly. -enum class SqlTransactionMode +enum class SqlTransactionMode : std::uint8_t { NONE, COMMIT, @@ -33,19 +33,25 @@ enum class SqlTransactionMode class LIGHTWEIGHT_API SqlTransaction { public: + SqlTransaction() = default; + SqlTransaction(SqlTransaction const&) = default; + SqlTransaction(SqlTransaction&&) = default; + SqlTransaction& operator=(SqlTransaction const&) = default; + SqlTransaction& operator=(SqlTransaction&&) = default; + // Construct a new SqlTransaction object, and disable the auto-commit mode, so that the transaction can be // controlled manually. explicit SqlTransaction(SqlConnection& connection, SqlTransactionMode defaultMode = SqlTransactionMode::COMMIT) noexcept; // Automatically commit the transaction if not done so - ~SqlTransaction(); + ~SqlTransaction() noexcept; // Rollback the transaction - void Rollback(); + bool Rollback() noexcept; // Commit the transaction - void Commit(); + bool Commit() noexcept; private: SQLHDBC m_hDbc; diff --git a/src/Lightweight/SqlUtils.hpp b/src/Lightweight/SqlUtils.hpp deleted file mode 100644 index c3031b64..00000000 --- a/src/Lightweight/SqlUtils.hpp +++ /dev/null @@ -1,124 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -#pragma once - -#if defined(_WIN32) || defined(_WIN64) - #include -#endif - -#include "Api.hpp" -#include - -namespace detail -{ - -template -struct LIGHTWEIGHT_API DefaultDeleter -{ - void operator()(T* object) - { - delete object; - } -}; - -// We cannot use std::unique_ptr in Windows DLLs as struct/class members, -// because of how Windows handles DLLs and the potential of incompatibilities (See C4251) -template > -class LIGHTWEIGHT_API UniquePtr -{ - public: - explicit UniquePtr() noexcept = default; - - explicit UniquePtr(std::nullptr_t) noexcept: - _value { nullptr } - { - } - - explicit UniquePtr(T* value, Deleter deleter): - _value { value }, - _deleter { deleter } - { - } - - UniquePtr(UniquePtr const&) = delete; - UniquePtr& operator=(UniquePtr const&) = delete; - - UniquePtr(UniquePtr&& source) noexcept: - _value { source._value }, - _deleter { std::move(source._deleter) } - { - } - - UniquePtr& operator=(UniquePtr&& source) noexcept - { - _value = source._value; - _deleter = std::move(source._deleter); - return *this; - } - - ~UniquePtr() - { - _deleter(_value); - } - - T* release() noexcept - { - T* oldValue = _value; - _value = nullptr; - return oldValue; - } - - void reset(T* newValue) noexcept - { - if (_value) - delete _value; - _value = newValue; - } - - T* get() noexcept - { - return _value; - } - - T const* get() const noexcept - { - return _value; - } - - T& operator*() noexcept - { - return *_value; - } - - T const& operator*() const noexcept - { - return *_value; - } - - T& operator->() noexcept - { - return *_value; - } - - T const& operator->() const noexcept - { - return *_value; - } - - explicit operator bool() const noexcept - { - return _value != nullptr; - } - - private: - T* _value = nullptr; - Deleter _deleter = Deleter {}; -}; - -template -UniquePtr MakeUnique(Args&&... args) -{ - return UniquePtr(new T(std::forward(args)...)); -} - -} // namespace detail diff --git a/src/tests/CoreTests.cpp b/src/tests/CoreTests.cpp index 6637a74a..b1ce6713 100644 --- a/src/tests/CoreTests.cpp +++ b/src/tests/CoreTests.cpp @@ -21,6 +21,8 @@ #include #include +// NOLINTBEGIN(readability-container-size-empty) + #if defined(_MSC_VER) // Disable the warning C4834: discarding return value of function with 'nodiscard' attribute. // Because we are simply testing and demonstrating the library and not using it in production code. @@ -432,7 +434,7 @@ struct SqlDataBinder static constexpr int PostProcess(int value) noexcept { - return value |= 0x01; + return value | 0x01; } }; @@ -883,7 +885,7 @@ template // requires(std::is_invocable_v) void checkSqlQueryBuilder(TheSqlQuery const& sqlQueryBuilder, QueryExpectations const& expectations, - std::function&& postCheck = {}, + std::function const& postCheck = {}, std::source_location const& location = std::source_location::current()) { auto const eraseLinefeeds = [](std::string str) noexcept -> std::string { @@ -906,7 +908,7 @@ void checkSqlQueryBuilder(TheSqlQuery const& sqlQueryBuilder, CHECK(actualSqlServer == expectations.sqlServer); if (postCheck) postCheck(); -}; +} TEST_CASE_METHOD(SqlTestFixture, "SqlQueryBuilder.Select.Count", "[SqlQueryBuilder]") { @@ -1243,8 +1245,8 @@ TEST_CASE_METHOD(SqlTestFixture, CreateEmployeesTable(stmt, quoted); auto businessObject = TestBusinessObject { - .sqlInsertEmployee = { sharedConnection }, - .sqlSelectEmployee = { sharedConnection }, + .sqlInsertEmployee = SqlStatement { sharedConnection }, + .sqlSelectEmployee = SqlStatement { sharedConnection }, }; auto const insertQuery = stmt.Query("Employees") @@ -1283,3 +1285,5 @@ TEST_CASE_METHOD(SqlTestFixture, // // CHECK(lastName.value == "Smith"); // } } + +// NOLINTEND(readability-container-size-empty) diff --git a/src/tests/Utils.hpp b/src/tests/Utils.hpp index 70d6a642..89d48f21 100644 --- a/src/tests/Utils.hpp +++ b/src/tests/Utils.hpp @@ -51,6 +51,7 @@ auto const inline DefaultTestConnectionString = SqlConnectionString { "file::memory:"), }; +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) class ScopedSqlNullLogger: public SqlLogger { private: @@ -67,20 +68,21 @@ class ScopedSqlNullLogger: public SqlLogger SqlLogger::SetLogger(m_previousLogger); } - void OnWarning(std::string_view const&) override {} - void OnError(SqlError, std::source_location) override {} - void OnError(SqlErrorInfo const&, std::source_location) override {} - void OnConnectionOpened(SqlConnection const&) override {} - void OnConnectionClosed(SqlConnection const&) override {} - void OnConnectionIdle(SqlConnection const&) override {} - void OnConnectionReuse(SqlConnection const&) override {} - void OnExecuteDirect(std::string_view const&) override {} - void OnPrepare(std::string_view const&) override {} - void OnExecute(std::string_view const&) override {} + void OnWarning(std::string_view const& /*message*/) override {} + void OnError(SqlError /*errorCode*/, std::source_location /*sourceLocation*/) override {} + void OnError(SqlErrorInfo const& /*errorInfo*/, std::source_location /*sourceLocation*/) override {} + void OnConnectionOpened(SqlConnection const& /*connection*/) override {} + void OnConnectionClosed(SqlConnection const& /*connection*/) override {} + void OnConnectionIdle(SqlConnection const& /*connection*/) override {} + void OnConnectionReuse(SqlConnection const& /*connection*/) override {} + void OnExecuteDirect(std::string_view const& /*query*/) override {} + void OnPrepare(std::string_view const& /*query*/) override {} + void OnExecute(std::string_view const& /*query*/) override {} void OnExecuteBatch() override {} void OnFetchedRow() override {} }; +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) class SqlTestFixture { public: @@ -155,7 +157,7 @@ class SqlTestFixture case SqlServerType::SQLITE: { auto stmt = SqlStatement { connection }; // Enable foreign key constraints for SQLite - (void) stmt.ExecuteDirect("PRAGMA foreign_keys = ON"); + stmt.ExecuteDirect("PRAGMA foreign_keys = ON"); break; } case SqlServerType::MICROSOFT_SQL: @@ -228,20 +230,20 @@ class SqlTestFixture switch (stmt.Connection().ServerType()) { case SqlServerType::MICROSOFT_SQL: - (void) stmt.ExecuteDirect(std::format("USE {}", "master")); - (void) stmt.ExecuteDirect(std::format("DROP DATABASE IF EXISTS \"{}\"", testDatabaseName)); - (void) stmt.ExecuteDirect(std::format("CREATE DATABASE \"{}\"", testDatabaseName)); - (void) stmt.ExecuteDirect(std::format("USE {}", testDatabaseName)); + stmt.ExecuteDirect(std::format("USE {}", "master")); + stmt.ExecuteDirect(std::format("DROP DATABASE IF EXISTS \"{}\"", testDatabaseName)); + stmt.ExecuteDirect(std::format("CREATE DATABASE \"{}\"", testDatabaseName)); + stmt.ExecuteDirect(std::format("USE {}", testDatabaseName)); break; case SqlServerType::POSTGRESQL: if (m_createdTables.empty()) m_createdTables = GetAllTableNames(); for (auto& createdTable: std::views::reverse(m_createdTables)) - (void) stmt.ExecuteDirect(std::format("DROP TABLE IF EXISTS \"{}\" CASCADE", createdTable)); + stmt.ExecuteDirect(std::format("DROP TABLE IF EXISTS \"{}\" CASCADE", createdTable)); break; default: for (auto& createdTable: std::views::reverse(m_createdTables)) - (void) stmt.ExecuteDirect(std::format("DROP TABLE IF EXISTS \"{}\"", createdTable)); + stmt.ExecuteDirect(std::format("DROP TABLE IF EXISTS \"{}\"", createdTable)); break; } m_createdTables.clear(); diff --git a/src/tools/ddl2cpp.cpp b/src/tools/ddl2cpp.cpp index a21fd4b0..fb62bd66 100644 --- a/src/tools/ddl2cpp.cpp +++ b/src/tools/ddl2cpp.cpp @@ -19,6 +19,7 @@ namespace constexpr auto finally(auto&& cleanupRoutine) noexcept { + // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) struct Finally { std::remove_cvref_t cleanup; @@ -70,6 +71,7 @@ std::string MakeType(SqlSchema::Column const& column) std::string MakeVariableName(SqlSchema::FullyQualifiedTableName const& table) { auto name = std::format("{}", table.table); + // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) name.at(0) = std::tolower(name.at(0)); return name; } @@ -95,11 +97,11 @@ std::string MakePluralVariableName(SqlSchema::FullyQualifiedTableName const& tab if (sqlName.back() == 'y' && sqlName.size() > 1 && !isVowel(sqlName.at(sqlName.size() - 2))) { auto name = std::format("{}ies", sqlName.substr(0, sqlName.size() - 1)); - name.at(0) = std::tolower(name.at(0)); + name.at(0) = static_cast(std::tolower(name.at(0))); return name; } auto name = std::format("{}s", sqlName); - name.at(0) = std::tolower(name.at(0)); + name.at(0) = static_cast(std::tolower(name.at(0))); return name; } @@ -128,6 +130,7 @@ class CxxModelPrinter return output.str(); } + // NOLINTNEXTLINE(readability-function-cognitive-complexity) void PrintTable(SqlSchema::Table const& table) { m_forwwardDeclarations.push_back(table.name); @@ -253,7 +256,7 @@ void PostConnectedHook(SqlConnection& connection) case SqlServerType::SQLITE: { auto stmt = SqlStatement { connection }; // Enable foreign key constraints for SQLite - (void) stmt.ExecuteDirect("PRAGMA foreign_keys = ON"); + stmt.ExecuteDirect("PRAGMA foreign_keys = ON"); break; } case SqlServerType::MICROSOFT_SQL: @@ -288,6 +291,7 @@ struct Configuration bool createTestTables = false; }; +// NOLINTNEXTLINE(readability-function-cognitive-complexity) std::variant ParseArguments(int argc, char const* argv[]) { using namespace std::string_view_literals; @@ -360,7 +364,7 @@ std::variant ParseArguments(int argc, char const* argv[]) if (i < argc) argv[i - 1] = argv[0]; - return { std::move(config) }; + return { config }; } int main(int argc, char const* argv[])