0.08.01
C++ Open Travel Request Parsing Library
Toggle main menu visibility
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
10
namespace
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
// //////////////////////////////////////////////////////////////////////
105
std::string
DBType::describeLabels
() {
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
// //////////////////////////////////////////////////////////////////////
117
DBType::EN_DBType
DBType::getType
()
const
{
118
return
_type;
119
}
120
121
// //////////////////////////////////////////////////////////////////////
122
char
DBType::getTypeAsChar
()
const
{
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
}
DBType.hpp
OPENTREP::CodeConversionException
Definition
OPENTREP_exceptions.hpp:226
OPENTREP::LOG::LAST_VALUE
@ LAST_VALUE
Definition
OPENTREP_log.hpp:22
OPENTREP
Definition
BasChronometer.cpp:10
OPENTREP::DBType::describeLabels
static std::string describeLabels()
Definition
DBType.cpp:105
OPENTREP::DBType::DBType
DBType(const EN_DBType &)
Definition
DBType.cpp:30
OPENTREP::DBType::describe
const std::string describe() const
Definition
DBType.cpp:135
OPENTREP::DBType::getTypeLabelAsString
static std::string getTypeLabelAsString(const EN_DBType &)
Definition
DBType.cpp:98
OPENTREP::DBType::getType
EN_DBType getType() const
Definition
DBType.cpp:117
OPENTREP::DBType::operator==
bool operator==(const EN_DBType &) const
Definition
DBType.cpp:142
OPENTREP::DBType::getTypeAsString
std::string getTypeAsString() const
Definition
DBType.cpp:128
OPENTREP::DBType::getTypeLabel
static char getTypeLabel(const EN_DBType &)
Definition
DBType.cpp:93
OPENTREP::DBType::getType
static EN_DBType getType(const char)
Definition
DBType.cpp:35
OPENTREP::DBType::getLabel
static const std::string & getLabel(const EN_DBType &)
Definition
DBType.cpp:88
OPENTREP::DBType::getTypeAsChar
char getTypeAsChar() const
Definition
DBType.cpp:122
OPENTREP::DBType::EN_DBType
EN_DBType
Definition
DBType.hpp:19
OPENTREP::DBType::MYSQL
@ MYSQL
Definition
DBType.hpp:23
OPENTREP::DBType::PG
@ PG
Definition
DBType.hpp:22
OPENTREP::DBType::LAST_VALUE
@ LAST_VALUE
Definition
DBType.hpp:24
OPENTREP::DBType::SQLITE3
@ SQLITE3
Definition
DBType.hpp:21
OPENTREP::DBType::NODB
@ NODB
Definition
DBType.hpp:20
Generated on
for OpenTREP by
1.17.0