10
10
#include < library/cpp/protobuf/util/simple_reflection.h>
11
11
12
12
#include < util/folder/pathsplit.h>
13
+ #include < util/stream/str.h>
13
14
#include < util/string/builder.h>
14
15
#include < util/string/strip.h>
15
16
@@ -47,13 +48,26 @@ TString RewriteAbsolutePath(TStringBuf path, TStringBuf backupRoot, TStringBuf r
47
48
namespace {
48
49
49
50
struct TAbsolutePathRewriter {
51
+ const TString Database;
50
52
const TStringBuf BackupRoot;
51
53
const TStringBuf RestoreRoot;
52
54
55
+ static TString BuildDatabaseToken (TStringBuf database) {
56
+ if (database) {
57
+ return TStringBuilder () << " `" << database << " /" ;
58
+ } else {
59
+ return " " ;
60
+ }
61
+ }
62
+
53
63
static bool IsAbsolutePath (TStringBuf path) {
54
64
return path.StartsWith (" `/" ) && path.EndsWith (' `' );
55
65
}
56
66
67
+ bool IsInDatabase (TStringBuf path) const {
68
+ return path.StartsWith (Database);
69
+ }
70
+
57
71
TString RewriteAbsolutePath (TStringBuf path) const {
58
72
if (BackupRoot == RestoreRoot) {
59
73
return TString (path);
@@ -63,14 +77,15 @@ struct TAbsolutePathRewriter {
63
77
}
64
78
65
79
public:
66
- explicit TAbsolutePathRewriter (TStringBuf backupRoot, TStringBuf restoreRoot)
67
- : BackupRoot(backupRoot)
80
+ explicit TAbsolutePathRewriter (TStringBuf database, TStringBuf backupRoot, TStringBuf restoreRoot)
81
+ : Database(BuildDatabaseToken(database))
82
+ , BackupRoot(backupRoot)
68
83
, RestoreRoot(restoreRoot)
69
84
{
70
85
}
71
86
72
87
TString operator ()(const TString& path) const {
73
- if (IsAbsolutePath (path)) {
88
+ if (IsAbsolutePath (path) && (!Database || IsInDatabase (path)) ) {
74
89
return RewriteAbsolutePath (path);
75
90
}
76
91
@@ -175,8 +190,28 @@ struct TTableRefValidator {
175
190
NYql::TIssues& Issues;
176
191
};
177
192
193
+ TString GetOpt (TStringInput query, TStringBuf pattern) {
194
+ TString line;
195
+ while (query.ReadLine (line)) {
196
+ StripInPlace (line);
197
+ if (line.StartsWith (pattern)) {
198
+ return TString (TStringBuf (line).Skip (pattern.size ()).Chop (1 /* last " */ ));
199
+ }
200
+ }
201
+
202
+ return " " ;
203
+ }
204
+
178
205
} // anonymous
179
206
207
+ TString GetBackupRoot (const TString& query) {
208
+ return GetOpt (query, R"( -- backup root: ")" );
209
+ }
210
+
211
+ TString GetDatabase (const TString& query) {
212
+ return GetOpt (query, R"( -- database: ")" );
213
+ }
214
+
180
215
bool SqlToProtoAst (const TString& queryStr, TRule_sql_query& queryProto, NYql::TIssues& issues) {
181
216
NSQLTranslation::TTranslationSettings settings;
182
217
if (!NSQLTranslation::ParseTranslationSettings (queryStr, settings, issues)) {
@@ -215,20 +250,20 @@ bool ValidateTableRefs(const TRule_sql_query& query, NYql::TIssues& issues) {
215
250
}
216
251
217
252
template <typename TRef>
218
- TString RewriteRefs (const TRule_sql_query& query, TStringBuf backupRoot, TStringBuf restoreRoot) {
219
- TTokenCollector tokenCollector (TAbsolutePathRewriter (backupRoot, restoreRoot));
253
+ TString RewriteRefs (const TRule_sql_query& query, TStringBuf db, TStringBuf backupRoot, TStringBuf restoreRoot) {
254
+ TTokenCollector tokenCollector (TAbsolutePathRewriter (db, backupRoot, restoreRoot));
220
255
VisitAllFields<TRef>(query, tokenCollector);
221
256
return tokenCollector.Tokens ;
222
257
}
223
258
224
259
template <typename TRef>
225
- bool RewriteRefs (TString& queryStr, TStringBuf backupRoot, TStringBuf restoreRoot, NYql::TIssues& issues) {
260
+ bool RewriteRefs (TString& queryStr, TStringBuf db, TStringBuf backupRoot, TStringBuf restoreRoot, NYql::TIssues& issues) {
226
261
TRule_sql_query queryProto;
227
262
if (!SqlToProtoAst (queryStr, queryProto, issues)) {
228
263
return false ;
229
264
}
230
265
231
- const auto rewrittenQuery = RewriteRefs<TRef>(queryProto, backupRoot, restoreRoot);
266
+ const auto rewrittenQuery = RewriteRefs<TRef>(queryProto, db, backupRoot, restoreRoot);
232
267
// formatting here is necessary for the view to have pretty text inside it after the creation
233
268
if (!Format (rewrittenQuery, queryStr, issues)) {
234
269
return false ;
@@ -238,11 +273,15 @@ bool RewriteRefs(TString& queryStr, TStringBuf backupRoot, TStringBuf restoreRoo
238
273
}
239
274
240
275
bool RewriteTableRefs (TString& query, TStringBuf backupRoot, TStringBuf restoreRoot, NYql::TIssues& issues) {
241
- return RewriteRefs<TRule_table_ref>(query, backupRoot, restoreRoot, issues);
276
+ return RewriteRefs<TRule_table_ref>(query, " " , backupRoot, restoreRoot, issues);
277
+ }
278
+
279
+ bool RewriteTableRefs (TString& query, TStringBuf restoreRoot, NYql::TIssues& issues) {
280
+ return RewriteRefs<TRule_table_ref>(query, GetDatabase (query), GetBackupRoot (query), restoreRoot, issues);
242
281
}
243
282
244
- bool RewriteObjectRefs (TString& query, TStringBuf backupRoot, TStringBuf restoreRoot, NYql::TIssues& issues) {
245
- return RewriteRefs<TRule_object_ref>(query, backupRoot , restoreRoot, issues);
283
+ bool RewriteObjectRefs (TString& query, TStringBuf restoreRoot, NYql::TIssues& issues) {
284
+ return RewriteRefs<TRule_object_ref>(query, GetDatabase (query), GetBackupRoot (query) , restoreRoot, issues);
246
285
}
247
286
248
287
bool RewriteCreateQuery (TString& query, std::string_view pattern, const std::string& dbPath, NYql::TIssues& issues) {
@@ -255,19 +294,4 @@ bool RewriteCreateQuery(TString& query, std::string_view pattern, const std::str
255
294
return false ;
256
295
}
257
296
258
- TString GetBackupRoot (TStringInput query) {
259
- constexpr TStringBuf targetLinePrefix = " -- backup root: \" " ;
260
- constexpr TStringBuf discardedSuffix = " \" " ;
261
-
262
- TString line;
263
- while (query.ReadLine (line)) {
264
- StripInPlace (line);
265
- if (line.StartsWith (targetLinePrefix)) {
266
- return TString (TStringBuf (line).Skip (targetLinePrefix.size ()).Chop (discardedSuffix.size ()));
267
- }
268
- }
269
-
270
- return " " ;
271
- }
272
-
273
297
} // NYdb::NDump
0 commit comments