aboutsummaryrefslogtreecommitdiff
path: root/odb/option-types.cxx
blob: d1cccb47fada419203c1e7cdb1e3d80ef82c6b3d (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
128
129
130
131
132
133
134
135
136
// file      : odb/option-types.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <string>
#include <istream>
#include <ostream>
#include <algorithm> // std::lower_bound

#include <odb/option-types.hxx>

using namespace std;

// database
//
static const char* database_[] =
{
  "mysql",
  "oracle",
  "pgsql",
  "sqlite",
  "tracer"
};

const char* database::
string () const
{
  return database_[v_];
}

istream&
operator>> (istream& is, database& db)
{
  string s;
  is >> s;

  if (!is.fail ())
  {
    const char** e (database_ + sizeof (database_) / sizeof (char*));
    const char** i (lower_bound (database_, e, s));

    if (i != e && *i == s)
      db = database::value (i - database_);
    else
      is.setstate (istream::failbit);
  }

  return is;
}

ostream&
operator<< (ostream& os, database db)
{
  return os << db.string ();
}

// schema_format
//
static const char* schema_format_[] =
{
  "embedded",
  "sql"
};

const char* schema_format::
string () const
{
  return schema_format_[v_];
}

istream&
operator>> (istream& is, schema_format& sf)
{
  string s;
  is >> s;

  if (!is.fail ())
  {
    const char** e (schema_format_ + sizeof (schema_format_) / sizeof (char*));
    const char** i (lower_bound (schema_format_, e, s));

    if (i != e && *i == s)
      sf = schema_format::value (i - schema_format_);
    else
      is.setstate (istream::failbit);
  }

  return is;
}

ostream&
operator<< (ostream& os, schema_format sf)
{
  return os << sf.string ();
}

// oracle_version
//
istream&
operator>> (istream& is, oracle_version& v)
{
  unsigned short major, minor;

  // Extract the major version.
  //
  is >> major;

  if (!is.fail ())
  {
    // Extract the decimal point.
    //
    char p;
    is >> p;

    if (!is.fail () && p == '.')
    {
      // Extract the minor version.
      //
      is >> minor;

      if (!is.fail ())
        v = oracle_version (major, minor);
    }
    else
      is.setstate (istream::failbit);
  }

  return is;
}

ostream&
operator<< (ostream& os, oracle_version v)
{
  return os << v.ver_major () << '.' << v.ver_minor ();
}