From 89de275db2b77d0abf9fa1ec066ef11e262c88af Mon Sep 17 00:00:00 2001
From: Boris Kolpackov <boris@codesynthesis.com>
Date: Thu, 24 Jan 2013 15:10:22 +0200
Subject: Add support for mapping char[N] to CHAR/VARCHAR database types

Also improve query support for arrays (decaying).
---
 pgsql/types/driver.cxx | 35 +++++++++++++++++++++++++++++++++++
 pgsql/types/test.hxx   | 48 +++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 74 insertions(+), 9 deletions(-)

(limited to 'pgsql/types')

diff --git a/pgsql/types/driver.cxx b/pgsql/types/driver.cxx
index 8b969aa..d2e5e80 100644
--- a/pgsql/types/driver.cxx
+++ b/pgsql/types/driver.cxx
@@ -85,6 +85,41 @@ main (int argc, char* argv[])
 
       assert (o == *o1);
     }
+
+    // Test char array.
+    //
+    {
+      char_array o1 (1, "");
+      char_array o2 (2, "1234567890");
+      char_array o3 (3, "1234567890123456");
+
+
+      {
+        transaction t (db->begin ());
+        db->persist (o1);
+        db->persist (o2);
+        db->persist (o3);
+        t.commit ();
+      }
+
+      // PostgreSQL returns padded values for CHAR(N).
+      //
+      memcpy (o1.s2, "                ", 16);
+      o1.s3[0] = o1.c1 = ' ';
+      memcpy (o2.s2, "1234567890      ", 16);
+
+      {
+        transaction t (db->begin ());
+        auto_ptr<char_array> p1 (db->load<char_array> (1));
+        auto_ptr<char_array> p2 (db->load<char_array> (2));
+        auto_ptr<char_array> p3 (db->load<char_array> (3));
+        t.commit ();
+
+        assert (o1 == *p1);
+        assert (o2 == *p2);
+        assert (o3 == *p3);
+      }
+    }
   }
   catch (const odb::exception& e)
   {
diff --git a/pgsql/types/test.hxx b/pgsql/types/test.hxx
index 34f913f..3078576 100644
--- a/pgsql/types/test.hxx
+++ b/pgsql/types/test.hxx
@@ -9,7 +9,7 @@
 #include <string>
 #include <vector>
 #include <memory>  // std::auto_ptr
-#include <cstring> // std::memcmp
+#include <cstring> // std::memcmp, std::strncpy, std::str[n]cmp
 #include <cstddef> // std::size_t
 
 #include <odb/core.hxx>
@@ -79,14 +79,8 @@ enum color {red, green, blue};
 #pragma db object
 struct object
 {
-  object (unsigned long id)
-      : id_ (id)
-  {
-  }
-
-  object ()
-  {
-  }
+  object () {}
+  object (unsigned long id): id_ (id) {}
 
   #pragma db id
   unsigned long id_;
@@ -188,4 +182,40 @@ struct object
   }
 };
 
+// Test char array.
+//
+#pragma db object
+struct char_array
+{
+  char_array () {}
+  char_array (unsigned long id, const char* s)
+      : id_ (id)
+  {
+    std::strncpy (s1, s, sizeof (s1));
+    std::strncpy (s2, s, sizeof (s2));
+    s3[0] = c1 = *s;
+  }
+
+  #pragma db id
+  unsigned long id_;
+
+  char s1[17];
+
+  #pragma db type("CHAR(16)")
+  char s2[16];
+
+  char s3[1];
+  char c1;
+
+  bool
+  operator== (const char_array& y) const
+  {
+    return id_ == y.id_ &&
+      std::strcmp (s1, y.s1) == 0 &&
+      std::strncmp (s2, y.s2, sizeof (s2)) == 0 &&
+      s3[0] == y.s3[0] &&
+      c1 == y.c1;
+  }
+};
+
 #endif // TEST_HXX
-- 
cgit v1.1