0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 04:47:18 +08:00

Added missing result_field.h

This commit is contained in:
rbock 2014-07-30 23:10:45 +02:00
parent f60f1504b8
commit ff9a6ff8f0
6 changed files with 103 additions and 6 deletions

View File

@ -214,10 +214,9 @@ namespace sqlpp
template<typename Db, typename FieldSpec> template<typename Db, typename FieldSpec>
inline std::ostream& operator<<(std::ostream& os, const result_field_t<detail::boolean, Db, FieldSpec>& e) inline std::ostream& operator<<(std::ostream& os, const result_field_t<detail::boolean, Db, FieldSpec>& e)
{ {
return os << e.value(); return serialize(e, os);
} }
using boolean = detail::boolean; using boolean = detail::boolean;
} }

View File

@ -269,7 +269,7 @@ namespace sqlpp
template<typename Db, typename FieldSpec> template<typename Db, typename FieldSpec>
inline std::ostream& operator<<(std::ostream& os, const result_field_t<detail::floating_point, Db, FieldSpec>& e) inline std::ostream& operator<<(std::ostream& os, const result_field_t<detail::floating_point, Db, FieldSpec>& e)
{ {
return os << e.value(); return serialize(e, os);
} }

View File

@ -281,7 +281,7 @@ namespace sqlpp
template<typename Db, typename FieldSpec> template<typename Db, typename FieldSpec>
inline std::ostream& operator<<(std::ostream& os, const result_field_t<detail::integral, Db, FieldSpec>& e) inline std::ostream& operator<<(std::ostream& os, const result_field_t<detail::integral, Db, FieldSpec>& e)
{ {
return os << e.value(); return serialize(e, os);
} }
using tinyint = detail::integral; using tinyint = detail::integral;

View File

@ -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 <sqlpp11/wrong.h>
#include <sqlpp11/result_field_methods.h>
namespace sqlpp
{
template<typename ValueType, typename Db, typename Field>
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<result_field_t<ValueType, Db, Field>>::value, "Missing specialization for result_field_t");
};
template<typename Context, typename ValueType, typename Db, typename FieldSpec>
struct serializer_t<Context, result_field_t<ValueType, Db, FieldSpec>>
{
using T = result_field_t<ValueType, Db, FieldSpec>;
static Context& _(const T& t, Context& context)
{
if (t.is_null() and not null_is_trivial_value_t<T>::value)
{
context << "NULL";
}
else
{
context << t.value();
}
return context;
}
};
}
#endif

View File

@ -47,7 +47,7 @@ namespace sqlpp
struct _parameter_t struct _parameter_t
{ {
using _value_type = integral; using _value_type = text;
_parameter_t(): _parameter_t():
_value(""), _value(""),
@ -217,10 +217,37 @@ namespace sqlpp
size_t _len; size_t _len;
}; };
template<typename Context, typename Db, typename FieldSpec>
struct serializer_t<Context, result_field_t<detail::text, Db, FieldSpec>>
{
using T = result_field_t<detail::text, Db, FieldSpec>;
static Context& _(const T& t, Context& context)
{
if (t.is_null() and not null_is_trivial_value_t<T>::value)
{
context << "NULL";
}
else
{
context << '\'' << context.escape(t.value()) << '\'';
}
return context;
}
};
template<typename Db, typename FieldSpec> template<typename Db, typename FieldSpec>
inline std::ostream& operator<<(std::ostream& os, const result_field_t<detail::text, Db, FieldSpec>& e) inline std::ostream& operator<<(std::ostream& os, const result_field_t<detail::text, Db, FieldSpec>& e)
{ {
return os << e.value(); if (e.is_null() and not null_is_trivial_value_t<FieldSpec>::value)
{
os << "NULL";
}
else
{
os << e.value();
}
return serialize(e, os);
} }
using text = detail::text; using text = detail::text;

View File

@ -156,6 +156,7 @@ int main()
serialize(s, printer).str(); serialize(s, printer).str();
} }
// distinct aggregate // distinct aggregate
serialize(count(sqlpp::distinct, t.alpha % 7), printer).str(); serialize(count(sqlpp::distinct, t.alpha % 7), printer).str();
serialize(avg(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(true), printer).str();
serialize(select(all_of(t)).from(t).where(false), 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; return 0;
} }