From ff9a6ff8f08d8f267144dea83ae476f9ee98f94f Mon Sep 17 00:00:00 2001 From: rbock Date: Wed, 30 Jul 2014 23:10:45 +0200 Subject: [PATCH] Added missing result_field.h --- include/sqlpp11/boolean.h | 3 +- include/sqlpp11/floating_point.h | 2 +- include/sqlpp11/integral.h | 2 +- include/sqlpp11/result_field.h | 63 ++++++++++++++++++++++++++++++++ include/sqlpp11/text.h | 31 +++++++++++++++- tests/InterpretTest.cpp | 8 ++++ 6 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 include/sqlpp11/result_field.h diff --git a/include/sqlpp11/boolean.h b/include/sqlpp11/boolean.h index 626323cd..669b49bb 100644 --- a/include/sqlpp11/boolean.h +++ b/include/sqlpp11/boolean.h @@ -214,10 +214,9 @@ namespace sqlpp template inline std::ostream& operator<<(std::ostream& os, const result_field_t& e) { - return os << e.value(); + return serialize(e, os); } - using boolean = detail::boolean; } diff --git a/include/sqlpp11/floating_point.h b/include/sqlpp11/floating_point.h index 964b5cc1..683ae1f9 100644 --- a/include/sqlpp11/floating_point.h +++ b/include/sqlpp11/floating_point.h @@ -269,7 +269,7 @@ namespace sqlpp template inline std::ostream& operator<<(std::ostream& os, const result_field_t& e) { - return os << e.value(); + return serialize(e, os); } diff --git a/include/sqlpp11/integral.h b/include/sqlpp11/integral.h index 16695215..24b9ebe1 100644 --- a/include/sqlpp11/integral.h +++ b/include/sqlpp11/integral.h @@ -281,7 +281,7 @@ namespace sqlpp template inline std::ostream& operator<<(std::ostream& os, const result_field_t& e) { - return os << e.value(); + return serialize(e, os); } using tinyint = detail::integral; diff --git a/include/sqlpp11/result_field.h b/include/sqlpp11/result_field.h new file mode 100644 index 00000000..9fa41de9 --- /dev/null +++ b/include/sqlpp11/result_field.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2013-2014, Roland Bock + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SQLPP_RESULT_FIELD_H +#define SQLPP_RESULT_FIELD_H + +#include +#include + +namespace sqlpp +{ + template + struct result_field_t + { +#warning: Need to rewrite all uses of wrong like this to prevent them from being initialized at the wrong place... + static_assert(wrong_t>::value, "Missing specialization for result_field_t"); + }; + + template + struct serializer_t> + { + using T = result_field_t; + + static Context& _(const T& t, Context& context) + { + if (t.is_null() and not null_is_trivial_value_t::value) + { + context << "NULL"; + } + else + { + context << t.value(); + } + return context; + } + }; + + +} +#endif diff --git a/include/sqlpp11/text.h b/include/sqlpp11/text.h index 99e95e73..85201f33 100644 --- a/include/sqlpp11/text.h +++ b/include/sqlpp11/text.h @@ -47,7 +47,7 @@ namespace sqlpp struct _parameter_t { - using _value_type = integral; + using _value_type = text; _parameter_t(): _value(""), @@ -217,10 +217,37 @@ namespace sqlpp size_t _len; }; + template + struct serializer_t> + { + using T = result_field_t; + + static Context& _(const T& t, Context& context) + { + if (t.is_null() and not null_is_trivial_value_t::value) + { + context << "NULL"; + } + else + { + context << '\'' << context.escape(t.value()) << '\''; + } + return context; + } + }; + template inline std::ostream& operator<<(std::ostream& os, const result_field_t& e) { - return os << e.value(); + if (e.is_null() and not null_is_trivial_value_t::value) + { + os << "NULL"; + } + else + { + os << e.value(); + } + return serialize(e, os); } using text = detail::text; diff --git a/tests/InterpretTest.cpp b/tests/InterpretTest.cpp index 02f00dd5..ea62ace4 100644 --- a/tests/InterpretTest.cpp +++ b/tests/InterpretTest.cpp @@ -156,6 +156,7 @@ int main() serialize(s, printer).str(); } + // distinct aggregate serialize(count(sqlpp::distinct, t.alpha % 7), printer).str(); serialize(avg(sqlpp::distinct, t.alpha - 7), printer).str(); @@ -164,5 +165,12 @@ int main() serialize(select(all_of(t)).from(t).where(true), printer).str(); serialize(select(all_of(t)).from(t).where(false), printer).str(); + for (const auto& row : db(select(all_of(t)).from(t).where(true))) + { + serialize(row.alpha, printer); + serialize(row.beta, printer); + serialize(row.gamma, printer); + } + return 0; }