aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--odb/pgsql/traits.cxx58
-rw-r--r--odb/pgsql/traits.hxx291
2 files changed, 349 insertions, 0 deletions
diff --git a/odb/pgsql/traits.cxx b/odb/pgsql/traits.cxx
new file mode 100644
index 0000000..bdffa45
--- /dev/null
+++ b/odb/pgsql/traits.cxx
@@ -0,0 +1,58 @@
+// file : odb/pgsql/traits.cxx
+// author : Constantin Michael <constantin@codesynthesis.com>
+// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#include <cstring> // std::memcpy, std::strlen
+
+#include <odb/pgsql/traits.hxx>
+
+using namespace std;
+
+namespace odb
+{
+ namespace pgsql
+ {
+ using details::buffer;
+
+ //
+ // string_value_traits
+ //
+
+ void string_value_traits::
+ set_image (buffer& b,
+ size_t& n,
+ bool& is_null,
+ const string& v)
+ {
+ is_null = false;
+ n = v.size ();
+
+ if (n > b.capacity ())
+ b.capacity (n);
+
+ if (n != 0)
+ memcpy (b.data (), v.c_str (), n);
+ }
+
+ //
+ // c_string_value_traits
+ //
+
+ void c_string_value_traits::
+ set_image (buffer& b,
+ size_t& n,
+ bool& is_null,
+ const char* v)
+ {
+ is_null = false;
+ n = strlen (v);
+
+ if (n > b.capacity ())
+ b.capacity (n);
+
+ if (n != 0)
+ memcpy (b.data (), v, n);
+ }
+ }
+}
diff --git a/odb/pgsql/traits.hxx b/odb/pgsql/traits.hxx
new file mode 100644
index 0000000..62a6df2
--- /dev/null
+++ b/odb/pgsql/traits.hxx
@@ -0,0 +1,291 @@
+// file : odb/pgsql/traits.hxx
+// author : Constantin Michael <constantin@codesynthesis.com>
+// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef ODB_PGSQL_TRAITS_HXX
+#define ODB_PGSQL_TRAITS_HXX
+
+#include <odb/pre.hxx>
+
+#include <string>
+#include <cstddef> // std::size_t
+
+#include <odb/traits.hxx>
+
+#include <odb/details/buffer.hxx>
+
+#include <odb/pgsql/version.hxx>
+#include <odb/pgsql/pgsql-types.hxx>
+
+#include <odb/pgsql/details/export.hxx>
+
+namespace odb
+{
+ namespace pgsql
+ {
+ enum database_type_id
+ {
+ id_boolean,
+ id_smallint,
+ id_integer,
+ id_bigint,
+ id_numeric,
+ id_real,
+ id_double,
+ id_serial,
+ id_bigserial,
+
+ id_string
+ };
+
+ //
+ // image_traits
+ //
+
+ template <database_type_id>
+ struct image_traits;
+
+ template <>
+ struct image_traits<id_smallint> {typedef short image_type;};
+
+ template <>
+ struct image_traits<id_integer> {typedef int image_type;};
+
+ template <>
+ struct image_traits<id_bigint> {typedef long long image_type;};
+
+ template <>
+ struct image_traits<id_numeric> {typedef details::buffer image_type;};
+
+ template <>
+ struct image_traits<id_real> {typedef float image_type;};
+
+ template <>
+ struct image_traits<id_double> {typedef double image_type;};
+
+ //
+ // value_traits
+ //
+
+ template <typename T, database_type_id>
+ struct default_value_traits;
+
+ template <typename T, database_type_id ID>
+ class value_traits: public default_value_traits<T, ID>
+ {
+ };
+
+ template <typename T, database_type_id ID>
+ struct default_value_traits
+ {
+ typedef T value_type;
+ typedef T query_type;
+ typedef typename image_traits<ID>::image_type image_type;
+
+ static void
+ set_value (T& v, const image_type& i, bool is_null)
+ {
+ if (!is_null)
+ v = T (i);
+ else
+ v = T ();
+ }
+
+ static void
+ set_image (image_type& i, bool& is_null, T v)
+ {
+ is_null = false;
+ i = image_type (v);
+ }
+ };
+
+ // std::string specialization.
+ //
+ class LIBODB_PGSQL_EXPORT string_value_traits
+ {
+ public:
+ typedef std::string value_type;
+ typedef std::string query_type;
+ typedef details::buffer image_type;
+
+ static void
+ set_value (std::string& v,
+ const details::buffer& b,
+ std::size_t n,
+ bool is_null)
+ {
+ if (!is_null)
+ v.assign (b.data (), n);
+ else
+ v.erase ();
+ }
+
+ static void
+ set_image (details::buffer&,
+ std::size_t& n,
+ bool& is_null,
+ const std::string&);
+ };
+
+ template <>
+ struct LIBODB_PGSQL_EXPORT default_value_traits<std::string, id_numeric>:
+ string_value_traits
+ {
+ };
+
+ template <>
+ struct LIBODB_PGSQL_EXPORT default_value_traits<std::string, id_string>:
+ string_value_traits
+ {
+ };
+
+ // const char* specialization
+ //
+ // Specialization for const char* which only supports initialization
+ // of an image from the value but not the other way around. This way
+ // we can pass such values to the queries.
+ //
+ class LIBODB_PGSQL_EXPORT c_string_value_traits
+ {
+ public:
+ typedef const char* value_type;
+ typedef const char* query_type;
+ typedef details::buffer image_type;
+
+ static void
+ set_image (details::buffer&,
+ std::size_t& n,
+ bool& is_null,
+ const char*);
+ };
+
+ template <>
+ struct LIBODB_PGSQL_EXPORT default_value_traits<const char*, id_numeric>:
+ c_string_value_traits
+ {
+ };
+
+ template <>
+ struct LIBODB_PGSQL_EXPORT default_value_traits<const char*, id_string>:
+ c_string_value_traits
+ {
+ };
+
+ //
+ // type_traits
+ //
+
+ template <typename T>
+ struct default_type_traits;
+
+ template <typename T>
+ class type_traits: public default_type_traits<T>
+ {
+ };
+
+ // Integral types.
+ //
+ template <>
+ struct default_type_traits<bool>
+ {
+ static const database_type_id db_type_id = id_boolean;
+ };
+
+ template <>
+ struct default_type_traits<signed char>
+ {
+ static const database_type_id db_type_id = id_smallint;
+ };
+
+ template <>
+ struct default_type_traits<unsigned char>
+ {
+ static const database_type_id db_type_id = id_smallint;
+ };
+
+ template <>
+ struct default_type_traits<short>
+ {
+ static const database_type_id db_type_id = id_smallint;
+ };
+
+ template <>
+ struct default_type_traits<unsigned short>
+ {
+ static const database_type_id db_type_id = id_integer;
+ };
+
+ template <>
+ struct default_type_traits<int>
+ {
+ static const database_type_id db_type_id = id_integer;
+ };
+
+ template <>
+ struct default_type_traits<unsigned int>
+ {
+ static const database_type_id db_type_id = id_bigint;
+ };
+
+ template <>
+ struct default_type_traits<long>
+ {
+ static const database_type_id db_type_id = id_bigint;
+ };
+
+ template <>
+ struct default_type_traits<unsigned long>
+ {
+ static const database_type_id db_type_id = id_bigint;
+ };
+
+ template <>
+ struct default_type_traits<long long>
+ {
+ static const database_type_id db_type_id = id_bigint;
+ };
+
+ // @@ No representation for an unsigned 64 bit value.
+ // Should we force a compile time error with a message
+ // or just truncate?
+ //
+ // template <>
+ // struct default_type_traits<unsigned long long>
+ // {
+ // static const database_type_id db_type_id = id_ulonglong;
+ // };
+
+ // Float types.
+ //
+ template <>
+ struct default_type_traits<float>
+ {
+ static const database_type_id db_type_id = id_real;
+ };
+
+ template <>
+ struct default_type_traits<double>
+ {
+ static const database_type_id db_type_id = id_double;
+ };
+
+ // String type.
+ //
+ template <>
+ struct default_type_traits<std::string>
+ {
+ static const database_type_id db_type_id = id_string;
+ };
+
+ template <>
+ struct default_type_traits<const char*>
+ {
+ static const database_type_id db_type_id = id_string;
+ };
+ }
+}
+
+#include <odb/post.hxx>
+
+#endif // ODB_PGSQL_TRAITS_HXX