From d31e96535e3f41c36646f375680d7a4efc5772b2 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 3 Jul 2013 11:59:08 +0200 Subject: Test MySQL sub-second precision support --- boost/mysql/date-time/driver.cxx | 47 +++++++++++++++++++++++++++++++++++----- boost/mysql/date-time/makefile | 4 ++-- boost/mysql/date-time/test.hxx | 17 +++++++++++++++ 3 files changed, 61 insertions(+), 7 deletions(-) (limited to 'boost') diff --git a/boost/mysql/date-time/driver.cxx b/boost/mysql/date-time/driver.cxx index 49d94b7..6ffe051 100644 --- a/boost/mysql/date-time/driver.cxx +++ b/boost/mysql/date-time/driver.cxx @@ -37,6 +37,35 @@ main (int argc, char* argv[]) { auto_ptr db (create_database (argc, argv)); + mysql_version v; + { + transaction t (db->begin ()); + db->query ().begin ().load (v); + t.commit (); + } + + // If we are running against MySQL 5.6.4 or later alter the tables + // to allow sub-second precision. + // + bool fs (v.major > 5 || + (v.major == 5 && (v.minor > 6 || + (v.minor == 6 && v.release >= 4)))); + if (fs) + { + transaction t (db->begin ()); + + db->execute ("ALTER TABLE `boost_mysql_dt_object_durations`" \ + " MODIFY COLUMN `value` TIME(6)"); + + db->execute ("ALTER TABLE `boost_mysql_dt_object_times`" \ + " MODIFY COLUMN `value` DATETIME(6)"); + + db->execute ("ALTER TABLE `boost_mysql_dt_object_timestamps`" \ + " MODIFY COLUMN `value` TIMESTAMP(6) NULL"); + + t.commit (); + } + object o; // Test all valid date-time mappings. @@ -46,13 +75,16 @@ main (int argc, char* argv[]) o.dates.push_back (date (max_date_time)); o.dates.push_back (date (min_date_time)); - o.times.push_back (second_clock::local_time ()); + if (fs) + o.times.push_back (microsec_clock::local_time ()); + else + o.times.push_back (second_clock::local_time ()); o.times.push_back (not_a_date_time); o.times.push_back (min_date_time); - // MySQL time interface does not support fraction seconds. Construct - // with zero fractional seconds so that comparison test does not - // fail for invalid reasons. + // MySQL prior to 5.6.4 does not support fraction seconds. Construct + // with zero fractional seconds so that comparison test does not fail + // for invalid reasons. // o.times.push_back ( ptime ( @@ -62,10 +94,15 @@ main (int argc, char* argv[]) ptime (max_date_time).time_of_day ().minutes (), ptime (max_date_time).time_of_day ().seconds ()))); - o.timestamps.push_back (second_clock::local_time ()); + if (fs) + o.timestamps.push_back (microsec_clock::local_time ()); + else + o.timestamps.push_back (second_clock::local_time ()); o.timestamps.push_back (not_a_date_time); o.durations.push_back (time_duration (1, 2, 3)); + if (fs) + o.durations.back () += time_duration (microseconds (123456)); o.durations.push_back (time_duration (-1, 2, 3)); o.durations.push_back (not_a_date_time); diff --git a/boost/mysql/date-time/makefile b/boost/mysql/date-time/makefile index aaff422..c6c60fd 100644 --- a/boost/mysql/date-time/makefile +++ b/boost/mysql/date-time/makefile @@ -41,8 +41,8 @@ $(boost.l.cpp-options) $(gen): $(odb) $(gen): odb := $(odb) -$(gen) $(dist): export odb_options += --database mysql --profile boost/date-time \ ---generate-schema --table-prefix boost_mysql_dt_ +$(gen) $(dist): export odb_options += --database mysql --generate-schema \ +--generate-query --profile boost/date-time --table-prefix boost_mysql_dt_ $(gen): cpp_options := -I$(src_base) $(gen): $(common.l.cpp-options) $(odb_boost.l.cpp-options) \ $(boost.l.cpp-options) diff --git a/boost/mysql/date-time/test.hxx b/boost/mysql/date-time/test.hxx index 6244177..253ebb1 100644 --- a/boost/mysql/date-time/test.hxx +++ b/boost/mysql/date-time/test.hxx @@ -46,4 +46,21 @@ struct object std::vector durations; }; +// MySQL server version view. +// +#pragma db view query( \ + "SELECT " \ + "CAST(SUBSTRING_INDEX(@@version, '.', 1) AS UNSIGNED)," \ + "CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(@@version, '.', 2), '.', -1) AS UNSIGNED)," \ + "CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(@@version, '-', 1), '.', -1) AS UNSIGNED)," \ + "@@protocol_version") +struct mysql_version +{ + unsigned int major; + unsigned int minor; + unsigned int release; + + unsigned int protocol; +}; + #endif // TEST_HXX -- cgit v1.1