OpenTREP Logo  0.07.20
C++ Open Travel Request Parsing Library
Loading...
Searching...
No Matches
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
10namespace OPENTREP {
11
12 // //////////////////////////////////////////////////////////////////////
13 const std::string DBType::_labels[LAST_VALUE] =
14 { "NoDB", "SQLite3", "PG", "MySQL/MariaDB" };
15
16 // //////////////////////////////////////////////////////////////////////
17 const char DBType::_typeLabels[LAST_VALUE] = { 'N', 'S', 'P', '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 'P': oType = PG; break;
41 case 'M': oType = MYSQL; break;
42 default: oType = LAST_VALUE; break;
43 }
44
45 if (oType == LAST_VALUE) {
46 const std::string& lLabels = describeLabels();
47 std::ostringstream oMessage;
48 oMessage << "The database type '" << iTypeChar
49 << "' is not known. Known database types: " << lLabels;
50 throw CodeConversionException (oMessage.str());
51 }
52
53 return oType;
54 }
55
56 // //////////////////////////////////////////////////////////////////////
57 DBType::DBType (const char iTypeChar)
58 : _type (getType (iTypeChar)) {
59 }
60
61 // //////////////////////////////////////////////////////////////////////
62 DBType::DBType (const std::string& iTypeStr) : _type (LAST_VALUE) {
63 if (iTypeStr == "sqlite3") {
64 _type = SQLITE3;
65 } else if (iTypeStr == "sqlite" || iTypeStr == "sqlite3") {
66 _type = SQLITE3;
67 } else if (iTypeStr == "pg" || iTypeStr == "postgres"
68 || iTypeStr == "postgresql") {
69 _type = PG;
70 } else if (iTypeStr == "mysql" || iTypeStr == "mariadb") {
71 _type = MYSQL;
72 } else if (iTypeStr == "nodb") {
73 _type = NODB;
74 } else {
75 _type = LAST_VALUE;
76 }
77
78 if (_type == LAST_VALUE) {
79 const std::string& lLabels = describeLabels();
80 std::ostringstream oMessage;
81 oMessage << "The database type '" << iTypeStr
82 << "' is not known. Known database types: " << lLabels;
83 throw CodeConversionException (oMessage.str());
84 }
85 }
86
87 // //////////////////////////////////////////////////////////////////////
88 const std::string& DBType::getLabel (const EN_DBType& iType) {
89 return _labels[iType];
90 }
91
92 // //////////////////////////////////////////////////////////////////////
93 char DBType::getTypeLabel (const EN_DBType& iType) {
94 return _typeLabels[iType];
95 }
96
97 // //////////////////////////////////////////////////////////////////////
98 std::string DBType::getTypeLabelAsString (const EN_DBType& iType) {
99 std::ostringstream oStr;
100 oStr << _typeLabels[iType];
101 return oStr.str();
102 }
103
104 // //////////////////////////////////////////////////////////////////////
106 std::ostringstream ostr;
107 for (unsigned short idx = 0; idx != LAST_VALUE; ++idx) {
108 if (idx != 0) {
109 ostr << ", ";
110 }
111 ostr << _labels[idx];
112 }
113 return ostr.str();
114 }
115
116 // //////////////////////////////////////////////////////////////////////
118 return _type;
119 }
120
121 // //////////////////////////////////////////////////////////////////////
123 const char oTypeChar = _typeLabels[_type];
124 return oTypeChar;
125 }
126
127 // //////////////////////////////////////////////////////////////////////
128 std::string DBType::getTypeAsString() const {
129 std::ostringstream oStr;
130 oStr << _typeLabels[_type];
131 return oStr.str();
132 }
133
134 // //////////////////////////////////////////////////////////////////////
135 const std::string DBType::describe() const {
136 std::ostringstream ostr;
137 ostr << _labels[_type];
138 return ostr.str();
139 }
140
141 // //////////////////////////////////////////////////////////////////////
142 bool DBType::operator== (const EN_DBType& iType) const {
143 return (_type == iType);
144 }
145
146 // //////////////////////////////////////////////////////////////////////
147 bool DBType::operator== (const DBType& iDBType) const {
148 return (_type == iDBType._type);
149 }
150
151}
static std::string describeLabels()
Definition DBType.cpp:105
DBType(const EN_DBType &)
Definition DBType.cpp:30
const std::string describe() const
Definition DBType.cpp:135
static std::string getTypeLabelAsString(const EN_DBType &)
Definition DBType.cpp:98
EN_DBType getType() const
Definition DBType.cpp:117
bool operator==(const EN_DBType &) const
Definition DBType.cpp:142
std::string getTypeAsString() const
Definition DBType.cpp:128
static char getTypeLabel(const EN_DBType &)
Definition DBType.cpp:93
static EN_DBType getType(const char)
Definition DBType.cpp:35
static const std::string & getLabel(const EN_DBType &)
Definition DBType.cpp:88
char getTypeAsChar() const
Definition DBType.cpp:122