From 6f4f07fea0f98bdb1debffcc40a32d6af61ab5c0 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 18 Apr 2011 18:50:04 +0200 Subject: Implement automatic mapping for C++ enums --- odb/mysql/enum.hxx | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 odb/mysql/enum.hxx (limited to 'odb/mysql/enum.hxx') diff --git a/odb/mysql/enum.hxx b/odb/mysql/enum.hxx new file mode 100644 index 0000000..95f4ed4 --- /dev/null +++ b/odb/mysql/enum.hxx @@ -0,0 +1,131 @@ +// file : odb/mysql/enums.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_MYSQL_ENUMS_HXX +#define ODB_MYSQL_ENUMS_HXX + +#include + +#include // std::size_t +#include // std::memmove +#include + +#include + +#include +#include +#include + +#include + +namespace odb +{ + namespace mysql + { + // Common interface for working with the dual enum image (integer or + // string). Used by the generated code and query machinery. + // + // We use overload resolution instead of template specialization to + // avoid having to specify the image type explicitly. + // + struct LIBODB_MYSQL_EXPORT enum_traits + { + // + // Integer image. + // + static void + bind (MYSQL_BIND& b, unsigned short& i, unsigned long&, my_bool* is_null) + { + b.buffer_type = MYSQL_TYPE_SHORT; + b.is_unsigned = 1; + b.buffer = &i; + b.is_null = is_null; + } + + static bool + grow (unsigned short&, unsigned long&) + { + return false; + } + + template + static bool + set_image (unsigned short& i, unsigned long&, bool& is_null, const T& v) + { + value_traits::set_image (i, is_null, v); + return false; + } + + template + static void + set_value (T& v, unsigned short i, unsigned long, bool is_null) + { + value_traits::set_value (v, i, is_null); + } + + // + // String image. + // + + static void + bind (MYSQL_BIND& b, + details::buffer& i, + unsigned long& size, + my_bool* is_null) + { + b.buffer_type = MYSQL_TYPE_STRING; + b.buffer = i.data (); + b.buffer_length = static_cast (i.capacity ()); + b.length = &size; + b.is_null = is_null; + } + + static bool + grow (details::buffer& i, unsigned long& size) + { + i.capacity (size); + return true; + } + + template + static bool + set_image (details::buffer& i, + unsigned long& size, + bool& is_null, + const T& v) + { + std::size_t n (0), c (i.capacity ()); + value_traits::set_image (i, n, is_null, v); + size = static_cast (n); + return c != i.capacity (); + } + + template + static void + set_value (T& v, + const details::buffer& i, + unsigned long size, + bool is_null) + { + // The buffer has the following content: " ". Get rid of + // the numeric value so that we have "". For more information + // why it is done this way, see mysql::object_columns class in the + // ODB compiler. + // + strip_value (i, size); + + value_traits::set_value (v, i, size, is_null); + } + + private: + void + strip_value (const details::buffer& i, unsigned long& size); + }; + } +} + +#include + +#endif // ODB_MYSQL_ENUMS_HXX -- cgit v1.1