Merge pull request #187 from an-tao/dev

Optimize the Field class
This commit is contained in:
An Tao 2019-07-08 17:27:16 +08:00 committed by GitHub
commit 64f9fc9acc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 1 deletions

View File

@ -196,7 +196,17 @@ template <>
std::vector<char> Field::as<std::vector<char>>() const; std::vector<char> Field::as<std::vector<char>>() const;
template <> template <>
string_view Field::as<string_view>() const; string_view Field::as<string_view>() const;
// template <> template <>
int Field::as<int>() const;
template <>
long Field::as<long>() const;
template <>
long long Field::as<long long>() const;
template <>
float Field::as<float>() const;
template <>
double Field::as<double>() const;
// std::vector<int32_t> Field::as<std::vector<int32_t>>() const; // std::vector<int32_t> Field::as<std::vector<int32_t>>() const;
// template <> // template <>
// std::vector<int64_t> Field::as<std::vector<int64_t>>() const; // std::vector<int64_t> Field::as<std::vector<int64_t>>() const;

View File

@ -15,6 +15,7 @@
#include <drogon/orm/Field.h> #include <drogon/orm/Field.h>
#include <drogon/utils/Utilities.h> #include <drogon/utils/Utilities.h>
#include <trantor/utils/Logger.h> #include <trantor/utils/Logger.h>
#include <stdlib.h>
using namespace drogon::orm; using namespace drogon::orm;
Field::Field(const Row &row, Row::size_type columnNum) noexcept Field::Field(const Row &row, Row::size_type columnNum) noexcept
@ -90,6 +91,37 @@ string_view Field::as<string_view>() const
auto length = _result.getLength(_row, _column); auto length = _result.getLength(_row, _column);
return string_view(first, length); return string_view(first, length);
} }
template <>
int Field::as<int>() const
{
return atoi(_result.getValue(_row, _column));
}
template <>
long Field::as<long>() const
{
return atol(_result.getValue(_row, _column));
}
template <>
long long Field::as<long long>() const
{
return atoll(_result.getValue(_row, _column));
}
template <>
float Field::as<float>() const
{
return atof(_result.getValue(_row, _column));
}
template <>
double Field::as<double>() const
{
return std::stod(_result.getValue(_row, _column));
}
const char *Field::c_str() const const char *Field::c_str() const
{ {
return as<const char *>(); return as<const char *>();