From 049aa1e88958815a61e779f65e4c601fec104a10 Mon Sep 17 00:00:00 2001
From: Constantin Michael <constantin@codesynthesis.com>
Date: Mon, 17 Oct 2011 09:54:14 +0200
Subject: Add Oracle types test

---
 oracle/makefile         |   3 +-
 oracle/types/driver.cxx |  85 +++++++++++++++++++++++++
 oracle/types/makefile   | 107 +++++++++++++++++++++++++++++++
 oracle/types/test.hxx   | 163 ++++++++++++++++++++++++++++++++++++++++++++++++
 oracle/types/test.std   |   0
 oracle/types/traits.hxx |  57 +++++++++++++++++
 6 files changed, 414 insertions(+), 1 deletion(-)
 create mode 100644 oracle/types/driver.cxx
 create mode 100644 oracle/types/makefile
 create mode 100644 oracle/types/test.hxx
 create mode 100644 oracle/types/test.std
 create mode 100644 oracle/types/traits.hxx

diff --git a/oracle/makefile b/oracle/makefile
index 7b7453b..8073a94 100644
--- a/oracle/makefile
+++ b/oracle/makefile
@@ -6,7 +6,8 @@
 include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make
 
 tests :=      \
-native
+native        \
+types
 
 default   := $(out_base)/
 dist      := $(out_base)/.dist
