Class Field

Synopsis

#include <orm_lib/inc/drogon/orm/Field.h>

class Field

Description

Reference to a field in a result set.

A field represents one entry in a row. It represents an actual value in the result set, and can be converted to various types.

Mentioned in

Inheritance

Decsendents: ConstRowIterator

Methods

Field
asConvert to a type T value.
asArray
c_strRead as plain C string.
getArrayParserParse the field as an SQL array.
isNullIs this field's value null?
lengthGet the length of the plain C string.
nameColumn name.

Source

Lines 42-160 in orm_lib/inc/drogon/orm/Field.h.

class Field
{
  public:
    using SizeType = unsigned long;

    /// Column name
    const char *name() const;

    /// Is this field's value null?
    bool isNull() const;

    /// Read as plain C string
    /**
     * Since the field's data is stored internally in the form of a
     * zero-terminated C string, this is the fastest way to read it.  Use the
     * to() or as() functions to convert the string to other types such as
     * @c int, or to C++ strings.
     */
    const char *c_str() const;

    /// Get the length of the plain C string
    size_t length() const
    {
        return result_.getLength(row_, column_);
    }

    /// Convert to a type T value
    template <typename T>
    T as() const
    {
        if (isNull())
            return T();
        auto data_ = result_.getValue(row_, column_);
        // auto dataLength_ = result_.getLength(row_, column_);
        // For binary format!
        // if (dataLength_ == 1)
        // {
        //     return *data_;
        // }
        // else if (dataLength_ == 4)
        // {
        //     const int32_t *n = (int32_t *)data_;
        //     return ntohl(*n);
        // }
        // else if (dataLength_ == 8)
        // {
        //     const int64_t *n = (int64_t *)data_;
        //     return ntohll(*n);
        // }
        // return 0;
        T value = T();
        if (data_)
        {
            try
            {
                std::stringstream ss(data_);
                ss >> value;
            }
            catch (...)
            {
                LOG_DEBUG << "Type error";
            }
        }
        return value;
    }

    /// Parse the field as an SQL array.
    /**
     * Call the parser to retrieve values (and structure) from the array.
     *
     * Make sure the @c result object stays alive until parsing is finished.  If
     * you keep the @c row of @c field object alive, it will keep the @c result
     * object alive as well.
     */
    ArrayParser getArrayParser() const
    {
        return ArrayParser(result_.getValue(row_, column_));
    }
    template <typename T>
    std::vector<std::shared_ptr<T>> asArray() const
    {
        std::vector<std::shared_ptr<T>> ret;
        auto arrParser = getArrayParser();
        while (1)
        {
            auto arrVal = arrParser.getNext();
            if (arrVal.first == ArrayParser::juncture::done)
            {
                break;
            }
            if (arrVal.first == ArrayParser::juncture::string_value)
            {
                T val;
                std::stringstream ss(std::move(arrVal.second));
                ss >> val;
                ret.push_back(std::shared_ptr<T>(new T(val)));
            }
            else if (arrVal.first == ArrayParser::juncture::null_value)
            {
                ret.push_back(std::shared_ptr<T>());
            }
        }
        return ret;
    }

  protected:
    Result::SizeType row_;
    /**
     * Column number
     * You'd expect this to be a size_t, but due to the way reverse iterators
     * are related to regular iterators, it must be allowed to underflow to -1.
     */
    long column_;
    friend class Row;
    Field(const Row &row, Row::SizeType columnNum) noexcept;

  private:
    const Result result_;
};





Add Discussion as Guest

Log in to DocsForge