| 1 | #include "duckdb/common/filename_pattern.hpp" |
| 2 | #include "duckdb/common/string_util.hpp" |
| 3 | |
| 4 | namespace duckdb { |
| 5 | |
| 6 | void FilenamePattern::SetFilenamePattern(const string &pattern) { |
| 7 | const string id_format {"{i}" }; |
| 8 | const string uuid_format {"{uuid}" }; |
| 9 | |
| 10 | _base = pattern; |
| 11 | |
| 12 | _pos = _base.find(str: id_format); |
| 13 | if (_pos != string::npos) { |
| 14 | _base = StringUtil::Replace(source: _base, from: id_format, to: "" ); |
| 15 | _uuid = false; |
| 16 | } |
| 17 | |
| 18 | _pos = _base.find(str: uuid_format); |
| 19 | if (_pos != string::npos) { |
| 20 | _base = StringUtil::Replace(source: _base, from: uuid_format, to: "" ); |
| 21 | _uuid = true; |
| 22 | } |
| 23 | |
| 24 | _pos = std::min(_pos, (idx_t)_base.length()); |
| 25 | } |
| 26 | |
| 27 | string FilenamePattern::CreateFilename(const FileSystem &fs, const string &path, const string &extension, |
| 28 | idx_t offset) const { |
| 29 | string result(_base); |
| 30 | string replacement; |
| 31 | |
| 32 | if (_uuid) { |
| 33 | replacement = UUID::ToString(input: UUID::GenerateRandomUUID()); |
| 34 | } else { |
| 35 | replacement = std::to_string(val: offset); |
| 36 | } |
| 37 | result.insert(pos1: _pos, str: replacement); |
| 38 | return fs.JoinPath(a: path, b: result + "." + extension); |
| 39 | } |
| 40 | |
| 41 | } // namespace duckdb |
| 42 | |