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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2010 Tetsuro IKEDA
Copyright(C) 2011-2013 Kentoku SHIBA
Copyright(C) 2011-2015 Kouhei Sutou <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
*/
#include <mrn_mysql.h>
#include "mrn_path_mapper.hpp"
#include <string.h>
namespace mrn {
char *PathMapper::default_path_prefix = NULL;
char *PathMapper::default_mysql_data_home_path = NULL;
PathMapper::PathMapper(const char *original_mysql_path,
const char *path_prefix,
const char *mysql_data_home_path)
: original_mysql_path_(original_mysql_path),
path_prefix_(path_prefix),
mysql_data_home_path_(mysql_data_home_path) {
db_path_[0] = '\0';
db_name_[0] = '\0';
table_name_[0] = '\0';
mysql_table_name_[0] = '\0';
mysql_path_[0] = '\0';
}
/**
* "./${db}/${table}" ==> "${db}.mrn"
* "./${db}/" ==> "${db}.mrn"
* "/tmp/mysql-test/var/tmp/mysqld.1/#sql27c5_1_0" ==>
* "/tmp/mysql-test/var/tmp/mysqld.1/#sql27c5_1_0.mrn"
*/
const char *PathMapper::db_path() {
if (db_path_[0] != '\0') {
return db_path_;
}
if (original_mysql_path_[0] == FN_CURLIB &&
original_mysql_path_[1] == FN_LIBCHAR) {
if (path_prefix_) {
strcpy(db_path_, path_prefix_);
}
int i = 2, j = strlen(db_path_), len;
len = strlen(original_mysql_path_);
while (original_mysql_path_[i] != FN_LIBCHAR && i < len) {
db_path_[j++] = original_mysql_path_[i++];
}
db_path_[j] = '\0';
} else if (mysql_data_home_path_) {
int len = strlen(original_mysql_path_);
int mysql_data_home_len = strlen(mysql_data_home_path_);
if (len > mysql_data_home_len &&
!strncmp(original_mysql_path_,
mysql_data_home_path_,
mysql_data_home_len)) {
int i = mysql_data_home_len, j;
if (path_prefix_ && path_prefix_[0] == FN_LIBCHAR) {
strcpy(db_path_, path_prefix_);
j = strlen(db_path_);
} else {
memcpy(db_path_, mysql_data_home_path_, mysql_data_home_len);
if (path_prefix_) {
if (path_prefix_[0] == FN_CURLIB &&
path_prefix_[1] == FN_LIBCHAR) {
strcpy(&db_path_[mysql_data_home_len], &path_prefix_[2]);
} else {
strcpy(&db_path_[mysql_data_home_len], path_prefix_);
}
j = strlen(db_path_);
} else {
j = mysql_data_home_len;
}
}
while (original_mysql_path_[i] != FN_LIBCHAR && i < len) {
db_path_[j++] = original_mysql_path_[i++];
}
if (i == len) {
memcpy(db_path_, original_mysql_path_, len);
} else {
db_path_[j] = '\0';
}
} else {
strcpy(db_path_, original_mysql_path_);
}
} else {
strcpy(db_path_, original_mysql_path_);
}
strcat(db_path_, MRN_DB_FILE_SUFFIX);
return db_path_;
}
/**
* "./${db}/${table}" ==> "${db}"
* "./${db}/" ==> "${db}"
* "/tmp/mysql-test/var/tmp/mysqld.1/#sql27c5_1_0" ==>
* "/tmp/mysql-test/var/tmp/mysqld.1/#sql27c5_1_0"
*/
const char *PathMapper::db_name() {
if (db_name_[0] != '\0') {
return db_name_;
}
if (original_mysql_path_[0] == FN_CURLIB &&
original_mysql_path_[1] == FN_LIBCHAR) {
int i = 2, j = 0, len;
len = strlen(original_mysql_path_);
while (original_mysql_path_[i] != FN_LIBCHAR && i < len) {
db_name_[j++] = original_mysql_path_[i++];
}
db_name_[j] = '\0';
} else if (mysql_data_home_path_) {
int len = strlen(original_mysql_path_);
int mysql_data_home_len = strlen(mysql_data_home_path_);
if (len > mysql_data_home_len &&
!strncmp(original_mysql_path_,
mysql_data_home_path_,
mysql_data_home_len)) {
int i = mysql_data_home_len, j = 0;
while (original_mysql_path_[i] != FN_LIBCHAR && i < len) {
db_name_[j++] = original_mysql_path_[i++];
}
if (i == len) {
memcpy(db_name_, original_mysql_path_, len);
} else {
db_name_[j] = '\0';
}
} else {
strcpy(db_name_, original_mysql_path_);
}
} else {
strcpy(db_name_, original_mysql_path_);
}
return db_name_;
}
/**
* "./${db}/${table}" ==> "${table}" (with encoding first '_')
*/
const char *PathMapper::table_name() {
if (table_name_[0] != '\0') {
return table_name_;
}
int len = strlen(original_mysql_path_);
int i = len, j = 0;
for (; original_mysql_path_[--i] != FN_LIBCHAR ;) {}
if (original_mysql_path_[i + 1] == '_') {
table_name_[j++] = '@';
table_name_[j++] = '0';
table_name_[j++] = '0';
table_name_[j++] = '5';
table_name_[j++] = 'f';
i++;
}
for (; i < len ;) {
table_name_[j++] = original_mysql_path_[++i];
}
table_name_[j] = '\0';
return table_name_;
}
/**
* "./${db}/${table}" ==> "${table}" (without encoding first '_')
*/
const char *PathMapper::mysql_table_name() {
if (mysql_table_name_[0] != '\0') {
return mysql_table_name_;
}
int len = strlen(original_mysql_path_);
int i = len, j = 0;
for (; original_mysql_path_[--i] != FN_LIBCHAR ;) {}
for (; i < len ;) {
if (len - i - 1 >= 3 &&
strncmp(original_mysql_path_ + i + 1, "#P#", 3) == 0) {
break;
}
mysql_table_name_[j++] = original_mysql_path_[++i];
}
mysql_table_name_[j] = '\0';
return mysql_table_name_;
}
/**
* "./${db}/${table}" ==> "./${db}/${table}"
* "./${db}/${table}#P#xxx" ==> "./${db}/${table}"
*/
const char *PathMapper::mysql_path() {
if (mysql_path_[0] != '\0') {
return mysql_path_;
}
int i;
int len = strlen(original_mysql_path_);
for (i = 0; i < len; i++) {
if (len - i >= 3 &&
strncmp(original_mysql_path_ + i, "#P#", 3) == 0) {
break;
}
mysql_path_[i] = original_mysql_path_[i];
}
mysql_path_[i] = '\0';
return mysql_path_;
}
bool PathMapper::is_internal_table_name() {
return mysql_table_name()[0] == '#';
}
}
|