diff --git a/oracle/types/driver.cxx b/oracle/types/driver.cxx
new file mode 100644
index 0000000..aa7a2cf
--- /dev/null
+++ b/oracle/types/driver.cxx
@@ -0,0 +1,85 @@
+// file      : oracle/types/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 Oracle type conversion.
+//
+
+#include <memory>   // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/oracle/database.hxx>
+#include <odb/oracle/transaction.hxx>
+
+#include <common/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+using namespace std;
+using namespace odb::core;
+
+int
+main (int argc, char* argv[])
+{
+  try
+  {
+    // Create an Oracle database instance, setting both the client database
+    // and national character set to UTF-8.
+    //
+    auto_ptr<database> db (create_database (argc, argv, false, 873, 873));
+
+    object o (1);
+
+    o.int_ = -123456;
+    o.uint_ = 123456;
+    o.long_long_ = -123456;
+    o.ulong_long_ = 123456;
+
+    o.float_ = 1.123F;
+    o.double_ = 1.123;
+    o.binary_float_ = 1.123F;
+    o.binary_double_ = 1.123;
+
+    o.date_ = date_time (2010, 8, 29, 15, 33, 18);
+
+    string short_str (32, 's');
+    string medium_str (104, 'm');
+    string long_str (1018, 'l');
+    string vlong_str (15000, 'v');
+
+    o.char_ = short_str;
+    o.varchar2_ = medium_str;
+    o.nchar_ = short_str;
+    o.nvarchar2_ = medium_str;o.raw_.assign (long_str.data (), long_str.data () + long_str.size ());
+    o.blob_.assign (vlong_str.data (), vlong_str.data () + vlong_str.size ());
+
+    const char* unicode_str = "a \xD5\x95 \xEA\xAA\xAA \xF2\xAA\xAA\xAA";
+
+    o.clob_ = unicode_str;
+    o.nclob_ = unicode_str;
+
+    {
+      transaction t (db->begin ());
+      db->persist (o);
+      t.commit ();
+    }
+
+    //
+    //
+    {
+      transaction t (db->begin ());
+      auto_ptr<object> o1 (db->load<object> (1));
+      t.commit ();
+
+      assert (o == *o1);
+    }
+  }
+  catch (const odb::exception& e)
+  {
+    cerr << e.what () << endl;
+    return 1;
+  }
+}
diff --git a/oracle/types/makefile b/oracle/types/makefile
new file mode 100644
index 0000000..7f01dd6
--- /dev/null
+++ b/oracle/types/makefile
@@ -0,0 +1,107 @@
+# file      : oracle/types/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)
+
+# Build.
+#
+$(driver): $(cxx_obj) $(common.l)
+$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base)
+$(cxx_obj) $(cxx_od): $(common.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 oracle --generate-schema \
+--table-prefix oracle_types_ --hxx-prologue '\#include "traits.hxx"'
+$(gen): cpp_options := -I$(src_base)
+$(gen): $(common.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): export extra_headers := traits.hxx
+$(dist): data_dist := test.std
+$(dist): export name := $(subst /,-,$(subst $(src_root)/oracle/,,$(src_base)))
+$(dist): export extra_dist := $(data_dist) $(name)-vc9.vcproj \
+$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters
+$(dist):
+	$(call dist-data,$(sources) $(headers) $(extra_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 schema)
+	$(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/oracle/types/test.hxx b/oracle/types/test.hxx
new file mode 100644
index 0000000..0d9f5f7
--- /dev/null
+++ b/oracle/types/test.hxx
@@ -0,0 +1,163 @@
+// file      : oracle/types/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 <string>
+#include <vector>
+
+#include <odb/core.hxx>
+
+struct date_time
+{
+  date_time ()
+  {
+  }
+
+  date_time (unsigned short y,
+             unsigned char m,
+             unsigned char d,
+             unsigned char h,
+             unsigned char min,
+             unsigned char sec)
+      : year (y),
+        month (m),
+        day (d),
+        hour (h),
+        minute (min),
+        second (sec)
+  {
+  }
+
+  bool
+  operator== (const date_time& y) const
+  {
+    return
+      year == y.year &&
+      month == y.month &&
+      day == y.day &&
+      hour == y.hour &&
+      minute == y.minute &&
+      second == y.second;
+  }
+
+  unsigned short year;
+  unsigned char month;
+  unsigned char day;
+  unsigned char hour;
+  unsigned char minute;
+  unsigned char second;
+};
+
+#pragma db object
+struct object
+{
+  object (unsigned int id)
+      : id_ (id)
+  {
+  }
+
+  object ()
+  {
+  }
+
+  #pragma db id
+  unsigned int id_;
+
+  // Integral types.
+  //
+  #pragma db type ("NUMBER(10)")
+  int int_;
+
+  #pragma db type ("NUMBER(10)")
+  unsigned uint_;
+
+  #pragma db type ("NUMBER(19)")
+  long long long_long_;
+
+  #pragma db type ("NUMBER(20)")
+  unsigned long long ulong_long_;
+
+  // Float types.
+  //
+  #pragma db type ("FLOAT(24)")
+  float float_;
+
+  #pragma db type ("FLOAT(53)")
+  double double_;
+
+  #pragma db type ("BINARY_FLOAT")
+  float binary_float_;
+
+  #pragma db type ("BINARY_DOUBLE")
+  double binary_double_;
+
+  // Data-time types.
+  //
+  #pragma db type ("DATE")
+  date_time date_;
+
+  // @@
+  // #pragma db type ("TIMESTAMP(6)")
+  // date_time timestamp_;
+
+  // String and binary types.
+  //
+  #pragma db type ("CHAR(32)")
+  std::string char_;
+
+  #pragma db type ("VARCHAR2(512)")
+  std::string varchar2_;
+
+  #pragma db type ("NCHAR(32)")
+  std::string nchar_;
+
+  #pragma db type ("NVARCHAR2(512)")
+  std::string nvarchar2_;
+
+  #pragma db type ("RAW(1024)")
+  std::vector<char> raw_;
+
+  // LOB types.
+  //
+  #pragma db type ("BLOB")
+  std::vector<char> blob_;
+
+  #pragma db type ("CLOB")
+  std::string clob_;
+
+  #pragma db type ("NCLOB")
+  std::string nclob_;
+
+  // Test NULL value.
+  //
+
+  bool
+  operator== (const object& y) const
+  {
+    return
+      id_ == y.id_ &&
+      int_ == y.int_ &&
+      uint_ == y.uint_ &&
+      long_long_ == y.long_long_ &&
+      ulong_long_ == y.ulong_long_ &&
+      float_ == y.float_ &&
+      double_ == y.double_ &&
+      binary_float_ == y.binary_float_ &&
+      binary_double_ == y.binary_double_ &&
+      date_ == y.date_ &&
+      char_ == y.char_ &&
+      varchar2_ == y.varchar2_ &&
+      nchar_ == y.nchar_ &&
+      nvarchar2_ == y.nvarchar2_ &&
+      raw_ == y.raw_ &&
+      blob_ == y.blob_ &&
+      clob_ == y.clob_ &&
+      nclob_ == y.nclob_;
+  }
+};
+
+#endif // TEST_HXX
diff --git a/oracle/types/test.std b/oracle/types/test.std
new file mode 100644
index 0000000..e69de29
diff --git a/oracle/types/traits.hxx b/oracle/types/traits.hxx
new file mode 100644
index 0000000..521bc9c
--- /dev/null
+++ b/oracle/types/traits.hxx
@@ -0,0 +1,57 @@
+// file      : oracle/types/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 TRAITS_HXX
+#define TRAITS_HXX
+
+#include <odb/oracle/traits.hxx>
+
+#include "test.hxx" // date_time
+
+namespace odb
+{
+  namespace oracle
+  {
+    template <>
+    class value_traits<date_time, id_date>
+    {
+    public:
+      typedef long long value_type;
+      typedef long long query_type;
+      typedef char* image_type;
+
+      static void
+      set_value (date_time& v, const char* i, bool is_null)
+      {
+        if (!is_null)
+        {
+          v.year = (i[0] - 100) * 100;
+          v.year += (i[1] - 100);
+          v.month = i[2];
+          v.day = i[3];
+          v.hour = i[4] - 1;
+          v.minute = i[5] - 1;
+          v.second = i[6] - 1;
+        }
+      }
+
+      static void
+      set_image (char* i, bool& is_null, const date_time& v)
+      {
+        is_null = false;
+
+        i[0] = static_cast<char> (v.year / 100 + 100);
+        i[1] = static_cast<char> (v.year % 100 + 100);
+        i[2] = static_cast<char> (v.month);
+        i[3] = static_cast<char> (v.day);
+        i[4] = static_cast<char> (v.hour + 1);
+        i[5] = static_cast<char> (v.minute + 1);
+        i[6] = static_cast<char> (v.second + 1);
+      }
+    };
+  }
+}
+
+#endif // TRAITS_HXX
-- 
cgit v1.1