OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RegexParserDataWrapper.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2021 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "RegexParserDataWrapper.h"
18 
19 #include <boost/regex.hpp>
20 
23 
24 namespace foreign_storage {
26  : AbstractTextFileDataWrapper(), regex_file_buffer_parser_{nullptr} {}
27 
29  const ForeignTable* foreign_table)
30  : AbstractTextFileDataWrapper(db_id, foreign_table)
31  , regex_file_buffer_parser_{foreign_table} {}
32 
33 namespace {
34 void validate_regex(const std::string& regex, const std::string& option_name) {
35  try {
36  boost::regex{regex};
37  } catch (const std::exception& e) {
38  throw ForeignStorageException{"Error parsing " + option_name + " \"" + regex +
39  "\": " + e.what()};
40  }
41 }
42 } // namespace
43 
45  const ForeignTable* foreign_table) const {
47  auto line_regex_it = foreign_table->options.find(RegexFileBufferParser::LINE_REGEX_KEY);
48  if (line_regex_it == foreign_table->options.end() || line_regex_it->second.empty()) {
50  "Foreign table options must contain a non-empty value for \"" +
52  }
54 
55  auto line_start_regex_it =
57  if (line_start_regex_it != foreign_table->options.end()) {
58  if (line_start_regex_it->second.empty()) {
59  throw ForeignStorageException{"Foreign table option \"" +
61  "\", when set, must contain a non-empty value."};
62  }
63  validate_regex(line_start_regex_it->second,
65  }
66 }
67 
68 const std::set<std::string_view>& RegexParserDataWrapper::getSupportedTableOptions()
69  const {
70  static const auto supported_table_options = getAllRegexTableOptions();
71  return supported_table_options;
72 }
73 
74 std::set<std::string_view> RegexParserDataWrapper::getAllRegexTableOptions() const {
75  std::set<std::string_view> supported_table_options(
78  supported_table_options.insert(regex_table_options_.begin(),
79  regex_table_options_.end());
80  return supported_table_options;
81 }
82 
85 }
86 
87 const std::set<std::string_view> RegexParserDataWrapper::regex_table_options_{
91 } // namespace foreign_storage
const RegexFileBufferParser regex_file_buffer_parser_
std::set< std::string_view > getAllRegexTableOptions() const
static const std::set< std::string_view > regex_table_options_
void validateTableOptions(const ForeignTable *foreign_table) const override
const std::set< std::string_view > & getSupportedTableOptions() const override
const TextFileBufferParser & getFileBufferParser() const override
void validateTableOptions(const ForeignTable *foreign_table) const override
const std::set< std::string_view > & getSupportedTableOptions() const override
void validate_regex(const std::string &regex, const std::string &option_name)