Added for_each_field function

This commit is contained in:
rbock
2016-08-17 09:51:24 +02:00
parent 471fb7bf14
commit 57797ffffa
3 changed files with 119 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015, Roland Bock
* Copyright (c) 2013-2016, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
@@ -27,13 +27,13 @@
#ifndef SQLPP_RESULT_FIELD_BASE_H
#define SQLPP_RESULT_FIELD_BASE_H
#include <utility>
#include <sqlpp11/basic_expression_operators.h>
#include <sqlpp11/alias_operators.h>
#include <sqlpp11/result_field.h>
#include <sqlpp11/exception.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/bad_statement.h>
#include <sqlpp11/basic_expression_operators.h>
#include <sqlpp11/exception.h>
#include <sqlpp11/result_field.h>
#include <sqlpp11/type_traits.h>
#include <utility>
namespace sqlpp
{
@@ -42,6 +42,7 @@ namespace sqlpp
{
using _db_t = Db;
using _field_spec_t = FieldSpec;
using _alias_t = typename FieldSpec::_alias_t;
using _cpp_value_type = typename value_type_of<FieldSpec>::_cpp_value_type;
using _cpp_storage_type = StorageType;
@@ -116,9 +117,8 @@ namespace sqlpp
return _value;
}
operator typename std::conditional<_null_is_trivial or (not _can_be_null::value),
_cpp_value_type,
bad_statement>::type() const
operator typename std::conditional<_null_is_trivial or (not _can_be_null::value), _cpp_value_type, bad_statement>::
type() const
{
return value();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015, Roland Bock
* Copyright (c) 2013-2016, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
@@ -28,12 +28,12 @@
#define SQLPP_RESULT_ROW_H
#include <map>
#include <sqlpp11/result_row_fwd.h>
#include <sqlpp11/data_types/text.h>
#include <sqlpp11/detail/field_index_sequence.h>
#include <sqlpp11/dynamic_select_column_list.h>
#include <sqlpp11/field_spec.h>
#include <sqlpp11/no_name.h>
#include <sqlpp11/data_types/text.h>
#include <sqlpp11/detail/field_index_sequence.h>
#include <sqlpp11/result_row_fwd.h>
namespace sqlpp
{
@@ -70,6 +70,18 @@ namespace sqlpp
{
_field::operator()()._post_bind(target, index);
}
template <typename Callable>
void _apply(Callable& callable) const
{
callable(_field::operator()());
}
template <typename Callable>
void _apply(const Callable& callable) const
{
callable(_field::operator()());
}
};
template <std::size_t index, typename AliasProvider, typename Db, typename... FieldSpecs>
@@ -104,6 +116,18 @@ namespace sqlpp
{
_multi_field::operator()()._post_bind(target);
}
template <typename Callable>
void _apply(Callable& callable) const
{
_multi_field::operator()()._apply(callable);
}
template <typename Callable>
void _apply(const Callable& callable) const
{
_multi_field::operator()()._apply(callable);
}
};
template <typename Db, std::size_t NextIndex, std::size_t... Is, typename... FieldSpecs>
@@ -137,6 +161,20 @@ namespace sqlpp
using swallow = int[];
(void)swallow{(result_field<Db, Is, FieldSpecs>::_post_bind(target), 0)...};
}
template <typename Callable>
void _apply(Callable& callable) const
{
using swallow = int[];
(void)swallow{(result_field<Db, Is, FieldSpecs>::_apply(callable), 0)...};
}
template <typename Callable>
void _apply(const Callable& callable) const
{
using swallow = int[];
(void)swallow{(result_field<Db, Is, FieldSpecs>::_apply(callable), 0)...};
}
};
}
@@ -199,6 +237,18 @@ namespace sqlpp
{
_impl::_post_bind(target);
}
template <typename Callable>
void _apply(Callable& callable) const
{
_impl::_apply(callable);
}
template <typename Callable>
void _apply(const Callable& callable) const
{
_impl::_apply(callable);
}
};
template <typename Db, typename... FieldSpecs>
@@ -291,6 +341,32 @@ namespace sqlpp
++index;
}
}
template <typename Callable>
void _apply(Callable& callable) const
{
_impl::_apply(callable);
std::size_t index = _field_index_sequence::_next_index;
for (const auto& field_name : _dynamic_field_names)
{
_dynamic_fields.at(field_name)._apply(callable);
++index;
}
}
template <typename Callable>
void _apply(const Callable& callable) const
{
_impl::_apply(callable);
std::size_t index = _field_index_sequence::_next_index;
for (const auto& field_name : _dynamic_field_names)
{
_dynamic_fields.at(field_name)._apply(callable);
++index;
}
}
};
template <typename T>
@@ -307,6 +383,18 @@ namespace sqlpp
template <typename T>
using is_static_result_row_t = typename is_static_result_row_impl<T>::type;
template <typename Row, typename Callable>
void for_each_field(const Row& row, const Callable& callable)
{
row._apply(callable);
}
template <typename Row, typename Callable>
void for_each_field(const Row& row, Callable& callable)
{
row._apply(callable);
}
}
#endif

View File

@@ -23,14 +23,14 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include "Sample.h"
#include "MockDb.h"
#include "Sample.h"
#include "is_regular.h"
#include <iostream>
#include <sqlpp11/alias_provider.h>
#include <sqlpp11/select.h>
#include <sqlpp11/functions.h>
#include <sqlpp11/connection.h>
#include <sqlpp11/functions.h>
#include <sqlpp11/select.h>
#include <sqlpp11/without_table_check.h>
template <typename Db, typename Column>
@@ -43,6 +43,15 @@ int64_t getColumn(Db&& db, const Column& column)
return 0;
}
struct to_cerr
{
template <typename Field>
auto operator()(const Field& field) const -> void
{
std::cerr << get_sql_name(field) << " = " << field << std::endl;
}
};
int Select(int, char* [])
{
MockDb db = {};
@@ -168,5 +177,10 @@ int Select(int, char* [])
std::cerr << row.beta << std::endl;
}
for (const auto& row : db(select(all_of(t)).from(t).unconditionally()))
{
for_each_field(row, to_cerr{});
}
return 0;
}