aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorConstantin Michael <constantin@codesynthesis.com>2011-02-25 13:19:52 +0200
committerConstantin Michael <constantin@codesynthesis.com>2011-03-02 10:43:19 +0200
commit7772c7c6d6ee600095b394ddf2abf557aa53aacd (patch)
tree9be1f84bf69bdde687c12f8f5b22b9490e9b85fb
parent76108c5211713934c20fdd8a78755e5b21697e1b (diff)
Add support for boost date, ptime, and time_duration
-rw-r--r--boost/mysql/date-time/driver.cxx183
-rw-r--r--boost/mysql/date-time/makefile120
-rw-r--r--boost/mysql/date-time/test.hxx50
-rw-r--r--boost/mysql/date-time/test.std0
-rw-r--r--boost/mysql/makefile1
5 files changed, 354 insertions, 0 deletions
diff --git a/boost/mysql/date-time/driver.cxx b/boost/mysql/date-time/driver.cxx
new file mode 100644
index 0000000..5dfd374
--- /dev/null
+++ b/boost/mysql/date-time/driver.cxx
@@ -0,0 +1,183 @@
+// file : boost/mysql/date-time/driver.cxx
+// author : Constantin Michael <constantin@codesynthesis.com>
+// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test boost date/time type persistence. MySQL version.
+//
+
+#include <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/mysql/database.hxx>
+#include <odb/mysql/transaction.hxx>
+
+#include <common/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+using namespace std;
+
+using namespace boost::gregorian;
+using namespace boost::posix_time;
+
+using namespace odb::core;
+
+bool
+test_invalid_special_value (object&, auto_ptr<database>&);
+
+bool
+test_out_of_range_value (object&, auto_ptr<database>&);
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_database (argc, argv));
+
+ object o;
+
+ // Test all valid date-time mappings.
+ //
+ o.dates.push_back (day_clock::local_day ());
+ o.dates.push_back (date (not_a_date_time));
+ o.dates.push_back (date (max_date_time));
+ o.dates.push_back (date (min_date_time));
+
+ 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.
+ //
+ o.times.push_back (
+ ptime (
+ date (max_date_time),
+ time_duration (
+ ptime (max_date_time).time_of_day ().hours (),
+ ptime (max_date_time).time_of_day ().minutes (),
+ ptime (max_date_time).time_of_day ().seconds ())));
+
+ 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));
+ o.durations.push_back (time_duration (-1, 2, 3));
+ o.durations.push_back (not_a_date_time);
+
+ {
+ transaction t (db->begin ());
+ db->persist (o);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ auto_ptr<object> ol (db->load<object> (o.id));
+ t.commit ();
+
+ assert (*ol == o);
+ }
+
+ {
+ // Test invalid date mappings.
+ //
+ object sv1, sv2;
+ sv1.dates.push_back (date (neg_infin));
+ sv2.dates.push_back (date (pos_infin));
+
+ transaction t (db->begin ());
+ assert (test_invalid_special_value (sv1, db));
+ assert (test_invalid_special_value (sv2, db));
+ t.commit ();
+ }
+
+
+ {
+ // Test invalid ptime (DATETIME) mappings.
+ //
+ object sv1, sv2;
+ sv1.times.push_back (neg_infin);
+ sv2.times.push_back (pos_infin);
+
+ transaction t (db->begin ());
+ assert (test_invalid_special_value (sv1, db));
+ assert (test_invalid_special_value (sv2, db));
+ t.commit ();
+ }
+
+ {
+ // Test invalid ptime (TIMESTAMP) mappings.
+ //
+ object or1, or2, sv1, sv2;
+ or1.timestamps.push_back (min_date_time);
+ or2.timestamps.push_back (max_date_time);
+ sv1.timestamps.push_back (neg_infin);
+ sv2.timestamps.push_back (pos_infin);
+
+ transaction t (db->begin ());
+ assert (test_out_of_range_value (or1, db));
+ assert (test_out_of_range_value (or2, db));
+ assert (test_invalid_special_value (sv1, db));
+ assert (test_invalid_special_value (sv2, db));
+ t.commit ();
+ }
+
+ {
+ // Test invalid time_duration mappings.
+ //
+ object or1, or2, sv1, sv2;
+ or1.durations.push_back (time_duration (850, 0, 0));
+ or2.durations.push_back (time_duration (-850, 0, 0));
+ sv1.durations.push_back (pos_infin);
+ sv2.durations.push_back (neg_infin);
+
+ transaction t (db->begin ());
+ assert (test_out_of_range_value (or1, db));
+ assert (test_out_of_range_value (or2, db));
+ assert (test_invalid_special_value (sv1, db));
+ assert (test_invalid_special_value (sv2, db));
+ t.commit ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
+
+bool
+test_invalid_special_value (object& x, auto_ptr<database>& db)
+{
+ try
+ {
+ db->persist (x);
+ return false;
+ }
+ catch (const odb::boost::date_time::special_value&)
+ {
+ }
+
+ return true;
+}
+
+bool
+test_out_of_range_value (object& x, auto_ptr<database>& db)
+{
+ try
+ {
+ db->persist (x);
+ return false;
+ }
+ catch (const odb::boost::date_time::value_out_of_range&)
+ {
+ }
+
+ return true;
+}
diff --git a/boost/mysql/date-time/makefile b/boost/mysql/date-time/makefile
new file mode 100644
index 0000000..c6de621
--- /dev/null
+++ b/boost/mysql/date-time/makefile
@@ -0,0 +1,120 @@
+# file : boost/mysql/date-time/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
+# license : GNU GPL v2; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../../build/bootstrap.make
+
+cxx_tun := driver.cxx
+odb_hdr := test.hxx
+cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o))
+cxx_od := $(cxx_obj:.o=.o.d)
+
+common.l := $(out_root)/libcommon/common/common.l
+common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options
+
+driver := $(out_base)/driver
+dist := $(out_base)/.dist
+test := $(out_base)/.test
+clean := $(out_base)/.clean
+
+# Import.
+#
+$(call import,\
+ $(scf_root)/import/odb/stub.make,\
+ odb: odb,odb-rules: odb_rules)
+
+$(call import,\
+ $(scf_root)/import/libodb-boost/stub.make,\
+ l: odb_boost.l,cpp-options: odb_boost.l.cpp-options)
+
+$(call import,\
+ $(scf_root)/import/libboost/header-only/stub.make,\
+ cpp-options: boost.l.cpp-options)
+
+$(call import,\
+ $(scf_root)/import/libboost/date-time/stub.make,\
+ l: boost_date_time.l)
+
+# Build.
+#
+$(driver): $(cxx_obj) $(odb_boost.l) $(common.l) $(boost_date_time.l)
+$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base)
+$(cxx_obj) $(cxx_od): $(common.l.cpp-options) $(odb_boost.l.cpp-options) \
+$(boost.l.cpp-options)
+
+genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql)
+gen := $(addprefix $(out_base)/,$(genf))
+
+$(gen): $(odb)
+$(gen): odb := $(odb)
+$(gen) $(dist): export odb_options += --database mysql --profile boost/date-time \
+--generate-schema
+$(gen): cpp_options := -I$(out_base)
+$(gen): $(common.l.cpp-options) $(odb_boost.l.cpp-options) \
+$(boost.l.cpp-options)
+
+$(call include-dep,$(cxx_od),$(cxx_obj),$(gen))
+
+# Alias for default target.
+#
+$(out_base)/: $(driver)
+
+# Dist
+#
+$(dist): sources := $(cxx_tun)
+$(dist): headers := $(odb_hdr)
+$(dist): data_dist := test.std
+$(dist): export name := $(notdir $(src_base))
+$(dist): export extra_dist := $(data_dist) $(name)-vc9.vcproj \
+$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters
+$(dist):
+ $(call dist-data,$(sources) $(headers) $(data_dist))
+ $(call meta-automake,../template/Makefile.am)
+ $(call meta-vc9proj,../template/template-vc9.vcproj,$(name)-vc9.vcproj)
+ $(call meta-vc10proj,../template/template-vc10.vcxproj,$(name)-vc10.vcxproj)
+
+# Test.
+#
+$(test): $(driver) $(src_base)/test.std
+ $(call message,sql $$1,$(dcf_root)/db-driver $$1, $(src_base)/test.sql)
+ $(call message,test $<,$< --options-file $(dcf_root)/db.options \
+>$(out_base)/test.out)
+ $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out)
+ $(call message,,rm -f $(out_base)/test.out)
+
+# Clean.
+#
+$(clean): \
+ $(driver).o.clean \
+ $(addsuffix .cxx.clean,$(cxx_obj)) \
+ $(addsuffix .cxx.clean,$(cxx_od)) \
+ $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean))
+ $(call message,,rm -f $(out_base)/test.out)
+
+# Generated .gitignore.
+#
+ifeq ($(out_base),$(src_base))
+$(driver): | $(out_base)/.gitignore
+
+$(out_base)/.gitignore: files := driver $(genf)
+$(clean): $(out_base)/.gitignore.clean
+
+$(call include,$(bld_root)/git/gitignore.make)
+endif
+
+# How to.
+#
+$(call include,$(bld_root)/dist.make)
+$(call include,$(bld_root)/meta/vc9proj.make)
+$(call include,$(bld_root)/meta/vc10proj.make)
+$(call include,$(bld_root)/meta/automake.make)
+
+$(call include,$(odb_rules))
+$(call include,$(bld_root)/cxx/cxx-d.make)
+$(call include,$(bld_root)/cxx/cxx-o.make)
+$(call include,$(bld_root)/cxx/o-e.make)
+
+# Dependencies.
+#
+$(call import,$(src_root)/libcommon/makefile)
diff --git a/boost/mysql/date-time/test.hxx b/boost/mysql/date-time/test.hxx
new file mode 100644
index 0000000..235607f
--- /dev/null
+++ b/boost/mysql/date-time/test.hxx
@@ -0,0 +1,50 @@
+// file : boost/mysql/date-time/test.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 TEST_HXX
+#define TEST_HXX
+
+#include <vector>
+
+#include <boost/date_time/gregorian/gregorian_types.hpp>
+#include <boost/date_time/posix_time/posix_time_types.hpp>
+
+#include <odb/core.hxx>
+
+#pragma db object
+struct object
+{
+ object ()
+ {
+ }
+
+ bool
+ operator== (const object& x) const
+ {
+ return
+ id == x.id &&
+ dates == x.dates &&
+ times == x.times &&
+ timestamps == x.timestamps &&
+ durations == x.durations;
+ }
+
+ #pragma db id auto
+ unsigned long id;
+
+ std::vector<boost::gregorian::date> dates;
+ std::vector<boost::posix_time::ptime> times;
+
+ // Specify NULL explicitly to suppress auto-initialization and
+ // auto-update characteristics of TIMESTAMP datatype, and to allow
+ // NULL values.
+ //
+ #pragma db value_type ("TIMESTAMP NULL")
+ std::vector<boost::posix_time::ptime> timestamps;
+
+ std::vector<boost::posix_time::time_duration> durations;
+};
+
+#endif // TEST_HXX
diff --git a/boost/mysql/date-time/test.std b/boost/mysql/date-time/test.std
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/boost/mysql/date-time/test.std
diff --git a/boost/mysql/makefile b/boost/mysql/makefile
index 44853a6..6aa5a48 100644
--- a/boost/mysql/makefile
+++ b/boost/mysql/makefile
@@ -6,6 +6,7 @@
include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make
tests := \
+date-time \
template
default := $(out_base)/