OpenTREP Logo  0.07.7
C++ Open Travel Request Parsing Library
DBType.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 // OpenTREP
8 #include <opentrep/DBType.hpp>
9 
10 namespace OPENTREP {
11 
12  // //////////////////////////////////////////////////////////////////////
13  const std::string DBType::_labels[LAST_VALUE] =
14  { "NoDB", "SQLite3", "MySQL/MariaDB" };
15 
16  // //////////////////////////////////////////////////////////////////////
17  const char DBType::_typeLabels[LAST_VALUE] = { 'N', 'S', 'M' };
18 
19  // //////////////////////////////////////////////////////////////////////
20  DBType::DBType() : _type (LAST_VALUE) {
21  assert (false);
22  }
23 
24  // //////////////////////////////////////////////////////////////////////
25  DBType::DBType (const DBType& iDBType)
26  : _type (iDBType._type) {
27  }
28 
29  // //////////////////////////////////////////////////////////////////////
30  DBType::DBType (const EN_DBType& iDBType)
31  : _type (iDBType) {
32  }
33 
34  // //////////////////////////////////////////////////////////////////////
35  DBType::EN_DBType DBType::getType (const char iTypeChar) {
36  EN_DBType oType;
37  switch (iTypeChar) {
38  case 'N': oType = NODB; break;
39  case 'S': oType = SQLITE3; break;
40  case 'M': oType = MYSQL; break;
41  default: oType = LAST_VALUE; break;
42  }
43 
44  if (oType == LAST_VALUE) {
45  const std::string& lLabels = describeLabels();
46  std::ostringstream oMessage;
47  oMessage << "The database type '" << iTypeChar
48  << "' is not known. Known database types: " << lLabels;
49  throw CodeConversionException (oMessage.str());
50  }
51 
52  return oType;
53  }
54 
55  // //////////////////////////////////////////////////////////////////////
56  DBType::DBType (const char iTypeChar)
57  : _type (getType (iTypeChar)) {
58  }
59 
60  // //////////////////////////////////////////////////////////////////////
61  DBType::DBType (const std::string& iTypeStr) : _type (LAST_VALUE) {
62  if (iTypeStr == "sqlite3") {
63  _type = SQLITE3;
64  } else if (iTypeStr == "sqlite" || iTypeStr == "sqlite3") {
65  _type = SQLITE3;
66  } else if (iTypeStr == "mysql" || iTypeStr == "mariadb") {
67  _type = MYSQL;
68  } else if (iTypeStr == "nodb") {
69  _type = NODB;
70  } else {
71  _type = LAST_VALUE;
72  }
73 
74  if (_type == LAST_VALUE) {
75  const std::string& lLabels = describeLabels();
76  std::ostringstream oMessage;
77  oMessage << "The database type '" << iTypeStr
78  << "' is not known. Known database types: " << lLabels;
79  throw CodeConversionException (oMessage.str());
80  }
81  }
82 
83  // //////////////////////////////////////////////////////////////////////
84  const std::string& DBType::getLabel (const EN_DBType& iType) {
85  return _labels[iType];
86  }
87 
88  // //////////////////////////////////////////////////////////////////////
89  char DBType::getTypeLabel (const EN_DBType& iType) {
90  return _typeLabels[iType];
91  }
92 
93  // //////////////////////////////////////////////////////////////////////
94  std::string DBType::getTypeLabelAsString (const EN_DBType& iType) {
95  std::ostringstream oStr;
96  oStr << _typeLabels[iType];
97  return oStr.str();
98  }
99 
100  // //////////////////////////////////////////////////////////////////////
101  std::string DBType::describeLabels() {
102  std::ostringstream ostr;
103  for (unsigned short idx = 0; idx != LAST_VALUE; ++idx) {
104  if (idx != 0) {
105  ostr << ", ";
106  }
107  ostr << _labels[idx];
108  }
109  return ostr.str();
110  }
111 
112  // //////////////////////////////////////////////////////////////////////
114  return _type;
115  }
116 
117  // //////////////////////////////////////////////////////////////////////
118  char DBType::getTypeAsChar() const {
119  const char oTypeChar = _typeLabels[_type];
120  return oTypeChar;
121  }
122 
123  // //////////////////////////////////////////////////////////////////////
124  std::string DBType::getTypeAsString() const {
125  std::ostringstream oStr;
126  oStr << _typeLabels[_type];
127  return oStr.str();
128  }
129 
130  // //////////////////////////////////////////////////////////////////////
131  const std::string DBType::describe() const {
132  std::ostringstream ostr;
133  ostr << _labels[_type];
134  return ostr.str();
135  }
136 
137  // //////////////////////////////////////////////////////////////////////
138  bool DBType::operator== (const EN_DBType& iType) const {
139  return (_type == iType);
140  }
141 
142  // //////////////////////////////////////////////////////////////////////
143  bool DBType::operator== (const DBType& iDBType) const {
144  return (_type == iDBType._type);
145  }
146 
147 }
static std::string getTypeLabelAsString(const EN_DBType &)
Definition: DBType.cpp:94
char getTypeAsChar() const
Definition: DBType.cpp:118
static const std::string & getLabel(const EN_DBType &)
Definition: DBType.cpp:84
static char getTypeLabel(const EN_DBType &)
Definition: DBType.cpp:89
static std::string describeLabels()
Definition: DBType.cpp:101
Enumeration of database types.
Definition: DBType.hpp:17
const std::string describe() const
Definition: DBType.cpp:131
std::string getTypeAsString() const
Definition: DBType.cpp:124
EN_DBType getType() const
Definition: DBType.cpp:113
bool operator==(const EN_DBType &) const
Definition: DBType.cpp:138