aboutsummaryrefslogtreecommitdiff
path: root/mapping/traits-sqlite.hxx
blob: cd7afc3ddb7b029475f99bd62c66fa4d6a1f758a (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// file      : mapping/traits-sqlite.hxx
// copyright : not copyrighted - public domain

#ifndef TRAITS_SQLITE_HXX
#define TRAITS_SQLITE_HXX

//
// SQLite implementation.
//

#include <cstddef> // std::size_t
#include <cstring> // std::strncmp, std::memcpy
#include <sstream>

#include <odb/sqlite/traits.hxx>

#include "person.hxx" // date

namespace odb
{
  namespace sqlite
  {
    template <>
    class value_traits<bool, id_text>
    {
    public:
      typedef bool value_type;
      typedef bool query_type;
      typedef details::buffer image_type;

      static void
      set_value (bool& v,
                 const details::buffer& b,
                 std::size_t n,
                 bool is_null)
      {
        v = (!is_null && n == 4 && std::strncmp ("true", b.data (), n) == 0);
      }

      static void
      set_image (details::buffer& b,
                 std::size_t& n,
                 bool& is_null,
                 bool v)
      {
        is_null = false;
        n = v ? 4 : 5;

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

        std::memcpy (b.data (), (v ? "true" : "false"), n);
      }
    };

    // In SQLite there is no built-in DATE type. Rather, this type is
    // mapped to TEXT in the YYYY-MM-DD format. The below implementation
    // doesn't do any error checking for brevity.
    //
    template <>
    class value_traits<date, id_text>
    {
    public:
      typedef date value_type;
      typedef date query_type;
      typedef details::buffer image_type;

      static void
      set_value (date& v,
                 const details::buffer& b,
                 std::size_t n,
                 bool is_null)
      {
        using namespace std;

        if (!is_null)
        {
          istringstream is (string (b.data (), n));
          unsigned int y, m, d;

          is >> y;
          is.ignore (1);
          is >> m;
          is.ignore (1);
          is >> d;

          v = date (y, m, d);
        }
        else
          v = date (0, 0, 0);
      }

      static void
      set_image (details::buffer& b,
                 std::size_t& n,
                 bool& is_null,
                 const date& v)
      {
        using namespace std;

        ostringstream os;
        os.fill ('0');

        os.width (4);
        os << v.year () << '-';

        os.width (2);
        os << v.month () << '-';

        os.width (2);
        os << v.day ();

        const string& s (os.str ());

        is_null = false;
        n = s.size ();

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

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

#endif // TRAITS_SQLITE_HXX