aboutsummaryrefslogtreecommitdiff
path: root/oracle/custom/traits.hxx
blob: 8f3e81ef5a5eba235c1a0d0cd08b707c7e8773b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// file      : oracle/types/traits.hxx
// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef TRAITS_HXX
#define TRAITS_HXX

#include <vector>
#include <sstream>
#include <cstring> // std::memcpy
#include <cassert> // std::memcpy

#include <odb/oracle/traits.hxx>

namespace odb
{
  namespace oracle
  {

        template <>
    class value_traits<std::vector<int>, id_string>
    {
    public:
      typedef std::vector<int> value_type;
      typedef value_type query_type;
      typedef details::buffer image_type;

      static void
      set_value (value_type& v,
                 const char* b,
                 std::size_t n,
                 bool is_null)
      {
        v.clear ();

        if (!is_null)
        {
          // Array format is "n1,n2,n3...".
          //
          std::istringstream is (std::string (b, n));

          for (char c; !is.eof (); is >> c)
          {
            v.push_back (int ());
            is >> v.back ();
          }
        }
      }

      static void
      set_image (char* b,
                 std::size_t c,
                 std::size_t& n,
                 bool& is_null,
                 const value_type& v)
      {
        is_null = false;
        std::ostringstream os;

        for (value_type::const_iterator i (v.begin ()), e (v.end ()); i != e;)
        {
          os << *i;

          if (++i != e)
            os << ',';
        }

        const std::string& s (os.str ());
        n = s.size ();
        assert (n <= c);
        std::memcpy (b, s.c_str (), n);
      }
    };
  }
}

#endif // TRAITS_HXX