mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
prepared to detemplatify connector results
This commit is contained in:
parent
9ca174054d
commit
6e5ee56577
@ -30,7 +30,6 @@
|
||||
#include <cstdlib>
|
||||
#include <sqlpp11/detail/basic_operators.h>
|
||||
#include <sqlpp11/type_traits.h>
|
||||
#include <sqlpp11/raw_result_row.h>
|
||||
#include <sqlpp11/exception.h>
|
||||
|
||||
namespace sqlpp
|
||||
|
@ -24,17 +24,17 @@
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef SQLPP_RAW_RESULT_ROW_H
|
||||
#define SQLPP_RAW_RESULT_ROW_H
|
||||
#ifndef SQLPP_CHAR_RESULT_ROW_H
|
||||
#define SQLPP_CHAR_RESULT_ROW_H
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
struct raw_result_row_t
|
||||
struct char_result_row_t
|
||||
{
|
||||
char** data;
|
||||
size_t* len;
|
||||
|
||||
bool operator==(const raw_result_row_t& rhs) const
|
||||
bool operator==(const char_result_row_t& rhs) const
|
||||
{
|
||||
return data == rhs.data and len == rhs.len;
|
||||
}
|
@ -30,7 +30,7 @@
|
||||
#include <cstdlib>
|
||||
#include <sqlpp11/detail/basic_operators.h>
|
||||
#include <sqlpp11/type_traits.h>
|
||||
#include <sqlpp11/raw_result_row.h>
|
||||
#include <sqlpp11/char_result_row.h> // FIXME: Need to update floating_point
|
||||
#include <sqlpp11/exception.h>
|
||||
|
||||
namespace sqlpp
|
||||
@ -113,13 +113,13 @@ namespace sqlpp
|
||||
_value(0)
|
||||
{}
|
||||
|
||||
_result_entry_t(const raw_result_row_t& row):
|
||||
_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& operator=(const raw_result_row_t& row)
|
||||
_result_entry_t& operator=(const char_result_row_t& row)
|
||||
{
|
||||
_is_valid = (row.data != nullptr);
|
||||
_is_null = row.data == nullptr or row.data[index] == nullptr;
|
||||
|
@ -30,7 +30,6 @@
|
||||
#include <cstdlib>
|
||||
#include <sqlpp11/detail/basic_operators.h>
|
||||
#include <sqlpp11/type_traits.h>
|
||||
#include <sqlpp11/raw_result_row.h>
|
||||
#include <sqlpp11/exception.h>
|
||||
|
||||
namespace sqlpp
|
||||
|
@ -41,7 +41,7 @@ namespace sqlpp
|
||||
using _prepared_query_t = typename Db::_prepared_query_t;
|
||||
|
||||
auto run(Db& db) const
|
||||
-> result_t<decltype(db.run_prepared_select(*this))>
|
||||
-> result_t<decltype(db.run_prepared_select(*this)), _result_row_t>
|
||||
{
|
||||
return {db.run_prepared_select(*this)};
|
||||
}
|
||||
|
@ -27,31 +27,30 @@
|
||||
#ifndef SQLPP_RESULT_H
|
||||
#define SQLPP_RESULT_H
|
||||
|
||||
#include <sqlpp11/raw_result_row.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
template<typename DbResult>
|
||||
template<typename DbResult, typename ResultRow>
|
||||
class result_t
|
||||
{
|
||||
using db_result_t = DbResult;
|
||||
using result_row_t = typename db_result_t::result_row_t;
|
||||
using result_row_t = ResultRow;
|
||||
|
||||
db_result_t _db_result;
|
||||
db_result_t _result;
|
||||
result_row_t _result_row;
|
||||
db_result_t _end;
|
||||
result_row_t _end_row;
|
||||
|
||||
public:
|
||||
result_t():
|
||||
_db_result(),
|
||||
_end()
|
||||
{}
|
||||
result_t() = default;
|
||||
|
||||
result_t(db_result_t&& result):
|
||||
_db_result(std::move(result)),
|
||||
_end()
|
||||
template<typename DynamicNames>
|
||||
result_t(db_result_t&& result, const DynamicNames& dynamic_names):
|
||||
_result(std::move(result)),
|
||||
_result_row(dynamic_names)
|
||||
{
|
||||
_result.next(_result_row);
|
||||
}
|
||||
|
||||
result_t(const result_t&) = delete;
|
||||
@ -63,24 +62,25 @@ namespace sqlpp
|
||||
class iterator
|
||||
{
|
||||
public:
|
||||
iterator(db_result_t& result):
|
||||
_result(result)
|
||||
iterator(db_result_t& result, result_row_t& result_row):
|
||||
_result(result),
|
||||
_result_row(result_row)
|
||||
{
|
||||
}
|
||||
|
||||
const result_row_t& operator*() const
|
||||
{
|
||||
return _result.front();
|
||||
return _result_row;
|
||||
}
|
||||
|
||||
const result_row_t* operator->() const
|
||||
{
|
||||
return &_result.front();
|
||||
return &_result_row;
|
||||
}
|
||||
|
||||
bool operator==(const iterator& rhs) const
|
||||
{
|
||||
return _result.front() == rhs._result.front();
|
||||
return _result_row == rhs._result_row;
|
||||
}
|
||||
|
||||
bool operator!=(const iterator& rhs) const
|
||||
@ -90,35 +90,36 @@ namespace sqlpp
|
||||
|
||||
void operator++()
|
||||
{
|
||||
_result.pop_front();
|
||||
_result.next(result_row);
|
||||
}
|
||||
|
||||
db_result_t& _result;
|
||||
result_row_t& _result_row;
|
||||
};
|
||||
|
||||
iterator begin()
|
||||
{
|
||||
return iterator(_db_result);
|
||||
return iterator(_result, _result_row);
|
||||
}
|
||||
|
||||
iterator end()
|
||||
{
|
||||
return iterator(_end);
|
||||
return iterator(_end, _end_row);
|
||||
}
|
||||
|
||||
const result_row_t& front() const
|
||||
{
|
||||
return _db_result.front();
|
||||
return _result_row;
|
||||
}
|
||||
|
||||
bool empty() const
|
||||
{
|
||||
return _db_result.front() == _end.front();
|
||||
return _result_row == _end_row;
|
||||
}
|
||||
|
||||
void pop_front()
|
||||
{
|
||||
_db_result.pop_front();
|
||||
_result.next(_result_row);
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -27,7 +27,7 @@
|
||||
#ifndef SQLPP_RESULT_ROW_H
|
||||
#define SQLPP_RESULT_ROW_H
|
||||
|
||||
#include <sqlpp11/raw_result_row.h>
|
||||
#include <sqlpp11/char_result_row.h>
|
||||
#include <sqlpp11/field.h>
|
||||
#include <sqlpp11/text.h>
|
||||
#include <iostream>
|
||||
@ -52,16 +52,16 @@ namespace sqlpp
|
||||
static constexpr size_t _last_index = _rest::_last_index;
|
||||
|
||||
result_row_impl() = default;
|
||||
result_row_impl(const raw_result_row_t& raw_result_row):
|
||||
_field({{raw_result_row.data[index], raw_result_row.len[index]}}),
|
||||
_rest(raw_result_row)
|
||||
result_row_impl(const char_result_row_t& char_result_row_t):
|
||||
_field({{char_result_row_t.data[index], char_result_row_t.len[index]}}),
|
||||
_rest(char_result_row_t)
|
||||
{
|
||||
}
|
||||
|
||||
result_row_impl& operator=(const raw_result_row_t& raw_result_row)
|
||||
result_row_impl& operator=(const char_result_row_t& char_result_row_t)
|
||||
{
|
||||
_field::operator()().assign(raw_result_row.data[index], raw_result_row.len[index]);
|
||||
_rest::operator=(raw_result_row);
|
||||
_field::operator()().assign(char_result_row_t.data[index], char_result_row_t.len[index]);
|
||||
_rest::operator=(char_result_row_t);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -90,15 +90,15 @@ namespace sqlpp
|
||||
static constexpr size_t _last_index = _rest::_last_index;
|
||||
|
||||
result_row_impl() = default;
|
||||
result_row_impl(const raw_result_row_t& raw_result_row):
|
||||
_multi_field({raw_result_row}),
|
||||
_rest(raw_result_row)
|
||||
result_row_impl(const char_result_row_t& char_result_row_t):
|
||||
_multi_field({char_result_row_t}),
|
||||
_rest(char_result_row_t)
|
||||
{}
|
||||
|
||||
result_row_impl& operator=(const raw_result_row_t& raw_result_row)
|
||||
result_row_impl& operator=(const char_result_row_t& char_result_row_t)
|
||||
{
|
||||
_multi_field::operator=({raw_result_row});
|
||||
_rest::operator=(raw_result_row);
|
||||
_multi_field::operator=({char_result_row_t});
|
||||
_rest::operator=(char_result_row_t);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -121,11 +121,11 @@ namespace sqlpp
|
||||
{
|
||||
static constexpr size_t _last_index = index;
|
||||
result_row_impl() = default;
|
||||
result_row_impl(const raw_result_row_t& raw_result_row)
|
||||
result_row_impl(const char_result_row_t& char_result_row_t)
|
||||
{
|
||||
}
|
||||
|
||||
result_row_impl& operator=(const raw_result_row_t& raw_result_row)
|
||||
result_row_impl& operator=(const char_result_row_t& char_result_row_t)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
@ -154,10 +154,10 @@ namespace sqlpp
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
result_row_t(const raw_result_row_t& raw_result_row, const T&):
|
||||
_impl(raw_result_row),
|
||||
_is_valid(true)
|
||||
template<typename DynamicNames>
|
||||
result_row_t(const DynamicNames&):
|
||||
_impl(),
|
||||
_is_valid(false)
|
||||
{
|
||||
}
|
||||
|
||||
@ -166,9 +166,9 @@ namespace sqlpp
|
||||
result_row_t& operator=(const result_row_t&) = delete;
|
||||
result_row_t& operator=(result_row_t&&) = default;
|
||||
|
||||
result_row_t& operator=(const raw_result_row_t& raw_result_row)
|
||||
result_row_t& operator=(const char_result_row_t& char_result_row_t)
|
||||
{
|
||||
_impl::operator=(raw_result_row);
|
||||
_impl::operator=(char_result_row_t);
|
||||
_is_valid = true;
|
||||
return *this;
|
||||
}
|
||||
@ -218,20 +218,11 @@ namespace sqlpp
|
||||
{
|
||||
}
|
||||
|
||||
dynamic_result_row_t(const raw_result_row_t& raw_result_row, std::vector<std::string> dynamic_columns):
|
||||
_impl(raw_result_row),
|
||||
_is_valid(true),
|
||||
dynamic_result_row_t(const std::vector<std::string>& dynamic_columns):
|
||||
_impl(),
|
||||
_is_valid(false),
|
||||
_dynamic_columns(dynamic_columns)
|
||||
{
|
||||
raw_result_row_t dynamic_row = raw_result_row;
|
||||
dynamic_row.data += _last_static_index;
|
||||
dynamic_row.len += _last_static_index;
|
||||
for (const auto& column : _dynamic_columns)
|
||||
{
|
||||
_dynamic_fields.insert(std::make_pair(column, _field_type(dynamic_row.data[0], dynamic_row.len[0])));
|
||||
++dynamic_row.data;
|
||||
++dynamic_row.len;
|
||||
}
|
||||
}
|
||||
|
||||
dynamic_result_row_t(const dynamic_result_row_t&) = delete;
|
||||
@ -239,12 +230,12 @@ namespace sqlpp
|
||||
dynamic_result_row_t& operator=(const dynamic_result_row_t&) = delete;
|
||||
dynamic_result_row_t& operator=(dynamic_result_row_t&&) = default;
|
||||
|
||||
dynamic_result_row_t& operator=(const raw_result_row_t& raw_result_row)
|
||||
dynamic_result_row_t& operator=(const char_result_row_t& char_result_row)
|
||||
{
|
||||
_impl::operator=(raw_result_row);
|
||||
_impl::operator=(char_result_row);
|
||||
_is_valid = true;
|
||||
|
||||
raw_result_row_t dynamic_row = raw_result_row;
|
||||
char_result_row_t dynamic_row = char_result_row;
|
||||
|
||||
dynamic_row.data += _last_static_index;
|
||||
dynamic_row.len += _last_static_index;
|
||||
|
@ -586,7 +586,7 @@ namespace sqlpp
|
||||
// Execute
|
||||
template<typename Db>
|
||||
auto run(Db& db) const
|
||||
-> result_t<decltype(db.select(*this))>
|
||||
-> result_t<decltype(db.select(*this)), _result_row_t>
|
||||
{
|
||||
static_assert(not is_noop<ExpressionList>::value, "cannot run select without having selected anything");
|
||||
static_assert(is_from_t<From>::value, "cannot run select without a from()");
|
||||
|
@ -30,7 +30,6 @@
|
||||
#include <cstdlib>
|
||||
#include <sqlpp11/detail/basic_operators.h>
|
||||
#include <sqlpp11/type_traits.h>
|
||||
#include <sqlpp11/raw_result_row.h>
|
||||
#include <sqlpp11/exception.h>
|
||||
#include <sqlpp11/concat.h>
|
||||
#include <sqlpp11/like.h>
|
||||
|
Loading…
Reference in New Issue
Block a user