Commit 7623f76f authored by Miha Ravselj's avatar Miha Ravselj Committed by Vadim Zeitlin

More and better MSVC warnings fixes.

Fix warnings by changing the code whenever possible, in particular remove
firebird_rowid_backend class and src/backends/firebird/row-id.cpp file in
which it was defined entirely as it only resulted in "unreachable code"
warnings but was otherwise unused.

Also avoid implicit conversions by either using the correct types or by making
the casts explicit using static_cast<> in places where the casts are really
needed.

Add helpful SOCI_NOT_COPYABLE, SOCI_NOT_ASSIGNABLE and SOCI_UNUSED macros and
use them to suppress the corresponding warnings.

Finally, combine soci-config.h and soci-platform.h in a single file, always
include the latter as the first header and keep the former as a wrapper for
the latter just for compatibility.

Remove the MSVC "#pragma warning(disable: *)" as they are not needed any more.

Closes #355.
parent caa2370d
......@@ -40,7 +40,7 @@ void setTextParam(char const * s, std::size_t size, char * buf_,
std::string getTextParam(XSQLVAR const *var);
template <typename IntType>
const char *str2dec(const char * s, IntType &out, int &scale)
const char *str2dec(const char * s, IntType &out, short &scale)
{
int sign = 1;
if ('+' == *s)
......@@ -65,7 +65,7 @@ const char *str2dec(const char * s, IntType &out, int &scale)
int d = *s - '0';
if (d < 0 || d > 9)
return s;
res = res * 10 + d * sign;
res = res * 10 + static_cast<IntType>(d * sign);
if (1 == sign)
{
if (res < out)
......@@ -97,19 +97,30 @@ double round_for_isc(double value)
return value < 0 ? value - 0.5 : value + 0.5;
}
//helper template to generate proper code based on compile time type check
template<bool cond> struct cond_to_isc {};
template<> struct cond_to_isc<false>
{
static void checkInteger(short scale, short type)
{
if( scale >= 0 && (type == SQL_SHORT || type == SQL_LONG || type == SQL_INT64) )
throw soci_error("Can't convert non-integral value to integral column type");
}
};
template<> struct cond_to_isc<true>
{
static void checkInteger(short scale,short type) { SOCI_UNUSED(scale) SOCI_UNUSED(type) }
};
template<typename T1>
void to_isc(void * val, XSQLVAR * var, int x_scale = 0)
void to_isc(void * val, XSQLVAR * var, short x_scale = 0)
{
T1 value = *reinterpret_cast<T1*>(val);
short scale = var->sqlscale + x_scale;
short type = var->sqltype & ~1;
long long divisor = 1, multiplier = 1;
if ((std::numeric_limits<T1>::is_integer == false) && scale >= 0 &&
(type == SQL_SHORT || type == SQL_LONG || type == SQL_INT64))
{
throw soci_error("Can't convert non-integral value to integral column type");
}
cond_to_isc<std::numeric_limits<T1>::is_integer>::checkInteger(scale,type);
for (int i = 0; i > scale; --i)
multiplier *= 10;
......@@ -156,7 +167,7 @@ void to_isc(void * val, XSQLVAR * var, int x_scale = 0)
template<typename IntType, typename UIntType>
void parse_decimal(void * val, XSQLVAR * var, const char * s)
{
int scale;
short scale;
UIntType t1;
IntType t2;
if (!*str2dec(s, t1, scale))
......@@ -189,6 +200,22 @@ std::string format_decimal(const void *sqldata, int sqlscale)
return r + std::string(sqlscale, '0');
}
template<bool cond> struct cond_from_isc {};
template<> struct cond_from_isc<true> {
static void checkInteger(short scale)
{
std::ostringstream msg;
msg << "Can't convert value with scale " << -scale
<< " to integral type";
throw soci_error(msg.str());
}
};
template<> struct cond_from_isc<false>
{
static void checkInteger(short scale) { SOCI_UNUSED(scale) }
};
template<typename T1>
T1 from_isc(XSQLVAR * var)
{
......@@ -197,14 +224,7 @@ T1 from_isc(XSQLVAR * var)
if (scale < 0)
{
if (std::numeric_limits<T1>::is_integer)
{
std::ostringstream msg;
msg << "Can't convert value with scale " << -scale
<< " to integral type";
throw soci_error(msg.str());
}
cond_from_isc<std::numeric_limits<T1>::is_integer>::checkInteger(scale);
for (int i = 0; i > scale; --i)
{
tens *= 10;
......
......@@ -58,6 +58,8 @@ private:
use_type_vector &p;
Indicator &ind;
private:
SOCI_NOT_COPYABLE(use_sequence)
};
template <typename T>
......@@ -73,6 +75,8 @@ private:
}
use_type_vector &p;
private:
SOCI_NOT_COPYABLE(use_sequence)
};
template <typename T, typename Indicator>
......@@ -144,6 +148,8 @@ private:
into_type_vector &p;
Indicator &ind;
private:
SOCI_NOT_COPYABLE(into_sequence)
};
template <typename T>
......@@ -159,6 +165,8 @@ private:
}
into_type_vector &p;
private:
SOCI_NOT_COPYABLE(into_sequence)
};
template <typename T, typename Indicator>
......
......@@ -8,7 +8,7 @@
#ifndef SOCI_BLOB_H_INCLUDED
#define SOCI_BLOB_H_INCLUDED
#include "soci/soci-config.h"
#include "soci/soci-platform.h"
// std
#include <cstddef>
......
......@@ -8,7 +8,7 @@
#ifndef SOCI_CONNECTION_PARAMETERS_H_INCLUDED
#define SOCI_CONNECTION_PARAMETERS_H_INCLUDED
#include "soci/soci-config.h"
#include "soci/soci-platform.h"
#include <map>
#include <string>
......
......@@ -8,7 +8,7 @@
#ifndef SOCI_CONNECTION_POOL_H_INCLUDED
#define SOCI_CONNECTION_POOL_H_INCLUDED
#include "soci/soci-config.h"
#include "soci/soci-platform.h"
// std
#include <cstddef>
......
......@@ -201,7 +201,7 @@ struct SOCI_DB2_DECL db2_statement_backend : details::statement_backend
int prepare_for_describe();
void describe_column(int colNum, data_type& dtype, std::string& columnName);
std::size_t column_size(int col);
size_t column_size(int col);
db2_standard_into_type_backend* make_into_type_backend();
db2_standard_use_type_backend* make_use_type_backend();
......
......@@ -9,7 +9,7 @@
#ifndef SOCI_ERROR_H_INCLUDED
#define SOCI_ERROR_H_INCLUDED
#include "soci/soci-config.h"
#include "soci/soci-platform.h"
// std
#include <stdexcept>
#include <string>
......
......@@ -219,7 +219,7 @@ protected:
long long rowsAffectedBulk_; // number of rows affected by the last bulk operation
virtual void exchangeData(bool gotData, int row);
virtual void prepareSQLDA(XSQLDA ** sqldap, int size = 10);
virtual void prepareSQLDA(XSQLDA ** sqldap, short size = 10);
virtual void rewriteQuery(std::string const & query,
std::vector<char> & buffer);
virtual void rewriteParameters(std::string const & src,
......@@ -238,13 +238,6 @@ protected:
bool procedure_;
};
struct firebird_rowid_backend : details::rowid_backend
{
firebird_rowid_backend(firebird_session_backend &session);
~firebird_rowid_backend();
};
struct firebird_blob_backend : details::blob_backend
{
firebird_blob_backend(firebird_session_backend &session);
......@@ -314,7 +307,7 @@ struct firebird_session_backend : details::session_backend
void cleanUp();
virtual firebird_statement_backend * make_statement_backend();
virtual firebird_rowid_backend * make_rowid_backend();
virtual details::rowid_backend* make_rowid_backend();
virtual firebird_blob_backend * make_blob_backend();
bool get_option_decimals_as_strings() { return decimals_as_strings_; }
......
......@@ -32,6 +32,8 @@ struct into_container
T &t;
Indicator &ind;
private:
SOCI_NOT_ASSIGNABLE(into_container)
};
typedef void no_indicator;
......@@ -42,6 +44,8 @@ struct into_container<T, no_indicator>
: t(_t) {}
T &t;
private:
SOCI_NOT_ASSIGNABLE(into_container)
};
} // namespace details
......
......@@ -23,11 +23,11 @@
# define SOCI_ODBC_DECL
#endif
#include "soci/soci-platform.h"
#include <vector>
#include <soci/soci-backend.h>
#include <sstream>
#if defined(_MSC_VER) || defined(__MINGW32__)
#include "soci/soci-platform.h"
#include <windows.h>
#endif
#include <sqlext.h> // ODBC
......@@ -77,6 +77,8 @@ protected:
};
odbc_statement_backend &statement_;
private:
SOCI_NOT_COPYABLE(odbc_standard_type_backend_base)
};
struct odbc_standard_into_type_backend : details::standard_into_type_backend,
......@@ -101,6 +103,8 @@ struct odbc_standard_into_type_backend : details::standard_into_type_backend,
int position_;
SQLSMALLINT odbcType_;
SQLLEN valueLen_;
private:
SOCI_NOT_COPYABLE(odbc_standard_into_type_backend)
};
struct odbc_vector_into_type_backend : details::vector_into_type_backend,
......
......@@ -28,10 +28,6 @@
#include <libpq-fe.h>
#include <vector>
#ifdef _MSC_VER
#pragma warning(disable:4512 4511)
#endif
namespace soci
{
......@@ -114,9 +110,7 @@ private:
PGresult* result_;
// This class can't be copied as it owns result_ which can't be duplicated.
postgresql_result(postgresql_result const &);
postgresql_result& operator=(postgresql_result const &);
SOCI_NOT_COPYABLE(postgresql_result)
};
} // namespace details
......
......@@ -8,7 +8,7 @@
#ifndef SOCI_QUERY_TRANSFORMATION_H_INCLUDED
#define SOCI_QUERY_TRANSFORMATION_H_INCLUDED
#include "soci/soci-config.h"
#include "soci/soci-platform.h"
#include <functional>
#include <string>
......
......@@ -62,9 +62,7 @@ protected:
session & session_;
private:
// noncopyable
ref_counted_statement_base(ref_counted_statement_base const&);
ref_counted_statement_base& operator=(ref_counted_statement_base const&);
SOCI_NOT_COPYABLE(ref_counted_statement_base)
};
// this class is supposed to be a vehicle for the "once" statements
......
......@@ -62,6 +62,8 @@ private:
virtual void convert_from_base() {}
row & r_;
SOCI_NOT_COPYABLE(into_type)
};
template <>
......
......@@ -122,9 +122,7 @@ public:
}
private:
// copy not supported
row(row const &);
void operator=(row const &);
SOCI_NOT_COPYABLE(row)
std::size_t find_column(std::string const& name) const;
......
......@@ -8,7 +8,7 @@
#ifndef SOCI_ROWID_H_INCLUDED
#define SOCI_ROWID_H_INCLUDED
#include "soci/soci-config.h"
#include "soci/soci-platform.h"
namespace soci
{
......
......@@ -154,12 +154,7 @@ private:
const std::auto_ptr<statement> st_;
const std::auto_ptr<T> define_;
#endif
// Non-copyable
rowset_impl(rowset_impl const &);
rowset_impl & operator=(rowset_impl const &);
SOCI_NOT_COPYABLE(rowset_impl)
}; // class rowset_impl
} // namespace details
......
......@@ -132,8 +132,7 @@ public:
details::blob_backend * make_blob_backend();
private:
session(session const &);
session& operator=(session const &);
SOCI_NOT_COPYABLE(session)
std::ostringstream query_stream_;
details::query_transformation_function* query_transformation_;
......
......@@ -8,7 +8,7 @@
#ifndef SOCI_BACKEND_H_INCLUDED
#define SOCI_BACKEND_H_INCLUDED
#include "soci/soci-config.h"
#include "soci/soci-platform.h"
#include "soci/error.h"
// std
#include <cstddef>
......@@ -71,9 +71,7 @@ public:
virtual void clean_up() = 0;
private:
// noncopyable
standard_into_type_backend(standard_into_type_backend const&);
standard_into_type_backend& operator=(standard_into_type_backend const&);
SOCI_NOT_COPYABLE(standard_into_type_backend)
};
class vector_into_type_backend
......@@ -94,9 +92,7 @@ public:
virtual void clean_up() = 0;
private:
// noncopyable
vector_into_type_backend(vector_into_type_backend const&);
vector_into_type_backend& operator=(vector_into_type_backend const&);
SOCI_NOT_COPYABLE(vector_into_type_backend)
};
// polymorphic use type backend
......@@ -118,9 +114,7 @@ public:
virtual void clean_up() = 0;
private:
// noncopyable
standard_use_type_backend(standard_use_type_backend const&);
standard_use_type_backend& operator=(standard_use_type_backend const&);
SOCI_NOT_COPYABLE(standard_use_type_backend)
};
class vector_use_type_backend
......@@ -140,9 +134,7 @@ public:
virtual void clean_up() = 0;
private:
// noncopyable
vector_use_type_backend(vector_use_type_backend const&);
vector_use_type_backend& operator=(vector_use_type_backend const&);
SOCI_NOT_COPYABLE(vector_use_type_backend)
};
// polymorphic statement backend
......@@ -184,9 +176,7 @@ public:
virtual vector_use_type_backend* make_vector_use_type_backend() = 0;
private:
// noncopyable
statement_backend(statement_backend const&);
statement_backend& operator=(statement_backend const&);
SOCI_NOT_COPYABLE(statement_backend)
};
// polymorphic RowID backend
......@@ -214,9 +204,7 @@ public:
virtual void trim(std::size_t newLen) = 0;
private:
// noncopyable
blob_backend(blob_backend const&);
blob_backend& operator=(blob_backend const&);
SOCI_NOT_COPYABLE(blob_backend)
};
// polymorphic session backend
......@@ -253,9 +241,7 @@ public:
virtual blob_backend* make_blob_backend() = 0;
private:
// noncopyable
session_backend(session_backend const&);
session_backend& operator=(session_backend const&);
SOCI_NOT_COPYABLE(session_backend)
};
} // namespace details
......
//
// Copyright (C) 2006-2008 Mateusz Loskot
// Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef SOCI_CONFIG_H_INCLUDED
#define SOCI_CONFIG_H_INCLUDED
#ifndef SOCICONFIG_H_INCLUDED
#define SOCICONFIG_H_INCLUDED
//
// On Windows platform, define SOCI_DECL depending on
// static or dynamic (SOCI_DLL) linkage.
//
// For details, see
// http://www.boost.org/more/separate_compilation.html
//
#ifdef _WIN32
# ifdef SOCI_DLL
# ifdef SOCI_SOURCE
# define SOCI_DECL __declspec(dllexport)
# else
# define SOCI_DECL __declspec(dllimport)
# endif // SOCI_SOURCE
# endif // SOCI_DLL
#endif // _WIN32
//
// If SOCI_DECL isn't defined yet define it now
#ifndef SOCI_DECL
# define SOCI_DECL
#endif
#ifdef _MSC_VER
#pragma warning(disable:4251 4275)
#endif // _MSC_VER
#include "soci/soci-platform.h"
#endif // SOCI_CONFIG_H_INCLUDED
#endif // SOCICONFIG_H_INCLUDED
......@@ -8,6 +8,18 @@
#ifndef SOCI_PLATFORM_H_INCLUDED
#define SOCI_PLATFORM_H_INCLUDED
//disable MSVC deprecated warnings
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdarg.h>
#include <string.h>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
#if defined(_MSC_VER) || defined(__MINGW32__)
#define LL_FMT_FLAGS "I64"
#else
......@@ -18,6 +30,11 @@
#ifdef _MSC_VER
#include <stdlib.h>
//Disables warnings about STL objects need to have dll-interface and/or
//base class must have dll interface
#pragma warning(disable:4251 4275)
// Define if you have the vsnprintf variants.
#if _MSC_VER < 1500
# define vsnprintf _vsnprintf
......@@ -52,4 +69,31 @@ namespace std {
}
#endif
//define DLL import/export on WIN32
#ifdef _WIN32
# ifdef SOCI_DLL
# ifdef SOCI_SOURCE
# define SOCI_DECL __declspec(dllexport)
# else
# define SOCI_DECL __declspec(dllimport)
# endif // SOCI_SOURCE
# endif // SOCI_DLL
#endif // _WIN32
//
// If SOCI_DECL isn't defined yet define it now
#ifndef SOCI_DECL
# define SOCI_DECL
#endif
#define SOCI_NOT_ASSIGNABLE(classname) \
classname& operator=(const classname&);
#define SOCI_NOT_COPYABLE(classname) \
classname(const classname&); \
SOCI_NOT_ASSIGNABLE(classname)
#define SOCI_UNUSED(x) (void)x;
#endif // SOCI_PLATFORM_H_INCLUDED
......@@ -8,7 +8,7 @@
#ifndef SOCI_SIMPLE_H_INCLUDED
#define SOCI_SIMPLE_H_INCLUDED
#include "soci/soci-config.h"
#include "soci/soci-platform.h"
#ifdef __cplusplus
extern "C"
......
......@@ -8,11 +8,8 @@
#ifndef SOCI_H_INCLUDED
#define SOCI_H_INCLUDED
#ifdef _MSC_VER
#pragma warning(disable:4251 4512 4511)
#endif
// namespace soci
#include "soci/soci-platform.h"
#include "soci/backend-loader.h"
#include "soci/blob.h"
#include "soci/blob-exchange.h"
......@@ -33,8 +30,6 @@
#include "soci/rowset.h"
#include "soci/session.h"
#include "soci/soci-backend.h"
#include "soci/soci-config.h"
#include "soci/soci-platform.h"
#include "soci/statement.h"
#include "soci/transaction.h"
#include "soci/type-conversion.h"
......
......@@ -158,10 +158,7 @@ private:
soci::details::statement_backend * backEnd_;
// The type is noncopyable.
statement_impl(statement_impl const &);
statement_impl& operator=(statement_impl const &);
SOCI_NOT_COPYABLE(statement_impl)
};
} // namespace details
......
......@@ -8,8 +8,8 @@
#ifndef SOCI_TRANSACTION_H_INCLUDED
#define SOCI_TRANSACTION_H_INCLUDED
#include "soci/soci-platform.h"
#include "soci/session.h"
#include "soci/soci-config.h"
namespace soci
{
......@@ -28,9 +28,7 @@ private:
bool handled_;
session& sql_;
// Disable copying
transaction(transaction const& other);
transaction& operator=(transaction const& other);
SOCI_NOT_COPYABLE(transaction)
};
} // namespace soci
......
......@@ -73,6 +73,8 @@ private:
// in any case, ind_ refers to some valid indicator
// and can be used by conversion routines
indicator & ind_;
SOCI_NOT_COPYABLE(conversion_into_type)
};
// Automatically create use_type from a type_conversion
......@@ -161,6 +163,8 @@ private:
indicator & ind_;
bool readOnly_;
SOCI_NOT_COPYABLE(conversion_use_type)
};
// this class is used to ensure correct order of construction
......@@ -239,6 +243,8 @@ private:
// in any case, ind_ refers to some valid vector of indicators
// and can be used by conversion routines
std::vector<indicator> & ind_;
SOCI_NOT_COPYABLE(conversion_into_type)
};
......@@ -309,6 +315,8 @@ private:
// in any case, ind_ refers to some valid vector of indicators
// and can be used by conversion routines
std::vector<indicator> & ind_;
SOCI_NOT_COPYABLE(conversion_use_type)
};
template <typename T>
......
......@@ -8,6 +8,7 @@
#ifndef SOCI_USE_TYPE_H_INCLUDED
#define SOCI_USE_TYPE_H_INCLUDED
#include "soci/soci-platform.h"
#include "soci/soci-backend.h"
#include "soci/type-ptr.h"
#include "soci/exchange-traits.h"
......
......@@ -27,6 +27,8 @@ struct use_container
T &t;
Indicator &ind;
const std::string &name;
private:
SOCI_NOT_ASSIGNABLE(use_container)
};
typedef void no_indicator;
......@@ -38,6 +40,8 @@ struct use_container<T, no_indicator>
T &t;
const std::string &name;
private:
SOCI_NOT_ASSIGNABLE(use_container)
};
} // namespace details
......
......@@ -98,6 +98,8 @@ public:
private:
values & v_;
SOCI_NOT_COPYABLE(use_type)
};
// this is not supposed to be used - no support for bulk ORM
......@@ -127,6 +129,8 @@ public:
private:
values & v_;
SOCI_NOT_COPYABLE(into_type)
};
// this is not supposed to be used - no support for bulk ORM
......
......@@ -39,7 +39,7 @@ void db2_standard_into_type_backend::define_by_pos(
cType = SQL_C_CHAR;
// Patch: set to min between column size and 100MB (used ot be 32769)
// Column size for text data type can be too large for buffer allocation
size = statement_.column_size(this->position);
size = static_cast<SQLUINTEGER>(statement_.column_size(this->position));
size = size > details::db2::cli_max_buffer ? details::db2::cli_max_buffer : size;
size++;
buf = new char[size];
......
......@@ -6,6 +6,7 @@
// http://www.boost.org/LICENSE_1_0.txt)
#define SOCI_DB2_SOURCE
#include "soci/soci-platform.h"
#include "soci/db2/soci-db2.h"
#include "soci-exchange-cast.h"
#include <cctype>
......@@ -68,7 +69,7 @@ void *db2_standard_use_type_backend::prepare_for_bind(
std::string const& s = exchange_type_cast<x_stdstring>(data);
sqlType = SQL_LONGVARCHAR;
cType = SQL_C_CHAR;
size = s.size() + 1;
size = static_cast<SQLINTEGER>(s.size()) + 1;
buf = new char[size];
strncpy(buf, s.c_str(), size);
ind = SQL_NTS;
......
......@@ -10,14 +10,9 @@
#include "soci/db2/soci-db2.h"
#include <cctype>
#ifdef _MSC_VER