/
column_description.cc
149 lines (135 loc) · 4.52 KB
/
column_description.cc
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
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2020-2020 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "include/bareos.h"
#include "dird/dbcopy/column_description.h"
#include "dird/dbcopy/row_data.h"
#include <cstring>
#include <vector>
ColumnDescription::ColumnDescription(const char* column_name_in,
const char* data_type_in,
const char* max_length_in)
: column_name(column_name_in), data_type(data_type_in)
{
std::string field;
try {
field = max_length_in ? max_length_in : "";
if (!field.empty()) { character_maximum_length = std::stoul(field); }
} catch (const std::exception& e) {
std::string err{__FILE__};
err += ":";
err += std::to_string(__LINE__);
err += ": Could not convert number ";
err += "<";
err += field;
err += ">";
throw std::runtime_error(err);
}
}
static void no_conversion(FieldData& fd)
{
fd.data_pointer = fd.data_pointer ? fd.data_pointer : "";
}
static void timestamp_conversion_postgresql(FieldData& fd)
{
if (!fd.data_pointer) {
fd.data_pointer = "1970-01-01 00:00:00";
} else if (fd.data_pointer[0] == '0') {
fd.data_pointer = "1970-01-01 00:00:00";
}
}
static void string_conversion_postgresql(FieldData& fd)
{
if (fd.data_pointer) {
std::size_t len{strlen(fd.data_pointer)};
fd.converted_data.resize(len * 2 + 1);
char* n = fd.converted_data.data();
const char* o = fd.data_pointer;
while (len--) {
switch (*o) {
case '\'':
*n++ = '\'';
*n++ = '\'';
o++;
break;
case 0:
*n++ = '\\';
*n++ = 0;
o++;
break;
default:
*n++ = *o++;
break;
}
}
*n = 0;
fd.data_pointer = fd.converted_data.data();
} else {
fd.data_pointer = "";
}
}
const DataTypeConverterMap ColumnDescriptionMysql::db_import_converter_map{
{"bigint", no_conversion},
{"binary", no_conversion},
{"blob", string_conversion_postgresql},
{"char", no_conversion},
{"datetime", timestamp_conversion_postgresql},
{"decimal", no_conversion},
{"enum", no_conversion},
{"int", no_conversion},
{"longblob", no_conversion},
{"smallint", no_conversion},
{"text", string_conversion_postgresql},
{"timestamp", timestamp_conversion_postgresql},
{"tinyblob", no_conversion},
{"tinyint", no_conversion},
{"varchar", string_conversion_postgresql}};
ColumnDescriptionMysql::ColumnDescriptionMysql(const char* column_name_in,
const char* data_type_in,
const char* max_length_in)
: ColumnDescription(column_name_in, data_type_in, max_length_in)
{
try {
db_import_converter = db_import_converter_map.at(data_type_in);
} catch (const std::out_of_range& e) {
std::string err{"Mysql: Data type not found in conversion map: "};
err += data_type_in;
throw std::runtime_error(err);
}
}
const DataTypeConverterMap ColumnDescriptionPostgresql::db_export_converter_map{
{"bigint", no_conversion},
{"bytea", no_conversion},
{"character", no_conversion},
{"integer", no_conversion},
{"numeric", no_conversion},
{"smallint", no_conversion},
{"text", no_conversion},
{"timestamp without time zone", no_conversion}};
ColumnDescriptionPostgresql::ColumnDescriptionPostgresql(
const char* column_name_in,
const char* data_type_in,
const char* max_length_in)
: ColumnDescription(column_name_in, data_type_in, max_length_in)
{
try {
db_export_converter = db_export_converter_map.at(data_type_in);
} catch (const std::out_of_range& e) {
std::string err{"Postgresql: Data type not found in conversion map: "};
err += data_type_in;
throw std::runtime_error(err);
}
}