aboutsummaryrefslogtreecommitdiff
path: root/libbrep/package-traits.cxx
blob: a03cbac032b93cbbe4cf502c9971d4a7ed2720f7 (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
// file      : libbrep/package-traits.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <libbrep/package-traits.hxx>

#include <string>
#include <ostream>
#include <sstream>
#include <cstring> // memcpy

#include <odb/pgsql/traits.hxx>

using namespace std;

namespace odb
{
  namespace pgsql
  {
    static inline void
    to_pg_string (ostream& os, const string& s)
    {
      os << '"';

      for (auto c: s)
      {
        if (c == '\\' || c == '"')
          os << '\\';

        os << c;
      }

      os << '"';
    }

    // Convert C++ weighted_text struct to PostgreSQL weighted_text
    // composite type.
    //
    void value_traits<brep::weighted_text, id_string>::
    set_image (details::buffer& b,
               size_t& n,
               bool& is_null,
               const value_type& v)
    {
      is_null = v.a.empty () && v.b.empty () && v.c.empty () && v.d.empty ();

      if (!is_null)
      {
        ostringstream o;
        o << "(";
        to_pg_string (o, v.a);
        o << ",";
        to_pg_string (o, v.b);
        o << ",";
        to_pg_string (o, v.c);
        o << ",";
        to_pg_string (o, v.d);
        o << ")";

        const string& s (o.str ());
        n = s.size ();

        if (n > b.capacity ())
          b.capacity (n);

        memcpy (b.data (), s.c_str (), n);
      }
    }
  }
}