diff options
author | Constantin Michael <constantin@codesynthesis.com> | 2011-03-31 15:36:02 +0200 |
---|---|---|
committer | Constantin Michael <constantin@codesynthesis.com> | 2011-04-22 14:38:40 +0200 |
commit | 621bc135b9825fc9f18ada204f67d3800c5f6fda (patch) | |
tree | fa94cea520c51c44f5fc25b85d7932b7f738a4aa /odb/qt/date-time/mysql/qdate-time-traits.hxx | |
parent | f43df635ef3efbb9ac242024c95c5949fb21a919 (diff) |
Complete qt/basic and qt/date-time implementations
Diffstat (limited to 'odb/qt/date-time/mysql/qdate-time-traits.hxx')
-rw-r--r-- | odb/qt/date-time/mysql/qdate-time-traits.hxx | 127 |
1 files changed, 127 insertions, 0 deletions
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 <constantin@codesynthesis.com> +// 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 <odb/pre.hxx> + +#include <QDateTime> + +#include <odb/mysql/traits.hxx> + +namespace odb +{ + namespace mysql + { + template <> + class default_value_traits<QDateTime, MYSQL_TIME, id_datetime> + { + 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<int> (i.year), + static_cast<int> (i.month), + static_cast<int> (i.day)), + QTime (static_cast<int> (i.hour), + static_cast<int> (i.minute), + static_cast<int> (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<unsigned int> (d.year ()); + i.month = static_cast<unsigned int> (d.month ()); + i.day = static_cast<unsigned int> (d.day ()); + + const QTime& t (v.time ()); + i.hour = static_cast<unsigned int> (t.hour ()); + i.minute = static_cast<unsigned int> (t.minute ()); + i.second = static_cast<unsigned int> (t.second ()); + } + } + }; + + template <> + class default_value_traits<QDateTime, MYSQL_TIME, id_timestamp> + { + 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<int> (i.year), + static_cast<int> (i.month), + static_cast<int> (i.day)), + QTime (static_cast<int> (i.hour), + static_cast<int> (i.minute), + static_cast<int> (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<unsigned int> (d.year ()); + i.month = static_cast<unsigned int> (d.month ()); + i.day = static_cast<unsigned int> (d.day ()); + + const QTime& t (v.time ()); + i.hour = static_cast<unsigned int> (t.hour ()); + i.minute = static_cast<unsigned int> (t.minute ()); + i.second = static_cast<unsigned int> (t.second ()); + } + } + }; + + template <> + class default_type_traits<QDateTime> + { + static const database_type_id db_type_od = id_datetime; + }; + } +} + +#endif // ODB_QT_DATE_TIME_MYSQL_QDATETIME_TRAITS_HXX |