diff --git a/include/soci/column-info.h b/include/soci/column-info.h index 2105628e7..647391d27 100644 --- a/include/soci/column-info.h +++ b/include/soci/column-info.h @@ -33,21 +33,22 @@ struct type_conversion static std::size_t get_numeric_value(const values & v, const std::string & field_name) { - data_type dt = v.get_properties(field_name).get_data_type(); + std::size_t pos = v.find_column(field_name); + data_type dt = v.get_properties(pos).get_data_type(); switch (dt) { case dt_double: return static_cast( - v.get(field_name, 0.0)); + v.get(pos, 0.0)); case dt_integer: return static_cast( - v.get(field_name, 0)); + v.get(pos, 0)); case dt_long_long: return static_cast( - v.get(field_name, 0ll)); + v.get(pos, 0ll)); case dt_unsigned_long_long: return static_cast( - v.get(field_name, 0ull)); + v.get(pos, 0ull)); break; default: return 0u; diff --git a/include/soci/row.h b/include/soci/row.h index 8cd816dc4..3cece8fac 100644 --- a/include/soci/row.h +++ b/include/soci/row.h @@ -46,6 +46,8 @@ class SOCI_DECL row row(row &&other) = default; row &operator=(row &&other) = default; + std::size_t find_column(std::string const& name) const; + void uppercase_column_names(bool forceToUpper); void add_properties(column_properties const& cp); std::size_t size() const; @@ -97,13 +99,7 @@ class SOCI_DECL row T get(std::string const &name, T const &nullValue) const { std::size_t const pos = find_column(name); - - if (i_null == *indicators_[pos]) - { - return nullValue; - } - - return get(pos); + return get(pos, nullValue); } template @@ -127,8 +123,6 @@ class SOCI_DECL row private: SOCI_NOT_COPYABLE(row) - std::size_t find_column(std::string const& name) const; - std::vector columns_; std::vector holders_; std::vector indicators_; diff --git a/include/soci/values.h b/include/soci/values.h index 8d782ad21..03d46a16b 100644 --- a/include/soci/values.h +++ b/include/soci/values.h @@ -51,8 +51,25 @@ class SOCI_DECL values values() : row_(NULL), currentPos_(0), uppercaseColumnNames_(false) {} + std::size_t find_column(std::string const& name) const + { + if (row_ != NULL) + return row_->find_column(name); + + const auto pos = index_.find(name); + if (pos != index_.end()) + { + return pos->second; + } + throw soci_error("Value named " + name + " not found."); + } + indicator get_indicator(std::size_t pos) const; - indicator get_indicator(std::string const & name) const; + indicator get_indicator(std::string const & name) const + { + std::size_t pos = find_column(name); + return get_indicator(pos); + } template T get(std::size_t pos) const @@ -82,28 +99,22 @@ class SOCI_DECL values { return row_->get(pos, nullValue); } - else if (*indicators_[pos] == i_null) - { - return nullValue; - } else { - return get_from_uses(pos); + return get_from_uses(pos, nullValue); } } template T get(std::string const & name) const { - return row_ != NULL ? row_->get(name) : get_from_uses(name); + return get(find_column(name)); } template T get(std::string const & name, T const & nullValue) const { - return row_ != NULL - ? row_->get(name, nullValue) - : get_from_uses(name, nullValue); + return get(find_column(name), nullValue); } template @@ -254,30 +265,14 @@ class SOCI_DECL values // without an underlying row object. In that case, get_from_uses() // returns the underlying field values template - T get_from_uses(std::string const & name, T const & nullValue) const + T get_from_uses(std::size_t pos, T const & nullValue) const { - std::map::const_iterator pos = index_.find(name); - if (pos != index_.end()) + if (*indicators_[pos] == i_null) { - if (*indicators_[pos->second] == i_null) - { - return nullValue; - } - - return get_from_uses(pos->second); + return nullValue; } - throw soci_error("Value named " + name + " not found."); - } - template - T get_from_uses(std::string const & name) const - { - std::map::const_iterator pos = index_.find(name); - if (pos != index_.end()) - { - return get_from_uses(pos->second); - } - throw soci_error("Value named " + name + " not found."); + return get_from_uses(pos); } template diff --git a/src/core/values.cpp b/src/core/values.cpp index 7fa9d1a7d..8474c99b3 100644 --- a/src/core/values.cpp +++ b/src/core/values.cpp @@ -29,25 +29,6 @@ indicator values::get_indicator(std::size_t pos) const } } -indicator values::get_indicator(std::string const& name) const -{ - if (row_) - { - return row_->get_indicator(name); - } - else - { - std::map::const_iterator it = index_.find(name); - if (it == index_.end()) - { - std::ostringstream msg; - msg << "Column '" << name << "' not found"; - throw soci_error(msg.str()); - } - return *indicators_[it->second]; - } -} - column_properties const& values::get_properties(std::size_t pos) const { if (row_)