From fda5cb404d7dcb926233f8b27e920e44f4e56635 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Thu, 24 Mar 2011 17:02:24 +0200 Subject: Add first draft support for QDate, QTime, QDateTime, and QByteArray --- odb/qt/mysql/default-mapping.hxx | 15 ++++++ odb/qt/mysql/qbytearray-traits.hxx | 71 ++++++++++++++++++++++++ odb/qt/mysql/qdate-traits.hxx | 55 +++++++++++++++++++ odb/qt/mysql/qdatetime-traits.hxx | 108 +++++++++++++++++++++++++++++++++++++ odb/qt/mysql/qstring-traits.hxx | 14 ++--- odb/qt/mysql/qtime-traits.hxx | 55 +++++++++++++++++++ 6 files changed, 311 insertions(+), 7 deletions(-) create mode 100644 odb/qt/mysql/qbytearray-traits.hxx create mode 100644 odb/qt/mysql/qdate-traits.hxx create mode 100644 odb/qt/mysql/qdatetime-traits.hxx create mode 100644 odb/qt/mysql/qtime-traits.hxx diff --git a/odb/qt/mysql/default-mapping.hxx b/odb/qt/mysql/default-mapping.hxx index 6647bbd..6a948c4 100644 --- a/odb/qt/mysql/default-mapping.hxx +++ b/odb/qt/mysql/default-mapping.hxx @@ -7,9 +7,24 @@ #define ODB_QT_MYSQL_DEFAULT_MAPPING_HXX #include +#include +#include +#include // Map QString to MySQL TEXT by default. // #pragma db value(QString) type("VARCHAR(56) NOT NULL") +// Map QDate to MySQL DATE by default. +// +#pragma db value(QDate) type("DATE") + +// Map QTime to MySQL TIME by default. +// +#pragma db value(QTime) type("TIME") + +// Map QTime to MySQL DATETIME by default. +// +#pragma db value(QDateTime) type("DATETIME") + #endif // ODB_QT_MYSQL_DEFAULT_MAPPING_HXX diff --git a/odb/qt/mysql/qbytearray-traits.hxx b/odb/qt/mysql/qbytearray-traits.hxx new file mode 100644 index 0000000..d94bf44 --- /dev/null +++ b/odb/qt/mysql/qbytearray-traits.hxx @@ -0,0 +1,71 @@ +// file : odb/qt/mysql/qbytearray-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_MYSQL_QBYTEARRAY_TRAITS_HXX +#define ODB_QT_MYSQL_QBYTEARRAY_TRAITS_HXX + +#include + +#include // std::memcpy +#include // std::size_t + +#include + +#include +#include + +namespace odb +{ + namespace mysql + { + template <> + class default_value_traits + { + public: + typedef QByteArray value_type; + typedef QByteArray query_type; + typedef details::buffer image_type; + + static void + set_value (QByteArray& v, + const details::buffer& b, + std::size_t n, + bool is_null) + { + if (is_null) + v = QByteArray (); + else + { + if (v.capacity () < n + 1) + v.reserve (n + 1); + + std::memcpy (v.data (), b.data (), n); + v.resize (n); + } + } + + static void + set_image (details::buffer& b, + std::size_t& n, + bool& is_null, + const QByteArray& v) + { + if (v.is_null) + is_null = true; + else + { + n = v.size (); + if (n > b.capacity ()) + b.capacity (n); + + if (n != 0) + std::memcpy (v.data (), b.data (), n); + } + } + }; + } +} + +#endif // ODB_QT_MYSQL_QBYTEARRAY_TRAITS_HXX diff --git a/odb/qt/mysql/qdate-traits.hxx b/odb/qt/mysql/qdate-traits.hxx new file mode 100644 index 0000000..168e6f9 --- /dev/null +++ b/odb/qt/mysql/qdate-traits.hxx @@ -0,0 +1,55 @@ +// file : odb/qt/mysql/qdate-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_MYSQL_QDATE_TRAITS_HXX +#define ODB_QT_MYSQL_QDATE_TRAITS_HXX + +#include + +#include + +#include + +namespace odb +{ + namespace mysql + { + template <> + class default_value_traits + { + public: + typedef QDate value_type; + typedef QDate query_type; + typedef MYSQL_TIME image_type; + + static void + set_value (QDate& v, const MYSQL_TIME& i, bool is_null) + { + if (is_null) + // Set the date to be invalid. + // + v.setDate (0, 0, 0); + else + v.setDate (i.year, i.month, i.day); + } + + static void + set_image (MYSQL_TIME& i, bool& is_null, const QDate& v) + { + if (v.isNull ()) + is_null = true; + else + { + is_null = false; + i.year = v.year (); + i.month = v.month (); + i.day = v.day (); + } + } + }; + } +} + +#endif // ODB_QT_MYSQL_QDATE_TRAITS_HXX diff --git a/odb/qt/mysql/qdatetime-traits.hxx b/odb/qt/mysql/qdatetime-traits.hxx new file mode 100644 index 0000000..2ed8139 --- /dev/null +++ b/odb/qt/mysql/qdatetime-traits.hxx @@ -0,0 +1,108 @@ +// file : odb/qt/mysql/qdatetime-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_MYSQL_QDATETIME_TRAITS_HXX +#define ODB_QT_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) + // Set the date part to be invalid. + // + v.setDate (QDate (0, 0, 0)); + else + { + v.setDate (QDate (i.year, i.month, i.day)); + v.setTime (QTime (i.hour, i.minute, i.second)); + } + } + + static void + set_image (MYSQL_TIME& i, bool& is_null, const QDateTime& v) + { + if (v.is_null ()) + is_null = true; + else + { + is_null = false; + + const QDate& d (v.date ()); + i.year = d.year; + i.month = d.month; + i.day = d.day; + + const QTime& t (v.time ()); + i.hour = t.hour (); + i.minute = t.minute (); + i.second = t.second (); + } + } + }; + + 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) + // Set the date part to be invalid. + // + v.setDate (QDate (0, 0, 0)); + else + { + v.setDate (QDate (i.year, i.month, i.day)); + v.setDate (QTime (i.hour, i.minute, i.second)); + } + } + + static void + set_image (MYSQL_TIME& i, bool& is_null, const QDateTime& v) + { + if (v.is_null ()) + is_null = true; + else + { + is_null = false; + + const QDate& d (v.date ()); + i.year = d.year; + i.month = d.month; + i.day = d.day; + + const QTime& t (v.time ()); + i.hour = t.hour (); + i.minute = t.minute (); + i.second = t.second (); + } + } + }; + } +} + +#endif // ODB_QT_MYSQL_QDATETIME_TRAITS_HXX diff --git a/odb/qt/mysql/qstring-traits.hxx b/odb/qt/mysql/qstring-traits.hxx index d179972..facb569 100644 --- a/odb/qt/mysql/qstring-traits.hxx +++ b/odb/qt/mysql/qstring-traits.hxx @@ -8,19 +8,19 @@ #include +#include // std::memcpy #include // std::size_t #include #include #include -#include namespace odb { namespace mysql { - class LIBODB_QT_EXPORT qstring_value_traits + class qstring_value_traits { public: typedef QString value_type; @@ -54,30 +54,30 @@ namespace odb b.capacity (n); if (n != 0) - memcpy (b.data (), a.data (), n); + std::memcpy (b.data (), a.data (), n); } }; template <> - struct LIBODB_QT_EXPORT default_value_traits< + struct default_value_traits< QString, details::buffer, id_string>: qstring_value_traits { }; template <> - struct LIBODB_QT_EXPORT default_value_traits< + struct default_value_traits< QString, details::buffer, id_decimal>: qstring_value_traits { }; template <> - struct LIBODB_QT_EXPORT default_value_traits< + struct default_value_traits< QString, details::buffer, id_enum>: qstring_value_traits { }; template <> - struct LIBODB_QT_EXPORT default_value_traits< + struct default_value_traits< QString, details::buffer, id_set>: qstring_value_traits { }; diff --git a/odb/qt/mysql/qtime-traits.hxx b/odb/qt/mysql/qtime-traits.hxx new file mode 100644 index 0000000..9789114 --- /dev/null +++ b/odb/qt/mysql/qtime-traits.hxx @@ -0,0 +1,55 @@ +// file : odb/qt/mysql/qtime-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_MYSQL_QTIME_TRAITS_HXX +#define ODB_QT_MYSQL_QTIME_TRAITS_HXX + +#include + +#include + +#include + +namespace odb +{ + namespace mysql + { + template <> + class default_value_traits + { + public: + typedef QTime value_type; + typedef QTime query_type; + typedef MYSQL_TIME image_type; + + static void + set_value (QTime& v, const MYSQL_TIME& i, bool is_null) + { + if (is_null) + // Set the time to be invalid. + // + v.setHMS (24, 0, 0); + else + v.setHMS (i.hour, i.minute, i.second); + } + + static void + set_image (MYSQL_TIME& i, bool& is_null, const QTime& v) + { + if (v.isNull ()) + is_null = true; + else + { + is_null = false; + i.hour = v.hour (); + i.minute = v.minute (); + i.second = v.second (); + } + } + }; + } +} + +#endif // ODB_QT_MYSQL_QTIME_TRAITS_HXX -- cgit v1.1