From 099250945cc9743e91e606a9a0e7e23293aa01f0 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Fri, 3 Jan 2014 14:27:06 +0100 Subject: [PATCH] Successfully running the first prepared select statement including result evaluation --- include/sqlpp11/any.h | 2 + include/sqlpp11/avg.h | 2 + include/sqlpp11/count.h | 2 + include/sqlpp11/exists.h | 2 + include/sqlpp11/floating_point.h | 65 +++++++++++++++++++++---------- include/sqlpp11/integral.h | 16 ++++---- include/sqlpp11/max.h | 2 + include/sqlpp11/min.h | 2 + include/sqlpp11/prepared_select.h | 2 +- include/sqlpp11/result.h | 2 +- include/sqlpp11/result_row.h | 26 ++++++++++++- include/sqlpp11/select.h | 2 +- include/sqlpp11/some.h | 2 + include/sqlpp11/sum.h | 2 + 14 files changed, 94 insertions(+), 35 deletions(-) diff --git a/include/sqlpp11/any.h b/include/sqlpp11/any.h index 43b6f3c4..12393dff 100644 --- a/include/sqlpp11/any.h +++ b/include/sqlpp11/any.h @@ -52,6 +52,8 @@ namespace sqlpp struct _member_t { T any; + T& operator()() { return any; } + const T& operator()() const { return any; } }; }; diff --git a/include/sqlpp11/avg.h b/include/sqlpp11/avg.h index 01223923..b0782dd3 100644 --- a/include/sqlpp11/avg.h +++ b/include/sqlpp11/avg.h @@ -51,6 +51,8 @@ namespace sqlpp struct _member_t { T avg; + T& operator()() { return avg; } + const T& operator()() const { return avg; } }; }; diff --git a/include/sqlpp11/count.h b/include/sqlpp11/count.h index f1b4d187..28e331e4 100644 --- a/include/sqlpp11/count.h +++ b/include/sqlpp11/count.h @@ -51,6 +51,8 @@ namespace sqlpp struct _member_t { T count; + T& operator()() { return count; } + const T& operator()() const { return count; } }; }; diff --git a/include/sqlpp11/exists.h b/include/sqlpp11/exists.h index 83f96b44..0451ba36 100644 --- a/include/sqlpp11/exists.h +++ b/include/sqlpp11/exists.h @@ -51,6 +51,8 @@ namespace sqlpp struct _member_t { T exists; + T& operator()() { return exists; } + const T& operator()() const { return exists; } }; }; diff --git a/include/sqlpp11/floating_point.h b/include/sqlpp11/floating_point.h index ad6c3f29..484ec538 100644 --- a/include/sqlpp11/floating_point.h +++ b/include/sqlpp11/floating_point.h @@ -48,25 +48,31 @@ namespace sqlpp using _is_expression = std::true_type; using _cpp_value_type = double; - template struct _parameter_t { - using _value_type = floating_point; + using _value_type = integral; - _parameter_t(): + _parameter_t(const std::true_type&): + _trivial_value_is_null(true), _value(0), - _is_null(TrivialValueIsNull and _is_trivial()) + _is_null(_trivial_value_is_null and _is_trivial()) + {} + + _parameter_t(const std::false_type&): + _trivial_value_is_null(false), + _value(0), + _is_null(_trivial_value_is_null and _is_trivial()) {} _parameter_t(const _cpp_value_type& value): _value(value), - _is_null(TrivialValueIsNull and _is_trivial()) + _is_null(_trivial_value_is_null and _is_trivial()) {} _parameter_t& operator=(const _cpp_value_type& value) { _value = value; - _is_null = (TrivialValueIsNull and _is_trivial()); + _is_null = (_trivial_value_is_null and _is_trivial()); return *this; } @@ -90,22 +96,28 @@ namespace sqlpp return _is_null; } - _cpp_value_type value() const + const _cpp_value_type& value() const { return _value; } - operator _cpp_value_type() const { return value(); } + operator _cpp_value_type() const { return _value; } + + template + void bind(Target& target, size_t index) const + { + target.bind_floating_point_parameter(index, &_value, _is_null); + } private: + bool _trivial_value_is_null; _cpp_value_type _value; bool _is_null; }; - template struct _result_entry_t { - using _value_type = floating_point; + using _value_type = integral; _result_entry_t(): _is_valid(false), @@ -113,18 +125,24 @@ namespace sqlpp _value(0) {} - _result_entry_t(const char_result_row_t& row): - _is_valid(row.data != nullptr), - _is_null(row.data == nullptr or row.data[index] == nullptr), - _value(_is_null ? 0 : std::strtod(row.data[index], nullptr)) + _result_entry_t(const char* data, size_t): + _is_valid(true), + _is_null(data == nullptr), + _value(_is_null ? 0 : std::strtoll(data, nullptr, 10)) {} - _result_entry_t& operator=(const char_result_row_t& row) + void assign(const char* data, size_t) { - _is_valid = (row.data != nullptr); - _is_null = row.data == nullptr or row.data[index] == nullptr; - _value = _is_null ? 0 : std::strtod(row.data[index], nullptr); - return *this; + _is_valid = true; + _is_null = data == nullptr; + _value = _is_null ? 0 : std::strtoll(data, nullptr, 10); + } + + void invalidate() + { + _is_valid = false; + _is_null = true; + _value = 0; } template @@ -151,6 +169,12 @@ namespace sqlpp operator _cpp_value_type() const { return value(); } + template + void bind(Target& target, size_t i) + { + target.bind_floating_point_result(i, &_value, &_is_null); + } + private: bool _is_valid; bool _is_null; @@ -243,8 +267,7 @@ namespace sqlpp }; }; - template - std::ostream& operator<<(std::ostream& os, const floating_point::_result_entry_t& e) + inline std::ostream& operator<<(std::ostream& os, const floating_point::_result_entry_t& e) { return os << e.value(); } diff --git a/include/sqlpp11/integral.h b/include/sqlpp11/integral.h index c1208874..ac2e6b82 100644 --- a/include/sqlpp11/integral.h +++ b/include/sqlpp11/integral.h @@ -63,13 +63,7 @@ namespace sqlpp _is_null(_trivial_value_is_null and _is_trivial()) {} - _parameter_t(bool trivial_value_is_null): - _trivial_value_is_null(trivial_value_is_null), - _value(0), - _is_null(_trivial_value_is_null and _is_trivial()) - {} - - _parameter_t(const _cpp_value_type& value): + explicit _parameter_t(const _cpp_value_type& value): _value(value), _is_null(_trivial_value_is_null and _is_trivial()) {} @@ -81,11 +75,10 @@ namespace sqlpp return *this; } - _parameter_t& operator=(const std::nullptr_t&) + void set_null() { _value = 0; _is_null = true; - return *this; } template @@ -150,6 +143,11 @@ namespace sqlpp _value = 0; } + void validate() + { + _is_valid = true; + } + template void serialize(std::ostream& os, Db& db) const { diff --git a/include/sqlpp11/max.h b/include/sqlpp11/max.h index d3e32690..1310e674 100644 --- a/include/sqlpp11/max.h +++ b/include/sqlpp11/max.h @@ -51,6 +51,8 @@ namespace sqlpp struct _member_t { T max; + T& operator()() { return max; } + const T& operator()() const { return max; } }; }; diff --git a/include/sqlpp11/min.h b/include/sqlpp11/min.h index 3d1c76b4..2e0f6205 100644 --- a/include/sqlpp11/min.h +++ b/include/sqlpp11/min.h @@ -51,6 +51,8 @@ namespace sqlpp struct _member_t { T min; + T& operator()() { return min; } + const T& operator()() const { return min; } }; }; diff --git a/include/sqlpp11/prepared_select.h b/include/sqlpp11/prepared_select.h index 27696bf3..6553aa4e 100644 --- a/include/sqlpp11/prepared_select.h +++ b/include/sqlpp11/prepared_select.h @@ -43,7 +43,7 @@ namespace sqlpp auto run(Db& db) const -> result_t { - return {db.run_prepared_select(*this)}; + return {db.run_prepared_select(*this), _dynamic_names}; } void bind_params() const diff --git a/include/sqlpp11/result.h b/include/sqlpp11/result.h index 7f893ee6..f27f3937 100644 --- a/include/sqlpp11/result.h +++ b/include/sqlpp11/result.h @@ -90,7 +90,7 @@ namespace sqlpp void operator++() { - _result.next(result_row); + _result.next(_result_row); } db_result_t& _result; diff --git a/include/sqlpp11/result_row.h b/include/sqlpp11/result_row.h index 644c0869..15479324 100644 --- a/include/sqlpp11/result_row.h +++ b/include/sqlpp11/result_row.h @@ -65,6 +65,12 @@ namespace sqlpp return *this; } + void validate() + { + _field::operator()().validate(); + _rest::validate(); + } + void invalidate() { _field::operator()().invalidate(); @@ -97,14 +103,20 @@ namespace sqlpp result_row_impl& operator=(const char_result_row_t& char_result_row_t) { - _multi_field::operator=({char_result_row_t}); + _multi_field::operator()() = char_result_row_t; _rest::operator=(char_result_row_t); return *this; } + void validate() + { + _multi_field::operator()().validate(); + _rest::validate(); + } + void invalidate() { - _multi_field::invalidate(); + _multi_field::operator()().invalidate(); _rest::invalidate(); } @@ -130,6 +142,10 @@ namespace sqlpp return *this; } + void validate() + { + } + void invalidate() { } @@ -173,6 +189,12 @@ namespace sqlpp return *this; } + void validate() + { + _impl::validate(); + _is_valid = true; + } + void invalidate() { _impl::invalidate(); diff --git a/include/sqlpp11/select.h b/include/sqlpp11/select.h index df5f9732..df6d860f 100644 --- a/include/sqlpp11/select.h +++ b/include/sqlpp11/select.h @@ -594,7 +594,7 @@ namespace sqlpp // FIXME: Check for missing aliases (if references are used) // FIXME: Check for missing tables, well, actually, check for missing tables at the where(), order_by(), etc. - return {db.select(*this)}; + return {db.select(*this), get_dynamic_names()}; } template diff --git a/include/sqlpp11/some.h b/include/sqlpp11/some.h index 0a538822..f4c8bd40 100644 --- a/include/sqlpp11/some.h +++ b/include/sqlpp11/some.h @@ -52,6 +52,8 @@ namespace sqlpp struct _member_t { T some; + T& operator()() { return some; } + const T& operator()() const { return some; } }; }; diff --git a/include/sqlpp11/sum.h b/include/sqlpp11/sum.h index 1f9b3ef4..7d11201e 100644 --- a/include/sqlpp11/sum.h +++ b/include/sqlpp11/sum.h @@ -51,6 +51,8 @@ namespace sqlpp struct _member_t { T sum; + T& operator()() { return sum; } + const T& operator()() const { return sum; } }; };