ClickHouse/dbms/include/DB/DataTypes/DataTypeDate.h

79 lines
1.8 KiB
C
Raw Normal View History

2010-05-20 19:29:04 +00:00
#ifndef DBMS_DATA_TYPES_NUMBER_FIXED_H
#define DBMS_DATA_TYPES_NUMBER_FIXED_H
#include <Poco/DateTimeParser.h>
#include <DB/Core/Exception.h>
#include <DB/Core/ErrorCodes.h>
2010-06-04 18:25:25 +00:00
#include <DB/IO/ReadHelpers.h>
#include <DB/IO/WriteHelpers.h>
2010-05-20 19:29:04 +00:00
#include <DB/Columns/ColumnsNumber.h>
#include <DB/DataTypes/IDataTypeNumberFixed.h>
namespace DB
{
class DataTypeDate : public IDataTypeNumberFixed<UInt16, ColumnUInt64>
{
private:
DateLUTSingleton & date_lut;
public:
DataTypeDate() : date_lut(DateLUTSingleton::instance()) {}
std::string getName() const { return "Date"; }
2010-05-21 19:52:50 +00:00
SharedPtr<IDataType> clone() const { return new DataTypeDate; }
2010-05-20 19:29:04 +00:00
2010-06-04 18:25:25 +00:00
void serializeText(const Field & field, WriteBuffer & ostr) const
2010-05-20 19:29:04 +00:00
{
DateLUT::Values & values = date_lut.getValues(boost::get<UInt16>(field));
2010-06-04 18:25:25 +00:00
writeIntText(values.year, ostr);
writeChar('-', ostr);
writeIntText(values.month, ostr);
writeChar('-', ostr);
writeIntText(values.day_of_month, ostr);
2010-05-20 19:29:04 +00:00
}
2010-06-04 18:25:25 +00:00
void deserializeText(Field & field, ReadBuffer & istr) const
2010-05-20 19:29:04 +00:00
{
std::string s;
2010-06-04 18:25:25 +00:00
readString(s, istr);
2010-05-20 19:29:04 +00:00
2010-06-04 18:25:25 +00:00
// TODO: тормоза
2010-05-20 19:29:04 +00:00
int time_zone_diff = 0;
field = date_lut.toDayNum(Poco::DateTimeParser::parse(
s, time_zone_diff).timestamp().epochTime());
}
2010-06-04 18:25:25 +00:00
void serializeTextEscaped(const Field & field, WriteBuffer & ostr) const
2010-05-20 19:29:04 +00:00
{
serializeText(field, ostr);
}
2010-06-04 18:25:25 +00:00
void deserializeTextEscaped(Field & field, ReadBuffer & istr) const
2010-05-20 19:29:04 +00:00
{
deserializeText(field, istr);
}
2010-06-04 18:25:25 +00:00
void serializeTextQuoted(const Field & field, WriteBuffer & ostr, bool compatible = false) const
2010-05-20 19:29:04 +00:00
{
2010-06-04 18:25:25 +00:00
writeChar('\'', ostr);
2010-05-20 19:29:04 +00:00
serializeText(field, ostr);
2010-06-04 18:25:25 +00:00
writeChar('\'', ostr);
2010-05-20 19:29:04 +00:00
}
2010-06-04 18:25:25 +00:00
void deserializeTextQuoted(Field & field, ReadBuffer & istr, bool compatible = false) const
2010-05-20 19:29:04 +00:00
{
2010-06-04 18:25:25 +00:00
assertString("'", istr);
2010-05-20 19:29:04 +00:00
deserializeText(field, istr);
2010-06-04 18:25:25 +00:00
assertString("'", istr);
2010-05-20 19:29:04 +00:00
}
};
}
#endif