2014-04-08 06:51:53 +00:00
|
|
|
|
#pragma once
|
2011-03-03 19:57:34 +00:00
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <time.h>
|
2014-02-21 01:57:01 +00:00
|
|
|
|
#include <math.h>
|
2011-03-03 19:57:34 +00:00
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
2018-02-16 14:51:14 +00:00
|
|
|
|
#include <common/preciseExp10.h>
|
2020-03-19 10:38:34 +00:00
|
|
|
|
#include <common/types.h>
|
2015-09-29 19:19:54 +00:00
|
|
|
|
#include <common/DateLUT.h>
|
2011-03-03 19:57:34 +00:00
|
|
|
|
|
|
|
|
|
#include <mysqlxx/Types.h>
|
2017-01-16 19:47:11 +00:00
|
|
|
|
#include <common/LocalDateTime.h>
|
|
|
|
|
|
2011-03-03 19:57:34 +00:00
|
|
|
|
|
2011-03-18 20:26:54 +00:00
|
|
|
|
namespace mysqlxx
|
2011-03-03 19:57:34 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2011-03-18 20:26:54 +00:00
|
|
|
|
class ResultBase;
|
|
|
|
|
|
|
|
|
|
|
2020-03-05 19:55:39 +00:00
|
|
|
|
/** Represents a single value read from MySQL.
|
|
|
|
|
* It doesn't owns the value. It's just a wrapper of a pair (const char *, size_t).
|
|
|
|
|
* If the UseQueryResult/StoreQueryResult or Connection is destroyed,
|
|
|
|
|
* or you have read the next Row while using UseQueryResult, then the object is invalidated.
|
|
|
|
|
* Allows to transform (parse) the value to various data types:
|
|
|
|
|
* - with getUInt(), getString(), ... (recommended);
|
|
|
|
|
* - with template function get<Type>() that is specialized for multiple data types;
|
|
|
|
|
* - the template function get<Type> also works for all types that can be constructed from Value
|
|
|
|
|
* (it is an extension point);
|
|
|
|
|
* - with operator Type() - this is done for compatibility and not recommended because ambiguities possible.
|
2011-03-18 20:26:54 +00:00
|
|
|
|
*
|
2020-03-05 19:55:39 +00:00
|
|
|
|
* On parsing error, exception is thrown.
|
|
|
|
|
* When trying to extract a value that is nullptr, exception is thrown
|
|
|
|
|
* - use isNull() method to check.
|
2011-03-18 20:26:54 +00:00
|
|
|
|
*
|
2020-03-05 19:55:39 +00:00
|
|
|
|
* As time_t is just an alias for integer data type
|
|
|
|
|
* to allow to write row[0].get<time_t>(), and expect that the values like '2011-01-01 00:00:00'
|
|
|
|
|
* will be successfully parsed according to the current time zone,
|
|
|
|
|
* the getUInt method and the corresponding get<>() methods
|
|
|
|
|
* are capable of parsing Date and DateTime.
|
2011-03-18 20:26:54 +00:00
|
|
|
|
*/
|
2014-05-19 22:05:33 +00:00
|
|
|
|
class Value
|
2011-03-03 19:57:34 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
/** Параметр res_ используется только для генерации подробной информации в исключениях.
|
|
|
|
|
* Можно передать NULL - тогда подробной информации в исключениях не будет.
|
|
|
|
|
*/
|
|
|
|
|
Value(const char * data_, size_t length_, const ResultBase * res_) : m_data(data_), m_length(length_), res(res_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить значение bool.
|
|
|
|
|
bool getBool() const
|
|
|
|
|
{
|
|
|
|
|
if (unlikely(isNull()))
|
|
|
|
|
throwException("Value is NULL");
|
|
|
|
|
|
|
|
|
|
return m_length > 0 && m_data[0] != '0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить беззнаковое целое.
|
|
|
|
|
UInt64 getUInt() const
|
|
|
|
|
{
|
|
|
|
|
if (unlikely(isNull()))
|
|
|
|
|
throwException("Value is NULL");
|
|
|
|
|
|
2019-01-04 14:18:49 +00:00
|
|
|
|
return readUIntText(m_data, m_length);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить целое со знаком или дату или дату-время (в unix timestamp согласно текущей тайм-зоне).
|
|
|
|
|
Int64 getInt() const
|
|
|
|
|
{
|
|
|
|
|
return getIntOrDateTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить число с плавающей запятой.
|
|
|
|
|
double getDouble() const
|
|
|
|
|
{
|
|
|
|
|
if (unlikely(isNull()))
|
|
|
|
|
throwException("Value is NULL");
|
|
|
|
|
|
|
|
|
|
return readFloatText(m_data, m_length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить дату-время (из значения вида '2011-01-01 00:00:00').
|
|
|
|
|
LocalDateTime getDateTime() const
|
|
|
|
|
{
|
|
|
|
|
return LocalDateTime(data(), size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить дату (из значения вида '2011-01-01' или '2011-01-01 00:00:00').
|
|
|
|
|
LocalDate getDate() const
|
|
|
|
|
{
|
|
|
|
|
return LocalDate(data(), size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить строку.
|
|
|
|
|
std::string getString() const
|
|
|
|
|
{
|
|
|
|
|
if (unlikely(isNull()))
|
|
|
|
|
throwException("Value is NULL");
|
|
|
|
|
|
|
|
|
|
return std::string(m_data, m_length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Является ли NULL.
|
|
|
|
|
bool isNull() const
|
|
|
|
|
{
|
|
|
|
|
return m_data == nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Для совместимости (используйте вместо этого метод isNull())
|
|
|
|
|
bool is_null() const { return isNull(); }
|
|
|
|
|
|
|
|
|
|
/** Получить любой поддерживаемый тип (для шаблонного кода).
|
|
|
|
|
* Поддерживаются основные типы, а также любые типы с конструктором от Value (для удобства расширения).
|
|
|
|
|
*/
|
|
|
|
|
template <typename T> T get() const;
|
|
|
|
|
|
|
|
|
|
/// Для совместимости. Не рекомендуется к использованию, так как неудобен (часто возникают неоднозначности).
|
|
|
|
|
template <typename T> operator T() const { return get<T>(); }
|
|
|
|
|
|
|
|
|
|
const char * data() const { return m_data; }
|
|
|
|
|
size_t length() const { return m_length; }
|
|
|
|
|
size_t size() const { return m_length; }
|
|
|
|
|
bool empty() const { return 0 == m_length; }
|
2011-03-10 20:31:02 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
const char * m_data;
|
|
|
|
|
size_t m_length;
|
|
|
|
|
const ResultBase * res;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool checkDateTime() const
|
|
|
|
|
{
|
|
|
|
|
return (m_length == 10 || m_length == 19) && m_data[4] == '-' && m_data[7] == '-';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
time_t getDateTimeImpl() const
|
|
|
|
|
{
|
|
|
|
|
const auto & date_lut = DateLUT::instance();
|
|
|
|
|
|
|
|
|
|
if (m_length == 10)
|
|
|
|
|
{
|
|
|
|
|
return date_lut.makeDate(
|
|
|
|
|
(m_data[0] - '0') * 1000 + (m_data[1] - '0') * 100 + (m_data[2] - '0') * 10 + (m_data[3] - '0'),
|
|
|
|
|
(m_data[5] - '0') * 10 + (m_data[6] - '0'),
|
|
|
|
|
(m_data[8] - '0') * 10 + (m_data[9] - '0'));
|
|
|
|
|
}
|
|
|
|
|
else if (m_length == 19)
|
|
|
|
|
{
|
|
|
|
|
return date_lut.makeDateTime(
|
|
|
|
|
(m_data[0] - '0') * 1000 + (m_data[1] - '0') * 100 + (m_data[2] - '0') * 10 + (m_data[3] - '0'),
|
|
|
|
|
(m_data[5] - '0') * 10 + (m_data[6] - '0'),
|
|
|
|
|
(m_data[8] - '0') * 10 + (m_data[9] - '0'),
|
|
|
|
|
(m_data[11] - '0') * 10 + (m_data[12] - '0'),
|
|
|
|
|
(m_data[14] - '0') * 10 + (m_data[15] - '0'),
|
|
|
|
|
(m_data[17] - '0') * 10 + (m_data[18] - '0'));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throwException("Cannot parse DateTime");
|
|
|
|
|
|
2020-03-05 19:55:39 +00:00
|
|
|
|
return 0; /// avoid warning.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
time_t getDateImpl() const
|
|
|
|
|
{
|
|
|
|
|
const auto & date_lut = DateLUT::instance();
|
|
|
|
|
|
|
|
|
|
if (m_length == 10 || m_length == 19)
|
|
|
|
|
{
|
|
|
|
|
return date_lut.makeDate(
|
|
|
|
|
(m_data[0] - '0') * 1000 + (m_data[1] - '0') * 100 + (m_data[2] - '0') * 10 + (m_data[3] - '0'),
|
|
|
|
|
(m_data[5] - '0') * 10 + (m_data[6] - '0'),
|
|
|
|
|
(m_data[8] - '0') * 10 + (m_data[9] - '0'));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throwException("Cannot parse Date");
|
|
|
|
|
|
2020-03-05 19:55:39 +00:00
|
|
|
|
return 0; /// avoid warning.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Int64 getIntImpl() const
|
|
|
|
|
{
|
2019-01-04 14:18:49 +00:00
|
|
|
|
return readIntText(m_data, m_length);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Int64 getIntOrDateTime() const
|
|
|
|
|
{
|
|
|
|
|
if (unlikely(isNull()))
|
|
|
|
|
throwException("Value is NULL");
|
|
|
|
|
|
|
|
|
|
if (checkDateTime())
|
|
|
|
|
return getDateTimeImpl();
|
|
|
|
|
else
|
|
|
|
|
return getIntImpl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Int64 getIntOrDate() const
|
|
|
|
|
{
|
|
|
|
|
if (unlikely(isNull()))
|
|
|
|
|
throwException("Value is NULL");
|
|
|
|
|
|
|
|
|
|
if (checkDateTime())
|
|
|
|
|
return getDateImpl();
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const auto & date_lut = DateLUT::instance();
|
|
|
|
|
return date_lut.toDate(getIntImpl());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Прочитать беззнаковое целое в простом формате из не-0-terminated строки.
|
|
|
|
|
UInt64 readUIntText(const char * buf, size_t length) const;
|
|
|
|
|
|
|
|
|
|
/// Прочитать знаковое целое в простом формате из не-0-terminated строки.
|
|
|
|
|
Int64 readIntText(const char * buf, size_t length) const;
|
|
|
|
|
|
|
|
|
|
/// Прочитать число с плавающей запятой в простом формате, с грубым округлением, из не-0-terminated строки.
|
|
|
|
|
double readFloatText(const char * buf, size_t length) const;
|
|
|
|
|
|
|
|
|
|
/// Выкинуть исключение с подробной информацией
|
2020-03-05 19:55:39 +00:00
|
|
|
|
[[noreturn]] void throwException(const char * text) const;
|
2011-03-03 19:57:34 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2017-04-16 05:40:17 +00:00
|
|
|
|
template <> inline bool Value::get<bool >() const { return getBool(); }
|
|
|
|
|
template <> inline char Value::get<char >() const { return getInt(); }
|
|
|
|
|
template <> inline signed char Value::get<signed char >() const { return getInt(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
template <> inline unsigned char Value::get<unsigned char >() const { return getUInt(); }
|
2020-03-05 19:55:39 +00:00
|
|
|
|
template <> inline char8_t Value::get<char8_t >() const { return getUInt(); }
|
2017-04-16 05:40:17 +00:00
|
|
|
|
template <> inline short Value::get<short >() const { return getInt(); }
|
|
|
|
|
template <> inline unsigned short Value::get<unsigned short >() const { return getUInt(); }
|
|
|
|
|
template <> inline int Value::get<int >() const { return getInt(); }
|
|
|
|
|
template <> inline unsigned int Value::get<unsigned int >() const { return getUInt(); }
|
|
|
|
|
template <> inline long Value::get<long >() const { return getInt(); }
|
|
|
|
|
template <> inline unsigned long Value::get<unsigned long >() const { return getUInt(); }
|
|
|
|
|
template <> inline long long Value::get<long long >() const { return getInt(); }
|
|
|
|
|
template <> inline unsigned long long Value::get<unsigned long long >() const { return getUInt(); }
|
|
|
|
|
template <> inline float Value::get<float >() const { return getDouble(); }
|
|
|
|
|
template <> inline double Value::get<double >() const { return getDouble(); }
|
|
|
|
|
template <> inline std::string Value::get<std::string >() const { return getString(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
template <> inline LocalDate Value::get<LocalDate >() const { return getDate(); }
|
|
|
|
|
template <> inline LocalDateTime Value::get<LocalDateTime >() const { return getDateTime(); }
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
2017-04-16 05:40:17 +00:00
|
|
|
|
template <typename T> inline T Value::get() const { return T(*this); }
|
2011-03-05 20:47:46 +00:00
|
|
|
|
|
2011-03-04 20:58:19 +00:00
|
|
|
|
|
2014-05-19 22:05:33 +00:00
|
|
|
|
inline std::ostream & operator<< (std::ostream & ostr, const Value & x)
|
2011-03-03 19:57:34 +00:00
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
|
return ostr.write(x.data(), x.size());
|
2011-03-03 19:57:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|