From 621bc135b9825fc9f18ada204f67d3800c5f6fda Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Thu, 31 Mar 2011 15:36:02 +0200 Subject: Complete qt/basic and qt/date-time implementations --- odb/qt/date-time/mysql/qdate-time-traits.hxx | 127 +++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 odb/qt/date-time/mysql/qdate-time-traits.hxx (limited to 'odb/qt/date-time/mysql/qdate-time-traits.hxx') diff --git a/odb/qt/date-time/mysql/qdate-time-traits.hxx b/odb/qt/date-time/mysql/qdate-time-traits.hxx new file mode 100644 index 0000000..9d212a3 --- /dev/null +++ b/odb/qt/date-time/mysql/qdate-time-traits.hxx @@ -0,0 +1,127 @@ +// file : odb/qt/date-time/mysql/qdate-time-traits.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_QT_DATE_TIME_MYSQL_QDATETIME_TRAITS_HXX +#define ODB_QT_DATE_TIME_MYSQL_QDATETIME_TRAITS_HXX + +#include + +#include + +#include + +namespace odb +{ + namespace mysql + { + template <> + class default_value_traits + { + public: + typedef QDateTime value_type; + typedef QDateTime query_type; + typedef MYSQL_TIME image_type; + + static void + set_value (QDateTime& v, const MYSQL_TIME& i, bool is_null) + { + if (is_null) + // Default constructor creates a null QDateTime. + // + v = QDateTime (); + else + v = QDateTime (QDate (static_cast (i.year), + static_cast (i.month), + static_cast (i.day)), + QTime (static_cast (i.hour), + static_cast (i.minute), + static_cast (i.second))); + } + + static void + set_image (MYSQL_TIME& i, bool& is_null, const QDateTime& v) + { + if (v.isNull ()) + is_null = true; + else + { + if ((v < QDateTime (QDate (1000, 1, 1))) || + (v >= QDateTime (QDate (10000, 1, 1)))) + throw odb::qt::date_time::value_out_of_range (); + + is_null = false; + + const QDate& d (v.date ()); + i.year = static_cast (d.year ()); + i.month = static_cast (d.month ()); + i.day = static_cast (d.day ()); + + const QTime& t (v.time ()); + i.hour = static_cast (t.hour ()); + i.minute = static_cast (t.minute ()); + i.second = static_cast (t.second ()); + } + } + }; + + template <> + class default_value_traits + { + public: + typedef QDateTime value_type; + typedef QDateTime query_type; + typedef MYSQL_TIME image_type; + + static void + set_value (QDateTime& v, const MYSQL_TIME& i, bool is_null) + { + if (is_null) + // Default constructor creates a null QDateTime. + // + v = QDateTime (); + else + v = QDateTime (QDate (static_cast (i.year), + static_cast (i.month), + static_cast (i.day)), + QTime (static_cast (i.hour), + static_cast (i.minute), + static_cast (i.second))); + } + + static void + set_image (MYSQL_TIME& i, bool& is_null, const QDateTime& v) + { + if (v.isNull ()) + is_null = true; + else + { + if ((v <= QDateTime (QDate (1970, 1, 1))) || + (v > QDateTime (QDate (2038, 1, 19), QTime (3, 14, 7)))) + throw odb::qt::date_time::value_out_of_range (); + + is_null = false; + + const QDate& d (v.date ()); + i.year = static_cast (d.year ()); + i.month = static_cast (d.month ()); + i.day = static_cast (d.day ()); + + const QTime& t (v.time ()); + i.hour = static_cast (t.hour ()); + i.minute = static_cast (t.minute ()); + i.second = static_cast (t.second ()); + } + } + }; + + template <> + class default_type_traits + { + static const database_type_id db_type_od = id_datetime; + }; + } +} + +#endif // ODB_QT_DATE_TIME_MYSQL_QDATETIME_TRAITS_HXX -- cgit v1.1