diff options
Diffstat (limited to 'libmysqlpp')
-rw-r--r-- | libmysqlpp/my-column.h | 2 | ||||
-rw-r--r-- | libmysqlpp/my-command.cpp | 64 | ||||
-rw-r--r-- | libmysqlpp/my-connection.cpp | 6 | ||||
-rw-r--r-- | libmysqlpp/my-selectcommand.cpp | 2 |
4 files changed, 26 insertions, 48 deletions
diff --git a/libmysqlpp/my-column.h b/libmysqlpp/my-column.h index be1d41d..2b59a49 100644 --- a/libmysqlpp/my-column.h +++ b/libmysqlpp/my-column.h @@ -10,7 +10,7 @@ namespace MySQL { public: ColumnBase(const char * name, unsigned int field); - bool isNull() const override; + [[nodiscard]] bool isNull() const override; protected: my_bool is_null; diff --git a/libmysqlpp/my-command.cpp b/libmysqlpp/my-command.cpp index fa1ebef..380d1f9 100644 --- a/libmysqlpp/my-command.cpp +++ b/libmysqlpp/my-command.cpp @@ -2,6 +2,7 @@ #include "my-connection.h" #include <cstdlib> #include <cstring> +#include <boost/numeric/conversion/cast.hpp> MySQL::Command::Command(const Connection * conn, const std::string & sql) : DB::Command(sql), @@ -45,84 +46,61 @@ MySQL::Command::realloc(void * buffer, size_t size) return newBuffer; } +template<typename B, enum_field_types b, typename P> void -MySQL::Command::bindParamI(unsigned int n, int v) +bindNumber(MYSQL_BIND & bind, const P & v) { - binds[n].buffer_type = MYSQL_TYPE_LONG; + bind.buffer_type = b; // NOLINTNEXTLINE(hicpp-no-malloc) - binds[n].buffer = realloc(binds[n].buffer, sizeof(int)); - *static_cast<int*>(binds[n].buffer) = v; - binds[n].is_unsigned = 0; + bind.buffer = realloc(bind.buffer, sizeof(B)); + *static_cast<B *>(bind.buffer) = boost::numeric_cast<B>(v); + bind.is_unsigned = std::is_unsigned<P>::value; +} + +void +MySQL::Command::bindParamI(unsigned int n, int v) +{ + bindNumber<int, MYSQL_TYPE_LONG>(binds[n], v); } void MySQL::Command::bindParamI(unsigned int n, long int v) { - binds[n].buffer_type = MYSQL_TYPE_LONGLONG; - // NOLINTNEXTLINE(hicpp-no-malloc) - binds[n].buffer = realloc(binds[n].buffer, sizeof(long long int)); - *static_cast<long long int*>(binds[n].buffer) = v; - binds[n].is_unsigned = 0; + bindNumber<long long int, MYSQL_TYPE_LONGLONG>(binds[n], v); } void MySQL::Command::bindParamI(unsigned int n, long long int v) { - binds[n].buffer_type = MYSQL_TYPE_LONGLONG; - // NOLINTNEXTLINE(hicpp-no-malloc) - binds[n].buffer = realloc(binds[n].buffer, sizeof(long long int)); - *static_cast<long long int*>(binds[n].buffer) = v; - binds[n].is_unsigned = 0; + bindNumber<long long int, MYSQL_TYPE_LONGLONG>(binds[n], v); } void MySQL::Command::bindParamI(unsigned int n, unsigned int v) { - binds[n].buffer_type = MYSQL_TYPE_LONG; - // NOLINTNEXTLINE(hicpp-no-malloc) - binds[n].buffer = realloc(binds[n].buffer, sizeof(int)); - *static_cast<int*>(binds[n].buffer) = v; - binds[n].is_unsigned = 1; + bindNumber<int, MYSQL_TYPE_LONG>(binds[n], v); } void MySQL::Command::bindParamI(unsigned int n, long unsigned int v) { - binds[n].buffer_type = MYSQL_TYPE_LONGLONG; - // NOLINTNEXTLINE(hicpp-no-malloc) - binds[n].buffer = realloc(binds[n].buffer, sizeof(long long int)); - *static_cast<long long int*>(binds[n].buffer) = v; - binds[n].is_unsigned = 1; + bindNumber<long long int, MYSQL_TYPE_LONGLONG>(binds[n], v); } void MySQL::Command::bindParamI(unsigned int n, long long unsigned int v) { - binds[n].buffer_type = MYSQL_TYPE_LONGLONG; - // NOLINTNEXTLINE(hicpp-no-malloc) - binds[n].buffer = realloc(binds[n].buffer, sizeof(long long int)); - *static_cast<long long int*>(binds[n].buffer) = v; - binds[n].is_unsigned = 1; + bindNumber<long long int, MYSQL_TYPE_LONGLONG>(binds[n], v); } void MySQL::Command::bindParamB(unsigned int n, bool v) { - binds[n].buffer_type = MYSQL_TYPE_TINY; - // NOLINTNEXTLINE(hicpp-no-malloc) - binds[n].buffer = realloc(binds[n].buffer, sizeof(bool)); - *static_cast<bool*>(binds[n].buffer) = (v ? 1 : 0); - binds[n].is_unsigned = 1; + bindNumber<char, MYSQL_TYPE_TINY>(binds[n], (v ? 1 : 0)); } void MySQL::Command::bindParamF(unsigned int n, double v) { - binds[n].buffer_type = MYSQL_TYPE_DOUBLE; - // NOLINTNEXTLINE(hicpp-no-malloc) - binds[n].buffer = realloc(binds[n].buffer, sizeof(double)); - *static_cast<double*>(binds[n].buffer) = v; + bindNumber<double, MYSQL_TYPE_DOUBLE>(binds[n], v); } void MySQL::Command::bindParamF(unsigned int n, float v) { - binds[n].buffer_type = MYSQL_TYPE_FLOAT; - // NOLINTNEXTLINE(hicpp-no-malloc) - binds[n].buffer = realloc(binds[n].buffer, sizeof(float)); - *static_cast<float*>(binds[n].buffer) = v; + bindNumber<float, MYSQL_TYPE_FLOAT>(binds[n], v); } void MySQL::Command::bindParamS(unsigned int n, const Glib::ustring & s) diff --git a/libmysqlpp/my-connection.cpp b/libmysqlpp/my-connection.cpp index 135ef34..e5193ac 100644 --- a/libmysqlpp/my-connection.cpp +++ b/libmysqlpp/my-connection.cpp @@ -167,7 +167,7 @@ namespace MySQL { return ctx->bufOff; } } - unsigned int copy = std::min(ctx->loadBufLen - ctx->bufOff, bufSize); + auto copy = std::min<size_t>(ctx->loadBufLen - ctx->bufOff, bufSize); memcpy(buf, ctx->loadBuf + ctx->bufOff, copy); ctx->bufOff += copy; return copy; @@ -186,8 +186,8 @@ namespace MySQL { } const char * loadBuf; - unsigned int loadBufLen; - int bufOff; + size_t loadBufLen; + size_t bufOff; MYSQL * conn; int loadReturn; }; diff --git a/libmysqlpp/my-selectcommand.cpp b/libmysqlpp/my-selectcommand.cpp index 893f79f..bb7ca10 100644 --- a/libmysqlpp/my-selectcommand.cpp +++ b/libmysqlpp/my-selectcommand.cpp @@ -24,7 +24,7 @@ MySQL::SelectCommand::execute() } MYSQL_RES * prepare_meta_result = mysql_stmt_result_metadata(stmt); MYSQL_FIELD * fieldDefs = mysql_fetch_fields(prepare_meta_result); - for (unsigned int i = 0; i < fields.size(); i += 1) { + for (std::size_t i = 0; i < fields.size(); i += 1) { switch (fieldDefs[i].type) { case MYSQL_TYPE_TINY: case MYSQL_TYPE_SHORT: |