From 0ce05850b30eb5635e3f8648a005633636922ee0 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sat, 22 Oct 2022 16:29:40 +0200 Subject: [PATCH] add FT::date_only, FT::time_only and FT::no_time_zone flags --- src/baseexpression.cpp | 21 ++++++++++++++++++++- src/dbconnector.cpp | 15 ++++++++++++++- src/flatexpression.cpp | 7 +------ src/flatexpression.h | 3 +-- src/ft.h | 20 +++++++++++++++++++- src/postgresqlexpression.cpp | 6 ------ src/postgresqlexpression.h | 2 -- 7 files changed, 55 insertions(+), 19 deletions(-) diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index f60ee54..7d3c1e0 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -602,7 +602,26 @@ void BaseExpression::esc(long double val, pt::TextStream & stream, const FT & fi void BaseExpression::esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type) { - stream << date; + if( field_type.is_date_only() ) + { + date.SerializeYearMonthDay(stream, false); + } + else + if( field_type.is_time_only() ) + { + date.SerializeHourMinSec(stream); + } + else + { + if( field_type.is_no_time_zone() ) + { + date.Serialize(stream); + } + else + { + date.SerializeISO(stream); + } + } } diff --git a/src/dbconnector.cpp b/src/dbconnector.cpp index 427faaa..1e02309 100644 --- a/src/dbconnector.cpp +++ b/src/dbconnector.cpp @@ -850,7 +850,20 @@ void DbConnector::get_value(const char * value_str, long double & field_value, c void DbConnector::get_value(const char * value_str, pt::Date & field_value, const FT & field_type) { // IMPROVE ME give some log if parsing failed - field_value.Parse(value_str); + + if( field_type.is_date_only() ) + { + field_value.ParseDayMonthYear(value_str); + } + else + if( field_type.is_time_only() ) + { + field_value.ParseHourMinSec(value_str); + } + else + { + field_value.Parse(value_str, !field_type.is_no_time_zone()); + } } diff --git a/src/flatexpression.cpp b/src/flatexpression.cpp index 7a14d87..925a4c6 100644 --- a/src/flatexpression.cpp +++ b/src/flatexpression.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018-2021, Tomasz Sowa + * Copyright (c) 2018-2022, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -39,11 +39,6 @@ namespace morm { -void FlatExpression::esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type) -{ - date.SerializeISO(stream); -} - } diff --git a/src/flatexpression.h b/src/flatexpression.h index 7c97fed..3c8adc0 100644 --- a/src/flatexpression.h +++ b/src/flatexpression.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018-2021, Tomasz Sowa + * Copyright (c) 2018-2022, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -45,7 +45,6 @@ class FlatExpression : public BaseExpression { public: - void esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type); diff --git a/src/ft.h b/src/ft.h index 5c3c98b..5224f73 100644 --- a/src/ft.h +++ b/src/ft.h @@ -58,13 +58,16 @@ public: no_fetchable = 32, /* not supported yet */ no_removable = 64, raw_field_name = 128, - dont_use_utf8 = 256, /* used only with wchar_t and std::wstring, ignored if binary or hexadecimal flags are used */ + dont_use_utf8 = 256, /* used only with wchar_t and std::wstring, ignored if a binary or a hexadecimal flags are used */ hexadecimal = 512, binary = 1024, json = 2048, space = 4096, pretty_print = 8192, numeric = 16384, + date_only = 32768, /* use only year, month and day from pt::Date, no_time_zone flag is not used here */ + time_only = 65536, /* use only hour, min, sec from pt::Date, no_time_zone flag is not used here */ + no_time_zone = 131072, /* no time zone, used only with pt::Date */ }; /* @@ -194,6 +197,21 @@ public: return is_flag_set(numeric); } + bool is_date_only() const + { + return is_flag_set(date_only); + } + + bool is_time_only() const + { + return is_flag_set(time_only); + } + + bool is_no_time_zone() const + { + return is_flag_set(no_time_zone); + } + }; } diff --git a/src/postgresqlexpression.cpp b/src/postgresqlexpression.cpp index 4847570..9e32897 100644 --- a/src/postgresqlexpression.cpp +++ b/src/postgresqlexpression.cpp @@ -92,12 +92,6 @@ bool PostgreSQLExpression::esc_char(wchar_t val, pt::TextStream & stream) } -void PostgreSQLExpression::esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type) -{ - stream << date << "+00"; -} - - DbExpression & PostgreSQLExpression::page(pt::TextStream & stream, size_t page_number, size_t page_size) { stream << " OFFSET " << (page_number*page_size) << " LIMIT " << page_size << " "; diff --git a/src/postgresqlexpression.h b/src/postgresqlexpression.h index 34830b2..4f0b348 100644 --- a/src/postgresqlexpression.h +++ b/src/postgresqlexpression.h @@ -45,8 +45,6 @@ class PostgreSQLExpression : public DbExpression { public: - void esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type); - DbExpression & page(pt::TextStream & stream, size_t page_number, size_t page_size);